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 "distributedschedstubsix_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
StartAbilityFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)39 void StartAbilityFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
40 {
41 if ((data == nullptr) || (size < sizeof(int32_t))) {
42 return;
43 }
44 FuzzUtil::MockPermission();
45 MessageParcel dataParcel;
46 MessageParcel reply;
47 MessageOption option;
48 DistributedWant dstbWant;
49 AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
50 FuzzedDataProvider fdp(data, size);
51 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
52 std::string str = fdp.ConsumeRandomLengthString();
53 std::vector<std::string> strVector = {str};
54 std::string dstDeviceId = fdp.ConsumeRandomLengthString();
55 std::string bundleName = fdp.ConsumeRandomLengthString();
56 std::string abilityName = fdp.ConsumeRandomLengthString();
57
58 dstbWant.SetDeviceId(dstDeviceId);
59 dstbWant.SetElementName(bundleName, abilityName);
60
61 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &dstbWant);
62 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &compatibleAbilityInfo);
63 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
64 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
65 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
66 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
67 PARCEL_WRITE_HELPER_NORET(dataParcel, StringVector, strVector);
68 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
69 DistributedSchedService::GetInstance().StartAbilityFromRemoteInner(dataParcel, reply);
70 }
71
SendResultFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)72 void SendResultFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
73 {
74 if ((data == nullptr) || (size < sizeof(int32_t))) {
75 return;
76 }
77 FuzzUtil::MockPermission();
78 MessageParcel dataParcel;
79 MessageParcel reply;
80 MessageOption option;
81 AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
82 FuzzedDataProvider fdp(data, size);
83 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
84 std::string str = fdp.ConsumeRandomLengthString();
85 std::vector<std::string> strVector = {str};
86 DistributedWant dstbWant;
87 std::string dstDeviceId = fdp.ConsumeRandomLengthString();
88 std::string bundleName = fdp.ConsumeRandomLengthString();
89 std::string abilityName = fdp.ConsumeRandomLengthString();
90
91 dstbWant.SetDeviceId(dstDeviceId);
92 dstbWant.SetElementName(bundleName, abilityName);
93
94 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &dstbWant);
95 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
96 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
97 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
98 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
99 PARCEL_WRITE_HELPER_NORET(dataParcel, StringVector, strVector);
100 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
101 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
102 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
103 DistributedSchedService::GetInstance().SendResultFromRemoteInner(dataParcel, reply);
104 }
105
NotifyDSchedEventResultFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)106 void NotifyDSchedEventResultFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
107 {
108 if ((data == nullptr) || (size < sizeof(int32_t))) {
109 return;
110 }
111 FuzzUtil::MockPermission();
112 MessageParcel dataParcel;
113 MessageParcel reply;
114 MessageOption option;
115 FuzzedDataProvider fdp(data, size);
116 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
117 std::string str = fdp.ConsumeRandomLengthString();
118
119 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
120 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
121 DistributedSchedService::GetInstance().NotifyDSchedEventResultFromRemoteInner(dataParcel, reply);
122 }
123
NotifyContinuationResultFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)124 void NotifyContinuationResultFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
125 {
126 if ((data == nullptr) || (size < sizeof(int32_t))) {
127 return;
128 }
129 FuzzUtil::MockPermission();
130 DmsContinueTime::GetInstance().Init();
131 MessageParcel dataParcel;
132 MessageParcel reply;
133 MessageOption option;
134 AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
135 FuzzedDataProvider fdp(data, size);
136 bool boolData = fdp.ConsumeBool();
137 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
138 std::string str = fdp.ConsumeRandomLengthString();
139
140 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
141 PARCEL_WRITE_HELPER_NORET(dataParcel, Bool, boolData);
142 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
143 DistributedSchedService::GetInstance().NotifyContinuationResultFromRemoteInner(dataParcel, reply);
144 }
145
StopExtensionAbilityFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)146 void StopExtensionAbilityFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
147 {
148 if ((data == nullptr) || (size < sizeof(int32_t))) {
149 return;
150 }
151 FuzzUtil::MockPermission();
152 MessageParcel dataParcel;
153 MessageParcel reply;
154 MessageOption option;
155 FuzzedDataProvider fdp(data, size);
156 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
157 std::string str = fdp.ConsumeRandomLengthString();
158 std::vector<std::string> strVector = {str};
159 DistributedWant dstbWant;
160 std::string dstDeviceId = fdp.ConsumeRandomLengthString();
161 std::string bundleName = fdp.ConsumeRandomLengthString();
162 std::string abilityName = fdp.ConsumeRandomLengthString();
163
164 dstbWant.SetDeviceId(dstDeviceId);
165 dstbWant.SetElementName(bundleName, abilityName);
166
167 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &dstbWant);
168 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
169 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
170 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
171 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
172 PARCEL_WRITE_HELPER_NORET(dataParcel, StringVector, strVector);
173 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
174 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
175 DistributedSchedService::GetInstance().StopExtensionAbilityFromRemoteInner(dataParcel, reply);
176 }
177 }
178 }
179
180 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)181 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
182 {
183 OHOS::DistributedSchedule::StartAbilityFromRemoteInnerFuzzTest(data, size);
184 OHOS::DistributedSchedule::SendResultFromRemoteInnerFuzzTest(data, size);
185 OHOS::DistributedSchedule::NotifyDSchedEventResultFromRemoteInnerFuzzTest(data, size);
186 OHOS::DistributedSchedule::NotifyContinuationResultFromRemoteInnerFuzzTest(data, size);
187 OHOS::DistributedSchedule::StopExtensionAbilityFromRemoteInnerFuzzTest(data, size);
188 return 0;
189 }
190