1 /*
2  * Copyright (c) 2022-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_ipc_test.h"
17 
18 #include "nlohmann/json.hpp"
19 
20 #include "dinput_errcode.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::DistributedHardware::DistributedInput;
24 using namespace std;
25 namespace OHOS {
26 namespace DistributedHardware {
27 namespace DistributedInput {
SetUp()28 void DistributedInputIpcTest::SetUp()
29 {
30 }
31 
TearDown()32 void DistributedInputIpcTest::TearDown()
33 {
34 }
35 
SetUpTestCase()36 void DistributedInputIpcTest::SetUpTestCase()
37 {
38 }
39 
TearDownTestCase()40 void DistributedInputIpcTest::TearDownTestCase()
41 {
42 }
43 
OnNodeOnLine(const std::string & srcDevId,const std::string & sinkDevId,const std::string & sinkNodeId,const std::string & sinkNodeDesc)44 void DistributedInputIpcTest::TestInputNodeListener::OnNodeOnLine(const std::string &srcDevId,
45     const std::string &sinkDevId, const std::string &sinkNodeId, const std::string &sinkNodeDesc)
46 {
47     (void)srcDevId;
48     (void)sinkDevId;
49     (void)sinkNodeId;
50     (void)sinkNodeDesc;
51     return;
52 }
53 
OnNodeOffLine(const std::string & srcDevId,const std::string & sinkDevId,const std::string & sinkNodeId)54 void DistributedInputIpcTest::TestInputNodeListener::OnNodeOffLine(const std::string &srcDevId,
55     const std::string &sinkDevId, const std::string &sinkNodeId)
56 {
57     (void)srcDevId;
58     (void)sinkDevId;
59     (void)sinkNodeId;
60     return;
61 }
62 
OnSimulationEvent(uint32_t type,uint32_t code,int32_t value)63 int32_t DistributedInputIpcTest::TestSimulationEventListenerStub::OnSimulationEvent(
64     uint32_t type, uint32_t code, int32_t value)
65 {
66     (void)type;
67     (void)code;
68     (void)value;
69     return DH_SUCCESS;
70 }
71 
72 HWTEST_F(DistributedInputIpcTest, GetDInputSinkProxy01, testing::ext::TestSize.Level1)
73 {
74     bool ret = DInputSAManager::GetInstance().GetDInputSinkProxy();
75     EXPECT_EQ(false, ret);
76 }
77 
78 HWTEST_F(DistributedInputIpcTest, CheckSourceRegisterCallback01, testing::ext::TestSize.Level1)
79 {
80     std::string deviceId = "46qweqwe46q5qw4e";
81     BusinessEvent event;
82     DistributedInputClient::GetInstance().CheckSourceRegisterCallback();
83     bool ret = DistributedInputClient::GetInstance().IsNeedFilterOut(deviceId, event);
84     EXPECT_EQ(false, ret);
85 }
86 
87 HWTEST_F(DistributedInputIpcTest, CheckSinkRegisterCallback01, testing::ext::TestSize.Level1)
88 {
89     std::string deviceId = "46qweqwe46q5qw4e";
90     BusinessEvent event;
91     DistributedInputClient::GetInstance().CheckSinkRegisterCallback();
92     bool ret = DistributedInputClient::GetInstance().IsNeedFilterOut(deviceId, event);
93     EXPECT_EQ(false, ret);
94 }
95 
96 HWTEST_F(DistributedInputIpcTest, CheckSharingDhIdsCallback01, testing::ext::TestSize.Level1)
97 {
98     std::string deviceId = "46qweqwe46q5qw4e";
99     BusinessEvent event;
100     DistributedInputClient::GetInstance().CheckSharingDhIdsCallback();
101     bool ret = DistributedInputClient::GetInstance().IsNeedFilterOut(deviceId, event);
102     EXPECT_EQ(false, ret);
103 }
104 
105 HWTEST_F(DistributedInputIpcTest, CheckSinkScreenInfoCallback01, testing::ext::TestSize.Level1)
106 {
107     std::string deviceId = "46qweqwe46q5qw4e";
108     BusinessEvent event;
109     DistributedInputClient::GetInstance().CheckSinkScreenInfoCallback();
110     bool ret = DistributedInputClient::GetInstance().IsNeedFilterOut(deviceId, event);
111     EXPECT_EQ(false, ret);
112 }
113 
114 HWTEST_F(DistributedInputIpcTest, IsNeedFilterOut01, testing::ext::TestSize.Level1)
115 {
116     std::string deviceId = "46qweqwe46q5qw4e";
117     BusinessEvent event;
118     bool ret = DistributedInputClient::GetInstance().IsNeedFilterOut(deviceId, event);
119     EXPECT_EQ(false, ret);
120 }
121 
122 HWTEST_F(DistributedInputIpcTest, IsNeedFilterOut02, testing::ext::TestSize.Level1)
123 {
124     std::string deviceId;
125     BusinessEvent event;
126     bool ret = DistributedInputClient::GetInstance().IsNeedFilterOut(deviceId, event);
127     EXPECT_EQ(false, ret);
128 }
129 
130 HWTEST_F(DistributedInputIpcTest, IsNeedFilterOut03, testing::ext::TestSize.Level1)
131 {
132     std::string deviceId(280, 'e');
133     BusinessEvent event;
134     bool ret = DistributedInputClient::GetInstance().IsNeedFilterOut(deviceId, event);
135     EXPECT_EQ(false, ret);
136 }
137 
138 HWTEST_F(DistributedInputIpcTest, IsTouchEventNeedFilterOut01, testing::ext::TestSize.Level1)
139 {
140     TouchScreenEvent event = {100, 100};
141     bool ret = DistributedInputClient::GetInstance().IsTouchEventNeedFilterOut(event);
142     EXPECT_EQ(false, ret);
143 }
144 
145 HWTEST_F(DistributedInputIpcTest, IsTouchEventNeedFilterOut02, testing::ext::TestSize.Level1)
146 {
147     TouchScreenEvent event = {100, 100};
148     nlohmann::json jsonObj;
149     jsonObj = {{10, 10, 100, 100}};
150     DistributedInputClient::GetInstance().UpdateSinkScreenInfos(jsonObj.dump());
151     bool ret = DistributedInputClient::GetInstance().IsTouchEventNeedFilterOut(event);
152     EXPECT_EQ(true, ret);
153 }
154 
155 HWTEST_F(DistributedInputIpcTest, IsTouchEventNeedFilterOut03, testing::ext::TestSize.Level1)
156 {
157     TouchScreenEvent event = {100, 100};
158     nlohmann::json jsonObj;
159     jsonObj = {{10, 10, 100, 80}};
160     DistributedInputClient::GetInstance().UpdateSinkScreenInfos(jsonObj.dump());
161     bool ret = DistributedInputClient::GetInstance().IsTouchEventNeedFilterOut(event);
162     EXPECT_EQ(false, ret);
163 }
164 
165 HWTEST_F(DistributedInputIpcTest, IsTouchEventNeedFilterOut04, testing::ext::TestSize.Level1)
166 {
167     TouchScreenEvent event = {100, 100};
168     nlohmann::json jsonObj;
169     jsonObj = {{10, 10, 80, 100}};
170     DistributedInputClient::GetInstance().UpdateSinkScreenInfos(jsonObj.dump());
171     bool ret = DistributedInputClient::GetInstance().IsTouchEventNeedFilterOut(event);
172     EXPECT_EQ(false, ret);
173 }
174 
175 HWTEST_F(DistributedInputIpcTest, IsTouchEventNeedFilterOut05, testing::ext::TestSize.Level1)
176 {
177     TouchScreenEvent event = {100, 100};
178     nlohmann::json jsonObj;
179     jsonObj = {{110, 10, 100, 100}};
180     DistributedInputClient::GetInstance().UpdateSinkScreenInfos(jsonObj.dump());
181     bool ret = DistributedInputClient::GetInstance().IsTouchEventNeedFilterOut(event);
182     EXPECT_EQ(false, ret);
183 }
184 
185 HWTEST_F(DistributedInputIpcTest, IsTouchEventNeedFilterOut06, testing::ext::TestSize.Level1)
186 {
187     TouchScreenEvent event = {100, 100};
188     nlohmann::json jsonObj;
189     jsonObj = {{10, 110, 100, 100}};
190     DistributedInputClient::GetInstance().UpdateSinkScreenInfos(jsonObj.dump());
191     bool ret = DistributedInputClient::GetInstance().IsTouchEventNeedFilterOut(event);
192     EXPECT_EQ(false, ret);
193 }
194 
195 HWTEST_F(DistributedInputIpcTest, IsStartDistributedInput01, testing::ext::TestSize.Level1)
196 {
197     std::string dhId;
198     bool ret = DistributedInputClient::GetInstance().IsStartDistributedInput(dhId);
199     EXPECT_EQ(false, ret);
200 }
201 
202 HWTEST_F(DistributedInputIpcTest, IsStartDistributedInput02, testing::ext::TestSize.Level1)
203 {
204     std::string dhId = "654ew6qw4f6w1e6f1w6e5f";
205     bool ret = DistributedInputClient::GetInstance().IsStartDistributedInput(dhId);
206     EXPECT_EQ(false, ret);
207 }
208 
209 HWTEST_F(DistributedInputIpcTest, IsStartDistributedInput03, testing::ext::TestSize.Level1)
210 {
211     std::string dhId(280, 'e');
212     bool ret = DistributedInputClient::GetInstance().IsStartDistributedInput(dhId);
213     EXPECT_EQ(false, ret);
214 }
215 
216 HWTEST_F(DistributedInputIpcTest, RegisterSimulationEventListener01, testing::ext::TestSize.Level1)
217 {
218     sptr<TestSimulationEventListenerStub> listener = nullptr;
219     int32_t ret = DistributedInputClient::GetInstance().RegisterSimulationEventListener(listener);
220     EXPECT_EQ(ERR_DH_INPUT_CLIENT_REG_UNREG_KEY_STATE_FAIL, ret);
221 }
222 
223 HWTEST_F(DistributedInputIpcTest, RegisterSimulationEventListener02, testing::ext::TestSize.Level1)
224 {
225     sptr<TestSimulationEventListenerStub> listener(new TestSimulationEventListenerStub());
226     int32_t ret = DistributedInputClient::GetInstance().RegisterSimulationEventListener(listener);
227     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
228 }
229 
230 HWTEST_F(DistributedInputIpcTest, UnregisterSimulationEventListener01, testing::ext::TestSize.Level1)
231 {
232     sptr<TestSimulationEventListenerStub> listener = nullptr;
233     int32_t ret = DistributedInputClient::GetInstance().UnregisterSimulationEventListener(listener);
234     EXPECT_EQ(ERR_DH_INPUT_CLIENT_REG_UNREG_KEY_STATE_FAIL, ret);
235 }
236 
237 HWTEST_F(DistributedInputIpcTest, UnregisterSimulationEventListener02, testing::ext::TestSize.Level1)
238 {
239     sptr<TestSimulationEventListenerStub> listener(new TestSimulationEventListenerStub());
240     int32_t ret = DistributedInputClient::GetInstance().UnregisterSimulationEventListener(listener);
241     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
242 }
243 
244 HWTEST_F(DistributedInputIpcTest, IsJsonData01, testing::ext::TestSize.Level1)
245 {
246     std::string strData = "123";
247     bool ret = DistributedInputClient::GetInstance().IsJsonData(strData);
248     EXPECT_EQ(false, ret);
249 }
250 
251 HWTEST_F(DistributedInputIpcTest, IsJsonData02, testing::ext::TestSize.Level1)
252 {
253     std::string strData = "{{{}}}";
254     bool ret = DistributedInputClient::GetInstance().IsJsonData(strData);
255     EXPECT_EQ(true, ret);
256 }
257 
258 HWTEST_F(DistributedInputIpcTest, IsJsonData03, testing::ext::TestSize.Level1)
259 {
260     std::string strData = "{3413";
261     bool ret = DistributedInputClient::GetInstance().IsJsonData(strData);
262     EXPECT_EQ(false, ret);
263 }
264 
265 HWTEST_F(DistributedInputIpcTest, AddWhiteListInfos01, testing::ext::TestSize.Level1)
266 {
267     std::string deviceId = "4646565465asdqweqwe";
268     std::string strJson = "[[[2000][2000][1]]]";
269     DistributedInputClient::GetInstance().AddWhiteListInfos(deviceId, strJson);
270     std::string strData = "{3413";
271     bool ret = DistributedInputClient::GetInstance().IsJsonData(strData);
272     EXPECT_EQ(false, ret);
273 }
274 
275 HWTEST_F(DistributedInputIpcTest, AddWhiteListInfos02, testing::ext::TestSize.Level1)
276 {
277     std::string deviceId = "4646565465asdqweqwe";
278     std::string strJson = "";
279     DistributedInputClient::GetInstance().AddWhiteListInfos(deviceId, strJson);
280     std::string strData = "{3413";
281     bool ret = DistributedInputClient::GetInstance().IsJsonData(strData);
282     EXPECT_EQ(false, ret);
283 }
284 
285 HWTEST_F(DistributedInputIpcTest, DelWhiteListInfos01, testing::ext::TestSize.Level1)
286 {
287     std::string deviceId = "4646565465asdqweqwe";
288     DistributedInputClient::GetInstance().DelWhiteListInfos(deviceId);
289     std::string strData = "{3413";
290     bool ret = DistributedInputClient::GetInstance().IsJsonData(strData);
291     EXPECT_EQ(false, ret);
292 }
293 
294 HWTEST_F(DistributedInputIpcTest, UpdateSinkScreenInfos01, testing::ext::TestSize.Level1)
295 {
296     nlohmann::json jsonObj;
297     jsonObj = {{1080, 720, 10, 10}};
298     DistributedInputClient::GetInstance().UpdateSinkScreenInfos(jsonObj.dump());
299     std::string strData = "{3413";
300     bool ret = DistributedInputClient::GetInstance().IsJsonData(strData);
301     EXPECT_EQ(false, ret);
302 }
303 
304 HWTEST_F(DistributedInputIpcTest, NotifyStartDScreen01, testing::ext::TestSize.Level1)
305 {
306     std::string sinkDevId = "46qw4e61dq6w1dq6w5e4q6";
307     std::string srcDevId = "erq6w54e9q8w4eqw19q6d1";
308     uint64_t srcWinId = 5;
309     int32_t ret = DistributedInputClient::GetInstance().NotifyStartDScreen(sinkDevId, srcDevId, srcWinId);
310     EXPECT_EQ(ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL, ret);
311 }
312 
313 HWTEST_F(DistributedInputIpcTest, NotifyStopDScreen01, testing::ext::TestSize.Level1)
314 {
315     std::string networkId = "46qw4e61dq6w1dq6w5e4q6";
316     std::string srcScreenInfoKey = "q65we46qw54e6q5we46q";
317     int32_t ret = DistributedInputClient::GetInstance().NotifyStopDScreen(networkId, srcScreenInfoKey);
318     EXPECT_EQ(ERR_DH_INPUT_RPC_GET_REMOTE_DINPUT_FAIL, ret);
319 }
320 
321 HWTEST_F(DistributedInputIpcTest, RegisterDistributedHardware01, testing::ext::TestSize.Level1)
322 {
323     std::string devId;
324     std::string dhId;
325     std::string parameters;
326     std::shared_ptr<RegisterCallback> callback = nullptr;
327     int32_t ret = DistributedInputClient::GetInstance().RegisterDistributedHardware(devId, dhId, parameters, callback);
328     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
329 }
330 
331 HWTEST_F(DistributedInputIpcTest, UnregisterDistributedHardware01, testing::ext::TestSize.Level1)
332 {
333     std::string devId;
334     std::string dhId;
335     std::shared_ptr<UnregisterCallback> callback = nullptr;
336     int32_t ret = DistributedInputClient::GetInstance().UnregisterDistributedHardware(devId, dhId, callback);
337     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
338 }
339 
340 HWTEST_F(DistributedInputIpcTest, PrepareRemoteInput01, testing::ext::TestSize.Level1)
341 {
342     string deviceId = "PrepareRemoteInput01";
343     sptr<IPrepareDInputCallback> callback = nullptr;
344     int32_t ret = DistributedInputClient::GetInstance().PrepareRemoteInput(deviceId, callback);
345     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
346 }
347 
348 HWTEST_F(DistributedInputIpcTest, UnprepareRemoteInput01, testing::ext::TestSize.Level0)
349 {
350     string deviceId = "UnprepareRemoteInput01";
351     sptr<IUnprepareDInputCallback> callback = nullptr;
352     int32_t ret = DistributedInputClient::GetInstance().UnprepareRemoteInput(deviceId, callback);
353     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
354 }
355 
356 HWTEST_F(DistributedInputIpcTest, StartRemoteInput01, testing::ext::TestSize.Level0)
357 {
358     string deviceId = "StartRemoteInput01";
359     sptr<IStartDInputCallback> callback = nullptr;
360     int32_t ret = DistributedInputClient::GetInstance().StartRemoteInput(
361         deviceId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
362     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
363 }
364 
365 HWTEST_F(DistributedInputIpcTest, StartRemoteInput02, testing::ext::TestSize.Level0)
366 {
367     std::string sinkId = "StartRemoteInput_sink";
368     std::vector<std::string> dhIds = {"dhIds_test"};
369     sptr<IStartStopDInputsCallback> callback = nullptr;
370     int32_t ret = DistributedInputClient::GetInstance().StartRemoteInput(sinkId, dhIds, callback);
371     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
372 }
373 
374 HWTEST_F(DistributedInputIpcTest, StartRemoteInput03, testing::ext::TestSize.Level0)
375 {
376     string srcId = "StartRemoteInput01-src";
377     string sinkId = "StartRemoteInput01-sink";
378     sptr<IStartDInputCallback> callback = nullptr;
379     int32_t ret = DistributedInputClient::GetInstance().StartRemoteInput(
380         srcId, sinkId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
381     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
382 }
383 
384 HWTEST_F(DistributedInputIpcTest, StartRemoteInput04, testing::ext::TestSize.Level0)
385 {
386     string srcId = "StartRemoteInput01-src";
387     string sinkId = "StartRemoteInput01-sink";
388     std::vector<std::string> dhIds = {"dhIds_test"};
389     sptr<IStartStopDInputsCallback> callback = nullptr;
390     int32_t ret = DistributedInputClient::GetInstance().StartRemoteInput(srcId, sinkId, dhIds, callback);
391     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
392 }
393 
394 HWTEST_F(DistributedInputIpcTest, StopRemoteInput01, testing::ext::TestSize.Level0)
395 {
396     string deviceId = "StopRemoteInput01";
397     sptr<IStopDInputCallback> callback = nullptr;
398     int32_t ret = DistributedInputClient::GetInstance().StopRemoteInput(
399         deviceId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
400     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
401 }
402 
403 HWTEST_F(DistributedInputIpcTest, StopRemoteInput02, testing::ext::TestSize.Level0)
404 {
405     std::string sinkId = "StartRemoteInput_test";
406     std::vector<std::string> dhIds = {"dhIds_test"};
407     sptr<IStartStopDInputsCallback> callback = nullptr;
408     int32_t ret = DistributedInputClient::GetInstance().StopRemoteInput(sinkId, dhIds, callback);
409     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
410 }
411 
412 HWTEST_F(DistributedInputIpcTest, StopRemoteInput03, testing::ext::TestSize.Level0)
413 {
414     string srcId = "StopRemoteInput03-src";
415     string sinkId = "StopRemoteInput03-sink";
416     sptr<IStopDInputCallback> callback = nullptr;
417     int32_t ret = DistributedInputClient::GetInstance().StopRemoteInput(
418         srcId, sinkId, static_cast<uint32_t>(DInputDeviceType::ALL), callback);
419     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
420 }
421 
422 HWTEST_F(DistributedInputIpcTest, StopRemoteInput04, testing::ext::TestSize.Level0)
423 {
424     string srcId = "StartRemoteInput01-src";
425     string sinkId = "StartRemoteInput01-sink";
426     std::vector<std::string> dhIds = {"dhIds_test"};
427     sptr<IStartStopDInputsCallback> callback = nullptr;
428     int32_t ret = DistributedInputClient::GetInstance().StopRemoteInput(srcId, sinkId, dhIds, callback);
429     EXPECT_EQ(ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL, ret);
430 }
431 
432 } // namespace DistributedInput
433 } // namespace DistributedHardware
434 } // namespace OHOS