• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "distributedschedstubthree_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 uint32_t ONE = 1;
40 }
41 
GetRemoteMissionSnapshotInfoInnerFuzzTest(const uint8_t * data,size_t size)42 void GetRemoteMissionSnapshotInfoInnerFuzzTest(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 missionId = fdp.ConsumeIntegral<int32_t>();
53     std::string networkId = fdp.ConsumeRandomLengthString();
54 
55     dataParcel.WriteString(networkId);
56     dataParcel.WriteInt32(missionId);
57     DistributedSchedService::GetInstance().GetRemoteMissionSnapshotInfoInner(dataParcel, reply);
58     DistributedSchedService::GetInstance().DurationStart(networkId, networkId);
59 }
60 
StartRemoteAbilityByCallInnerFuzzTest(const uint8_t * data,size_t size)61 void StartRemoteAbilityByCallInnerFuzzTest(const uint8_t* data, size_t size)
62 {
63     if ((data == nullptr) || (size < sizeof(int32_t))) {
64         return;
65     }
66     FuzzUtil::MockPermission();
67     MessageParcel dataParcel;
68     MessageParcel reply;
69     MessageOption option;
70     Want want;
71     sptr<IRemoteObject> obj(new MockDistributedSched());
72     FuzzedDataProvider fdp(data, size);
73     int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
74     uint32_t uint32Data = fdp.ConsumeIntegral<uint32_t>();
75     std::string dstDeviceId = fdp.ConsumeRandomLengthString();
76 
77     want.SetDeviceId(dstDeviceId);
78 
79     PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &want);
80     PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, obj);
81     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
82     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
83     PARCEL_WRITE_HELPER_NORET(dataParcel, Uint32, uint32Data);
84     DistributedSchedService::GetInstance().StartRemoteAbilityByCallInner(dataParcel, reply);
85     DistributedSchedService::GetInstance().SetWantForContinuation(want, int32Data);
86 }
87 
ReleaseRemoteAbilityInnerFuzzTest(const uint8_t * data,size_t size)88 void ReleaseRemoteAbilityInnerFuzzTest(const uint8_t* data, size_t size)
89 {
90     if ((data == nullptr) || (size < sizeof(int32_t))) {
91         return;
92     }
93     FuzzUtil::MockPermission();
94     FuzzedDataProvider fdp(data, size);
95     MessageParcel dataParcel;
96     MessageParcel reply;
97     MessageOption option;
98     sptr<IRemoteObject> connect(new MockDistributedSched());
99     AppExecFwk::ElementName element;
100     CallerInfo callerInfo;
101     std::string deviceId = fdp.ConsumeRandomLengthString();
102 
103     PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, connect);
104     PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &element);
105     DistributedSchedService::GetInstance().ReleaseRemoteAbilityInner(dataParcel, reply);
106     callerInfo.uid = ONE;
107     DistributedSchedService::GetInstance().CheckDistributedConnectLocked(callerInfo);
108     DistributedSchedService::GetInstance().DecreaseConnectLocked(ONE);
109     DistributedSchedService::GetInstance().RemoteConnectAbilityMappingLocked(connect, deviceId,
110         deviceId, element, callerInfo, TargetComponent::HARMONY_COMPONENT);
111     DistributedSchedService::GetInstance().NotifyProcessDied(deviceId, callerInfo, TargetComponent::HARMONY_COMPONENT);
112     DistributedSchedService::GetInstance().ProcessDeviceOffline(deviceId);
113 }
114 
GetDistributedComponentListInnerFuzzTest(const uint8_t * data,size_t size)115 int32_t GetDistributedComponentListInnerFuzzTest(const uint8_t* data, size_t size)
116 {
117     if ((data == nullptr) || (size < sizeof(int32_t))) {
118         return INVALID_PARAMETERS_ERR;
119     }
120     FuzzUtil::MockPermission();
121     MessageParcel dataParcel;
122     MessageParcel reply;
123     std::vector<std::string> distributedComponents;
124     FuzzedDataProvider fdp(data, size);
125     int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
126     std::string str = fdp.ConsumeRandomLengthString();
127     distributedComponents.push_back(str);
128     PARCEL_WRITE_HELPER(reply, Int32, int32Data);
129     PARCEL_WRITE_HELPER(reply, StringVector, distributedComponents);
130 
131     DistributedSchedService::GetInstance().GetDistributedComponentListInner(dataParcel, reply);
132     return ERR_OK;
133 }
134 
StartSyncMissionsFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)135 void StartSyncMissionsFromRemoteInnerFuzzTest(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>();
146     std::string str = fdp.ConsumeRandomLengthString();
147 
148     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
149     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
150     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
151     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
152     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
153     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
154     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
155     DistributedSchedService::GetInstance().StartSyncMissionsFromRemoteInner(dataParcel, reply);
156 }
157 }
158 }
159 
160 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)161 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
162 {
163     OHOS::DistributedSchedule::GetRemoteMissionSnapshotInfoInnerFuzzTest(data, size);
164     OHOS::DistributedSchedule::StartRemoteAbilityByCallInnerFuzzTest(data, size);
165     OHOS::DistributedSchedule::ReleaseRemoteAbilityInnerFuzzTest(data, size);
166     OHOS::DistributedSchedule::GetDistributedComponentListInnerFuzzTest(data, size);
167     OHOS::DistributedSchedule::StartSyncMissionsFromRemoteInnerFuzzTest(data, size);
168     return 0;
169 }
170