1 /*
2 * Copyright (c) 2023 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 "capabilityinfomanager_fuzzer.h"
17
18 #include "capability_info_manager.h"
19 #include "distributed_hardware_log.h"
20
21 namespace OHOS {
22 namespace DistributedHardware {
23
CapabilityInfoManagerFuzzTest(const uint8_t * data,size_t size)24 void CapabilityInfoManagerFuzzTest(const uint8_t* data, size_t size)
25 {
26 if ((data == nullptr) || (size <= sizeof(DistributedKv::ChangeNotification))) {
27 return;
28 }
29
30 DistributedKv::Entry insert;
31 DistributedKv::Entry update;
32 DistributedKv::Entry del;
33 insert.key = std::string(reinterpret_cast<const char*>(data), size);
34 update.key = std::string(reinterpret_cast<const char*>(data), size);
35 del.key = std::string(reinterpret_cast<const char*>(data), size);
36 insert.value = std::string(reinterpret_cast<const char*>(data), size);
37 update.value = std::string(reinterpret_cast<const char*>(data), size);
38 del.value = std::string(reinterpret_cast<const char*>(data), size);
39 std::vector<DistributedKv::Entry> inserts;
40 std::vector<DistributedKv::Entry> updates;
41 std::vector<DistributedKv::Entry> deleteds;
42 inserts.push_back(insert);
43 updates.push_back(update);
44 deleteds.push_back(del);
45 std::string deviceId(reinterpret_cast<const char*>(data), size);
46
47 DistributedKv::ChangeNotification changeIn(std::move(inserts), std::move(updates), std::move(deleteds),
48 deviceId, true);
49 CapabilityInfoManager::GetInstance()->OnChange(changeIn);
50 }
51
RemoveCapabilityInfoInMemFuzzTest(const uint8_t * data,size_t size)52 void RemoveCapabilityInfoInMemFuzzTest(const uint8_t* data, size_t size)
53 {
54 if ((data == nullptr) || (size == 0)) {
55 return;
56 }
57 std::string deviceId(reinterpret_cast<const char*>(data), size);
58 CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoInMem(deviceId);
59 }
60
RemoveCapabilityInfoByKeyFuzzTest(const uint8_t * data,size_t size)61 void RemoveCapabilityInfoByKeyFuzzTest(const uint8_t* data, size_t size)
62 {
63 if ((data == nullptr) || (size == 0)) {
64 return;
65 }
66 std::string key(reinterpret_cast<const char*>(data), size);
67 CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(key);
68 }
69
RemoveCapabilityInfoInDBFuzzTest(const uint8_t * data,size_t size)70 void RemoveCapabilityInfoInDBFuzzTest(const uint8_t* data, size_t size)
71 {
72 if ((data == nullptr) || (size == 0)) {
73 return;
74 }
75 std::string deviceId(reinterpret_cast<const char*>(data), size);
76 CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoInDB(deviceId);
77 }
78
SyncDeviceInfoFromDBFuzzTest(const uint8_t * data,size_t size)79 void SyncDeviceInfoFromDBFuzzTest(const uint8_t* data, size_t size)
80 {
81 if ((data == nullptr) || (size == 0)) {
82 return;
83 }
84 std::string deviceId(reinterpret_cast<const char*>(data), size);
85 CapabilityInfoManager::GetInstance()->Init();
86 CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId);
87 CapabilityInfoManager::GetInstance()->SyncRemoteCapabilityInfos();
88 CapabilityInfoManager::GetInstance()->UnInit();
89 }
90 }
91 }
92
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
95 {
96 /* Run your code on data */
97 OHOS::DistributedHardware::CapabilityInfoManagerFuzzTest(data, size);
98 OHOS::DistributedHardware::RemoveCapabilityInfoInMemFuzzTest(data, size);
99 OHOS::DistributedHardware::RemoveCapabilityInfoByKeyFuzzTest(data, size);
100 OHOS::DistributedHardware::RemoveCapabilityInfoInDBFuzzTest(data, size);
101 OHOS::DistributedHardware::SyncDeviceInfoFromDBFuzzTest(data, size);
102 return 0;
103 }
104
105