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 #include "dh_data_sync_trigger_listener.h"
28 #include "dh_state_listener.h"
29 #include "event_handler.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
33 namespace {
34 const uint32_t DH_TYPE_SIZE = 10;
35 const DHType dhTypeFuzz[DH_TYPE_SIZE] = {
36 DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_AUDIO,
37 DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP
38 };
39 }
ComponentManagerFuzzTest(const uint8_t * data,size_t size)40 void ComponentManagerFuzzTest(const uint8_t* data, size_t size)
41 {
42 if ((data == nullptr) || (size == 0)) {
43 return;
44 }
45
46 std::string networkId(reinterpret_cast<const char*>(data), size);
47 std::string uuid(reinterpret_cast<const char*>(data), size);
48 std::string dhId(reinterpret_cast<const char*>(data), size);
49 DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE];
50
51 ComponentManager::GetInstance().Init();
52 ComponentManager::GetInstance().Enable(networkId, uuid, dhId, dhType);
53 ComponentManager::GetInstance().Disable(networkId, uuid, dhId, dhType);
54 ComponentManager::GetInstance().UnInit();
55 }
56
OnDataSyncTriggerFuzzTest(const uint8_t * data,size_t size)57 void OnDataSyncTriggerFuzzTest(const uint8_t* data, size_t size)
58 {
59 if ((data == nullptr) || (size == 0)) {
60 return;
61 }
62
63 std::string networkId(reinterpret_cast<const char*>(data), size);
64 DHDataSyncTriggerListener dhDataSyncTrigger;
65 dhDataSyncTrigger.OnDataSyncTrigger(networkId);
66 }
67
OnStateChangedFuzzTest(const uint8_t * data,size_t size)68 void OnStateChangedFuzzTest(const uint8_t* data, size_t size)
69 {
70 if ((data == nullptr) || (size == 0)) {
71 return;
72 }
73
74 std::string networkId(reinterpret_cast<const char*>(data), size);
75 std::string dhId(reinterpret_cast<const char*>(data), size);
76 BusinessState state = BusinessState::UNKNOWN;
77 DHStateListener dhData;
78 dhData.OnStateChanged(networkId, dhId, state);
79 }
80 }
81 }
82
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
85 {
86 /* Run your code on data */
87 OHOS::DistributedHardware::ComponentManagerFuzzTest(data, size);
88 OHOS::DistributedHardware::OnDataSyncTriggerFuzzTest(data, size);
89 OHOS::DistributedHardware::OnStateChangedFuzzTest(data, size);
90 return 0;
91 }
92
93