1 /*
2 * Copyright (c) 2021 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 #include "gtest/gtest.h"
16
17 #define private public
18 #define protected public
19 #include "ability_connect_callback_stub.h"
20 #include "distributed_sched_service.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "test_log.h"
26 #undef private
27 #undef protected
28
29 namespace OHOS {
30 namespace DistributedSchedule {
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace {
35 constexpr int32_t STDOUT_FD = 1;
36 }
37
38 class AbilityConnectCallbackTest : public AAFwk::AbilityConnectionStub {
39 public:
40 AbilityConnectCallbackTest() = default;
41 ~AbilityConnectCallbackTest() = default;
42
43 void OnAbilityConnectDone(const AppExecFwk::ElementName& element,
44 const sptr<IRemoteObject>& remoteObject, int32_t resultCode) override;
45 void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
46 int32_t resultCode) override;
47 };
48
49 class AbilityConnectionWrapperStubTest : public AAFwk::AbilityConnectionStub {
50 public:
AbilityConnectionWrapperStubTest(sptr<IRemoteObject> connection)51 explicit AbilityConnectionWrapperStubTest(sptr<IRemoteObject> connection) : distributedConnection_(connection) {}
52 ~AbilityConnectionWrapperStubTest() = default;
53
54 void OnAbilityConnectDone(const AppExecFwk::ElementName& element,
55 const sptr<IRemoteObject>& remoteObject, int32_t resultCode) override;
56 void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
57 int32_t resultCode) override;
58
59 private:
60 sptr<IRemoteObject> distributedConnection_;
61 };
62
63 class DistributedSchedConnectTest : public testing::Test {
64 public:
65 static void SetUpTestCase();
66 static void TearDownTestCase();
67 void SetUp();
68 void TearDown();
69
70 void AddSession(const sptr<IRemoteObject>& connect, const std::string& localDeviceId,
71 const std::string& remoteDeviceId, const AAFwk::Want& want) const;
72 void RemoveSession(const sptr<IRemoteObject>& connect) const;
73
74 void AddConnectInfo(const sptr<IRemoteObject>& connect, const std::string& localDeviceId,
75 const std::string& remoteDeviceId) const;
76 void RemoveConnectInfo(const sptr<IRemoteObject>& connect) const;
77
78 void AddConnectCount(int32_t uid) const;
79 void DecreaseConnectCount(int32_t uid) const;
80 };
81
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)82 void AbilityConnectCallbackTest::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
83 const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
84 {
85 }
86
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)87 void AbilityConnectCallbackTest::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
88 int32_t resultCode)
89 {
90 }
91
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)92 void AbilityConnectionWrapperStubTest::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
93 const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
94 {
95 }
96
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)97 void AbilityConnectionWrapperStubTest::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
98 int32_t resultCode)
99 {
100 }
101
SetUpTestCase()102 void DistributedSchedConnectTest::SetUpTestCase()
103 {
104 }
105
TearDownTestCase()106 void DistributedSchedConnectTest::TearDownTestCase()
107 {
108 }
109
SetUp()110 void DistributedSchedConnectTest::SetUp()
111 {
112 }
113
TearDown()114 void DistributedSchedConnectTest::TearDown()
115 {
116 }
117
AddSession(const sptr<IRemoteObject> & connect,const std::string & localDeviceId,const std::string & remoteDeviceId,const AAFwk::Want & want) const118 void DistributedSchedConnectTest::AddSession(const sptr<IRemoteObject>& connect,
119 const std::string& localDeviceId, const std::string& remoteDeviceId, const AAFwk::Want& want) const
120 {
121 if (connect == nullptr) {
122 return;
123 }
124
125 std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
126 CallerInfo callerInfo;
127 callerInfo.uid = IPCSkeleton::GetCallingUid();
128 callerInfo.pid = IPCSkeleton::GetCallingPid();
129 callerInfo.sourceDeviceId = localDeviceId;
130 callerInfo.callerType = CALLER_TYPE_HARMONY;
131 DistributedSchedService::GetInstance().RemoteConnectAbilityMappingLocked(connect, localDeviceId,
132 remoteDeviceId, want.GetElement(), callerInfo, TargetComponent::HARMONY_COMPONENT);
133 }
134
RemoveSession(const sptr<IRemoteObject> & connect) const135 void DistributedSchedConnectTest::RemoveSession(const sptr<IRemoteObject>& connect) const
136 {
137 if (connect == nullptr) {
138 return;
139 }
140
141 std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
142 DistributedSchedService::GetInstance().distributedConnectAbilityMap_.erase(connect);
143 }
144
AddConnectInfo(const sptr<IRemoteObject> & connect,const std::string & localDeviceId,const std::string & remoteDeviceId) const145 void DistributedSchedConnectTest::AddConnectInfo(const sptr<IRemoteObject>& connect,
146 const std::string& localDeviceId, const std::string& remoteDeviceId) const
147 {
148 if (connect == nullptr) {
149 return;
150 }
151
152 std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
153 CallerInfo callerInfo;
154 callerInfo.uid = IPCSkeleton::GetCallingUid();
155 callerInfo.pid = IPCSkeleton::GetCallingPid();
156 callerInfo.sourceDeviceId = localDeviceId;
157 callerInfo.callerType = CALLER_TYPE_HARMONY;
158
159 sptr<IRemoteObject> callbackWrapper = new AbilityConnectionWrapperStubTest(connect);
160 ConnectInfo connectInfo {callerInfo, callbackWrapper};
161 DistributedSchedService::GetInstance().connectAbilityMap_.emplace(connect, connectInfo);
162 }
163
RemoveConnectInfo(const sptr<IRemoteObject> & connect) const164 void DistributedSchedConnectTest::RemoveConnectInfo(const sptr<IRemoteObject>& connect) const
165 {
166 if (connect == nullptr) {
167 return;
168 }
169
170 std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
171 DistributedSchedService::GetInstance().connectAbilityMap_.erase(connect);
172 }
173
AddConnectCount(int32_t uid) const174 void DistributedSchedConnectTest::AddConnectCount(int32_t uid) const
175 {
176 if (uid < 0) {
177 return;
178 }
179
180 auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
181 ++trackingUidMap[uid];
182 }
183
DecreaseConnectCount(int32_t uid) const184 void DistributedSchedConnectTest::DecreaseConnectCount(int32_t uid) const
185 {
186 if (uid < 0) {
187 return;
188 }
189
190 DistributedSchedService::GetInstance().DecreaseConnectLocked(uid);
191 }
192
193 /**
194 * @tc.name: DumpConnectInfo_001
195 * @tc.desc: dump connect ability info by call Dump
196 * @tc.type: FUNC
197 */
198 HWTEST_F(DistributedSchedConnectTest, DumpConnectInfo_001, TestSize.Level1)
199 {
200 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_001 start " << std::endl;
201 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
202 if (samgr == nullptr) {
203 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_001 samgr null" << std::endl;
204 } else {
205 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_001 available" << std::endl;
206 }
207
208 auto dms = samgr->GetSystemAbility(DISTRIBUTED_SCHED_SA_ID);
209 std::vector<std::u16string> args;
210 args.push_back(u"-connect");
211 int32_t result = dms->Dump(STDOUT_FD, args);
212 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_001 dump result: " << result << std::endl;
213 }
214
215 /**
216 * @tc.name: DumpConnectInfo_002
217 * @tc.desc: dump connect ability info by call DumpConnectInfo
218 * @tc.type: FUNC
219 */
220 HWTEST_F(DistributedSchedConnectTest, DumpConnectInfo_002, TestSize.Level0)
221 {
222 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_002 start" << std::endl;
223 OHOS::AAFwk::Want want;
224 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
225
226 /**
227 * @tc.steps: step1. add one session
228 */
229 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
230 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
231 /**
232 * @tc.steps: step2. and then dump connection info
233 * @tc.expected: step2. can find the newly-added connect session
234 */
235 std::string dumpInfo;
236 DistributedSchedService::GetInstance().DumpConnectInfo(dumpInfo);
237 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_002 dumpInfo " << dumpInfo << std::endl;
238 std::string::size_type pos = dumpInfo.find("123_remote_device_id");
239 EXPECT_NE(pos, std::string::npos);
240
241 RemoveSession(connect);
242 }
243
244 /**
245 * @tc.name: ProcessConnectDied001
246 * @tc.desc: process connect died
247 * @tc.type: FUNC
248 */
249 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied001, TestSize.Level1)
250 {
251 DTEST_LOG << "DistributedSchedServiceTest ProcessConnectDied001 start" << std::endl;
252 OHOS::AAFwk::Want want;
253 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
254 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
255 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
256
257 /**
258 * @tc.steps: step1. add one session and check the map
259 * @tc.expected: step1. can find the newly-added connect session
260 */
261 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
262 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
263 {
264 std::lock_guard<std::mutex> autoLock(distributedLock);
265 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(1));
266 }
267
268 /**
269 * @tc.steps: step2. process connect died and then check the map
270 * @tc.expected: step2. the connect session is removed
271 */
272 DistributedSchedService::GetInstance().ProcessConnectDied(connect);
273 {
274 std::lock_guard<std::mutex> autoLock(distributedLock);
275 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(0));
276 }
277
278 RemoveSession(connect);
279 }
280
281 /**
282 * @tc.name: ProcessConnectDied002
283 * @tc.desc: process connect died which is not exist
284 * @tc.type: FUNC
285 */
286 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied002, TestSize.Level0)
287 {
288 DTEST_LOG << "DistributedSchedServiceTest ProcessConnectDied002 start" << std::endl;
289 OHOS::AAFwk::Want want;
290 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
291 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
292 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
293
294 /**
295 * @tc.steps: step1. add one session
296 * @tc.expected: step1. can find the newly-added connect session
297 */
298 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
299 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
300 {
301 std::lock_guard<std::mutex> autoLock(distributedLock);
302 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(1));
303 }
304
305 /**
306 * @tc.steps: step2. process connect died which is not exist
307 * @tc.expected: step2. still can find the newly-added connect session
308 */
309 DistributedSchedService::GetInstance().ProcessConnectDied(new AbilityConnectCallbackTest());
310 {
311 std::lock_guard<std::mutex> autoLock(distributedLock);
312 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(1));
313 }
314
315 RemoveSession(connect);
316 }
317
318 /**
319 * @tc.name: ProcessConnectDied003
320 * @tc.desc: process connect died and check the trackingUidMap_
321 * @tc.type: FUNC
322 * @tc.require: AR000GI8IE
323 */
324 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied003, TestSize.Level1)
325 {
326 DTEST_LOG << "DistributedSchedServiceTest ProcessConnectDied003 start" << std::endl;
327 OHOS::AAFwk::Want want;
328 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
329 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
330 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
331
332 auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
333 /**
334 * @tc.steps: step1. Increase connect count
335 * @tc.expected: step1. connect count increase one
336 */
337
338 int32_t uid = IPCSkeleton::GetCallingUid();
339 uint32_t oldCount = trackingUidMap[uid];
340 AddConnectCount(uid);
341 EXPECT_EQ(trackingUidMap[uid] - oldCount, static_cast<uint32_t>(1));
342
343 /**
344 * @tc.steps: step2. process connect died and then check the trackingUidMap_
345 * @tc.expected: step2. the connect count is decrease
346 */
347 DistributedSchedService::GetInstance().ProcessConnectDied(connect);
348 auto iter = trackingUidMap.find(uid);
349 if (iter != trackingUidMap.end()) {
350 EXPECT_EQ(trackingUidMap[uid], oldCount);
351 }
352
353 RemoveConnectInfo(connect);
354 }
355
356 /**
357 * @tc.name: ProcessConnectDied004
358 * @tc.desc: process connect died and check the connectAbilityMap_
359 * @tc.type: FUNC
360 * @tc.require: AR000GI8IE
361 */
362 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied004, TestSize.Level1)
363 {
364 DTEST_LOG << "DistributedSchedServiceTest ProcessConnectDied004 start" << std::endl;
365 auto& connectAbilityMap = DistributedSchedService::GetInstance().connectAbilityMap_;
366 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
367
368 /**
369 * @tc.steps: step1. add one connectInfo
370 * @tc.expected: step1. can find the newly-added connectInfo
371 */
372 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
373 AddConnectInfo(connect, "123_local_device_id", "123_remote_device_id");
374 {
375 std::lock_guard<std::mutex> autoLock(distributedLock);
376 EXPECT_EQ(connectAbilityMap.size(), static_cast<size_t>(1));
377 }
378
379 /**
380 * @tc.steps: step2. process connect died and then check the connectAbilityMap_
381 * @tc.expected: step2. the connectInfo is removed
382 */
383 DistributedSchedService::GetInstance().DisconnectAbilityFromRemote(connect,
384 IPCSkeleton::GetCallingUid(), "123_local_device_id");
385 {
386 std::lock_guard<std::mutex> autoLock(distributedLock);
387 EXPECT_EQ(connectAbilityMap.size(), static_cast<size_t>(0));
388 }
389
390 RemoveConnectInfo(connect);
391 }
392
393 /**
394 * @tc.name: ProcessDeviceOffline001
395 * @tc.desc: process device offline with only one connection
396 * @tc.type: FUNC
397 */
398 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline001, TestSize.Level0)
399 {
400 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline001 start" << std::endl;
401 OHOS::AAFwk::Want want;
402 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
403 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
404 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
405
406 /**
407 * @tc.steps: step1. add one session
408 */
409 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
410 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
411 /**
412 * @tc.steps: step2. process device offline and check the map
413 * @tc.expected: step2. the connect session is removed
414 */
415 DistributedSchedService::GetInstance().ProcessDeviceOffline("123_remote_device_id");
416 {
417 std::lock_guard<std::mutex> autoLock(distributedLock);
418 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(0));
419 }
420
421 RemoveSession(connect);
422 }
423
424 /**
425 * @tc.name: ProcessDeviceOffline002
426 * @tc.desc: process device offline with multiple connections
427 * @tc.type: FUNC
428 */
429 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline002, TestSize.Level0)
430 {
431 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline002 start" << std::endl;
432 OHOS::AAFwk::Want want;
433 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
434 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
435 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
436
437 /**
438 * @tc.steps: step1. add one session
439 * @tc.expected: step1. can find the two newly-added connect sessions
440 */
441 sptr<AbilityConnectCallbackTest> connect1 = new AbilityConnectCallbackTest();
442 AddSession(connect1, "123_local_device_id", "123_remote_device_id", want);
443 sptr<AbilityConnectCallbackTest> connect2 = new AbilityConnectCallbackTest();
444 AddSession(connect2, "123_local_device_id", "123_remote_device_id", want);
445 {
446 std::lock_guard<std::mutex> autoLock(distributedLock);
447 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(2));
448 }
449
450 /**
451 * @tc.steps: step2. process device offline
452 * @tc.expected: step2. the connect sessions are removed
453 */
454 DistributedSchedService::GetInstance().ProcessDeviceOffline("123_remote_device_id");
455 {
456 std::lock_guard<std::mutex> autoLock(distributedLock);
457 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(0));
458 }
459
460 RemoveSession(connect1);
461 RemoveSession(connect2);
462 }
463
464 /**
465 * @tc.name: ProcessDeviceOffline003
466 * @tc.desc: process device offline with multiple online devices
467 * @tc.type: FUNC
468 */
469 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline003, TestSize.Level0)
470 {
471 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline003 start" << std::endl;
472 OHOS::AAFwk::Want want;
473 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
474 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
475 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
476
477 /**
478 * @tc.steps: step1. add one session
479 */
480 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
481 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
482 /**
483 * @tc.steps: step2. process other device offline and check the map
484 * @tc.expected: step2. still can find the newly-added connect session
485 */
486 DistributedSchedService::GetInstance().ProcessDeviceOffline("456_remote_device_id");
487 {
488 std::lock_guard<std::mutex> autoLock(distributedLock);
489 auto iter = connectionMap.find(connect);
490 EXPECT_NE(iter, connectionMap.end());
491 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(1));
492 }
493
494 RemoveSession(connect);
495 }
496
497 /**
498 * @tc.name: ProcessDeviceOffline004
499 * @tc.desc: process device offline and check the trackingUidMap_
500 * @tc.type: FUNC
501 * @tc.require: AR000GI8IE
502 */
503 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline004, TestSize.Level1)
504 {
505 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline004 start" << std::endl;
506 OHOS::AAFwk::Want want;
507 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
508 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
509 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
510
511 auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
512 /**
513 * @tc.steps: step1. Increase connect count
514 * @tc.expected: step1. connect count increase one
515 */
516 int32_t uid = IPCSkeleton::GetCallingUid();
517 uint32_t oldCount = trackingUidMap[uid];
518 AddConnectCount(uid);
519 EXPECT_EQ(trackingUidMap[uid] - oldCount, static_cast<uint32_t>(1));
520
521 /**
522 * @tc.steps: step2. process device offline and then check the trackingUidMap_
523 * @tc.expected: step2. the connect count is decrease
524 */
525 DistributedSchedService::GetInstance().ProcessDeviceOffline("123_remote_device_id");
526 auto iter = trackingUidMap.find(uid);
527 if (iter != trackingUidMap.end()) {
528 EXPECT_EQ(trackingUidMap[uid], oldCount);
529 }
530
531 RemoveConnectInfo(connect);
532 }
533
534 /**
535 * @tc.name: ProcessDeviceOffline005
536 * @tc.desc: process device offline and check the connectAbilityMap_
537 * @tc.type: FUNC
538 * @tc.require: AR000GI8IE
539 */
540 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline005, TestSize.Level1)
541 {
542 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline005 start" << std::endl;
543 auto& connectAbilityMap = DistributedSchedService::GetInstance().connectAbilityMap_;
544 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
545
546 /**
547 * @tc.steps: step1. add one connectInfo
548 * @tc.expected: step1. can find the newly-added connectInfo
549 */
550 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
551 AddConnectInfo(connect, "123_local_device_id", "123_remote_device_id");
552 {
553 std::lock_guard<std::mutex> autoLock(distributedLock);
554 EXPECT_EQ(connectAbilityMap.size(), static_cast<size_t>(1));
555 }
556
557 /**
558 * @tc.steps: step2. process device offline and then check the connectAbilityMap_
559 * @tc.expected: step2. the connectInfo is removed
560 */
561 DistributedSchedService::GetInstance().ProcessDeviceOffline("123_local_device_id");
562 {
563 std::lock_guard<std::mutex> autoLock(distributedLock);
564 EXPECT_EQ(connectAbilityMap.size(), static_cast<size_t>(0));
565 }
566
567 RemoveConnectInfo(connect);
568 }
569
570 /**
571 * @tc.name: DisconnectRemoteAbility001
572 * @tc.desc: disconnect remote ability
573 * @tc.type: FUNC
574 */
575 HWTEST_F(DistributedSchedConnectTest, DisconnectRemoteAbility001, TestSize.Level0)
576 {
577 DTEST_LOG << "DistributedSchedServiceTest DisconnectRemoteAbility001 start" << std::endl;
578 OHOS::AAFwk::Want want;
579 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
580 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
581 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
582
583 /**
584 * @tc.steps: step1. add one session
585 */
586 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
587 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
588 /**
589 * @tc.steps: step2. disconnect the ability and check the map
590 * @tc.expected: step2. the connect session is removed
591 */
592 DistributedSchedService::GetInstance().DisconnectRemoteAbility(connect, 0, 0);
593 {
594 std::lock_guard<std::mutex> autoLock(distributedLock);
595 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(0));
596 }
597
598 RemoveSession(connect);
599 }
600
601 /**
602 * @tc.name: DisconnectRemoteAbility002
603 * @tc.desc: disconnect remote ability and check the trackingUidMap_
604 * @tc.type: FUNC
605 * @tc.require: AR000GI8IE
606 */
607 HWTEST_F(DistributedSchedConnectTest, DisconnectRemoteAbility002, TestSize.Level1)
608 {
609 DTEST_LOG << "DistributedSchedServiceTest DisconnectRemoteAbility002 start" << std::endl;
610 OHOS::AAFwk::Want want;
611 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
612 sptr<AbilityConnectCallbackTest> connect = new AbilityConnectCallbackTest();
613 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
614
615 auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
616 /**
617 * @tc.steps: step1. Increase connect count
618 * @tc.expected: step1. connect count increase one
619 */
620 int32_t uid = IPCSkeleton::GetCallingUid();
621 uint32_t oldCount = trackingUidMap[uid];
622 AddConnectCount(uid);
623 uint32_t newCount = trackingUidMap[uid];
624 EXPECT_EQ(newCount - oldCount, static_cast<uint32_t>(1));
625
626 /**
627 * @tc.steps: step2. disconnect remote ability and then check the trackingUidMap_
628 * @tc.expected: step2. the connect count is decrease
629 */
630 DistributedSchedService::GetInstance().DisconnectRemoteAbility(connect, 0, 0);
631 auto iter = trackingUidMap.find(uid);
632 if (iter != trackingUidMap.end()) {
633 EXPECT_EQ(trackingUidMap[uid], oldCount);
634 }
635
636 RemoveConnectInfo(connect);
637 }
638 }
639 }