1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bgtasksystemeventobserver_fuzzer.h"
17 #include "securec.h"
18 
19 #include "background_task_mgr_service.h"
20 #include "common_event_data.h"
21 #include "event_info.h"
22 #include "event_handler.h"
23 #include "event_runner.h"
24 #include "system_event_observer.h"
25 
26 
27 namespace OHOS {
28 namespace BackgroundTaskMgr {
29     constexpr int32_t U32_AT_SIZE = 4;
30 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)31     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
32     {
33         EventFwk::MatchingSkills matchingSkills;
34         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
35         EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
36         auto systemEventListener = std::make_shared<SystemEventObserver>(commonEventSubscribeInfo);
37         systemEventListener->Subscribe();
38         systemEventListener->Unsubscribe();
39 
40         EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
41         systemEventListener->OnReceiveEvent(eventData);
42 
43         auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
44         systemEventListener->SetEventHandler(handler);
45         systemEventListener->OnReceiveEventContinuousTask(eventData);
46         auto bgContinuousTaskMgr = std::make_shared<BgContinuousTaskMgr>();
47         systemEventListener->SetBgContinuousTaskMgr(bgContinuousTaskMgr);
48         systemEventListener->OnReceiveEventContinuousTask(eventData);
49         AAFwk::Want want = AAFwk::Want();
50         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
51         eventData.SetWant(want);
52         systemEventListener->OnReceiveEventContinuousTask(eventData);
53         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
54         eventData.SetWant(want);
55         systemEventListener->OnReceiveEventContinuousTask(eventData);
56         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
57         eventData.SetWant(want);
58         systemEventListener->OnReceiveEventContinuousTask(eventData);
59 
60         EventFwk::CommonEventData eventData2 = EventFwk::CommonEventData();
61         AAFwk::Want want2 = AAFwk::Want();
62         want2.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
63         eventData2.SetWant(want2);
64         systemEventListener->OnReceiveEventEfficiencyRes(eventData2);
65         return true;
66     }
67 } // BackgroundTaskMgr
68 } // OHOS
69 
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73     /* Run your code on data */
74     if (data == nullptr) {
75         return 0;
76     }
77 
78     if (size < OHOS::BackgroundTaskMgr::U32_AT_SIZE) {
79         return 0;
80     }
81 
82     char* ch = static_cast<char *>(malloc(size + 1));
83     if (ch == nullptr) {
84         return 0;
85     }
86 
87     (void)memset_s(ch, size + 1, 0x00, size + 1);
88     if (memcpy_s(ch, size + 1, data, size) != EOK) {
89         free(ch);
90         ch = nullptr;
91         return 0;
92     }
93 
94     OHOS::BackgroundTaskMgr::DoSomethingInterestingWithMyAPI(ch, size);
95     free(ch);
96     ch = nullptr;
97     return 0;
98 }
99