• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "componentmanager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "component_disable.h"
22 #include "component_enable.h"
23 #include "component_manager.h"
24 #include "constants.h"
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_log.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 namespace {
31     const uint32_t DH_TYPE_SIZE = 10;
32     const DHType dhTypeFuzz[DH_TYPE_SIZE] = {
33         DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_MIC,
34         DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP, DHType::VIRMODEM_SPEAKER
35     };
36 }
ComponentManagerFuzzTest(const uint8_t * data,size_t size)37 void ComponentManagerFuzzTest(const uint8_t* data, size_t size)
38 {
39     if ((data == nullptr) || (size == 0)) {
40         return;
41     }
42 
43     std::string networkId(reinterpret_cast<const char*>(data), size);
44     std::string uuid(reinterpret_cast<const char*>(data), size);
45     std::string dhId(reinterpret_cast<const char*>(data), size);
46     DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE];
47 
48     ComponentManager::GetInstance().Init();
49     ComponentManager::GetInstance().Enable(networkId, uuid, dhId, dhType);
50     ComponentManager::GetInstance().Disable(networkId, uuid, dhId, dhType);
51     ComponentManager::GetInstance().UnInit();
52 }
53 }
54 }
55 
56 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
58 {
59     /* Run your code on data */
60     OHOS::DistributedHardware::ComponentManagerFuzzTest(data, size);
61     return 0;
62 }
63 
64