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 ERR_DM_IPC_READ_FAILED
163     ASSERT_EQ(ret, ERR_DM_IPC_READ_FAILED);
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     int ret = 0;
231     sptr<IpcRemoteBroker> listener = nullptr;
232     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
233     ASSERT_EQ(ret, ERR_DM_POINT_NULL);
234 }
235 
236 /**
237  * @tc.name: RegisterDeviceManagerListener_002
238  * @tc.desc: 1. Call IpcServerStub RegisterDeviceManagerListener
239  *           2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
240  * @tc.type: FUNC
241  * @tc.require: AR000GHSJK
242  */
243 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_002, testing::ext::TestSize.Level0)
244 {
245     std::string pkgName = "com.ohos.test";
246     int ret = 0;
247     sptr<IpcRemoteBroker> listener = sptr<IpcClientStub>(new IpcClientStub());
248     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
249     ASSERT_EQ(ret, DM_OK);
250 }
251 
252 /**
253  * @tc.name: RegisterDeviceManagerListener_003
254  * @tc.desc: 1. Call IpcServerStub RegisterDeviceManagerListener
255  *           2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING
256  * @tc.type: FUNC
257  * @tc.require: AR000GHSJK
258  */
259 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_003, testing::ext::TestSize.Level0)
260 {
261     std::string pkgName = "";
262     int ret = 0;
263     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
264     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
265     ASSERT_EQ(ret, ERR_DM_POINT_NULL);
266 }
267 
268 /**
269  * @tc.name: RegisterDeviceManagerListener_004
270  * @tc.desc: 1. Set PkgName is com.ohos.test
271  *           2. Call IpcServerStub RegisterDeviceManagerListener with param
272  *           3. check ret is DM_OK
273  * @tc.type: FUNC
274  * @tc.require: AR000GHSJK
275  */
276 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_004, testing::ext::TestSize.Level0)
277 {
278     // 1. Set PkgName is com.ohos.test
279     std::string pkgName = "com.ohos.test";
280     int ret = 0;
281     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
282     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
283     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
284     // 3. check ret is DM_OK
285     ASSERT_EQ(ret, DM_OK);
286 }
287 
288 /**
289  * @tc.name: RegisterDeviceManagerListener_005
290  * @tc.desc: 1. Set PkgName is com.ohos.test
291  *                  2. Call IpcServerStub RegisterDeviceManagerListener with param
292  *                  3. check ret is DM_OK
293  *                  4. Call IpcServerStub RegisterDeviceManagerListener with same pkgName another listener
294  *                  5. check result is DM_OK
295  *                  6. earse pkgName for appRecipient_
296  *                  7. check result is DM_OK
297  * @tc.type: FUNC
298  * @tc.require: AR000GHSJK
299  */
300 HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_005, testing::ext::TestSize.Level0)
301 {
302     // 1. Set PkgName is com.ohos.test
303     std::string pkgName = "com.ohos.test";
304     int ret = 0;
305     int result = 0;
306     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
307     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
308     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
309     // 3. check ret is DM_OK
310     ASSERT_EQ(ret, DM_OK);
311     sptr<IpcClientStub> listener2 = sptr<IpcClientStub>(new IpcClientStub());
312     // 4. Call IpcServerStub RegisterDeviceManagerListener with same pkgName another listener
313     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener2);
314     // 5. check result is DM_OK
315     ASSERT_EQ(result, DM_OK);
316     sptr<IpcClientStub> listener3 = sptr<IpcClientStub>(new IpcClientStub());
317     // 6. earse pkgName for appRecipient_
318     IpcServerStub::GetInstance().appRecipient_.erase(pkgName);
319     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener3);
320     // 7. check result is DM_OK
321     ASSERT_EQ(result, DM_OK);
322 }
323 
324 /**
325  * @tc.name: UnRegisterDeviceManagerListener_001
326  * @tc.desc:  1. Call IpcServerStub UnRegisterDeviceManagerListener
327  *            2. check ret is ERR_DM_INPUT_PARA_INVALID
328  * @tc.type: FUNC
329  * @tc.require: AR000GHSJK
330  */
331 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_001, testing::ext::TestSize.Level0)
332 {
333     std::string pkgName;
334     int ret = 0;
335     ret = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
336     ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
337 }
338 
339 /**
340  * @tc.name: UnRegisterDeviceManagerListener_002
341  * @tc.desc:  1. Set PkgName is com.ohos.test
342  *            2. Call IpcServerStub RegisterDeviceManagerListener with param
343  *            3. check ret is DM_OK
344  *            4. Call IpcServerStub UnRegisterDeviceManagerListener
345  *            5. check ret is DM_OK
346  * @tc.type: FUNC
347  * @tc.require: AR000GHSJK
348  */
349 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_002, testing::ext::TestSize.Level0)
350 {
351     // 1. Set PkgName is com.ohos.test
352     std::string pkgName = "com.ohos.test";
353     int ret = 0;
354     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
355     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
356     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
357     // 3. check ret is DM_OK
358     ASSERT_EQ(ret, DM_OK);
359     int result = 0;
360     // 4. Call IpcServerStub UnRegisterDeviceManagerListener
361     result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
362     // 5. check ret is DM_OK
363     ASSERT_EQ(result, DM_OK);
364 }
365 
366 /**
367  * @tc.name: UnRegisterDeviceManagerListener_003
368  * @tc.desc:  1. Set pkgName is com.ohos.test
369  *            2. Call IpcServerStub UnRegisterDeviceManagerListener
370  *            3. check ret is DM_OK
371  * @tc.type: FUNC
372  * @tc.require: AR000GHSJK
373  */
374 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_003, testing::ext::TestSize.Level0)
375 {
376     // 1. Set pkgName is com.ohos.test
377     std::string pkgName = "com.ohos.test";
378     int ret = 0;
379     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
380     ret = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
381     // 3. check ret is DM_OK
382     ASSERT_EQ(ret, DM_OK);
383 }
384 
385 /**
386  * @tc.name: UnRegisterDeviceManagerListener_004
387  * @tc.desc:  1. Set PkgName is com.ohos.test
388  *            2. Call IpcServerStub RegisterDeviceManagerListener with param
389  *            3. check ret is DM_OK
390  *            4. Call IpcServerStub UnRegisterDeviceManagerListener
391  *            5. check ret is DM_OK
392  * @tc.type: FUNC
393  * @tc.require: AR000GHSJK
394  */
395 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_004, testing::ext::TestSize.Level0)
396 {
397     // 1. Set PkgName is com.ohos.test
398     std::string pkgName = "com.ohos.test1";
399     int ret = 0;
400     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
401     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
402     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
403     // 3. check ret is DM_OK
404     ASSERT_EQ(ret, DM_OK);
405     int result = 0;
406     // 4. Call IpcServerStub UnRegisterDeviceManagerListener
407     result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
408     // 5. check ret is DM_OK
409     ASSERT_EQ(result, DM_OK);
410     sptr<IpcRemoteBroker> dmListener = IpcServerStub::GetInstance().dmListener_[pkgName];
411     ASSERT_EQ(dmListener, nullptr);
412 }
413 
414 /**
415  * @tc.name: UnRegisterDeviceManagerListener_005
416  * @tc.desc:  1. Set PkgName is com.ohos.test
417  *            2. Call IpcServerStub RegisterDeviceManagerListener with param
418  *            3. check ret is DM_OK
419  *            4. Call IpcServerStub UnRegisterDeviceManagerListener
420  *            5. check ret is DM_OK
421  *            6. Call IpcServerStub UnRegisterDeviceManagerListener
422  *            7. check ret is DM_OK
423  * @tc.type: FUNC
424  * @tc.require: AR000GHSJK
425  */
426 HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_005, testing::ext::TestSize.Level0)
427 {
428     // 1. Set PkgName is com.ohos.test
429     std::string pkgName = "com.ohos.test2";
430     int ret = 0;
431     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
432     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
433     ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
434     // 3. check ret is DM_OK
435     ASSERT_EQ(ret, DM_OK);
436     int result = 0;
437     // 4. Call IpcServerStub UnRegisterDeviceManagerListener
438     std::string testPkgName = "com.test";
439     result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(testPkgName);
440     // 5. check ret is DM_OK
441     ASSERT_EQ(result, DM_OK);
442     IpcServerStub::GetInstance().appRecipient_.erase(pkgName);
443     // 6. Call IpcServerStub UnRegisterDeviceManagerListener
444     result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
445     // 7. check ret is DM_OK
446     ASSERT_EQ(result, DM_OK);
447 }
448 
449 /**
450  * @tc.name: SendALL_001
451  * @tc.desc:  1. Set PkgName1 is com.ohos.SendALL_001
452  *            2. Set PkgName2 is com.ohos.SendALL_002
453  *            3. Add listener1 (nullptr) to dmListener_ with key pkgName1
454  *            4. Add listener2 to dmListener_ with key listener2
455  *            5. Call IpcServerStub::SendALL with cmdCode, req, rsp
456  *            6. Check result is DM_OK
457  * @tc.type: FUNC
458  * @tc.require: AR000GHSJK
459  */
460 HWTEST_F(IpcServerStubTest, SendALL_001, testing::ext::TestSize.Level0)
461 {
462     int32_t cmdCode = -1;
463     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
464     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
465     std::string pkgName1 = "com.ohos.SendALL_001";
466     std::string pkgName2 = "com.ohos.SendALL_002";
467     sptr<IpcClientStub> listener1 = nullptr;
468     sptr<IpcClientStub> listener2 = sptr<IpcClientStub>(new IpcClientStub());
469     IpcServerStub::GetInstance().dmListener_[pkgName1] = listener1;
470     IpcServerStub::GetInstance().dmListener_[pkgName2] = listener2;
471     int32_t result = IpcServerStub::GetInstance().SendALL(cmdCode, req, rsp);
472     ASSERT_EQ(result, DM_OK);
473     IpcServerStub::GetInstance().dmListener_.clear();
474 }
475 
476 /**
477  * @tc.name: GetDmListenerPkgName_001
478  * @tc.desc:  1. Set pkgName is com.ohos.GetDmListenerPkgName_001
479  *            2. Create listener and add it to dmListener_ with key pkgName
480  *            3. Call IpcServerStub::GetDmListenerPkgName with remote object
481  *            4. Check the result is not empty
482  * @tc.type: FUNC
483  * @tc.require: AR000GHSJK
484  */
485 HWTEST_F(IpcServerStubTest, GetDmListenerPkgName_001, testing::ext::TestSize.Level0)
486 {
487     sptr<IRemoteObject> remote(new IpcClientStub());
488     std::string pkgName = "com.ohos.GetDmListenerPkgName_001";
489     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
490     IpcServerStub::GetInstance().dmListener_[pkgName] = listener;
491     std::string ret = IpcServerStub::GetInstance().GetDmListenerPkgName(remote);
492     EXPECT_TRUE(ret.empty() || (ret == pkgName));
493     IpcServerStub::GetInstance().dmListener_.clear();
494 }
495 
496 /**
497  * @tc.name: GetDmListener_001
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_001, testing::ext::TestSize.Level0)
505 {
506     // 1. Set pkgName is com.ohos.test
507     std::string pkgName = "com.ohos.test";
508     sptr<IpcRemoteBroker> ret = nullptr;
509     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
510     ret = IpcServerStub::GetInstance().GetDmListener(pkgName);
511     // 3. check ret is DM_OK
512     ASSERT_EQ(ret, nullptr);
513 }
514 
515 /**
516  * @tc.name: GetDmListener_002
517  * @tc.desc: 1. Set pkgName is com.ohos.test
518  *           2. Call IpcServerStub GetDmListener
519  *           3. check ret is DM_OK
520  * @tc.type: FUNC
521  * @tc.require: AR000GHSJK
522  */
523 HWTEST_F(IpcServerStubTest, GetDmListener_002, testing::ext::TestSize.Level0)
524 {
525     // 1. Set pkgName is com.ohos.test
526     std::string pkgName = "com.ohos.test";
527     int result = 0;
528     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
529     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
530     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
531     // 3. check ret is DM_OK
532     ASSERT_EQ(result, DM_OK);
533     sptr<IpcRemoteBroker> ret = nullptr;
534     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
535     ret = IpcServerStub::GetInstance().GetDmListener(pkgName);
536     // 3. check ret is DM_OK
537     ASSERT_NE(ret, nullptr);
538 }
539 
540 /**
541  * @tc.name: GetDmListener_003
542  * @tc.desc: 1. Set pkgName is com.ohos.test
543  *           2. Call IpcServerStub GetDmListener
544  *           3. check ret is DM_OK
545  * @tc.type: FUNC
546  * @tc.require: AR000GHSJK
547  */
548 HWTEST_F(IpcServerStubTest, GetDmListener_003, testing::ext::TestSize.Level0)
549 {
550     // 1. Set pkgName is com.ohos.test
551     std::string pkgName = "com.ohos.test";
552     int result = 0;
553     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
554     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
555     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
556     // 3. check ret is DM_OK
557     ASSERT_EQ(result, DM_OK);
558     sptr<IpcRemoteBroker> ret = nullptr;
559     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
560     std::string testPkgName = "test";
561     ret = IpcServerStub::GetInstance().GetDmListener(testPkgName);
562     // 3. check ret is DM_OK
563     ASSERT_EQ(ret, nullptr);
564 }
565 
566 /**
567  * @tc.name: GetDmListener_004
568  * @tc.desc: 1. Set pkgName is com.ohos.test
569  *           2. Call IpcServerStub GetDmListener
570  *           3. check ret is ERR_DM_POINT_NULL
571  * @tc.type: FUNC
572  * @tc.require: AR000GHSJK
573  */
574 HWTEST_F(IpcServerStubTest, GetDmListener_004, testing::ext::TestSize.Level0)
575 {
576     // 1. Set pkgName is null
577     std::string pkgName = "";
578     int result = 0;
579     sptr<IpcClientStub> listener = sptr<IpcClientStub>(new IpcClientStub());
580     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
581     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
582     // 3. check ret is ERR_DM_POINT_NULL
583     ASSERT_EQ(result, ERR_DM_POINT_NULL);
584     sptr<IpcRemoteBroker> ret = nullptr;
585     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
586     ret = IpcServerStub::GetInstance().GetDmListener(pkgName);
587     // 3. check ret is nullptr
588     ASSERT_EQ(ret, nullptr);
589 }
590 
591 /**
592  * @tc.name: GetDmListener_005
593  * @tc.desc: 1. Set pkgName is com.ohos.test
594  *           2. Call IpcServerStub GetDmListener
595  *           3. check ret is ERR_DM_POINT_NULL
596  * @tc.type: FUNC
597  * @tc.require: AR000GHSJK
598  */
599 HWTEST_F(IpcServerStubTest, GetDmListener_005, testing::ext::TestSize.Level0)
600 {
601     // 1. Set pkgName is null
602     std::string pkgName = "com.test.ohos";
603     int result = 0;
604     sptr<IpcClientStub> listener = nullptr;
605     // 2. Call IpcServerStub RegisterDeviceManagerListener with param
606     result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
607     // 3. check ret is ERR_DM_POINT_NULL
608     ASSERT_EQ(result, ERR_DM_POINT_NULL);
609     sptr<IpcRemoteBroker> ret = nullptr;
610     // 2. Call IpcServerStub UnRegisterDeviceManagerListener
611     ret = IpcServerStub::GetInstance().GetDmListener(pkgName);
612     // 3. check ret is nullptr
613     ASSERT_EQ(ret, nullptr);
614 }
615 
616 /**
617  * @tc.name: OnRemoveSystemAbility_001
618  * @tc.type: FUNC
619  */
620 HWTEST_F(IpcServerStubTest, OnRemoveSystemAbility_001, testing::ext::TestSize.Level0)
621 {
622     int32_t systemAbilityId = SOFTBUS_SERVER_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: OnRemoveSystemAbility_002
630  * @tc.type: FUNC
631  */
632 HWTEST_F(IpcServerStubTest, OnRemoveSystemAbility_002, testing::ext::TestSize.Level0)
633 {
634     int32_t systemAbilityId = DISTRIBUTED_HARDWARE_SA_ID;
635     std::string deviceId;
636     IpcServerStub::GetInstance().OnRemoveSystemAbility(systemAbilityId, deviceId);
637     ASSERT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
638 }
639 
640 /**
641  * @tc.name: OnAddSystemAbility_001
642  * @tc.type: FUNC
643  */
644 HWTEST_F(IpcServerStubTest, OnAddSystemAbility_001, testing::ext::TestSize.Level0)
645 {
646     int32_t systemAbilityId = SOFTBUS_SERVER_SA_ID;
647     std::string deviceId;
648     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
649     ASSERT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr);
650     IpcServerStub::GetInstance().OnRemoveSystemAbility(systemAbilityId, deviceId);
651 }
652 
653 /**
654  * @tc.name: OnAddSystemAbility_002
655  * @tc.type: FUNC
656  */
657 HWTEST_F(IpcServerStubTest, OnAddSystemAbility_002, testing::ext::TestSize.Level0)
658 {
659     int32_t systemAbilityId = 9999;
660     std::string deviceId;
661     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
662     EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
663 
664     systemAbilityId = SOFTBUS_SERVER_SA_ID;
665     IpcServerStub::GetInstance().registerToService_ = false;
666     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
667     EXPECT_NE(DeviceManagerService::GetInstance().softbusListener_, nullptr);
668 
669     DeviceManagerService::GetInstance().softbusListener_ = nullptr;
670     systemAbilityId = SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN;
671     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
672     EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
673 
674     systemAbilityId = SCREENLOCK_SERVICE_ID;
675     IpcServerStub::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
676     EXPECT_EQ(DeviceManagerService::GetInstance().softbusListener_, nullptr);
677 }
678 
679 /**
680  * @tc.name: GetAllPkgName_001
681  * @tc.type: FUNC
682  */
683 HWTEST_F(IpcServerStubTest, GetAllPkgName_001, testing::ext::TestSize.Level0)
684 {
685     std::vector<std::string>  pkgName;
686     pkgName = IpcServerStub::GetInstance().GetAllPkgName();
687     ASSERT_EQ(pkgName.empty(), false);
688 }
689 
690 /**
691  * @tc.name: Dump_001
692  * @tc.type: FUNC
693  */
694 HWTEST_F(IpcServerStubTest, Dump_001, testing::ext::TestSize.Level0)
695 {
696     int32_t fd = 0;
697     std::vector<std::u16string> args;
698     int32_t ret = IpcServerStub::GetInstance().Dump(fd, args);
699     ASSERT_NE(ret, DM_OK);
700 }
701 } // namespace
702 } // namespace DistributedHardware
703 } // namespace OHOS
704