1 /*
2 * Copyright (c) 2025 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 "distributedschedservicefour_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <singleton.h>
22
23 #include "distributed_sched_interface.h"
24 #include "distributed_sched_service.h"
25 #include "distributed_sched_stub.h"
26 #include "distributedWant/distributed_want.h"
27 #include "mock_fuzz_util.h"
28 #include "mock_distributed_sched.h"
29 #include "parcel_helper.h"
30 #include "dms_continue_time_dumper.h"
31
32 using namespace OHOS::AAFwk;
33 using namespace OHOS::AppExecFwk;
34
35 namespace OHOS {
36 namespace DistributedSchedule {
37
38 std::string GetDExtensionName(std::string bundleName, int32_t userId);
39 std::string GetDExtensionProcess(std::string bundleName, int32_t userId);
40
CheckCollabStartPermissionFuzzTest(const uint8_t * data,size_t size)41 void CheckCollabStartPermissionFuzzTest(const uint8_t* data, size_t size)
42 {
43 if ((data == nullptr) || (size < sizeof(int32_t))) {
44 return;
45 }
46 FuzzedDataProvider fdp(data, size);
47 AAFwk::Want want;
48 CallerInfo callerInfo;
49 AccountInfo accountInfo;
50 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
51 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
52 callerInfo.pid = fdp.ConsumeIntegral<int32_t>();
53 callerInfo.callerType = fdp.ConsumeIntegral<int32_t>();
54 bool needQueryExtension = fdp.ConsumeBool();
55 DistributedSchedService::GetInstance().CheckCollabStartPermission(want,
56 callerInfo, accountInfo, needQueryExtension);
57 }
58
CheckTargetPermission4DiffBundleFuzzTest(const uint8_t * data,size_t size)59 void CheckTargetPermission4DiffBundleFuzzTest(const uint8_t* data, size_t size)
60 {
61 if ((data == nullptr) || (size < sizeof(int32_t))) {
62 return;
63 }
64 FuzzedDataProvider fdp(data, size);
65 AAFwk::Want want;
66 CallerInfo callerInfo;
67 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
68 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
69 callerInfo.pid = fdp.ConsumeIntegral<int32_t>();
70 callerInfo.callerType = fdp.ConsumeIntegral<int32_t>();
71 AccountInfo accountInfo;
72 int32_t flag = fdp.ConsumeIntegral<int32_t>();
73 bool needQueryExtension = fdp.ConsumeBool();
74 DistributedSchedService::GetInstance().CheckTargetPermission4DiffBundle(want,
75 callerInfo, accountInfo, flag, needQueryExtension);
76 }
77
RegisterAppStateObserverFuzzTest(const uint8_t * data,size_t size)78 void RegisterAppStateObserverFuzzTest(const uint8_t* data, size_t size)
79 {
80 if ((data == nullptr) || (size < sizeof(int32_t))) {
81 return;
82 }
83 FuzzedDataProvider fdp(data, size);
84 AAFwk::Want want;
85 CallerInfo callerInfo;
86 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
87 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
88 callerInfo.pid = fdp.ConsumeIntegral<int32_t>();
89 callerInfo.callerType = fdp.ConsumeIntegral<int32_t>();
90 sptr<IRemoteObject> srcConnect = nullptr;
91 sptr<IRemoteObject> callbackWrapper = nullptr;
92 DistributedSchedService::GetInstance().RegisterAppStateObserver(want, callerInfo, srcConnect, callbackWrapper);
93 }
94
NotifyFreeInstallResultFuzzTest(const uint8_t * data,size_t size)95 void NotifyFreeInstallResultFuzzTest(const uint8_t* data, size_t size)
96 {
97 if ((data == nullptr) || (size < sizeof(int32_t))) {
98 return;
99 }
100 FuzzedDataProvider fdp(data, size);
101 CallbackTaskItem item;
102 int32_t resultCode = fdp.ConsumeIntegral<int32_t>();
103 DistributedSchedService::GetInstance().NotifyFreeInstallResult(item, resultCode);
104 }
HandleRemoteNotifyFuzzTest(const uint8_t * data,size_t size)105 void HandleRemoteNotifyFuzzTest(const uint8_t* data, size_t size)
106 {
107 if ((data == nullptr) || (size < sizeof(int32_t)) + (size < sizeof(int64_t))) {
108 return;
109 }
110 FuzzedDataProvider fdp(data, size);
111 DistributedSchedService::FreeInstallInfo info;
112 int64_t taskId = fdp.ConsumeIntegral<int64_t>();
113 int32_t resultCode = fdp.ConsumeIntegral<int32_t>();
114 DistributedSchedService::GetInstance().HandleRemoteNotify(info, taskId, resultCode);
115 }
116 }
117 }
118 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)119 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
120 {
121 OHOS::DistributedSchedule::CheckCollabStartPermissionFuzzTest(data, size);
122 OHOS::DistributedSchedule::CheckTargetPermission4DiffBundleFuzzTest(data, size);
123 OHOS::DistributedSchedule::RegisterAppStateObserverFuzzTest(data, size);
124 OHOS::DistributedSchedule::NotifyFreeInstallResultFuzzTest(data, size);
125 OHOS::DistributedSchedule::HandleRemoteNotifyFuzzTest(data, size);
126 return 0;
127 }
128