• 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 
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 < sizeof(int32_t))) {
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     bool enableSink = false;
51     bool enableSource = false;
52     sptr<IHDSinkStatusListener> sinkListener = nullptr;
53     sptr<IHDSourceStatusListener> sourceListener = nullptr;
54     int32_t callingUid = *(reinterpret_cast<const int32_t*>(data));
55     int32_t callingPid = *(reinterpret_cast<const int32_t*>(data));
56     DHDescriptor dhDescriptor {
57         .id = std::string(reinterpret_cast<const char*>(data), size),
58         .dhType = dhType
59     };
60 
61     ComponentManager::GetInstance().Init();
62     ComponentManager::GetInstance().Enable(networkId, uuid, dhId, dhType);
63     ComponentManager::GetInstance().Disable(networkId, uuid, dhId, dhType);
64     ComponentManager::GetInstance().CheckDemandStart(uuid, dhType, enableSink, enableSource);
65     ComponentManager::GetInstance().RegisterDHStatusListener(sinkListener, callingUid, callingPid);
66     ComponentManager::GetInstance().UnregisterDHStatusListener(sinkListener, callingUid, callingPid);
67     ComponentManager::GetInstance().RegisterDHStatusListener(networkId, sourceListener, callingUid, callingPid);
68     ComponentManager::GetInstance().UnregisterDHStatusListener(networkId, sourceListener, callingUid, callingPid);
69     ComponentManager::GetInstance().EnableSink(dhDescriptor, callingUid, callingPid);
70     ComponentManager::GetInstance().DisableSink(dhDescriptor, callingUid, callingPid);
71     ComponentManager::GetInstance().EnableSource(networkId, dhDescriptor, callingUid, callingPid);
72     ComponentManager::GetInstance().DisableSource(networkId, dhDescriptor, callingUid, callingPid);
73     ComponentManager::GetInstance().ForceDisableSink(dhDescriptor);
74     ComponentManager::GetInstance().ForceDisableSource(networkId, dhDescriptor);
75     ComponentManager::GetInstance().UnInit();
76 }
77 
OnDataSyncTriggerFuzzTest(const uint8_t * data,size_t size)78 void OnDataSyncTriggerFuzzTest(const uint8_t* data, size_t size)
79 {
80     if ((data == nullptr) || (size == 0)) {
81         return;
82     }
83 
84     std::string networkId(reinterpret_cast<const char*>(data), size);
85     DHDataSyncTriggerListener dhDataSyncTrigger;
86     dhDataSyncTrigger.OnDataSyncTrigger(networkId);
87 }
88 
OnStateChangedFuzzTest(const uint8_t * data,size_t size)89 void OnStateChangedFuzzTest(const uint8_t* data, size_t size)
90 {
91     if ((data == nullptr) || (size == 0)) {
92         return;
93     }
94 
95     std::string networkId(reinterpret_cast<const char*>(data), size);
96     std::string dhId(reinterpret_cast<const char*>(data), size);
97     BusinessState state = BusinessState::UNKNOWN;
98     DHStateListener dhData;
99     dhData.OnStateChanged(networkId, dhId, state);
100 }
101 }
102 }
103 
104 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)105 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
106 {
107     /* Run your code on data */
108     OHOS::DistributedHardware::ComponentManagerFuzzTest(data, size);
109     OHOS::DistributedHardware::OnDataSyncTriggerFuzzTest(data, size);
110     OHOS::DistributedHardware::OnStateChangedFuzzTest(data, size);
111     return 0;
112 }
113 
114