• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #include "component_disable.h"
23 #include "component_enable.h"
24 #include "component_manager.h"
25 #include "constants.h"
26 #include "distributed_hardware_errno.h"
27 #include "distributed_hardware_log.h"
28 #include "dh_data_sync_trigger_listener.h"
29 #include "dh_state_listener.h"
30 #include "event_handler.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 namespace {
35     const uint32_t DH_TYPE_SIZE = 10;
36     const DHType dhTypeFuzz[DH_TYPE_SIZE] = {
37         DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_AUDIO,
38         DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP
39     };
40 }
ComponentManagerFuzzTest(const uint8_t * data,size_t size)41 void ComponentManagerFuzzTest(const uint8_t* data, size_t size)
42 {
43     if ((data == nullptr) || (size < sizeof(int32_t))) {
44         return;
45     }
46 
47     std::string networkId(reinterpret_cast<const char*>(data), size);
48     std::string uuid(reinterpret_cast<const char*>(data), size);
49     std::string dhId(reinterpret_cast<const char*>(data), size);
50     DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE];
51     bool enableSource = false;
52     sptr<IHDSinkStatusListener> sinkListener = nullptr;
53     sptr<IHDSourceStatusListener> sourceListener = nullptr;
54     FuzzedDataProvider fdp(data, size);
55     int32_t callingUid = fdp.ConsumeIntegral<int32_t>();
56     int32_t callingPid = fdp.ConsumeIntegral<int32_t>();
57     DHDescriptor dhDescriptor {
58         .id = std::string(reinterpret_cast<const char*>(data), size),
59         .dhType = dhType
60     };
61 
62     ComponentManager::GetInstance().Init();
63     ComponentManager::GetInstance().Enable(networkId, uuid, dhId, dhType);
64     ComponentManager::GetInstance().Disable(networkId, uuid, dhId, dhType);
65     ComponentManager::GetInstance().CheckDemandStart(uuid, dhType, enableSource);
66     ComponentManager::GetInstance().RegisterDHStatusListener(sinkListener, callingUid, callingPid);
67     ComponentManager::GetInstance().UnregisterDHStatusListener(sinkListener, callingUid, callingPid);
68     ComponentManager::GetInstance().RegisterDHStatusListener(networkId, sourceListener, callingUid, callingPid);
69     ComponentManager::GetInstance().UnregisterDHStatusListener(networkId, sourceListener, callingUid, callingPid);
70     ComponentManager::GetInstance().EnableSink(dhDescriptor, callingUid, callingPid);
71     ComponentManager::GetInstance().DisableSink(dhDescriptor, callingUid, callingPid);
72     ComponentManager::GetInstance().EnableSource(networkId, dhDescriptor, callingUid, callingPid);
73     ComponentManager::GetInstance().DisableSource(networkId, dhDescriptor, callingUid, callingPid);
74     ComponentManager::GetInstance().ForceDisableSink(dhDescriptor);
75     ComponentManager::GetInstance().ForceDisableSource(networkId, dhDescriptor);
76     ComponentManager::GetInstance().UnInit();
77 }
78 
OnDataSyncTriggerFuzzTest(const uint8_t * data,size_t size)79 void OnDataSyncTriggerFuzzTest(const uint8_t* data, size_t size)
80 {
81     if ((data == nullptr) || (size == 0)) {
82         return;
83     }
84 
85     std::string networkId(reinterpret_cast<const char*>(data), size);
86     DHDataSyncTriggerListener dhDataSyncTrigger;
87     dhDataSyncTrigger.OnDataSyncTrigger(networkId);
88 }
89 
OnStateChangedFuzzTest(const uint8_t * data,size_t size)90 void OnStateChangedFuzzTest(const uint8_t* data, size_t size)
91 {
92     if ((data == nullptr) || (size == 0)) {
93         return;
94     }
95 
96     std::string networkId(reinterpret_cast<const char*>(data), size);
97     std::string dhId(reinterpret_cast<const char*>(data), size);
98     BusinessState state = BusinessState::UNKNOWN;
99     DHStateListener dhData;
100     dhData.OnStateChanged(networkId, dhId, state);
101 }
102 }
103 }
104 
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
107 {
108     /* Run your code on data */
109     OHOS::DistributedHardware::ComponentManagerFuzzTest(data, size);
110     OHOS::DistributedHardware::OnDataSyncTriggerFuzzTest(data, size);
111     OHOS::DistributedHardware::OnStateChangedFuzzTest(data, size);
112     return 0;
113 }
114 
115