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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <unistd.h>
21 #include <unordered_map>
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 #include "device_manager_impl.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28
29 namespace {}
30 class DevTrustChangeCallbackTest : public DevTrustChangeCallback {
31 public:
~DevTrustChangeCallbackTest()32 virtual ~DevTrustChangeCallbackTest() {}
OnDeviceTrustChange(const std::string & udid,const std::string & uuid,DmAuthForm authForm)33 void OnDeviceTrustChange(const std::string &udid, const std::string &uuid, DmAuthForm authForm) override {}
34 };
35
36 class CredentialAuthStatusCallbackTest : public CredentialAuthStatusCallback {
37 public:
~CredentialAuthStatusCallbackTest()38 virtual ~CredentialAuthStatusCallbackTest() {}
OnCredentialAuthStatus(const std::string & deviceList,uint16_t deviceTypeId,int32_t errcode)39 void OnCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode) override {}
40 };
41
StopAuthenticateDeviceTest(const uint8_t * data,size_t size)42 void StopAuthenticateDeviceTest(const uint8_t *data, size_t size)
43 {
44 if ((data == nullptr) || (size == 0)) {
45 return;
46 }
47 FuzzedDataProvider fdp(data, size);
48 std::string pkgName = fdp.ConsumeRandomLengthString();
49 DeviceManagerImpl::GetInstance().StopAuthenticateDevice(pkgName);
50 std::string emptyPkgName = "";
51 DeviceManagerImpl::GetInstance().StopAuthenticateDevice(emptyPkgName);
52 DeviceManagerImpl::GetInstance().OnDmServiceDied();
53 }
54
UnBindDeviceTest(const uint8_t * data,size_t size)55 void UnBindDeviceTest(const uint8_t *data, size_t size)
56 {
57 if ((data == nullptr) || (size == 0)) {
58 return;
59 }
60 FuzzedDataProvider fdp(data, size);
61 std::string pkgName = fdp.ConsumeRandomLengthString();
62 std::string deviceId = fdp.ConsumeRandomLengthString();
63 DeviceManagerImpl::GetInstance().UnBindDevice(pkgName, deviceId);
64 }
65
ShiftLNNGearTest(const uint8_t * data,size_t size)66 void ShiftLNNGearTest(const uint8_t *data, size_t size)
67 {
68 if ((data == nullptr) || (size == 0)) {
69 return;
70 }
71 FuzzedDataProvider fdp(data, size);
72 std::string pkgName = fdp.ConsumeRandomLengthString();
73 DeviceManagerImpl::GetInstance().ShiftLNNGear(pkgName);
74 }
75
RegDevTrustChangeCallbackTest(const uint8_t * data,size_t size)76 void RegDevTrustChangeCallbackTest(const uint8_t *data, size_t size)
77 {
78 if ((data == nullptr) || (size == 0)) {
79 return;
80 }
81 FuzzedDataProvider fdp(data, size);
82 std::shared_ptr<DevTrustChangeCallback> callback = nullptr;
83 std::string pkgName = fdp.ConsumeRandomLengthString();
84 DeviceManagerImpl::GetInstance().RegDevTrustChangeCallback(pkgName, callback);
85 callback = std::make_shared<DevTrustChangeCallbackTest>();
86 DeviceManagerImpl::GetInstance().RegDevTrustChangeCallback(pkgName, callback);
87 }
88
GetNetworkIdByUdidTest(const uint8_t * data,size_t size)89 void GetNetworkIdByUdidTest(const uint8_t *data, size_t size)
90 {
91 if ((data == nullptr) || (size == 0)) {
92 return;
93 }
94 FuzzedDataProvider fdp(data, size);
95 std::string pkgName = fdp.ConsumeRandomLengthString();
96 std::string udid = fdp.ConsumeRandomLengthString();
97 std::string networkId = fdp.ConsumeRandomLengthString();
98 DeviceManagerImpl::GetInstance().GetNetworkIdByUdid(pkgName, udid, networkId);
99 DeviceManagerImpl::GetInstance().GetNetworkIdByUdid("pkgName", "udid", networkId);
100 }
101
RegisterCredentialAuthStatusCallbackTest(const uint8_t * data,size_t size)102 void RegisterCredentialAuthStatusCallbackTest(const uint8_t *data, size_t size)
103 {
104 if ((data == nullptr) || (size == 0)) {
105 return;
106 }
107 FuzzedDataProvider fdp(data, size);
108 std::string pkgName = fdp.ConsumeRandomLengthString();
109 std::shared_ptr<CredentialAuthStatusCallback> callback = nullptr;
110 DeviceManagerImpl::GetInstance().RegisterCredentialAuthStatusCallback(pkgName, callback);
111 callback = std::make_shared<CredentialAuthStatusCallbackTest>();
112 DeviceManagerImpl::GetInstance().RegisterCredentialAuthStatusCallback(pkgName, callback);
113 DeviceManagerImpl::GetInstance().UnRegisterCredentialAuthStatusCallback(pkgName);
114 DeviceManagerImpl::GetInstance().UnRegisterCredentialAuthStatusCallback("");
115 }
116
GetAllTrustedDeviceListTest(const uint8_t * data,size_t size)117 void GetAllTrustedDeviceListTest(const uint8_t *data, size_t size)
118 {
119 if ((data == nullptr) || (size == 0)) {
120 return;
121 }
122 FuzzedDataProvider fdp(data, size);
123 std::string pkgName = fdp.ConsumeRandomLengthString();
124 std::string extra = fdp.ConsumeRandomLengthString();
125 std::vector<DmDeviceInfo> deviceList;
126 DeviceManagerImpl::GetInstance().GetAllTrustedDeviceList(pkgName, extra, deviceList);
127 std::string emptyPkgName = "";
128 DeviceManagerImpl::GetInstance().GetAllTrustedDeviceList(emptyPkgName, extra, deviceList);
129 }
130
RegisterSinkBindCallbackTest(const uint8_t * data,size_t size)131 void RegisterSinkBindCallbackTest(const uint8_t *data, size_t size)
132 {
133 if ((data == nullptr) || (size == 0)) {
134 return;
135 }
136 FuzzedDataProvider fdp(data, size);
137 std::string pkgName = fdp.ConsumeRandomLengthString();
138 std::shared_ptr<BindTargetCallback> callback = nullptr;
139 DeviceManagerImpl::GetInstance().RegisterSinkBindCallback(pkgName, callback);
140 std::string emptyPkgName = "";
141 DeviceManagerImpl::GetInstance().RegisterSinkBindCallback(emptyPkgName, callback);
142 }
143
GetDeviceProfileInfoListTest(const uint8_t * data,size_t size)144 void GetDeviceProfileInfoListTest(const uint8_t *data, size_t size)
145 {
146 if ((data == nullptr) || (size == 0)) {
147 return;
148 }
149 FuzzedDataProvider fdp(data, size);
150 std::string pkgName = fdp.ConsumeRandomLengthString();
151 DmDeviceProfileInfoFilterOptions filterOptions;
152 std::shared_ptr<GetDeviceProfileInfoListCallback> callback = nullptr;
153 DeviceManagerImpl::GetInstance().GetDeviceProfileInfoList(pkgName, filterOptions, callback);
154 }
155
GetDeviceIconInfoTest(const uint8_t * data,size_t size)156 void GetDeviceIconInfoTest(const uint8_t *data, size_t size)
157 {
158 if ((data == nullptr) || (size == 0)) {
159 return;
160 }
161 FuzzedDataProvider fdp(data, size);
162 std::string pkgName = fdp.ConsumeRandomLengthString();
163 DmDeviceIconInfoFilterOptions filterOptions;
164 std::shared_ptr<GetDeviceIconInfoCallback> callback = nullptr;
165 DeviceManagerImpl::GetInstance().GetDeviceIconInfo(pkgName, filterOptions, callback);
166 }
167
PutDeviceProfileInfoListTest(const uint8_t * data,size_t size)168 void PutDeviceProfileInfoListTest(const uint8_t *data, size_t size)
169 {
170 if ((data == nullptr) || (size == 0)) {
171 return;
172 }
173 FuzzedDataProvider fdp(data, size);
174 std::string pkgName = fdp.ConsumeRandomLengthString();
175 std::vector<OHOS::DistributedHardware::DmDeviceProfileInfo> deviceProfileInfoList;
176 DeviceManagerImpl::GetInstance().PutDeviceProfileInfoList(pkgName, deviceProfileInfoList);
177 }
178
GetLocalDisplayDeviceNameTest(const uint8_t * data,size_t size)179 void GetLocalDisplayDeviceNameTest(const uint8_t *data, size_t size)
180 {
181 if ((data == nullptr) || (size < sizeof(int32_t))) {
182 return;
183 }
184 FuzzedDataProvider fdp(data, size);
185 std::string pkgName = fdp.ConsumeRandomLengthString();
186 std::string displayName = fdp.ConsumeRandomLengthString();
187 int32_t maxNameLength = fdp.ConsumeIntegral<int32_t>();
188 DeviceManagerImpl::GetInstance().GetLocalDisplayDeviceName(pkgName, maxNameLength, displayName);
189 }
190
GetDeviceNetworkIdListTest(const uint8_t * data,size_t size)191 void GetDeviceNetworkIdListTest(const uint8_t *data, size_t size)
192 {
193 if ((data == nullptr) || (size == 0)) {
194 return;
195 }
196 FuzzedDataProvider fdp(data, size);
197 std::string bundleName = fdp.ConsumeRandomLengthString();
198 NetworkIdQueryFilter queryFilter;
199 std::vector<std::string> networkIds;
200 DeviceManagerImpl::GetInstance().GetDeviceNetworkIdList(bundleName, queryFilter, networkIds);
201 }
202
SetLocalDeviceNameTest(const uint8_t * data,size_t size)203 void SetLocalDeviceNameTest(const uint8_t *data, size_t size)
204 {
205 if ((data == nullptr) || (size == 0)) {
206 return;
207 }
208 FuzzedDataProvider fdp(data, size);
209 std::string pkgName = fdp.ConsumeRandomLengthString();
210 std::string deviceName = fdp.ConsumeRandomLengthString();
211 std::shared_ptr<SetLocalDeviceNameCallback> callback = nullptr;
212 DeviceManagerImpl::GetInstance().SetLocalDeviceName(pkgName, deviceName, callback);
213 }
214
SetRemoteDeviceNameTest(const uint8_t * data,size_t size)215 void SetRemoteDeviceNameTest(const uint8_t *data, size_t size)
216 {
217 if ((data == nullptr) || (size == 0)) {
218 return;
219 }
220 FuzzedDataProvider fdp(data, size);
221 std::string pkgName = fdp.ConsumeRandomLengthString();
222 std::string deviceId = fdp.ConsumeRandomLengthString();
223 std::string deviceName = fdp.ConsumeRandomLengthString();
224 std::shared_ptr<SetRemoteDeviceNameCallback> callback = nullptr;
225 DeviceManagerImpl::GetInstance().SetRemoteDeviceName(pkgName, deviceId, deviceName, callback);
226 }
227
RestoreLocalDeviceNameTest(const uint8_t * data,size_t size)228 void RestoreLocalDeviceNameTest(const uint8_t *data, size_t size)
229 {
230 if ((data == nullptr) || (size == 0)) {
231 return;
232 }
233 FuzzedDataProvider fdp(data, size);
234 std::string pkgName = fdp.ConsumeRandomLengthString();
235 DeviceManagerImpl::GetInstance().RestoreLocalDeviceName(pkgName);
236 std::string emptyPkgName = "";
237 DeviceManagerImpl::GetInstance().RestoreLocalDeviceName(emptyPkgName);
238 }
239
GetLocalServiceInfoByBundleNameAndPinExchangeTypeTest(const uint8_t * data,size_t size)240 void GetLocalServiceInfoByBundleNameAndPinExchangeTypeTest(const uint8_t *data, size_t size)
241 {
242 if ((data == nullptr) || (size < sizeof(int32_t))) {
243 return;
244 }
245 FuzzedDataProvider fdp(data, size);
246 std::string bundleName = fdp.ConsumeRandomLengthString();
247 int32_t maxNameLength = fdp.ConsumeIntegral<int32_t>();
248 DMLocalServiceInfo info;
249 DeviceManagerImpl::GetInstance().GetLocalServiceInfoByBundleNameAndPinExchangeType(bundleName, maxNameLength, info);
250 }
251
GetErrorCodeTest(const uint8_t * data,size_t size)252 void GetErrorCodeTest(const uint8_t *data, size_t size)
253 {
254 if ((data == nullptr) || (size < sizeof(int32_t))) {
255 return;
256 }
257 FuzzedDataProvider fdp(data, size);
258 int32_t errCode = fdp.ConsumeIntegral<int32_t>();
259 DeviceManagerImpl::GetInstance().GetErrCode(errCode);
260 }
261
UnRegisterPinHolderCallbackTest(const uint8_t * data,size_t size)262 void UnRegisterPinHolderCallbackTest(const uint8_t *data, size_t size)
263 {
264 if ((data == nullptr) || (size == 0)) {
265 return;
266 }
267 FuzzedDataProvider fdp(data, size);
268 std::string pkgName = fdp.ConsumeRandomLengthString();
269 DeviceManagerImpl::GetInstance().UnRegisterPinHolderCallback(pkgName);
270 std::string emptyPkgName = "";
271 DeviceManagerImpl::GetInstance().UnRegisterPinHolderCallback(emptyPkgName);
272 }
273
DeviceManagerImplFuzzTest(const uint8_t * data,size_t size)274 void DeviceManagerImplFuzzTest(const uint8_t *data, size_t size)
275 {
276 if ((data == nullptr) || (size < sizeof(int32_t))) {
277 return;
278 }
279 FuzzedDataProvider fdp(data, size);
280 std::string pkgName = fdp.ConsumeRandomLengthString();
281 std::string deviceName = fdp.ConsumeRandomLengthString();
282 std::string deviceId = fdp.ConsumeRandomLengthString();
283 std::string extra = fdp.ConsumeRandomLengthString();
284 std::string bundleName = fdp.ConsumeRandomLengthString();
285 int32_t pinExchangeType = fdp.ConsumeIntegral<int32_t>();
286 OHOS::DistributedHardware::DmDeviceIconInfoFilterOptions filterOptions;
287 DMLocalServiceInfo localServiceInfo;
288 DmAccessCaller caller;
289 DmAccessCallee callee;
290 DMIpcCmdInterfaceCode ipcCode = REGISTER_DEVICE_MANAGER_LISTENER;
291 DeviceManagerImpl::GetInstance().GetLocalDeviceName(pkgName, deviceName);
292 DeviceManagerImpl::GetInstance().GetLocalDeviceName(pkgName);
293 DeviceManagerImpl::GetInstance().UnBindDevice(pkgName, deviceId, extra);
294 DeviceManagerImpl::GetInstance().UnBindDevice("pkgName", "deviceId", extra);
295 DeviceManagerImpl::GetInstance().UnRegisterSinkBindCallback(pkgName);
296 DeviceManagerImpl::GetInstance().UnRegisterSinkBindCallback("");
297 DeviceManagerImpl::GetInstance().RegisterLocalServiceInfo(localServiceInfo);
298 DeviceManagerImpl::GetInstance().UnRegisterLocalServiceInfo(bundleName, pinExchangeType);
299 DeviceManagerImpl::GetInstance().UpdateLocalServiceInfo(localServiceInfo);
300 DeviceManagerImpl::GetInstance().CheckAccessControl(caller, callee);
301 DeviceManagerImpl::GetInstance().CheckIsSameAccount(caller, callee);
302 DeviceManagerImpl::GetInstance().CheckSrcAccessControl(caller, callee);
303 DeviceManagerImpl::GetInstance().CheckSinkAccessControl(caller, callee);
304 DeviceManagerImpl::GetInstance().CheckSrcIsSameAccount(caller, callee);
305 DeviceManagerImpl::GetInstance().CheckSinkIsSameAccount(caller, callee);
306 DeviceManagerImpl::GetInstance().CheckAclByIpcCode(caller, callee, ipcCode);
307 }
308
SyncCallbacksToServiceFuzzTest(const uint8_t * data,size_t size)309 void SyncCallbacksToServiceFuzzTest(const uint8_t* data, size_t size)
310 {
311 int32_t maxStringLength = 64;
312 if (data == nullptr || size < sizeof(int32_t) + maxStringLength) {
313 return;
314 }
315
316 FuzzedDataProvider fdp(data, size);
317
318 std::map<DmCommonNotifyEvent, std::set<std::string>> callbackMap;
319 DeviceManagerImpl::GetInstance().SyncCallbacksToService(callbackMap);
320
321 int32_t minCount = 1;
322 int32_t maxCount = 5;
323 DmCommonNotifyEvent event = static_cast<DmCommonNotifyEvent>(fdp.ConsumeIntegral<int32_t>());
324 std::set<std::string> pkgNames;
325 callbackMap[event] = pkgNames;
326 DeviceManagerImpl::GetInstance().SyncCallbacksToService(callbackMap);
327 int32_t pkgCount = fdp.ConsumeIntegralInRange<int32_t>(minCount, maxCount);
328 for (int32_t j = 0; j < pkgCount; ++j) {
329 pkgNames.insert(fdp.ConsumeRandomLengthString(maxStringLength));
330 }
331 callbackMap[event] = pkgNames;
332 DeviceManagerImpl::GetInstance().SyncCallbacksToService(callbackMap);
333 }
334 }
335 }
336
337 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)338 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
339 {
340 /* Run your code on data */
341 OHOS::DistributedHardware::StopAuthenticateDeviceTest(data, size);
342 OHOS::DistributedHardware::UnBindDeviceTest(data, size);
343 OHOS::DistributedHardware::ShiftLNNGearTest(data, size);
344 OHOS::DistributedHardware::RegDevTrustChangeCallbackTest(data, size);
345 OHOS::DistributedHardware::GetNetworkIdByUdidTest(data, size);
346 OHOS::DistributedHardware::RegisterCredentialAuthStatusCallbackTest(data, size);
347 OHOS::DistributedHardware::GetAllTrustedDeviceListTest(data, size);
348 OHOS::DistributedHardware::RegisterSinkBindCallbackTest(data, size);
349 OHOS::DistributedHardware::GetDeviceProfileInfoListTest(data, size);
350 OHOS::DistributedHardware::GetDeviceIconInfoTest(data, size);
351 OHOS::DistributedHardware::PutDeviceProfileInfoListTest(data, size);
352 OHOS::DistributedHardware::GetLocalDisplayDeviceNameTest(data, size);
353 OHOS::DistributedHardware::GetDeviceNetworkIdListTest(data, size);
354 OHOS::DistributedHardware::SetLocalDeviceNameTest(data, size);
355 OHOS::DistributedHardware::SetRemoteDeviceNameTest(data, size);
356 OHOS::DistributedHardware::RestoreLocalDeviceNameTest(data, size);
357 OHOS::DistributedHardware::GetLocalServiceInfoByBundleNameAndPinExchangeTypeTest(data, size);
358 OHOS::DistributedHardware::UnRegisterPinHolderCallbackTest(data, size);
359 OHOS::DistributedHardware::DeviceManagerImplFuzzTest(data, size);
360 OHOS::DistributedHardware::GetErrorCodeTest(data, size);
361 OHOS::DistributedHardware::SyncCallbacksToServiceFuzzTest(data, size);
362 return 0;
363 }
364