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 "workschedulergetworkstatus_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::GET_WORK_STATUS);
46         WorkInfo workInfo = WorkInfo();
47         int32_t workId = 1;
48         workInfo.SetWorkId(workId);
49         workInfo.SetElement("bundle_name", "ability_name");
50         workInfo.RequestStorageLevel(WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY);
51         if (!dataMessageParcel.WriteParcelable(&workInfo)) {
52             return false;
53         }
54         workSchedulerService_->OnStart();
55         workSchedulerService_->InitBgTaskSubscriber();
56         if (!workSchedulerService_->ready_) {
57             workSchedulerService_->ready_ = true;
58         }
59         workSchedulerService_->AddWorkInner(workInfo);
60         workSchedulerService_->OnRemoteRequest(code, dataMessageParcel, reply, option);
61         workSchedulerService_->OnStop();
62         return true;
63     }
64 } // WorkScheduler
65 } // OHOS
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     /* Run your code on data */
71     OHOS::WorkScheduler::DoSomethingInterestingWithMyAPI(data, size);
72     return 0;
73 }