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 "workschedulerstopwork_fuzzer.h"
17 
18 #include "iwork_sched_service_ipc_interface_code.h"
19 #include "work_scheduler_service.h"
20 #include "work_sched_common.h"
21 #include "work_condition.h"
22 
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 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)36     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
37     {
38         MessageParcel dataMessageParcel;
39         dataMessageParcel.WriteInterfaceToken(WORK_SCHEDULER_STUB_TOKEN);
40         dataMessageParcel.WriteBuffer(data, size);
41         dataMessageParcel.RewindRead(0);
42         MessageParcel reply;
43         MessageOption option;
44         workSchedulerService_ = DelayedSingleton<WorkSchedulerService>::GetInstance();
45         uint32_t code = static_cast<int32_t>(IWorkSchedServiceInterfaceCode::STOP_WORK);
46         WorkInfo workInfo = WorkInfo();
47         int32_t workId = 1;
48         workInfo.SetWorkId(workId);
49         workInfo.SetElement("bundle_name", "ability_name");
50         workInfo.RequestPersisted(true);
51         workInfo.RequestStorageLevel(WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY);
52         if (!dataMessageParcel.WriteParcelable(&workInfo)) {
53             return false;
54         }
55         workSchedulerService_->OnStart();
56         workSchedulerService_->InitBgTaskSubscriber();
57         if (!workSchedulerService_->ready_) {
58             workSchedulerService_->ready_ = true;
59         }
60         workSchedulerService_->StartWork(workInfo);
61         if (workSchedulerService_->checkBundle_) {
62             workSchedulerService_->checkBundle_ = false;
63         }
64         workSchedulerService_->OnRemoteRequest(code, dataMessageParcel, reply, option);
65         workSchedulerService_->OnStop();
66         return true;
67     }
68 } // WorkScheduler
69 } // OHOS
70 
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
73 {
74     /* Run your code on data */
75     OHOS::WorkScheduler::DoSomethingInterestingWithMyAPI(data, size);
76     return 0;
77 }