• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "UTTest_ipc_server_stub.h"
17 
18 #include <algorithm>
19 #include <thread>
20 #include <unistd.h>
21 
22 #include "device_manager_ipc_interface_code.h"
23 #include "device_manager_service.h"
24 #include "dm_device_info.h"
25 #include "ipc_remote_broker.h"
26 #include "ipc_server_stub.h"
27 #include "device_manager_impl.h"
28 #include "dm_constants.h"
29 #include "if_system_ability_manager.h"
30 #include "ipc_cmd_register.h"
31 #include "ipc_skeleton.h"
32 #include "ipc_types.h"
33 #include "iservice_registry.h"
34 #include "string_ex.h"
35 #include "system_ability_definition.h"
36 
37 namespace OHOS {
38 namespace DistributedHardware {
SetUp()39 void IpcServerStubTest::SetUp()
40 {
41 }
42 
TearDown()43 void IpcServerStubTest::TearDown()
44 {
45 }
46 
SetUpTestCase()47 void IpcServerStubTest::SetUpTestCase()
48 {
49 }
50 
TearDownTestCase()51 void IpcServerStubTest::TearDownTestCase()
52 {
53 }
54 
55 namespace {
56 /**
57  * @tc.name: OnStop_001
58  * @tc.desc: 1. Call IpcServerStub OnStop
59  *           2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
60  * @tc.type: FUNC
61  * @tc.require: AR000GHSJK
62  */
63 HWTEST_F(IpcServerStubTest, OnStop_001, testing::ext::TestSize.Level0)
64 {
65     // 1. Call IpcServerStub OnStop
66     IpcServerStub::GetInstance().OnStop();
67     // 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
68     ASSERT_EQ(ServiceRunningState::STATE_NOT_START, IpcServerStub::GetInstance().state_);
69     ASSERT_EQ(IpcServerStub::GetInstance().registerToService_, false);
70 }
71 
72 /**
73  * @tc.name: OnStart_001
74  * @tc.desc: 1. set IpcServerStub state is ServiceRunningState::STATE_RUNNING
75  *           2. Call IpcServerStub OnStart
76  *           3. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
77  * @tc.type: FUNC
78  * @tc.require: AR000GHSJK
79  */
80 HWTEST_F(IpcServerStubTest, OnStart_001, testing::ext::TestSize.Level0)
81 {
82     // 1. set IpcServerStub state is ServiceRunningState::STATE_RUNNING
83     IpcServerStub::GetInstance().state_ = ServiceRunningState::STATE_RUNNING;
84     // 2. Call IpcServerStub OnStart
85     IpcServerStub::GetInstance().OnStart();
86     // 3. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
87     ASSERT_EQ(ServiceRunningState::STATE_RUNNING, IpcServerStub::GetInstance().state_);
88 }
89 
90 /**
91  * @tc.name: OnStart_002
92  * @tc.desc:  1. Set initial state to STATE_NOT_START
93  *            2. Call OnStart to start the service
94  *            3. Call OnStop to stop the service
95  *            4. Check the final state is STATE_NOT_START
96  * @tc.type: FUNC
97  * @tc.require: AR000GHSJK
98  */
99 HWTEST_F(IpcServerStubTest, OnStart_002, testing::ext::TestSize.Level0)
100 {
101     IpcServerStub::GetInstance().state_ = ServiceRunningState::STATE_NOT_START;
102     IpcServerStub::GetInstance().OnStart();
103     IpcServerStub::GetInstance().OnStop();
104     ASSERT_EQ(ServiceRunningState::STATE_NOT_START, IpcServerStub::GetInstance().state_);
105 }
106 
107 /**
108  * @tc.name: Init_001
109  * @tc.desc: 1. Call IpcServerStub OnStart
110  *           2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
111  * @tc.type: FUNC
112  * @tc.require: AR000GHSJK
113  */
114 HWTEST_F(IpcServerStubTest, Init_001, testing::ext::TestSize.Level0)
115 {
116     IpcServerStub::GetInstance().registerToService_=true;
117     bool result = IpcServerStub::GetInstance().Init();
118     // 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
119     ASSERT_EQ(result, true);
120 }
121 
122 /**
123  * @tc.name: OnRemoteRequest_001
124  * @tc.desc: 1. Set Code = 999
125  *           2. Call IpcServerStub OnRemoteRequest with param
126  *           3. check ret not DM_OK
127  * @tc.type: FUNC
128  * @tc.require: AR000GHSJK
129  */
130 HWTEST_F(IpcServerStubTest, OnRemoteRequest_001, testing::ext::TestSize.Level0)
131 {
132     // 1. Set Code = 999
133     uint32_t code = 999;
134     MessageParcel data;
135     MessageParcel reply;
136     MessageOption option;
137     int ret = 0;
138     // 2. Call IpcServerStub OnRemoteRequest with param
139     ret = IpcServerStub::GetInstance().OnRemoteRequest(code, data, reply, option);
140     // 3. check ret not DM_OK
141     ASSERT_NE(ret, DM_OK);
142 }
143 
144 /**
145  * @tc.name: OnRemoteRequest_002
146  * @tc.desc: 1. Set Code = 999
147  *           2. Call IpcServerStub OnRemoteRequest with param
148  *           3. check ret not DM_OK
149  * @tc.type: FUNC
150  * @tc.require: AR000GHSJK
151  */
152 HWTEST_F(IpcServerStubTest, OnRemoteRequest_002, testing::ext::TestSize.Level0)
153 {
154     // 1. Set Code is SERVER_DEVICE_STATE_NOTIFY
155     uint32_t code = SERVER_DEVICE_STATE_NOTIFY;
156     MessageParcel data;
157     MessageParcel reply;
158     MessageOption option;
159     int ret = 0;
160     // 2. Call IpcServerStub OnRemoteRequest with param
161     ret = IpcServerStub::GetInstance().OnRemoteRequest(code, data, reply, option);
162     // 3. check ret not DM_OK
163     ASSERT_NE(ret, DM_OK);
164 }
165 
166 /**
167  * @tc.name: SendCmd_001
168  * @tc.desc: 1. Call IpcServerStub SendCmd
169  *           2. check ret is DM_OK
170  * @tc.type: FUNC
171  * @tc.require: AR000GHSJK
172  */
173 HWTEST_F(IpcServerStubTest, SendCmd_001, testing::ext::TestSize.Level0)
174 {
175     int result = 305;
176     int32_t cmdCode = -1;
177     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
178     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
179     // 1. Call IpcServerStub SendCmd
180     int32_t ret = IpcServerStub::GetInstance().SendCmd(cmdCode, req, rsp);
181     // 2. check ret is DM_OK
182     ASSERT_EQ(ret, result);
183 }
184 
185 /**
186  * @tc.name: SendCmd_002
187  * @tc.desc: 1. Call IpcServerStub SendCmd
188  *           2. check ret is ERR_DM_INPUT_PARA_INVALID
189  * @tc.type: FUNC
190  * @tc.require: AR000GHSJK
191  */
192 HWTEST_F(IpcServerStubTest, SendCmd_002, testing::ext::TestSize.Level0)
193 {
194     int result = 305;
195     int32_t cmdCode = IPC_MSG_BUTT;
196     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
197     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
198     // 1. Call IpcServerStub SendCmd
199     int32_t ret = IpcServerStub::GetInstance().SendCmd(cmdCode, req, rsp);
200     // 2. check ret is DM_OK
201     ASSERT_EQ(ret, result);
202 }
203 
204 /**
205  * @tc.name: QueryServiceState_001
206  * @tc.desc: 1. Call IpcServerStub QueryServiceState
207  *           2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
208  * @tc.type: FUNC
209  * @tc.require: AR000GHSJK
210  */
211 HWTEST_F(IpcServerStubTest, QueryServiceState_001, testing::ext::TestSize.Level0)
212 {
213     IpcServerStub::GetInstance().state_ = ServiceRunningState::STATE_NOT_START;
214     // 1. Call IpcServerStub QueryServiceState
215     ServiceRunningState state = IpcServerStub::GetInstance().QueryServiceState();
216     // 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
217     ASSERT_EQ(state, ServiceRunningState::STATE_NOT_START);
218 }
219 
220 /**
221  * @tc.name: RegisterDeviceManagerListener_001
222  * @tc.desc: 1. Call IpcServerStub RegisterDeviceManagerListener
223  *           2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
224  * @tc.type: FUNC
225  * @tc.require: AR000GHSJK
226  */
227 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_001, testing::ext::TestSize.Level0)
228 {
229     std::string pkgName = "";
230     ProcessInfo processInfo;
231     processInfo.pkgName = pkgName;
232     processInfo.userId = 100;
233     int ret = 0;
234     sptr<IpcRemoteBroker> listener = nullptr;
235     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
236     ASSERT_EQ(ret, ERR_DM_POINT_NULL);
237 }
238 
239 /**
240  * @tc.name: RegisterDeviceManagerListener_002
241  * @tc.desc: 1. Call IpcServerStub RegisterDeviceManagerListener
242  *           2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
243  * @tc.type: FUNC
244  * @tc.require: AR000GHSJK
245  */
246 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_002, testing::ext::TestSize.Level0)
247 {
248     std::string pkgName = "com.ohos.test";
249     ProcessInfo processInfo;
250     processInfo.pkgName = pkgName;
251     processInfo.userId = 100;
252     int ret = 0;
253     sptr<IpcRemoteBroker> listener = sptr<IpcClientStub>(new IpcClientStub());
254     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
255     ASSERT_EQ(ret, DM_OK);
256 }
257 
258 /**
259  * @tc.name: RegisterDeviceManagerListener_003
260  * @tc.desc: 1. Call IpcServerStub RegisterDeviceManagerListener
261  *           2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
262  * @tc.type: FUNC
263  * @tc.require: AR000GHSJK
264  */
265 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_003, testing::ext::TestSize.Level0)
266 {
267     std::string pkgName = "";
268     ProcessInfo processInfo;
269     processInfo.pkgName = pkgName;
270     processInfo.userId = 100;
271     int ret = 0;
272     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
273     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
274     ASSERT_EQ(ret, ERR_DM_POINT_NULL);
275 }
276 
277 /**
278  * @tc.name: RegisterDeviceManagerListener_004
279  * @tc.desc: 1. Set PkgName is com.ohos.test
280  *           2. Call IpcServerStub RegisterDeviceManagerListener with param
281  *           3. check ret is DM_OK
282  * @tc.type: FUNC
283  * @tc.require: AR000GHSJK
284  */
285 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_004, testing::ext::TestSize.Level0)
286 {
287     // 1. Set PkgName is com.ohos.test
288     std::string pkgName = "com.ohos.test";
289     ProcessInfo processInfo;
290     processInfo.pkgName = pkgName;
291     processInfo.userId = 100;
292     int ret = 0;
293     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
294     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
295     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
296     // 3. check ret is DM_OK
297     ASSERT_EQ(ret, DM_OK);
298 }
299 
300 /**
301  * @tc.name: RegisterDeviceManagerListener_005
302  * @tc.desc: 1. Set PkgName is com.ohos.test
303  *                  2. Call IpcServerStub RegisterDeviceManagerListener with param
304  *                  3. check ret is DM_OK
305  *                  4. Call IpcServerStub RegisterDeviceManagerListener with same pkgName another listener
306  *                  5. check result is DM_OK
307  *                  6. earse pkgName for appRecipient_
308  *                  7. check result is DM_OK
309  * @tc.type: FUNC
310  * @tc.require: AR000GHSJK
311  */
312 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_005, testing::ext::TestSize.Level0)
313 {
314     // 1. Set PkgName is com.ohos.test
315     std::string pkgName = "com.ohos.test";
316     ProcessInfo processInfo;
317     processInfo.pkgName = pkgName;
318     processInfo.userId = 100;
319     int ret = 0;
320     int result = 0;
321     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
322     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
323     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
324     // 3. check ret is DM_OK
325     ASSERT_EQ(ret, DM_OK);
326     sptr<IpcClientStub> listener2 = sptr<IpcClientStub>(new IpcClientStub());
327     // 4. Call IpcServerStub RegisterDeviceManagerListener with same pkgName another listener
328     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener2);
329     // 5. check result is DM_OK
330     ASSERT_EQ(result, DM_OK);
331     sptr<IpcClientStub> listener3 = sptr<IpcClientStub>(new IpcClientStub());
332     // 6. earse pkgName for appRecipient_
333     IpcServerStub::GetInstance().appRecipient_.erase(processInfo);
334     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener3);
335     // 7. check result is DM_OK
336     ASSERT_EQ(result, DM_OK);
337 }
338 
339 /**
340  * @tc.name: UnRegisterDeviceManagerListener_001
341  * @tc.desc:  1. Call IpcServerStub UnRegisterDeviceManagerListener
342  *            2. check ret is ERR_DM_INPUT_PARA_INVALID
343  * @tc.type: FUNC
344  * @tc.require: AR000GHSJK
345  */
346 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_001, testing::ext::TestSize.Level0)
347 {
348     ProcessInfo processInfo;
349     int ret = 0;
350     ret = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(processInfo);
351     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
352 }
353 
354 /**
355  * @tc.name: UnRegisterDeviceManagerListener_002
356  * @tc.desc:  1. Set PkgName is com.ohos.test
357  *            2. Call IpcServerStub RegisterDeviceManagerListener with param
358  *            3. check ret is DM_OK
359  *            4. Call IpcServerStub UnRegisterDeviceManagerListener
360  *            5. check ret is DM_OK
361  * @tc.type: FUNC
362  * @tc.require: AR000GHSJK
363  */
364 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_002, testing::ext::TestSize.Level0)
365 {
366     // 1. Set PkgName is com.ohos.test
367     std::string pkgName = "com.ohos.test";
368     ProcessInfo processInfo;
369     processInfo.pkgName = pkgName;
370     processInfo.userId = 100;
371     int ret = 0;
372     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
373     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
374     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
375     // 3. check ret is DM_OK
376     ASSERT_EQ(ret, DM_OK);
377     int result = 0;
378     // 4. Call IpcServerStub UnRegisterDeviceManagerListener
379     result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(processInfo);
380     // 5. check ret is DM_OK
381     ASSERT_EQ(result, DM_OK);
382 }
383 
384 /**
385  * @tc.name: UnRegisterDeviceManagerListener_003
386  * @tc.desc:  1. Set pkgName is com.ohos.test
387  *            2. Call IpcServerStub UnRegisterDeviceManagerListener
388  *            3. check ret is DM_OK
389  * @tc.type: FUNC
390  * @tc.require: AR000GHSJK
391  */
392 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_003, testing::ext::TestSize.Level0)
393 {
394     // 1. Set pkgName is com.ohos.test
395     std::string pkgName = "com.ohos.test";
396     ProcessInfo processInfo;
397     processInfo.pkgName = pkgName;
398     processInfo.userId = 100;
399     int ret = 0;
400     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
401     ret = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(processInfo);
402     // 3. check ret is DM_OK
403     ASSERT_EQ(ret, DM_OK);
404 }
405 
406 /**
407  * @tc.name: UnRegisterDeviceManagerListener_004
408  * @tc.desc:  1. Set PkgName is com.ohos.test
409  *            2. Call IpcServerStub RegisterDeviceManagerListener with param
410  *            3. check ret is DM_OK
411  *            4. Call IpcServerStub UnRegisterDeviceManagerListener
412  *            5. check ret is DM_OK
413  * @tc.type: FUNC
414  * @tc.require: AR000GHSJK
415  */
416 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_004, testing::ext::TestSize.Level0)
417 {
418     // 1. Set PkgName is com.ohos.test
419     std::string pkgName = "com.ohos.test1";
420     ProcessInfo processInfo;
421     processInfo.pkgName = pkgName;
422     processInfo.userId = 100;
423     int ret = 0;
424     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
425     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
426     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
427     // 3. check ret is DM_OK
428     ASSERT_EQ(ret, DM_OK);
429     int result = 0;
430     // 4. Call IpcServerStub UnRegisterDeviceManagerListener
431     result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(processInfo);
432     // 5. check ret is DM_OK
433     ASSERT_EQ(result, DM_OK);
434     sptr<IpcRemoteBroker> dmListener = IpcServerStub::GetInstance().dmListener_[processInfo];
435     ASSERT_EQ(dmListener, nullptr);
436 }
437 
438 /**
439  * @tc.name: UnRegisterDeviceManagerListener_005
440  * @tc.desc:  1. Set PkgName is com.ohos.test
441  *            2. Call IpcServerStub RegisterDeviceManagerListener with param
442  *            3. check ret is DM_OK
443  *            4. Call IpcServerStub UnRegisterDeviceManagerListener
444  *            5. check ret is DM_OK
445  *            6. Call IpcServerStub UnRegisterDeviceManagerListener
446  *            7. check ret is DM_OK
447  * @tc.type: FUNC
448  * @tc.require: AR000GHSJK
449  */
450 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_005, testing::ext::TestSize.Level0)
451 {
452     // 1. Set PkgName is com.ohos.test
453     std::string pkgName = "com.ohos.test2";
454     ProcessInfo processInfo;
455     processInfo.pkgName = pkgName;
456     processInfo.userId = 100;
457     int ret = 0;
458     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
459     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
460     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
461     // 3. check ret is DM_OK
462     ASSERT_EQ(ret, DM_OK);
463     int result = 0;
464     // 4. Call IpcServerStub UnRegisterDeviceManagerListener
465     std::string testPkgName = "com.test";
466     result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(processInfo);
467     // 5. check ret is DM_OK
468     ASSERT_EQ(result, DM_OK);
469     IpcServerStub::GetInstance().appRecipient_.erase(processInfo);
470     // 6. Call IpcServerStub UnRegisterDeviceManagerListener
471     result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(processInfo);
472     // 7. check ret is DM_OK
473     ASSERT_EQ(result, DM_OK);
474 }
475 
476 /**
477  * @tc.name: GetDmListener_001
478  * @tc.desc: 1. Set pkgName is com.ohos.test
479  *           2. Call IpcServerStub GetDmListener
480  *           3. check ret is DM_OK
481  * @tc.type: FUNC
482  * @tc.require: AR000GHSJK
483  */
484 HWTEST_F(IpcServerStubTest, GetDmListener_001, testing::ext::TestSize.Level0)
485 {
486     // 1. Set pkgName is com.ohos.test
487     ProcessInfo processInfo;
488     processInfo.pkgName = "com.ohos.test";
489     sptr<IpcRemoteBroker> ret = nullptr;
490     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
491     ret = IpcServerStub::GetInstance().GetDmListener(processInfo);
492     // 3. check ret is DM_OK
493     ASSERT_EQ(ret, nullptr);
494 }
495 
496 /**
497  * @tc.name: GetDmListener_002
498  * @tc.desc: 1. Set pkgName is com.ohos.test
499  *           2. Call IpcServerStub GetDmListener
500  *           3. check ret is DM_OK
501  * @tc.type: FUNC
502  * @tc.require: AR000GHSJK
503  */
504 HWTEST_F(IpcServerStubTest, GetDmListener_002, testing::ext::TestSize.Level0)
505 {
506     // 1. Set pkgName is com.ohos.test
507     std::string pkgName = "com.ohos.test";
508     ProcessInfo processInfo;
509     processInfo.pkgName = pkgName;
510     int result = 0;
511     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
512     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
513     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
514     // 3. check ret is DM_OK
515     ASSERT_EQ(result, DM_OK);
516     sptr<IpcRemoteBroker> ret = nullptr;
517     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
518     ret = IpcServerStub::GetInstance().GetDmListener(processInfo);
519     // 3. check ret is DM_OK
520     ASSERT_NE(ret, nullptr);
521 }
522 
523 /**
524  * @tc.name: GetDmListener_003
525  * @tc.desc: 1. Set pkgName is com.ohos.test
526  *           2. Call IpcServerStub GetDmListener
527  *           3. check ret is DM_OK
528  * @tc.type: FUNC
529  * @tc.require: AR000GHSJK
530  */
531 HWTEST_F(IpcServerStubTest, GetDmListener_003, testing::ext::TestSize.Level0)
532 {
533     // 1. Set pkgName is com.ohos.test
534     std::string pkgName = "com.ohos.test";
535     ProcessInfo processInfo;
536     processInfo.pkgName = pkgName;
537     int result = 0;
538     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
539     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
540     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
541     // 3. check ret is DM_OK
542     ASSERT_EQ(result, DM_OK);
543     sptr<IpcRemoteBroker> ret = nullptr;
544     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
545     ret = IpcServerStub::GetInstance().GetDmListener(processInfo);
546     // 3. check ret is DM_OK
547     ASSERT_NE(ret, nullptr);
548 }
549 
550 /**
551  * @tc.name: GetDmListener_004
552  * @tc.desc: 1. Set pkgName is com.ohos.test
553  *           2. Call IpcServerStub GetDmListener
554  *           3. check ret is ERR_DM_POINT_NULL
555  * @tc.type: FUNC
556  * @tc.require: AR000GHSJK
557  */
558 HWTEST_F(IpcServerStubTest, GetDmListener_004, testing::ext::TestSize.Level0)
559 {
560     // 1. Set pkgName is null
561     std::string pkgName = "";
562     ProcessInfo processInfo;
563     processInfo.pkgName = pkgName;
564     int result = 0;
565     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
566     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
567     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
568     // 3. check ret is ERR_DM_POINT_NULL
569     ASSERT_EQ(result, ERR_DM_POINT_NULL);
570     sptr<IpcRemoteBroker> ret = nullptr;
571     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
572     ret = IpcServerStub::GetInstance().GetDmListener(processInfo);
573     // 3. check ret is nullptr
574     ASSERT_EQ(ret, nullptr);
575 }
576 
577 /**
578  * @tc.name: GetDmListener_005
579  * @tc.desc: 1. Set pkgName is com.ohos.test
580  *           2. Call IpcServerStub GetDmListener
581  *           3. check ret is ERR_DM_POINT_NULL
582  * @tc.type: FUNC
583  * @tc.require: AR000GHSJK
584  */
585 HWTEST_F(IpcServerStubTest, GetDmListener_005, testing::ext::TestSize.Level0)
586 {
587     // 1. Set pkgName is null
588     std::string pkgName = "com.test.ohos";
589     ProcessInfo processInfo;
590     processInfo.pkgName = pkgName;
591     int result = 0;
592     sptr<IpcClientStub> listener = nullptr;
593     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
594     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(processInfo, listener);
595     // 3. check ret is ERR_DM_POINT_NULL
596     ASSERT_EQ(result, ERR_DM_POINT_NULL);
597     sptr<IpcRemoteBroker> ret = nullptr;
598     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
599     ret = IpcServerStub::GetInstance().GetDmListener(processInfo);
600     // 3. check ret is nullptr
601     ASSERT_EQ(ret, nullptr);
602 }
603 
604 /**
605  * @tc.name: OnRemoveSystemAbility_001
606  * @tc.type: FUNC
607  */
608 HWTEST_F(IpcServerStubTest, OnRemoveSystemAbility_001, testing::ext::TestSize.Level0)
609 {
610     int32_t systemAbilityId = SOFTBUS_SERVER_SA_ID;
611     std::string deviceId;
612     IpcServerStub::GetInstance().OnRemoveSystemAbility(systemAbilityId, deviceId);
613     ASSERT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
614 }
615 
616 /**
617  * @tc.name: OnRemoveSystemAbility_002
618  * @tc.type: FUNC
619  */
620 HWTEST_F(IpcServerStubTest, OnRemoveSystemAbility_002, testing::ext::TestSize.Level0)
621 {
622     int32_t systemAbilityId = DISTRIBUTED_HARDWARE_SA_ID;
623     std::string deviceId;
624     IpcServerStub::GetInstance().OnRemoveSystemAbility(systemAbilityId, deviceId);
625     ASSERT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
626 }
627 
628 /**
629  * @tc.name: OnAddSystemAbility_001
630  * @tc.type: FUNC
631  */
632 HWTEST_F(IpcServerStubTest, OnAddSystemAbility_001, testing::ext::TestSize.Level0)
633 {
634     int32_t systemAbilityId = SOFTBUS_SERVER_SA_ID;
635     std::string deviceId;
636     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
637     ASSERT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr);
638     IpcServerStub::GetInstance().OnRemoveSystemAbility(systemAbilityId, deviceId);
639 }
640 
641 /**
642  * @tc.name: OnAddSystemAbility_002
643  * @tc.type: FUNC
644  */
645 HWTEST_F(IpcServerStubTest, OnAddSystemAbility_002, testing::ext::TestSize.Level0)
646 {
647     int32_t systemAbilityId = 9999;
648     std::string deviceId;
649     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
650 
651     systemAbilityId = SOFTBUS_SERVER_SA_ID;
652     IpcServerStub::GetInstance().registerToService_ = false;
653     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
654     EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr);
655 
656     DeviceManagerService::GetInstance().softbusListener_ = nullptr;
657     systemAbilityId = SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN;
658     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
659     EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
660 
661     systemAbilityId = SCREENLOCK_SERVICE_ID;
662     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
663     EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
664 }
665 
666 /**
667  * @tc.name: GetAllProcessInfo_001
668  * @tc.type: FUNC
669  */
670 HWTEST_F(IpcServerStubTest, GetAllProcessInfo_001, testing::ext::TestSize.Level0)
671 {
672     std::vector<ProcessInfo>  processInfo;
673     processInfo = IpcServerStub::GetInstance().GetAllProcessInfo();
674     ASSERT_EQ(processInfo.empty(), false);
675 }
676 
677 /**
678  * @tc.name: Dump_001
679  * @tc.type: FUNC
680  */
681 HWTEST_F(IpcServerStubTest, Dump_001, testing::ext::TestSize.Level0)
682 {
683     int32_t fd = 0;
684     std::vector<std::u16string> args;
685     int32_t ret = IpcServerStub::GetInstance().Dump(fd, args);
686     ASSERT_NE(ret, DM_OK);
687 }
688 
689 HWTEST_F(IpcServerStubTest, HandleSoftBusServerAdd_001, testing::ext::TestSize.Level0)
690 {
691     IpcServerStub::GetInstance().registerToService_ = true;
692     IpcServerStub::GetInstance().state_ = ServiceRunningState::STATE_NOT_START;
693     IpcServerStub::GetInstance().HandleSoftBusServerAdd();
694 
695     EXPECT_EQ(ServiceRunningState::STATE_RUNNING, IpcServerStub::GetInstance().state_);
696     EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr);
697 }
698 
699 HWTEST_F(IpcServerStubTest, HandleSoftBusServerAdd_002, testing::ext::TestSize.Level0)
700 {
701     IpcServerStub::GetInstance().registerToService_ = false;
702     IpcServerStub::GetInstance().state_ = ServiceRunningState::STATE_NOT_START;
703     IpcServerStub::GetInstance().HandleSoftBusServerAdd();
704 
705     EXPECT_EQ(ServiceRunningState::STATE_NOT_START, IpcServerStub::GetInstance().state_);
706     EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr);
707 }
708 
709 HWTEST_F(IpcServerStubTest, AddDelimiter_001, testing::ext::TestSize.Level0)
710 {
711     std::string input = "";
712     std::string result = IpcServerStub::GetInstance().AddDelimiter(input);
713     ASSERT_EQ(result, "");
714 }
715 
716 HWTEST_F(IpcServerStubTest, AddDelimiter_002, testing::ext::TestSize.Level0)
717 {
718     std::string input = "path/";
719     std::string result = IpcServerStub::GetInstance().AddDelimiter(input);
720     ASSERT_EQ(result, "path/");
721 }
722 
723 HWTEST_F(IpcServerStubTest, AddDelimiter_003, testing::ext::TestSize.Level0)
724 {
725     std::string input = "path";
726     std::string result = IpcServerStub::GetInstance().AddDelimiter(input);
727     ASSERT_EQ(result, "path/");
728 }
729 
730 HWTEST_F(IpcServerStubTest, JoinPath_001, testing::ext::TestSize.Level0)
731 {
732     std::string prefixPath = "path";
733     std::string subPath = "subpath";
734     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, subPath);
735     ASSERT_EQ(result, "path/subpath");
736 }
737 
738 HWTEST_F(IpcServerStubTest, JoinPath_002, testing::ext::TestSize.Level0)
739 {
740     std::string prefixPath = "path/";
741     std::string subPath = "subpath";
742     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, subPath);
743     ASSERT_EQ(result, "path/subpath");
744 }
745 
746 HWTEST_F(IpcServerStubTest, JoinPath_003, testing::ext::TestSize.Level0)
747 {
748     std::string prefixPath = "";
749     std::string subPath = "subpath";
750     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, subPath);
751     ASSERT_EQ(result, "subpath");
752 }
753 
754 HWTEST_F(IpcServerStubTest, JoinPath_004, testing::ext::TestSize.Level0)
755 {
756     std::string prefixPath = "path";
757     std::string subPath = "";
758     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, subPath);
759     ASSERT_EQ(result, "path/");
760 }
761 
762 HWTEST_F(IpcServerStubTest, JoinPath_005, testing::ext::TestSize.Level0)
763 {
764     std::string prefixPath = "";
765     std::string subPath = "";
766     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, subPath);
767     ASSERT_EQ(result, "");
768 }
769 
770 HWTEST_F(IpcServerStubTest, JoinPath_006, testing::ext::TestSize.Level0)
771 {
772     std::string prefixPath = "path";
773     std::string subPath = "/subpath";
774     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, subPath);
775     ASSERT_EQ(result, "path//subpath");
776 }
777 
778 HWTEST_F(IpcServerStubTest, JoinPath_007, testing::ext::TestSize.Level0)
779 {
780     std::string prefixPath = "path/";
781     std::string subPath = "/subpath";
782     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, subPath);
783     ASSERT_EQ(result, "path//subpath");
784 }
785 
786 HWTEST_F(IpcServerStubTest, JoinPath2_001, testing::ext::TestSize.Level0)
787 {
788     std::string prefixPath = "path";
789     std::string midPath = "midpath";
790     std::string subPath = "subpath";
791     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, midPath, subPath);
792     ASSERT_EQ(result, "path/midpath/subpath");
793 }
794 
795 HWTEST_F(IpcServerStubTest, JoinPath2_002, testing::ext::TestSize.Level0)
796 {
797     std::string prefixPath = "path";
798     std::string midPath = "";
799     std::string subPath = "subpath";
800     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, midPath, subPath);
801     ASSERT_EQ(result, "path/subpath");
802 }
803 
804 HWTEST_F(IpcServerStubTest, JoinPath2_003, testing::ext::TestSize.Level0)
805 {
806     std::string prefixPath = "path";
807     std::string midPath = "midpath";
808     std::string subPath = "";
809     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, midPath, subPath);
810     ASSERT_EQ(result, "path/midpath/");
811 }
812 
813 HWTEST_F(IpcServerStubTest, JoinPath2_004, testing::ext::TestSize.Level0)
814 {
815     std::string prefixPath = "";
816     std::string midPath = "midpath";
817     std::string subPath = "";
818     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, midPath, subPath);
819     ASSERT_EQ(result, "midpath/");
820 }
821 
822 HWTEST_F(IpcServerStubTest, JoinPath2_005, testing::ext::TestSize.Level0)
823 {
824     std::string prefixPath = "";
825     std::string midPath = "";
826     std::string subPath = "";
827     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, midPath, subPath);
828     ASSERT_EQ(result, "");
829 }
830 
831 HWTEST_F(IpcServerStubTest, JoinPath2_006, testing::ext::TestSize.Level0)
832 {
833     std::string prefixPath = "path/";
834     std::string midPath = "/midpath/";
835     std::string subPath = "/subpath";
836     std::string result = IpcServerStub::GetInstance().JoinPath(prefixPath, midPath, subPath);
837     ASSERT_EQ(result, "path//midpath//subpath");
838 }
839 } // namespace
840 } // namespace DistributedHardware
841 } // namespace OHOS
842