1 /*
2 * Copyright (c) 2024-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 "distributedfwkservices_fuzzer.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 #include <unistd.h>
24
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_service.h"
27 #include "get_dh_descriptors_callback_stub.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 namespace {
32 const int32_t SAID = 4801;
33 const uint32_t QUERY_LOCAL_SYS_SIZE = 6;
34 const QueryLocalSysSpecType SPEC_TYPE[QUERY_LOCAL_SYS_SIZE] = {
35 QueryLocalSysSpecType::MIN, QueryLocalSysSpecType::HISTREAMER_AUDIO_ENCODER,
36 QueryLocalSysSpecType::HISTREAMER_AUDIO_DECODER, QueryLocalSysSpecType::HISTREAMER_VIDEO_ENCODER,
37 QueryLocalSysSpecType::HISTREAMER_VIDEO_DECODER, QueryLocalSysSpecType::MAX
38 };
39 }
40
41 class MyFwkServicesFuzzTest : public IRemoteStub<IPublisherListener> {
42 public:
AsObject()43 virtual sptr<IRemoteObject> AsObject() override
44 {
45 return nullptr;
46 }
OnMessage(const DHTopic topic,const std::string & message)47 void OnMessage(const DHTopic topic, const std::string& message) override
48 {
49 return;
50 }
51 };
52
53 class TestGetDistributedHardwareCallback : public GetDhDescriptorsCallbackStub {
54 public:
55 TestGetDistributedHardwareCallback() = default;
56 virtual ~TestGetDistributedHardwareCallback() = default;
57 protected:
OnSuccess(const std::string & networkId,const std::vector<DHDescriptor> & descriptors,EnableStep enableStep)58 void OnSuccess(const std::string &networkId, const std::vector<DHDescriptor> &descriptors,
59 EnableStep enableStep) override
60 {
61 (void)networkId;
62 (void)descriptors;
63 (void)enableStep;
64 }
OnError(const std::string & networkId,int32_t error)65 void OnError(const std::string &networkId, int32_t error) override
66 {
67 (void)networkId;
68 (void)error;
69 }
70 };
71
FwkServicesQueryLocalSysSpecFuzzTest(const uint8_t * data,size_t size)72 void FwkServicesQueryLocalSysSpecFuzzTest(const uint8_t* data, size_t size)
73 {
74 if ((data == nullptr) || (size < sizeof(uint32_t))) {
75 return;
76 }
77 DistributedHardwareService service(SAID, true);
78 uint32_t sysSpec = *(reinterpret_cast<const uint32_t*>(data));
79 QueryLocalSysSpecType spec = SPEC_TYPE[sysSpec % QUERY_LOCAL_SYS_SIZE];
80
81 service.QueryLocalSysSpec(spec);
82 }
83
FwkServicesQueryDhSysSpecFuzzTest(const uint8_t * data,size_t size)84 void FwkServicesQueryDhSysSpecFuzzTest(const uint8_t* data, size_t size)
85 {
86 if ((data == nullptr) || (size == 0)) {
87 return;
88 }
89 DistributedHardwareService service(SAID, true);
90 std::string targetKey(reinterpret_cast<const char*>(data), size);
91 std::string attrs(reinterpret_cast<const char*>(data), size);
92
93 service.QueryDhSysSpec(targetKey, attrs);
94 }
95
FwkServicesNotifySourceRemoteSinkStartedFuzzTest(const uint8_t * data,size_t size)96 void FwkServicesNotifySourceRemoteSinkStartedFuzzTest(const uint8_t* data, size_t size)
97 {
98 if ((data == nullptr) || (size == 0)) {
99 return;
100 }
101 DistributedHardwareService service(SAID, true);
102 std::string deviceId(reinterpret_cast<const char*>(data), size);
103
104 service.NotifySourceRemoteSinkStarted(deviceId);
105 }
106
FwkServicesPauseDistributedHardwareFuzzTest(const uint8_t * data,size_t size)107 void FwkServicesPauseDistributedHardwareFuzzTest(const uint8_t* data, size_t size)
108 {
109 if ((data == nullptr) || (size == 0)) {
110 return;
111 }
112 DistributedHardwareService service(SAID, true);
113 DHType dhType = DHType::AUDIO;
114 std::string networkId(reinterpret_cast<const char*>(data), size);
115
116 service.PauseDistributedHardware(dhType, networkId);
117 }
118
FwkServicesResumeDistributedHardwareFuzzTest(const uint8_t * data,size_t size)119 void FwkServicesResumeDistributedHardwareFuzzTest(const uint8_t* data, size_t size)
120 {
121 if ((data == nullptr) || (size == 0)) {
122 return;
123 }
124 DistributedHardwareService service(SAID, true);
125 DHType dhType = DHType::AUDIO;
126 std::string networkId(reinterpret_cast<const char*>(data), size);
127
128 service.ResumeDistributedHardware(dhType, networkId);
129 }
130
FwkServicesStopDistributedHardwareFuzzTest(const uint8_t * data,size_t size)131 void FwkServicesStopDistributedHardwareFuzzTest(const uint8_t* data, size_t size)
132 {
133 if ((data == nullptr) || (size == 0)) {
134 return;
135 }
136 DistributedHardwareService service(SAID, true);
137 DHType dhType = DHType::AUDIO;
138 std::string networkId(reinterpret_cast<const char*>(data), size);
139
140 service.StopDistributedHardware(dhType, networkId);
141 }
142
FwkServicesGetDistributedHardwareFuzzTest(const uint8_t * data,size_t size)143 void FwkServicesGetDistributedHardwareFuzzTest(const uint8_t* data, size_t size)
144 {
145 if ((data == nullptr) || (size == 0)) {
146 return;
147 }
148 DistributedHardwareService service(SAID, true);
149 std::string networkId(reinterpret_cast<const char*>(data), size);
150 sptr<IGetDhDescriptorsCallback> callback(new TestGetDistributedHardwareCallback());
151 EnableStep enableStep = EnableStep::ENABLE_SOURCE;
152 service.GetDistributedHardware(networkId, enableStep, callback);
153 }
154
FwkServicesRegisterDHStatusListenerFuzzTest(const uint8_t * data,size_t size)155 void FwkServicesRegisterDHStatusListenerFuzzTest(const uint8_t* data, size_t size)
156 {
157 if ((data == nullptr) || (size == 0)) {
158 return;
159 }
160 DistributedHardwareService service(SAID, true);
161 std::string networkId(reinterpret_cast<const char*>(data), size);
162 sptr<IHDSourceStatusListener> listener = nullptr;
163
164 service.RegisterDHStatusListener(networkId, listener);
165 }
166
FwkServicesUnregisterDHStatusListenerFuzzTest(const uint8_t * data,size_t size)167 void FwkServicesUnregisterDHStatusListenerFuzzTest(const uint8_t* data, size_t size)
168 {
169 if ((data == nullptr) || (size == 0)) {
170 return;
171 }
172 DistributedHardwareService service(SAID, true);
173 std::string networkId(reinterpret_cast<const char*>(data), size);
174 sptr<IHDSourceStatusListener> listener = nullptr;
175
176 service.UnregisterDHStatusListener(networkId, listener);
177 }
178
FwkServicesEnableSinkFuzzTest(const uint8_t * data,size_t size)179 void FwkServicesEnableSinkFuzzTest(const uint8_t* data, size_t size)
180 {
181 if ((data == nullptr) || (size == 0)) {
182 return;
183 }
184 DistributedHardwareService service(SAID, true);
185 std::vector<DHDescriptor> descriptors = {
186 { std::string(reinterpret_cast<const char*>(data), size), DHType::AUDIO }
187 };
188
189 service.EnableSink(descriptors);
190 }
191
FwkServicesDisableSinkFuzzTest(const uint8_t * data,size_t size)192 void FwkServicesDisableSinkFuzzTest(const uint8_t* data, size_t size)
193 {
194 if ((data == nullptr) || (size == 0)) {
195 return;
196 }
197 DistributedHardwareService service(SAID, true);
198 std::vector<DHDescriptor> descriptors = {
199 { std::string(reinterpret_cast<const char*>(data), size), DHType::AUDIO }
200 };
201
202 service.DisableSink(descriptors);
203 }
204
FwkServicesEnableSourceFuzzTest(const uint8_t * data,size_t size)205 void FwkServicesEnableSourceFuzzTest(const uint8_t* data, size_t size)
206 {
207 if ((data == nullptr) || (size == 0)) {
208 return;
209 }
210 DistributedHardwareService service(SAID, true);
211 std::string networkId(reinterpret_cast<const char*>(data), size);
212 std::vector<DHDescriptor> descriptors = {
213 { std::string(reinterpret_cast<const char*>(data), size), DHType::AUDIO }
214 };
215
216 service.EnableSource(networkId, descriptors);
217 }
218
FwkServicesDisableSourceFuzzTest(const uint8_t * data,size_t size)219 void FwkServicesDisableSourceFuzzTest(const uint8_t* data, size_t size)
220 {
221 if ((data == nullptr) || (size == 0)) {
222 return;
223 }
224 DistributedHardwareService service(SAID, true);
225 std::string networkId(reinterpret_cast<const char*>(data), size);
226 std::vector<DHDescriptor> descriptors = {
227 { std::string(reinterpret_cast<const char*>(data), size), DHType::AUDIO }
228 };
229
230 service.DisableSource(networkId, descriptors);
231 }
232 }
233 }
234
235 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)236 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
237 {
238 /* Run your code on data */
239 OHOS::DistributedHardware::FwkServicesQueryLocalSysSpecFuzzTest(data, size);
240 OHOS::DistributedHardware::FwkServicesQueryDhSysSpecFuzzTest(data, size);
241 OHOS::DistributedHardware::FwkServicesPauseDistributedHardwareFuzzTest(data, size);
242 OHOS::DistributedHardware::FwkServicesResumeDistributedHardwareFuzzTest(data, size);
243 OHOS::DistributedHardware::FwkServicesStopDistributedHardwareFuzzTest(data, size);
244 return 0;
245 }
246