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 "distributedschedstubnine_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
NotifyCloseCollabSessionInnerFuzzTest(const uint8_t * data,size_t size)39 void NotifyCloseCollabSessionInnerFuzzTest(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 FuzzedDataProvider fdp(data, size);
48 std::string tokenId = fdp.ConsumeRandomLengthString();
49 dataParcel.WriteString(tokenId);
50 DistributedSchedService::GetInstance().NotifyCloseCollabSessionInner(dataParcel, reply);
51 }
52
GetSinkCollabVersionInnerFuzzTest(const uint8_t * data,size_t size)53 void GetSinkCollabVersionInnerFuzzTest(const uint8_t* data, size_t size)
54 {
55 if ((data == nullptr) || (size < sizeof(int32_t))) {
56 return;
57 }
58 FuzzUtil::MockPermission();
59 MessageParcel dataParcel;
60 MessageParcel reply;
61 FuzzedDataProvider fdp(data, size);
62 int32_t collabSessionId = fdp.ConsumeIntegral<int32_t>();
63 std::string sinkDeviceId = fdp.ConsumeRandomLengthString();
64 std::string collabToken = fdp.ConsumeRandomLengthString();
65 sptr<IRemoteObject> connect(new MockDistributedSched());
66 PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, collabSessionId);
67 PARCEL_WRITE_HELPER_NORET(dataParcel, String, sinkDeviceId);
68 PARCEL_WRITE_HELPER_NORET(dataParcel, String, collabToken);
69 dataParcel.WriteRemoteObject(connect);
70 DistributedSchedService::GetInstance().GetSinkCollabVersionInner(dataParcel, reply);
71 }
72
NotifyRejectReasonFuzzTest(const uint8_t * data,size_t size)73 void NotifyRejectReasonFuzzTest(const uint8_t* data, size_t size)
74 {
75 if ((data == nullptr) || (size < sizeof(int32_t))) {
76 return;
77 }
78 FuzzUtil::MockPermission();
79 MessageParcel dataParcel;
80 MessageParcel reply;
81 FuzzedDataProvider fdp(data, size);
82 std::string token = fdp.ConsumeRandomLengthString();
83 std::string reason = fdp.ConsumeRandomLengthString();
84 dataParcel.WriteString(token);
85 dataParcel.WriteString(reason);
86 DistributedSchedService::GetInstance().NotifyRejectReason(dataParcel, reply);
87 }
88
NotifyCollabPrepareResultInnerFuzzTest(const uint8_t * data,size_t size)89 void NotifyCollabPrepareResultInnerFuzzTest(const uint8_t* data, size_t size)
90 {
91 if ((data == nullptr) || (size < sizeof(int32_t))) {
92 return;
93 }
94 FuzzUtil::MockPermission();
95 MessageParcel dataParcel;
96 MessageParcel reply;
97 FuzzedDataProvider fdp(data, size);
98 std::string collabToken = fdp.ConsumeRandomLengthString();
99 int32_t ret = fdp.ConsumeIntegral<int32_t>();
100 int32_t sinkCollabSessionId = fdp.ConsumeIntegral<int32_t>();
101 std::string sinkSocketName = fdp.ConsumeRandomLengthString();
102 sptr<IRemoteObject> connect(new MockDistributedSched());
103 dataParcel.WriteString(collabToken);
104 dataParcel.WriteInt32(ret);
105 dataParcel.WriteInt32(sinkCollabSessionId);
106 dataParcel.WriteString(sinkSocketName);
107 dataParcel.WriteRemoteObject(connect);
108 DistributedSchedService::GetInstance().NotifyCollabPrepareResultInner(dataParcel, reply);
109 }
110
NotifyStartAbilityResultInnerFuzzTest(const uint8_t * data,size_t size)111 void NotifyStartAbilityResultInnerFuzzTest(const uint8_t* data, size_t size)
112 {
113 if ((data == nullptr) || (size < sizeof(int32_t))) {
114 return;
115 }
116 FuzzUtil::MockPermission();
117 FuzzedDataProvider fdp(data, size);
118 std::string collabToken = fdp.ConsumeRandomLengthString();
119 int32_t ret = fdp.ConsumeIntegral<int32_t>();
120 int32_t sinkPid = fdp.ConsumeIntegral<int32_t>();
121 int32_t sinkUid = fdp.ConsumeIntegral<int32_t>();
122 int32_t sinkAccessTokenId = fdp.ConsumeIntegral<int32_t>();
123
124 MessageParcel dataParcel;
125 MessageParcel reply;
126 dataParcel.WriteString(collabToken);
127 dataParcel.WriteInt32(ret);
128 dataParcel.WriteInt32(sinkPid);
129 dataParcel.WriteInt32(sinkUid);
130 dataParcel.WriteInt32(sinkAccessTokenId);
131 DistributedSchedService::GetInstance().NotifyStartAbilityResultInner(dataParcel, reply);
132 }
133 }
134 }
135
136 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
138 {
139 OHOS::DistributedSchedule::NotifyCloseCollabSessionInnerFuzzTest(data, size);
140 OHOS::DistributedSchedule::GetSinkCollabVersionInnerFuzzTest(data, size);
141 OHOS::DistributedSchedule::NotifyRejectReasonFuzzTest(data, size);
142 OHOS::DistributedSchedule::NotifyCollabPrepareResultInnerFuzzTest(data, size);
143 OHOS::DistributedSchedule::NotifyStartAbilityResultInnerFuzzTest(data, size);
144 return 0;
145 }
146