1 /*
2 * Copyright (c) 2022-2024 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 <string>
19
20 #include "dm_constants.h"
21 #include "device_manager_impl.h"
22 #include "device_manager.h"
23 #include "device_manager_callback.h"
24 #include "un_authenticate_device_fuzzer.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
UnAuthenticateDeviceFuzzTest(const uint8_t * data,size_t size)28 void UnAuthenticateDeviceFuzzTest(const uint8_t* data, size_t size)
29 {
30 if ((data == nullptr) || (size < sizeof(int32_t) || (size > DM_MAX_DEVICE_ID_LEN))) {
31 return;
32 }
33
34 std::string bundleName(reinterpret_cast<const char*>(data), size);
35 std::string deviceId(reinterpret_cast<const char*>(data), size);
36 DmDeviceInfo deviceInfo;
37 if (memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, (reinterpret_cast<const char *>(data)), size) != DM_OK) {
38 return;
39 }
40 if (memcpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_ID_LEN, (reinterpret_cast<const char *>(data)), size) != DM_OK) {
41 return;
42 }
43 if (memcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, (reinterpret_cast<const char *>(data)), size) != DM_OK) {
44 return;
45 }
46 deviceInfo.deviceTypeId = *(reinterpret_cast<const uint16_t *>(data));
47 deviceInfo.range = *(reinterpret_cast<const int32_t *>(data));
48 deviceInfo.networkType = *(reinterpret_cast<const int32_t *>(data));
49 std::string extraData(reinterpret_cast<const char*>(data), size);
50 deviceInfo.extraData = extraData;
51 deviceInfo.authForm = *(reinterpret_cast<const DmAuthForm*>(data));
52
53 DeviceManager::GetInstance().UnAuthenticateDevice(bundleName, deviceInfo);
54 DeviceManager::GetInstance().UnBindDevice(bundleName, deviceId);
55 }
56 }
57 }
58
59 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)60 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
61 {
62 /* Run your code on data */
63 OHOS::DistributedHardware::UnAuthenticateDeviceFuzzTest(data, size);
64 return 0;
65 }
66