1 /*
2 * Copyright (c) 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 "metainfomgr_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22
23 #include "meta_info_manager.h"
24 #include "meta_capability_info.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
SyncMetaInfoFromDBFuzzTest(const uint8_t * data,size_t size)28 void SyncMetaInfoFromDBFuzzTest(const uint8_t* data, size_t size)
29 {
30 if ((data == nullptr) || (size == 0)) {
31 return;
32 }
33 std::string deviceId(reinterpret_cast<const char*>(data), size);
34 MetaInfoManager::GetInstance()->Init();
35 MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(deviceId);
36 }
37
GetDataByKeyPrefixFuzzTest(const uint8_t * data,size_t size)38 void GetDataByKeyPrefixFuzzTest(const uint8_t* data, size_t size)
39 {
40 if ((data == nullptr) || (size == 0)) {
41 return;
42 }
43 std::string keyPrefix(reinterpret_cast<const char*>(data), size);
44 MetaCapInfoMap metaCapMap;
45 MetaInfoManager::GetInstance()->GetDataByKeyPrefix(keyPrefix, metaCapMap);
46 }
47
RemoveMetaInfoByKeyFuzzTest(const uint8_t * data,size_t size)48 void RemoveMetaInfoByKeyFuzzTest(const uint8_t* data, size_t size)
49 {
50 if ((data == nullptr) || (size == 0)) {
51 return;
52 }
53 std::string key(reinterpret_cast<const char*>(data), size);
54 MetaInfoManager::GetInstance()->RemoveMetaInfoByKey(key);
55 }
56
GetMetaCapInfoFuzzTest(const uint8_t * data,size_t size)57 void GetMetaCapInfoFuzzTest(const uint8_t* data, size_t size)
58 {
59 if ((data == nullptr) || (size == 0)) {
60 return;
61 }
62 std::string deviceId(reinterpret_cast<const char*>(data), size);
63 std::string udidHash(reinterpret_cast<const char*>(data), size);
64 std::string dhId(reinterpret_cast<const char*>(data), size);
65 uint16_t deviceType = 14;
66 std::shared_ptr<MetaCapabilityInfo> metaCapPtr = std::make_shared<MetaCapabilityInfo>(
67 dhId, deviceId, "devName_test", deviceType, DHType::CAMERA, "attrs_test", "subtype", udidHash,
68 CompVersion{ .sinkVersion = "1.0" });
69 std::string key = deviceId + "###" + dhId;
70 MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapPtr;
71 MetaInfoManager::GetInstance()->GetMetaCapInfo(deviceId, dhId, metaCapPtr);
72 }
73 }
74 }
75
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79 /* Run your code on data */
80 OHOS::DistributedHardware::SyncMetaInfoFromDBFuzzTest(data, size);
81 OHOS::DistributedHardware::GetDataByKeyPrefixFuzzTest(data, size);
82 OHOS::DistributedHardware::RemoveMetaInfoByKeyFuzzTest(data, size);
83 OHOS::DistributedHardware::GetMetaCapInfoFuzzTest(data, size);
84 return 0;
85 }
86
87