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 "distributedschedstubone_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 const std::string TAG = "DistributedSchedFuzzTest";
38
NotifyCompleteContinuationInnerFuzzTest(const uint8_t * data,size_t size)39 void NotifyCompleteContinuationInnerFuzzTest(const uint8_t* data, size_t size)
40 {
41 if ((data == nullptr) || (size < sizeof(int32_t))) {
42 return;
43 }
44 FuzzUtil::MockPermission();
45 FuzzedDataProvider fdp(data, size);
46 bool isSuccess = fdp.ConsumeBool();
47 int32_t sessionId = fdp.ConsumeIntegral<int32_t>();
48 std::string devId = fdp.ConsumeRandomLengthString();
49 MessageParcel dataParcel;
50 MessageParcel reply;
51 MessageOption option;
52
53 dataParcel.WriteString16(Str8ToStr16(devId));
54 dataParcel.WriteInt32(sessionId);
55 dataParcel.WriteBool(isSuccess);
56 DistributedSchedService::GetInstance().NotifyCompleteContinuationInner(dataParcel, reply);
57 }
58
ContinueMissionInnerFuzzTest(const uint8_t * data,size_t size)59 void ContinueMissionInnerFuzzTest(const uint8_t* data, size_t size)
60 {
61 if ((data == nullptr) || (size < sizeof(int32_t))) {
62 return;
63 }
64 FuzzUtil::MockPermission();
65 MessageParcel dataParcel;
66 MessageParcel reply;
67 MessageOption option;
68 FuzzedDataProvider fdp(data, size);
69 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
70 std::string str = fdp.ConsumeRandomLengthString();
71 std::string deviceId = fdp.ConsumeRandomLengthString();
72 std::string bundleName = fdp.ConsumeRandomLengthString();
73 sptr<IRemoteObject> callback(new MockDistributedSched());
74 WantParams wantParams;
75
76 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
77 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
78 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
79 PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, callback);
80 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &wantParams);
81 DistributedSchedService::GetInstance().ContinueMissionInner(dataParcel, reply);
82 DistributedSchedService::GetInstance().ContinueLocalMission(deviceId, int32Data, callback, wantParams);
83 DistributedSchedService::GetInstance().ContinueRemoteMission(deviceId, deviceId, int32Data, callback, wantParams);
84 DistributedSchedService::GetInstance().ContinueMission(deviceId, deviceId, int32Data, callback, wantParams);
85 }
86
ContinueMissionOfBundleNameInnerFuzzTest(const uint8_t * data,size_t size)87 void ContinueMissionOfBundleNameInnerFuzzTest(const uint8_t* data, size_t size)
88 {
89 if ((data == nullptr) || (size < sizeof(int32_t))) {
90 return;
91 }
92 FuzzUtil::MockPermission();
93 MessageParcel dataParcel;
94 MessageParcel reply;
95 MessageOption option;
96 FuzzedDataProvider fdp(data, size);
97 std::string str = fdp.ConsumeRandomLengthString();
98 std::string deviceId = fdp.ConsumeRandomLengthString();
99 std::string bundleName = fdp.ConsumeRandomLengthString();
100 sptr<IRemoteObject> callback(new MockDistributedSched());
101 WantParams wantParams;
102
103 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
104 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
105 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
106 PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, callback);
107 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &wantParams);
108 DistributedSchedService::GetInstance().ContinueMissionOfBundleNameInner(dataParcel, reply);
109 DistributedSchedService::GetInstance().ContinueRemoteMission(deviceId, deviceId, bundleName,
110 callback, wantParams);
111 DistributedSchedService::GetInstance().ProcessContinueLocalMission(deviceId, deviceId, bundleName,
112 callback, wantParams);
113 DistributedSchedService::GetInstance().ProcessContinueRemoteMission(deviceId, deviceId, bundleName,
114 callback, wantParams);
115 }
116
GetMissionInfosInnerFuzzTest(const uint8_t * data,size_t size)117 void GetMissionInfosInnerFuzzTest(const uint8_t* data, size_t size)
118 {
119 if ((data == nullptr) || (size < sizeof(int32_t))) {
120 return;
121 }
122 FuzzUtil::MockPermission();
123 MessageParcel dataParcel;
124 MessageParcel reply;
125 MessageOption option;
126 FuzzedDataProvider fdp(data, size);
127 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
128 std::string str = fdp.ConsumeRandomLengthString();
129
130 PARCEL_WRITE_HELPER_NORET(dataParcel, String16, Str8ToStr16(str));
131 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
132 DistributedSchedService::GetInstance().GetMissionInfosInner(dataParcel, reply);
133 }
134
GetDSchedEventInfoInnerFuzzTest(const uint8_t * data,size_t size)135 void GetDSchedEventInfoInnerFuzzTest(const uint8_t* data, size_t size)
136 {
137 if ((data == nullptr) || (size < sizeof(int32_t))) {
138 return;
139 }
140 FuzzUtil::MockPermission();
141 MessageParcel dataParcel;
142 MessageParcel reply;
143 MessageOption option;
144 FuzzedDataProvider fdp(data, size);
145 int32_t int32Data = fdp.ConsumeIntegral<int32_t>() % DMS_ALL;
146
147 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
148 DistributedSchedService::GetInstance().GetDSchedEventInfoInner(dataParcel, reply);
149 }
150 }
151 }
152
153 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)154 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
155 {
156 OHOS::DistributedSchedule::NotifyCompleteContinuationInnerFuzzTest(data, size);
157 OHOS::DistributedSchedule::ContinueMissionInnerFuzzTest(data, size);
158 OHOS::DistributedSchedule::ContinueMissionOfBundleNameInnerFuzzTest(data, size);
159 OHOS::DistributedSchedule::GetMissionInfosInnerFuzzTest(data, size);
160 OHOS::DistributedSchedule::GetDSchedEventInfoInnerFuzzTest(data, size);
161 return 0;
162 }
163