• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "workschedulerstopandclearworks_fuzzer.h"
17 
18 #include "iwork_sched_service.h"
19 #include "work_scheduler_service.h"
20 #include "work_condition.h"
21 #include "work_policy_manager.h"
22 #include "work_queue_manager.h"
23 
DecStrongRef(void const * obj)24 void OHOS::RefBase::DecStrongRef(void const* obj) {}
25 
26 namespace OHOS {
27 namespace WorkScheduler {
28     const std::u16string WORK_SCHEDULER_STUB_TOKEN = u"ohos.workscheduler.iworkschedservice";
29     static std::shared_ptr<WorkSchedulerService> workSchedulerService_;
30 
GetUidByBundleName(const std::string & bundleName,int32_t & uid)31     bool WorkSchedulerService::GetUidByBundleName(const std::string &bundleName, int32_t &uid)
32     {
33         return true;
34     }
35 
TirggerBatteryStatusListener()36     void TirggerBatteryStatusListener()
37     {
38         std::vector<std::string> argsInStr;
39         std::string result;
40         WorkInfo workInfo = WorkInfo();
41         int32_t workId = 1;
42         workInfo.SetWorkId(workId);
43         workInfo.SetElement("bundle_name1", "ability_name1");
44         workInfo.RequestBatteryStatus(WorkCondition::BatteryStatus::BATTERY_STATUS_LOW_OR_OKAY);
45         workSchedulerService_->StartWork(workInfo);
46         argsInStr.clear();
47         result.clear();
48         argsInStr.push_back("-d");
49         argsInStr.push_back("batteryStatus");
50         argsInStr.push_back("ok");
51         workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
52     }
53 
TirggerStorageLevelListener()54     void TirggerStorageLevelListener()
55     {
56         std::vector<std::string> argsInStr;
57         std::string result;
58         WorkInfo workInfo = WorkInfo();
59         int32_t workId = 2;
60         workInfo.SetWorkId(workId);
61         workInfo.SetElement("bundle_name2", "ability_name2");
62         workInfo.RequestStorageLevel(WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY);
63         workSchedulerService_->StartWork(workInfo);
64         argsInStr.clear();
65         result.clear();
66         argsInStr.push_back("-d");
67         argsInStr.push_back("storage");
68         argsInStr.push_back("ok");
69         workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
70     }
71 
TirggerChargerListener()72     void TirggerChargerListener()
73     {
74         std::vector<std::string> argsInStr;
75         std::string result;
76         WorkInfo workInfo = WorkInfo();
77         int32_t workId = 3;
78         workInfo.SetWorkId(workId);
79         workInfo.SetElement("bundle_name3", "ability_name3");
80         workInfo.RequestChargerType(true, WorkCondition::Charger::CHARGING_PLUGGED_USB);
81         workSchedulerService_->StartWork(workInfo);
82         argsInStr.clear();
83         result.clear();
84         argsInStr.push_back("-d");
85         argsInStr.push_back("charging");
86         argsInStr.push_back("usb");
87         workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
88     }
89 
TirggerNetworkListener()90     void TirggerNetworkListener()
91     {
92         std::vector<std::string> argsInStr;
93         std::string result;
94         WorkInfo workInfo = WorkInfo();
95         int32_t workId = 4;
96         workInfo.SetWorkId(workId);
97         workInfo.SetElement("bundle_name4", "ability_name4");
98         workInfo.RequestNetworkType(WorkCondition::Network::NETWORK_TYPE_WIFI);
99         workSchedulerService_->StartWork(workInfo);
100         argsInStr.clear();
101         result.clear();
102         argsInStr.push_back("-d");
103         argsInStr.push_back("network");
104         argsInStr.push_back("wifi");
105         workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
106     }
107 
ShowTaskStatusInfo()108     void ShowTaskStatusInfo()
109     {
110         std::vector<std::string> argsInStr;
111         std::string result;
112         argsInStr.clear();
113         result.clear();
114         argsInStr.push_back("-a");
115         workSchedulerService_->DumpProcessForEngMode(argsInStr, result);
116     }
117 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)118     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
119     {
120         MessageParcel dataMessageParcel;
121         dataMessageParcel.WriteInterfaceToken(WORK_SCHEDULER_STUB_TOKEN);
122         dataMessageParcel.WriteBuffer(data, size);
123         dataMessageParcel.RewindRead(0);
124         MessageParcel reply;
125         MessageOption option;
126         workSchedulerService_ = DelayedSingleton<WorkSchedulerService>::GetInstance();
127         uint32_t code = static_cast<int32_t>(IWorkSchedServiceIpcCode::COMMAND_STOP_AND_CLEAR_WORKS);
128         workSchedulerService_->OnStart();
129         workSchedulerService_->InitBgTaskSubscriber();
130         if (!workSchedulerService_->ready_) {
131             workSchedulerService_->ready_ = true;
132         }
133         if (workSchedulerService_->workPolicyManager_ == nullptr) {
134             workSchedulerService_->workPolicyManager_ = std::make_shared<WorkPolicyManager>(workSchedulerService_);
135         }
136         if (workSchedulerService_->workQueueManager_ == nullptr) {
137             workSchedulerService_->workQueueManager_ = std::make_shared<WorkQueueManager>(workSchedulerService_);
138         }
139         TirggerBatteryStatusListener();
140         TirggerStorageLevelListener();
141         TirggerChargerListener();
142         TirggerNetworkListener();
143         ShowTaskStatusInfo();
144         if (workSchedulerService_->checkBundle_) {
145             workSchedulerService_->checkBundle_ = false;
146         }
147         workSchedulerService_->OnRemoteRequest(code, dataMessageParcel, reply, option);
148         workSchedulerService_->OnStop();
149         return true;
150     }
151 } // WorkScheduler
152 } // OHOS
153 
154 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)155 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
156 {
157     /* Run your code on data */
158     OHOS::WorkScheduler::DoSomethingInterestingWithMyAPI(data, size);
159     return 0;
160 }