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 "distributedschedstubfour_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 namespace {
39 const std::u16string DMS_STUB_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
40 }
41
StartRemoteFreeInstallInnerFuzzTest(const uint8_t * data,size_t size)42 void StartRemoteFreeInstallInnerFuzzTest(const uint8_t* data, size_t size)
43 {
44 if ((data == nullptr) || (size < sizeof(int32_t))) {
45 return;
46 }
47 FuzzUtil::MockPermission();
48 MessageParcel dataParcel;
49 MessageParcel reply;
50 MessageOption option;
51 FuzzedDataProvider fdp(data, size);
52 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
53 uint32_t uint32Data = fdp.ConsumeIntegral<uint32_t>();
54 sptr<IRemoteObject> obj(new MockDistributedSched());
55 Want want;
56 std::string dstDeviceId = fdp.ConsumeRandomLengthString();
57
58 want.SetDeviceId(dstDeviceId);
59 dataParcel.WriteInterfaceToken(DMS_STUB_INTERFACE_TOKEN);
60 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &want);
61 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
62 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
63 PARCEL_WRITE_HELPER_NORET(dataParcel, Uint32, uint32Data);
64 PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, obj);
65 DistributedSchedService::GetInstance().StartRemoteFreeInstallInner(dataParcel, reply);
66 DistributedSchedService::GetInstance().ProcessCallResult(obj, obj);
67 }
68
StartRemoteShareFormInnerFuzzTest(const uint8_t * data,size_t size)69 void StartRemoteShareFormInnerFuzzTest(const uint8_t* data, size_t size)
70 {
71 if ((data == nullptr) || (size < sizeof(int32_t))) {
72 return;
73 }
74 FuzzUtil::MockPermission();
75 int32_t code = static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_SHARE_FORM);
76 FuzzedDataProvider fdp(data, size);
77 MessageParcel dataParcel;
78 MessageParcel reply;
79 MessageOption option;
80 std::string str = fdp.ConsumeRandomLengthString();
81 Want want;
82 std::string dstDeviceId = fdp.ConsumeRandomLengthString();
83
84 want.SetDeviceId(dstDeviceId);
85 dataParcel.WriteInterfaceToken(DMS_STUB_INTERFACE_TOKEN);
86 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
87 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &want);
88 DistributedSchedService::GetInstance().OnRemoteRequest(code, dataParcel, reply, option);
89 }
90
StopRemoteExtensionAbilityInnerFuzzTest(const uint8_t * data,size_t size)91 void StopRemoteExtensionAbilityInnerFuzzTest(const uint8_t* data, size_t size)
92 {
93 if ((data == nullptr) || (size < sizeof(int32_t))) {
94 return;
95 }
96 FuzzUtil::MockPermission();
97 MessageParcel dataParcel;
98 MessageParcel reply;
99 MessageOption option;
100 Want want;
101 FuzzedDataProvider fdp(data, size);
102 int32_t callerUid = fdp.ConsumeIntegral<int32_t>();
103 int32_t serviceType = fdp.ConsumeIntegral<int32_t>();
104 uint32_t accessToken = fdp.ConsumeIntegral<uint32_t>();
105 std::string dstDeviceId = fdp.ConsumeRandomLengthString();
106 std::string bundleName = fdp.ConsumeRandomLengthString();
107 std::string abilityName = fdp.ConsumeRandomLengthString();
108
109 want.SetDeviceId(dstDeviceId);
110 want.SetElementName(bundleName, abilityName);
111 dataParcel.WriteParcelable(&want);
112 dataParcel.WriteInt32(callerUid);
113 dataParcel.WriteUint32(accessToken);
114 dataParcel.WriteInt32(serviceType);
115 DistributedSchedService::GetInstance().StopRemoteExtensionAbilityInner(dataParcel, reply);
116
117 CallerInfo callerInfo;
118 std::string localDeviceId = fdp.ConsumeRandomLengthString();
119 DistributedSchedService::GetInstance().GetCallerInfo(localDeviceId, callerUid, accessToken, callerInfo);
120 DistributedSchedService::GetInstance().CheckDeviceIdFromRemote(localDeviceId, localDeviceId, localDeviceId);
121 }
122
RegisterOnListenerInnerFuzzTest(const uint8_t * data,size_t size)123 void RegisterOnListenerInnerFuzzTest(const uint8_t* data, size_t size)
124 {
125 if ((data == nullptr) || (size < sizeof(int32_t))) {
126 return;
127 }
128 FuzzedDataProvider fdp(data, size);
129 FuzzUtil::MockPermission();
130 MessageParcel dataParcel;
131 MessageParcel reply;
132 MessageOption option;
133 std::string str = fdp.ConsumeRandomLengthString();
134 sptr<IRemoteObject> obj(new MockDistributedSched());
135
136 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
137 PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, obj);
138 DistributedSchedService::GetInstance().RegisterOnListenerInner(dataParcel, reply);
139 DistributedSchedService::GetInstance().HandleLocalCallerDied(obj);
140 DistributedSchedService::GetInstance().RemoveCallerComponent(obj);
141 DistributedSchedService::GetInstance().RemoveConnectAbilityInfo(str);
142 DistributedSchedService::GetInstance().DumpConnectInfo(str);
143 }
144
StopSyncMissionsFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)145 void StopSyncMissionsFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
146 {
147 if ((data == nullptr) || (size < sizeof(int32_t))) {
148 return;
149 }
150 FuzzUtil::MockPermission();
151 MessageParcel dataParcel;
152 MessageParcel reply;
153 MessageOption option;
154 FuzzedDataProvider fdp(data, size);
155 int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
156 std::string str = fdp.ConsumeRandomLengthString();
157
158 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
159 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
160 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
161 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
162 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
163 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
164 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
165
166 DistributedSchedService::GetInstance().StopSyncMissionsFromRemoteInner(dataParcel, reply);
167 }
168 }
169 }
170
171 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)172 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
173 {
174 OHOS::DistributedSchedule::StartRemoteFreeInstallInnerFuzzTest(data, size);
175 OHOS::DistributedSchedule::StartRemoteShareFormInnerFuzzTest(data, size);
176 OHOS::DistributedSchedule::StopRemoteExtensionAbilityInnerFuzzTest(data, size);
177 OHOS::DistributedSchedule::RegisterOnListenerInnerFuzzTest(data, size);
178 OHOS::DistributedSchedule::StopSyncMissionsFromRemoteInnerFuzzTest(data, size);
179 return 0;
180 }
181