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