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_MIC,
35 DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP, DHType::VIRMODEM_SPEAKER
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
52 std::shared_ptr<CapabilityInfo> capInfo =
53 std::make_shared<CapabilityInfo>(dhId, devId, devName, devType, dhType, dhAttrs);
54 std::vector<std::shared_ptr<CapabilityInfo>> resInfos;
55 resInfos.emplace_back(capInfo);
56
57 CapabilityInfoManager::GetInstance()->AddCapability(resInfos);
58
59 std::shared_ptr<CapabilityInfo> info;
60 CapabilityInfoManager::GetInstance()->GetCapability(capInfo->GetDeviceId(), capInfo->GetDHId(), info);
61 CapabilityInfoManager::GetInstance()->GetDataByKey(capInfo->GetKey(), info);
62
63 CapabilityInfoMap capMap;
64 CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(capInfo->GetDeviceId(), capMap);
65 CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(capInfo->GetKey());
66 }
67 }
68 }
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::DistributedHardware::ResourceManagerFuzzTest(data, size);
75 return 0;
76 }
77
78