• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "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 
GetMetaCapInfoFuzzTest(const uint8_t * data,size_t size)48 void GetMetaCapInfoFuzzTest(const uint8_t* data, size_t size)
49 {
50     if ((data == nullptr) || (size == 0)) {
51         return;
52     }
53     std::string deviceId(reinterpret_cast<const char*>(data), size);
54     std::string udidHash(reinterpret_cast<const char*>(data), size);
55     std::string dhId(reinterpret_cast<const char*>(data), size);
56     uint16_t deviceType = 14;
57     std::shared_ptr<MetaCapabilityInfo> metaCapPtr = std::make_shared<MetaCapabilityInfo>(
58         dhId, deviceId, "devName_test", deviceType, DHType::CAMERA, "attrs_test", "subtype", udidHash,
59         CompVersion{ .sinkVersion = "1.0" });
60     std::string key = deviceId + "###" + dhId;
61     MetaInfoManager::GetInstance()->globalMetaInfoMap_[key] = metaCapPtr;
62     MetaInfoManager::GetInstance()->GetMetaCapInfo(deviceId, dhId, metaCapPtr);
63 }
64 }
65 }
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     /* Run your code on data */
71     OHOS::DistributedHardware::SyncMetaInfoFromDBFuzzTest(data, size);
72     OHOS::DistributedHardware::GetDataByKeyPrefixFuzzTest(data, size);
73     OHOS::DistributedHardware::GetMetaCapInfoFuzzTest(data, size);
74     return 0;
75 }
76 
77