• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "componentenable_fuzzer.h"
17 
18 #include "component_enable.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 }
75 
ComponentEnableFuzzTest(const uint8_t * data,size_t size)76 void ComponentEnableFuzzTest(const uint8_t* data, size_t size)
77 {
78     if ((data == nullptr) || (size == 0)) {
79         return;
80     }
81 
82     auto compEnable = std::make_shared<ComponentEnable>();
83     std::string uuid(reinterpret_cast<const char*>(data), size);
84     std::string dhId(reinterpret_cast<const char*>(data), size);
85     int32_t status = DH_FWK_SUCCESS;
86     std::string enableData(reinterpret_cast<const char*>(data), size);
87     compEnable->OnRegisterResult(uuid, dhId, status, enableData);
88 }
89 
EnableFuzzTest(const uint8_t * data,size_t size)90 void EnableFuzzTest(const uint8_t* data, size_t size)
91 {
92     if ((data == nullptr) || (size == 0)) {
93         return;
94     }
95 
96     auto compEnable = std::make_shared<ComponentEnable>();
97     std::string networkId(reinterpret_cast<const char*>(data), size);
98     std::string dhId(reinterpret_cast<const char*>(data), size);
99     EnableParam param;
100     param.sourceVersion = std::string(reinterpret_cast<const char*>(data), size);
101     param.sourceAttrs = std::string(reinterpret_cast<const char*>(data), size);
102     param.sinkVersion = std::string(reinterpret_cast<const char*>(data), size);
103     param.sinkAttrs = std::string(reinterpret_cast<const char*>(data), size);
104     param.subtype = std::string(reinterpret_cast<const char*>(data), size);
105     auto handler = std::make_shared<FuzzDistributedHardwareSource>();
106     compEnable->Enable(networkId, dhId, param, handler.get());
107 }
108 }
109 }
110 
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114     /* Run your code on data */
115     OHOS::DistributedHardware::ComponentEnableFuzzTest(data, size);
116     OHOS::DistributedHardware::EnableFuzzTest(data, size);
117     return 0;
118 }
119 
120