1 /*
2 * Copyright (c) 2023 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 "componentdisable_fuzzer.h"
17
18 #include "component_disable.h"
19 #include "distributed_hardware_errno.h"
20
21 namespace OHOS {
22 namespace DistributedHardware {
23
InitSource(const std::string & networkId)24 int32_t FuzzDistributedHardwareSource::InitSource(const std::string& networkId)
25 {
26 (void)networkId;
27 return DH_FWK_SUCCESS;
28 }
29
ReleaseSource()30 int32_t FuzzDistributedHardwareSource::ReleaseSource()
31 {
32 return DH_FWK_SUCCESS;
33 }
34
RegisterDistributedHardware(const std::string & networkId,const std::string & dhId,const EnableParam & param,std::shared_ptr<RegisterCallback> callback)35 int32_t FuzzDistributedHardwareSource::RegisterDistributedHardware(
36 const std::string& networkId, const std::string& dhId, const EnableParam& param,
37 std::shared_ptr<RegisterCallback> callback)
38 {
39 (void)networkId;
40 (void)dhId;
41 (void)param;
42 (void)callback;
43 return DH_FWK_SUCCESS;
44 }
45
UnregisterDistributedHardware(const std::string & networkId,const std::string & dhId,std::shared_ptr<UnregisterCallback> callback)46 int32_t FuzzDistributedHardwareSource::UnregisterDistributedHardware(
47 const std::string& networkId, const std::string& dhId, std::shared_ptr<UnregisterCallback> callback)
48 {
49 (void)networkId;
50 (void)dhId;
51 (void)callback;
52 return DH_FWK_SUCCESS;
53 }
54
ConfigDistributedHardware(const std::string & networkId,const std::string & dhId,const std::string & key,const std::string & value)55 int32_t FuzzDistributedHardwareSource::ConfigDistributedHardware(
56 const std::string& networkId, const std::string& dhId, const std::string& key, const std::string& value)
57 {
58 (void)networkId;
59 (void)dhId;
60 (void)key;
61 (void)value;
62 return DH_FWK_SUCCESS;
63 }
64
RegisterDistributedHardwareStateListener(std::shared_ptr<DistributedHardwareStateListener> listener)65 void FuzzDistributedHardwareSource::RegisterDistributedHardwareStateListener(
66 std::shared_ptr<DistributedHardwareStateListener> listener)
67 {
68 (void)listener;
69 }
70
RegisterDataSyncTriggerListener(std::shared_ptr<DataSyncTriggerListener> listener)71 void FuzzDistributedHardwareSource::RegisterDataSyncTriggerListener(std::shared_ptr<DataSyncTriggerListener> listener)
72 {
73 (void)listener;
74 }
ComponentDisableFuzzTest(const uint8_t * data,size_t size)75 void ComponentDisableFuzzTest(const uint8_t* data, size_t size)
76 {
77 if ((data == nullptr) || (size == 0)) {
78 return;
79 }
80
81 auto compDisable = std::make_shared<ComponentDisable>();
82 std::string uuid(reinterpret_cast<const char*>(data), size);
83 std::string dhId(reinterpret_cast<const char*>(data), size);
84 int32_t status = DH_FWK_SUCCESS;
85 std::string disableData(reinterpret_cast<const char*>(data), size);
86 compDisable->OnUnregisterResult(uuid, dhId, status, disableData);
87 }
88
DisableFuzzTest(const uint8_t * data,size_t size)89 void DisableFuzzTest(const uint8_t* data, size_t size)
90 {
91 if ((data == nullptr) || (size == 0)) {
92 return;
93 }
94
95 auto compDisable = std::make_shared<ComponentDisable>();
96 std::string networkId(reinterpret_cast<const char*>(data), size);
97 std::string dhId(reinterpret_cast<const char*>(data), size);
98 auto handler = std::make_shared<FuzzDistributedHardwareSource>();
99 std::string disableData(reinterpret_cast<const char*>(data), size);
100 compDisable->Disable(networkId, dhId, handler.get());
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::ComponentDisableFuzzTest(data, size);
110 OHOS::DistributedHardware::DisableFuzzTest(data, size);
111 return 0;
112 }
113
114