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