1 /*
2 * Copyright (c) 2023-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 "accessmanageroffline_fuzzer.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdint>
22 #include <securec.h>
23 #include <string>
24 #include <unistd.h>
25
26 #include "access_manager.h"
27 #include "distributed_hardware_errno.h"
28 #include "distributed_hardware_manager_factory.h"
29 #include "dh_context.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
33 namespace {
34 constexpr uint32_t SLEEP_TIME_US = 10 * 1000;
35 }
36
AccessManagerOfflineFuzzTest(const uint8_t * data,size_t size)37 void AccessManagerOfflineFuzzTest(const uint8_t* data, size_t size)
38 {
39 if ((data == nullptr) || (size > DM_MAX_DEVICE_ID_LEN)) {
40 return;
41 }
42
43 AccessManager::GetInstance()->Init();
44 DmDeviceInfo deviceInfo;
45 int32_t ret = memcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, (reinterpret_cast<const char *>(data)), size);
46 if (ret != EOK) {
47 return;
48 }
49 AccessManager::GetInstance()->OnDeviceOffline(deviceInfo);
50
51 usleep(SLEEP_TIME_US);
52 }
53
AccessManagerOfflineSecondFuzzTest(const uint8_t * data,size_t size)54 void AccessManagerOfflineSecondFuzzTest(const uint8_t* data, size_t size)
55 {
56 if ((data == nullptr) || (size == 0) || (size > DM_MAX_DEVICE_ID_LEN)) {
57 return;
58 }
59
60 AccessManager::GetInstance()->Init();
61 std::string networkId(reinterpret_cast<const char*>(data), size);
62 std::string udId(reinterpret_cast<const char*>(data), size);
63 std::string uuId(reinterpret_cast<const char*>(data), size);
64 DHContext::GetInstance().AddOnlineDevice(udId, uuId, networkId);
65 DmDeviceInfo deviceInfo;
66 int32_t ret = memcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, networkId.c_str(), size);
67 if (ret != EOK) {
68 return;
69 }
70
71 AccessManager::GetInstance()->OnDeviceOffline(deviceInfo);
72 DHContext::GetInstance().RemoveOnlineDeviceIdEntryByNetworkId(networkId);
73 usleep(SLEEP_TIME_US);
74 }
75 }
76 }
77
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81 /* Run your code on data */
82 OHOS::DistributedHardware::AccessManagerOfflineFuzzTest(data, size);
83 OHOS::DistributedHardware::AccessManagerOfflineSecondFuzzTest(data, size);
84 return 0;
85 }
86
87