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