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 "versioninfomanager_fuzzer.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 #include <unistd.h>
24
25 #include "version_info_manager.h"
26 #include "distributed_hardware_errno.h"
27 #include "distributed_hardware_manager_factory.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 namespace {
32 constexpr uint32_t SLEEP_TIME_US = 10 * 1000;
33 const uint32_t DH_TYPE_SIZE = 10;
34 const DHType dhTypeFuzz[DH_TYPE_SIZE] = {
35 DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_AUDIO,
36 DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP
37 };
38 }
39
VersioninfoManagerFuzzTest(const uint8_t * data,size_t size)40 void VersioninfoManagerFuzzTest(const uint8_t* data, size_t size)
41 {
42 if ((data == nullptr) || (size == 0)) {
43 return;
44 }
45
46 VersionInfo versionInfo;
47 versionInfo.deviceId = std::string(reinterpret_cast<const char*>(data), size);
48 versionInfo.dhVersion = std::string(reinterpret_cast<const char*>(data), size);
49
50 CompVersion compVer;
51 compVer.dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE];
52 compVer.name = std::string(reinterpret_cast<const char*>(data), size);
53 compVer.handlerVersion = std::string(reinterpret_cast<const char*>(data), size);
54 compVer.sourceVersion = std::string(reinterpret_cast<const char*>(data), size);
55 compVer.sinkVersion = std::string(reinterpret_cast<const char*>(data), size);
56 versionInfo.compVersions.insert(std::pair<DHType, CompVersion>(compVer.dhType, compVer));
57
58 VersionInfo info;
59 VersionInfoManager::GetInstance()->AddVersion(versionInfo);
60 VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(versionInfo.deviceId, info);
61 VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(versionInfo.deviceId);
62 VersionInfoManager::GetInstance()->RemoveVersionInfoByDeviceId(versionInfo.deviceId);
63
64 usleep(SLEEP_TIME_US);
65 }
66 }
67 }
68
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
71 {
72 /* Run your code on data */
73 OHOS::DistributedHardware::VersioninfoManagerFuzzTest(data, size);
74 return 0;
75 }
76
77