• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "distributed_input_kit_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <functional>
21 #include <iostream>
22 #include <thread>
23 
24 #include <fuzzer/FuzzedDataProvider.h>
25 #include <refbase.h>
26 
27 #include "constants_dinput.h"
28 #include "distributed_input_handler.h"
29 #include "distributed_input_kit.h"
30 #include "distributed_input_sink_handler.h"
31 #include "distributed_input_source_handler.h"
32 #include "i_distributed_sink_input.h"
33 #include "i_distributed_source_input.h"
34 #include "prepare_d_input_call_back_stub.h"
35 #include "start_d_input_call_back_stub.h"
36 #include "stop_d_input_call_back_stub.h"
37 #include "unprepare_d_input_call_back_stub.h"
38 
39 namespace OHOS {
40 namespace DistributedHardware {
41 class TestPrepareDInputCallback :
42 public OHOS::DistributedHardware::DistributedInput::PrepareDInputCallbackStub {
43 public:
44     TestPrepareDInputCallback() = default;
45     virtual ~TestPrepareDInputCallback() = default;
OnResult(const std::string & deviceId,const int32_t & status)46     void OnResult(const std::string &deviceId, const int32_t &status)
47     {
48         (void)deviceId;
49         (void)status;
50     };
51 };
52 
53 class TestUnprepareDInputCallback :
54 public OHOS::DistributedHardware::DistributedInput::UnprepareDInputCallbackStub {
55 public:
56     TestUnprepareDInputCallback() = default;
57     virtual ~TestUnprepareDInputCallback() = default;
OnResult(const std::string & deviceId,const int32_t & status)58     void OnResult(const std::string &deviceId, const int32_t &status)
59     {
60         (void)deviceId;
61         (void)status;
62     };
63 };
64 
65 class TestStartDInputCallback :
66 public OHOS::DistributedHardware::DistributedInput::StartDInputCallbackStub {
67 public:
OnResult(const std::string & devId,const uint32_t & inputTypes,const int32_t & status)68     void OnResult(const std::string &devId, const uint32_t &inputTypes, const int32_t &status)
69     {
70         (void)devId;
71         (void)inputTypes;
72         (void)status;
73     };
74 };
75 
76 class TestStopDInputCallback :
77 public OHOS::DistributedHardware::DistributedInput::StopDInputCallbackStub {
78 public:
79     TestStopDInputCallback() = default;
80     virtual ~TestStopDInputCallback() = default;
OnResult(const std::string & devId,const uint32_t & inputTypes,const int32_t & status)81     void OnResult(const std::string &devId, const uint32_t &inputTypes, const int32_t &status)
82     {
83         (void)devId;
84         (void)inputTypes;
85         (void)status;
86     };
87 };
88 
89 class TestIStartStopDInputsCallback : public OHOS::DistributedHardware
90                                            ::DistributedInput::IStartStopDInputsCallback {
91 public:
OnResultDhids(const std::string & devId,const int32_t & status)92     void OnResultDhids(const std::string &devId, const int32_t &status) override
93     {
94         (void)devId;
95         (void)status;
96     };
97 
AsObject()98     virtual sptr<IRemoteObject> AsObject() override
99     {
100         return nullptr;
101     }
102 };
103 
PrepareInputFuzzTest(const uint8_t * data,size_t size)104 void PrepareInputFuzzTest(const uint8_t *data, size_t size)
105 {
106     if ((data == nullptr) || (size == 0)) {
107         return;
108     }
109 
110     std::string networkId(reinterpret_cast<const char*>(data), size);
111 
112     OHOS::sptr<TestPrepareDInputCallback> prepareCb(new(std::nothrow) TestPrepareDInputCallback());
113     OHOS::sptr<TestUnprepareDInputCallback> unprepareCb(new(std::nothrow) TestUnprepareDInputCallback());
114     DistributedInput::DistributedInputKit::PrepareRemoteInput(networkId, prepareCb);
115     DistributedInput::DistributedInputKit::UnprepareRemoteInput(networkId, unprepareCb);
116 }
117 
StartRemoteInputFuzzTest(const uint8_t * data,size_t size)118 void StartRemoteInputFuzzTest(const uint8_t *data, size_t size)
119 {
120     if ((data == nullptr) || (size < sizeof(uint32_t))) {
121         return;
122     }
123 
124     std::string srcId = "123";
125     std::string sinkId = "456";
126     uint32_t inputTypes = *(reinterpret_cast<const uint32_t*>(data));
127     std::vector<std::string> dhIds= {};
128     OHOS::sptr<TestStartDInputCallback> startCb(new (std::nothrow) TestStartDInputCallback());
129     OHOS::sptr<TestStopDInputCallback> stopCb(new (std::nothrow) TestStopDInputCallback());
130     DistributedInput::DistributedInputKit::StartRemoteInput(sinkId, inputTypes, startCb);
131     DistributedInput::DistributedInputKit::StartRemoteInput(srcId, sinkId, inputTypes, startCb);
132     OHOS::sptr<TestIStartStopDInputsCallback> callback(new (std::nothrow) TestIStartStopDInputsCallback());
133     DistributedInput::DistributedInputKit::StartRemoteInput(sinkId, dhIds, callback);
134     DistributedInput::DistributedInputKit::StartRemoteInput(srcId, sinkId, dhIds, callback);
135 }
136 
IsNeedFilterOutFuzzTest(const uint8_t * data,size_t size)137 void IsNeedFilterOutFuzzTest(const uint8_t *data, size_t size)
138 {
139     if ((data == nullptr) || (size < sizeof(int32_t))) {
140         return;
141     }
142 
143     FuzzedDataProvider fdp(data, size);
144     std::string deviceId = fdp.ConsumeRandomLengthString();
145     int32_t pressedKey = fdp.ConsumeIntegral<int32_t>();
146     int32_t keyCode = fdp.ConsumeIntegral<int32_t>();
147     int32_t keyAction = fdp.ConsumeIntegral<int32_t>();
148     DistributedInput::BusinessEvent event;
149     event.pressedKeys.push_back(pressedKey);
150     event.keyCode = keyCode;
151     event.keyAction = keyAction;
152 
153     DistributedInput::DistributedInputKit::IsNeedFilterOut(deviceId, event);
154 }
StopRemoteInputFuzzTest(const uint8_t * data,size_t size)155 void StopRemoteInputFuzzTest(const uint8_t* data, size_t  size)
156 {
157     if ((data == nullptr) || (size < sizeof(uint32_t))) {
158         return;
159     }
160 
161     std::string srcId = "123";
162     std::string sinkId = "456";
163     uint32_t inputTypes = *(reinterpret_cast<const uint32_t*>(data));
164     std::vector<std::string> dhIds = {};
165     OHOS::sptr<TestStartDInputCallback> startCb(new (std::nothrow) TestStartDInputCallback());
166     OHOS::sptr<TestStopDInputCallback> stopCb(new (std::nothrow) TestStopDInputCallback());
167     DistributedInput::DistributedInputKit::StopRemoteInput(sinkId, inputTypes, stopCb);
168     DistributedInput::DistributedInputKit::StopRemoteInput(srcId, sinkId, inputTypes, stopCb);
169     OHOS::sptr<TestIStartStopDInputsCallback> callback(new (std::nothrow) TestIStartStopDInputsCallback());
170     DistributedInput::DistributedInputKit::StopRemoteInput(sinkId, dhIds, callback);
171     DistributedInput::DistributedInputKit::StopRemoteInput(srcId, sinkId, dhIds, callback);
172 }
173 
IsTouchEventNeedFilterOutFuzzTest(const uint8_t * data,size_t size)174 void IsTouchEventNeedFilterOutFuzzTest(const uint8_t *data, size_t size)
175 {
176     if ((data == nullptr) || (size < sizeof(uint32_t))) {
177         return;
178     }
179 
180     FuzzedDataProvider fdp(data, size);
181     uint32_t absX = fdp.ConsumeIntegral<uint32_t>();
182     uint32_t absY = fdp.ConsumeIntegral<uint32_t>();
183     DistributedInput::TouchScreenEvent event;
184     event.absX = absX;
185     event.absY = absY;
186 
187     DistributedInput::DistributedInputKit::IsTouchEventNeedFilterOut(event);
188 }
189 } // namespace DistributedHardware
190 } // namespace OHOS
191 
192 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)193 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
194 {
195     /* Run your code on data */
196     OHOS::DistributedHardware::PrepareInputFuzzTest(data, size);
197     OHOS::DistributedHardware::StartRemoteInputFuzzTest(data, size);
198     OHOS::DistributedHardware::IsNeedFilterOutFuzzTest(data, size);
199     OHOS::DistributedHardware::StopRemoteInputFuzzTest(data, size);
200     OHOS::DistributedHardware::IsTouchEventNeedFilterOutFuzzTest(data, size);
201     return 0;
202 }
203