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 "distributedschedstubfive_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
RegisterOffListenerInnerFuzzTest(const uint8_t * data,size_t size)39 void RegisterOffListenerInnerFuzzTest(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 MessageParcel dataParcel;
47 MessageParcel reply;
48 MessageOption option;
49 std::string str = fdp.ConsumeRandomLengthString();
50 sptr<IRemoteObject> obj(new MockDistributedSched());
51
52 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
53 PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, obj);
54 DistributedSchedService::GetInstance().RegisterOffListenerInner(dataParcel, reply);
55 }
56
RegisterDSchedEventListenerInnerFuzzTest(const uint8_t * data,size_t size)57 void RegisterDSchedEventListenerInnerFuzzTest(const uint8_t* data, size_t size)
58 {
59 if ((data == nullptr) || (size < sizeof(int32_t))) {
60 return;
61 }
62 FuzzUtil::MockPermission();
63 MessageParcel dataParcel;
64 MessageParcel reply;
65 MessageOption option;
66 uint8_t uint8Data = *(reinterpret_cast<const uint8_t*>(data));
67 sptr<IRemoteObject> obj(new MockDistributedSched());
68
69 PARCEL_WRITE_HELPER_NORET(dataParcel, Uint8, uint8Data);
70 PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, obj);
71 DistributedSchedService::GetInstance().RegisterDSchedEventListenerInner(dataParcel, reply);
72 }
73
UnRegisterDSchedEventListenerInnerFuzzTest(const uint8_t * data,size_t size)74 void UnRegisterDSchedEventListenerInnerFuzzTest(const uint8_t* data, size_t size)
75 {
76 if ((data == nullptr) || (size < sizeof(int32_t))) {
77 return;
78 }
79 FuzzUtil::MockPermission();
80 MessageParcel dataParcel;
81 MessageParcel reply;
82 MessageOption option;
83 uint8_t uint8Data = *(reinterpret_cast<const uint8_t*>(data));
84 sptr<IRemoteObject> obj(new MockDistributedSched());
85
86 PARCEL_WRITE_HELPER_NORET(dataParcel, Uint8, uint8Data);
87 PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, obj);
88 DistributedSchedService::GetInstance().UnRegisterDSchedEventListenerInner(dataParcel, reply);
89 }
90
SetMissionContinueStateInnerFuzzTest(const uint8_t * data,size_t size)91 void SetMissionContinueStateInnerFuzzTest(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 FuzzedDataProvider fdp(data, size);
101 int32_t missionId = fdp.ConsumeIntegral<int32_t>();
102 int32_t state = fdp.ConsumeIntegral<int32_t>();
103 int32_t timeout = fdp.ConsumeIntegral<int32_t>();
104
105 dataParcel.WriteInt32(missionId);
106 dataParcel.WriteInt32(state);
107 DistributedSchedService::GetInstance().SetMissionContinueStateInner(dataParcel, reply);
108 DistributedSchedService::GetInstance().RemoveContinuationTimeout(missionId);
109 DistributedSchedService::GetInstance().SetContinuationTimeout(missionId, timeout);
110 DistributedSchedService::GetInstance().GetContinuaitonDevice(missionId);
111 }
112
StartShareFormFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)113 void StartShareFormFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
114 {
115 if ((data == nullptr) || (size < sizeof(int32_t))) {
116 return;
117 }
118 FuzzUtil::MockPermission();
119 MessageParcel dataParcel;
120 MessageParcel reply;
121 std::string str(reinterpret_cast<const char*>(data), size);
122 DistributedWant dstbWant;
123 FuzzedDataProvider fdp(data, size);
124 std::string dstDeviceId = fdp.ConsumeRandomLengthString();
125 std::string bundleName = fdp.ConsumeRandomLengthString();
126 std::string abilityName = fdp.ConsumeRandomLengthString();
127
128 dstbWant.SetDeviceId(dstDeviceId);
129 dstbWant.SetElementName(bundleName, abilityName);
130 PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
131 PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &dstbWant);
132 DistributedSchedService::GetInstance().StartShareFormFromRemoteInner(dataParcel, reply);
133 }
134
135 }
136 }
137
138 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)139 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
140 {
141 OHOS::DistributedSchedule::RegisterOffListenerInnerFuzzTest(data, size);
142 OHOS::DistributedSchedule::RegisterDSchedEventListenerInnerFuzzTest(data, size);
143 OHOS::DistributedSchedule::UnRegisterDSchedEventListenerInnerFuzzTest(data, size);
144 OHOS::DistributedSchedule::SetMissionContinueStateInnerFuzzTest(data, size);
145 OHOS::DistributedSchedule::StartShareFormFromRemoteInnerFuzzTest(data, size);
146 return 0;
147 }
148