1 /*
2  * Copyright (c) 2022 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 "resourcemanager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22 
23 #include "capability_info.h"
24 #include "capability_info_manager.h"
25 #include "dh_context.h"
26 #include "distributed_hardware_errno.h"
27 #include "distributed_hardware_log.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
31 namespace {
32     const uint32_t DH_TYPE_SIZE = 10;
33     const DHType dhTypeFuzz[DH_TYPE_SIZE] = {
34         DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_AUDIO,
35         DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP
36     };
37 }
38 
ResourceManagerFuzzTest(const uint8_t * data,size_t size)39 void ResourceManagerFuzzTest(const uint8_t* data, size_t size)
40 {
41     if ((data == nullptr) || (size < sizeof(uint16_t))) {
42         return;
43     }
44 
45     std::string dhId(reinterpret_cast<const char*>(data), size);
46     std::string devId(reinterpret_cast<const char*>(data), size);
47     std::string devName(reinterpret_cast<const char*>(data), size);
48     uint16_t devType = *(reinterpret_cast<const uint16_t*>(data));
49     DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE];
50     std::string dhAttrs(reinterpret_cast<const char*>(data), size);
51     std::string dhSubtype(reinterpret_cast<const char*>(data), size);
52 
53     std::shared_ptr<CapabilityInfo> capInfo =
54         std::make_shared<CapabilityInfo>(dhId, devId, devName, devType, dhType, dhAttrs, dhSubtype);
55     std::vector<std::shared_ptr<CapabilityInfo>> resInfos;
56     resInfos.emplace_back(capInfo);
57 
58     CapabilityInfoManager::GetInstance()->AddCapability(resInfos);
59 
60     std::shared_ptr<CapabilityInfo> info;
61     CapabilityInfoManager::GetInstance()->GetCapability(capInfo->GetDeviceId(), capInfo->GetDHId(), info);
62     CapabilityInfoManager::GetInstance()->GetDataByKey(capInfo->GetKey(), info);
63 
64     CapabilityInfoMap capMap;
65     CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(capInfo->GetDeviceId(), capMap);
66     CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(capInfo->GetKey());
67 }
68 }
69 }
70 
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
73 {
74     /* Run your code on data */
75     OHOS::DistributedHardware::ResourceManagerFuzzTest(data, size);
76     return 0;
77 }
78 
79