1 /*
2 * Copyright (c) 2021-2023 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 #include "ability_connect_callback_interface.h"
18 #include "ability_connect_callback_stub.h"
19 #include "device_manager.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24
25 #include "ability_connection_wrapper_stub.h"
26 #include "distributed_sched_test_util.h"
27 #include "dtbschedmgr_device_info_storage.h"
28 #include "dtbschedmgr_log.h"
29 #include "multi_user_manager.h"
30 #include "parcel_helper.h"
31 #include "test_log.h"
32 #include "mock/multi_user_manager_mock.h"
33
34 #define private public
35 #define protected public
36 #include "distributed_sched_service.h"
37 #undef private
38 #undef protected
39
40 namespace OHOS {
41 namespace DistributedSchedule {
42 using namespace AAFwk;
43 using namespace testing;
44 using namespace testing::ext;
45 using namespace OHOS::DistributedHardware;
46
47 namespace {
48 const std::string TAG = "DistributedSchedConnectTest";
49 const std::u16string CONNECTION_CALLBACK_INTERFACE_TOKEN = u"ohos.abilityshell.DistributedConnection";
50 constexpr int32_t STDOUT_FD = 1;
51 constexpr int32_t REQUEST_CODE_ERR = 305;
52 constexpr int32_t ERROR_CONNECT_CODE = 1000;
53 }
54
55 class AbilityConnectCallbackTest : public AAFwk::AbilityConnectionStub {
56 public:
57 AbilityConnectCallbackTest() = default;
58 ~AbilityConnectCallbackTest() = default;
59
60 void OnAbilityConnectDone(const AppExecFwk::ElementName& element,
61 const sptr<IRemoteObject>& remoteObject, int32_t resultCode) override;
62 void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
63 int32_t resultCode) override;
64 };
65
66 class AbilityConnectionWrapperStubTest : public AAFwk::AbilityConnectionStub {
67 public:
AbilityConnectionWrapperStubTest(sptr<IRemoteObject> connection)68 explicit AbilityConnectionWrapperStubTest(sptr<IRemoteObject> connection) : distributedConnection_(connection) {}
69 ~AbilityConnectionWrapperStubTest() = default;
70
71 void OnAbilityConnectDone(const AppExecFwk::ElementName& element,
72 const sptr<IRemoteObject>& remoteObject, int32_t resultCode) override;
73 void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
74 int32_t resultCode) override;
75
76 private:
77 sptr<IRemoteObject> distributedConnection_;
78 };
79
80 class DistributedSchedConnectTest : public testing::Test {
81 public:
82 static void SetUpTestCase();
83 static void TearDownTestCase();
84 void SetUp();
85 void TearDown();
86
87 void AddSession(const sptr<IRemoteObject>& connect, const std::string& localDeviceId,
88 const std::string& remoteDeviceId, const AAFwk::Want& want) const;
89 void RemoveSession(const sptr<IRemoteObject>& connect) const;
90
91 void AddConnectInfo(const sptr<IRemoteObject>& connect, const std::string& localDeviceId,
92 const std::string& remoteDeviceId) const;
93 void RemoveConnectInfo(const sptr<IRemoteObject>& connect) const;
94
95 void AddConnectCount(int32_t uid) const;
96 void DecreaseConnectCount(int32_t uid) const;
97 sptr<IDistributedSched> GetDms();
98 static inline std::shared_ptr<MultiUserManagerMock> multiUserMgrMock_ = nullptr;
99 class DeviceInitCallBack : public DmInitCallback {
100 void OnRemoteDied() override;
101 };
102 };
103
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)104 void AbilityConnectCallbackTest::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
105 const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
106 {
107 }
108
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)109 void AbilityConnectCallbackTest::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
110 int32_t resultCode)
111 {
112 }
113
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)114 void AbilityConnectionWrapperStubTest::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
115 const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
116 {
117 }
118
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)119 void AbilityConnectionWrapperStubTest::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
120 int32_t resultCode)
121 {
122 }
123
SetUpTestCase()124 void DistributedSchedConnectTest::SetUpTestCase()
125 {
126 if (!DistributedSchedUtil::LoadDistributedSchedService()) {
127 DTEST_LOG << "DistributedSchedConnectTest::SetUpTestCase LoadDistributedSchedService failed" << std::endl;
128 }
129 const std::string pkgName = "DBinderBus_" + std::to_string(getprocpid());
130 std::shared_ptr<DmInitCallback> initCallback_ = std::make_shared<DeviceInitCallBack>();
131 DeviceManager::GetInstance().InitDeviceManager(pkgName, initCallback_);
132 multiUserMgrMock_ = std::make_shared<MultiUserManagerMock>();
133 MultiUserManagerMock::multiUserMgrMock = multiUserMgrMock_;
134 }
135
TearDownTestCase()136 void DistributedSchedConnectTest::TearDownTestCase()
137 {
138 MultiUserManagerMock::multiUserMgrMock = nullptr;
139 multiUserMgrMock_ = nullptr;
140 }
141
SetUp()142 void DistributedSchedConnectTest::SetUp()
143 {
144 DistributedSchedUtil::MockPermission();
145 }
146
TearDown()147 void DistributedSchedConnectTest::TearDown()
148 {
149 }
150
OnRemoteDied()151 void DistributedSchedConnectTest::DeviceInitCallBack::OnRemoteDied()
152 {
153 }
154
AddSession(const sptr<IRemoteObject> & connect,const std::string & localDeviceId,const std::string & remoteDeviceId,const AAFwk::Want & want) const155 void DistributedSchedConnectTest::AddSession(const sptr<IRemoteObject>& connect,
156 const std::string& localDeviceId, const std::string& remoteDeviceId, const AAFwk::Want& want) const
157 {
158 if (connect == nullptr) {
159 return;
160 }
161
162 std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
163 CallerInfo callerInfo;
164 callerInfo.uid = IPCSkeleton::GetCallingUid();
165 callerInfo.pid = IPCSkeleton::GetCallingRealPid();
166 callerInfo.sourceDeviceId = localDeviceId;
167 callerInfo.callerType = CALLER_TYPE_HARMONY;
168 DistributedSchedService::GetInstance().RemoteConnectAbilityMappingLocked(connect, localDeviceId,
169 remoteDeviceId, want.GetElement(), callerInfo, TargetComponent::HARMONY_COMPONENT);
170 }
171
RemoveSession(const sptr<IRemoteObject> & connect) const172 void DistributedSchedConnectTest::RemoveSession(const sptr<IRemoteObject>& connect) const
173 {
174 if (connect == nullptr) {
175 return;
176 }
177
178 std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
179 DistributedSchedService::GetInstance().distributedConnectAbilityMap_.erase(connect);
180 }
181
AddConnectInfo(const sptr<IRemoteObject> & connect,const std::string & localDeviceId,const std::string & remoteDeviceId) const182 void DistributedSchedConnectTest::AddConnectInfo(const sptr<IRemoteObject>& connect,
183 const std::string& localDeviceId, const std::string& remoteDeviceId) const
184 {
185 if (connect == nullptr) {
186 return;
187 }
188
189 std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
190 CallerInfo callerInfo;
191 callerInfo.uid = IPCSkeleton::GetCallingUid();
192 callerInfo.pid = IPCSkeleton::GetCallingRealPid();
193 callerInfo.sourceDeviceId = localDeviceId;
194 callerInfo.callerType = CALLER_TYPE_HARMONY;
195
196 sptr<IRemoteObject> callbackWrapper(new AbilityConnectionWrapperStubTest(connect));
197 ConnectInfo connectInfo {callerInfo, callbackWrapper};
198 DistributedSchedService::GetInstance().connectAbilityMap_.emplace(connect, connectInfo);
199 }
200
RemoveConnectInfo(const sptr<IRemoteObject> & connect) const201 void DistributedSchedConnectTest::RemoveConnectInfo(const sptr<IRemoteObject>& connect) const
202 {
203 if (connect == nullptr) {
204 return;
205 }
206
207 std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
208 DistributedSchedService::GetInstance().connectAbilityMap_.erase(connect);
209 }
210
AddConnectCount(int32_t uid) const211 void DistributedSchedConnectTest::AddConnectCount(int32_t uid) const
212 {
213 if (uid < 0) {
214 return;
215 }
216
217 auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
218 ++trackingUidMap[uid];
219 }
220
DecreaseConnectCount(int32_t uid) const221 void DistributedSchedConnectTest::DecreaseConnectCount(int32_t uid) const
222 {
223 if (uid < 0) {
224 return;
225 }
226
227 DistributedSchedService::GetInstance().DecreaseConnectLocked(uid);
228 }
229
GetDms()230 sptr<IDistributedSched> DistributedSchedConnectTest::GetDms()
231 {
232 auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
233 if (sm == nullptr) {
234 DTEST_LOG << "DistributedSchedConnectTest sm is nullptr" << std::endl;
235 return nullptr;
236 }
237 auto distributedObject = sm->GetSystemAbility(DISTRIBUTED_SCHED_SA_ID);
238 if (distributedObject == nullptr) {
239 DTEST_LOG << "distributedObject sm is nullptr" << std::endl;
240 return nullptr;
241 }
242 return iface_cast<IDistributedSched>(distributedObject);
243 }
244
245 /**
246 * @tc.name: DumpConnectInfo_001
247 * @tc.desc: dump connect ability info by call Dump
248 * @tc.type: FUNC
249 */
250 HWTEST_F(DistributedSchedConnectTest, DumpConnectInfo_001, TestSize.Level1)
251 {
252 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_001 start " << std::endl;
253 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
254 if (samgr == nullptr) {
255 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_001 samgr null" << std::endl;
256 } else {
257 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_001 available" << std::endl;
258 }
259
260 auto dms = samgr->GetSystemAbility(DISTRIBUTED_SCHED_SA_ID);
261 ASSERT_NE(nullptr, dms);
262 std::vector<std::u16string> args;
263 args.push_back(u"-connect");
264 int32_t result = dms->Dump(STDOUT_FD, args);
265 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_001 dump result: " << result << std::endl;
266 }
267
268 /**
269 * @tc.name: DumpConnectInfo_002
270 * @tc.desc: dump connect ability info by call DumpConnectInfo
271 * @tc.type: FUNC
272 */
273 HWTEST_F(DistributedSchedConnectTest, DumpConnectInfo_002, TestSize.Level0)
274 {
275 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_002 start" << std::endl;
276 OHOS::AAFwk::Want want;
277 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
278
279 /**
280 * @tc.steps: step1. add one session
281 */
282 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
283 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
284 /**
285 * @tc.steps: step2. and then dump connection info
286 * @tc.expected: step2. can find the newly-added connect session
287 */
288 std::string dumpInfo;
289 DistributedSchedService::GetInstance().DumpConnectInfo(dumpInfo);
290 DTEST_LOG << "DistributedSchedServiceTest DumpConnectInfo_002 dumpInfo " << dumpInfo << std::endl;
291 std::string::size_type pos = dumpInfo.find("123_remote_device_id");
292 EXPECT_NE(pos, std::string::npos);
293
294 RemoveSession(connect);
295 }
296
297 /**
298 * @tc.name: ProcessConnectDied001
299 * @tc.desc: process connect died
300 * @tc.type: FUNC
301 */
302 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied001, TestSize.Level1)
303 {
304 DTEST_LOG << "DistributedSchedServiceTest ProcessConnectDied001 start" << std::endl;
305 OHOS::AAFwk::Want want;
306 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
307 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
308 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
309
310 /**
311 * @tc.steps: step1. add one session and check the map
312 * @tc.expected: step1. can find the newly-added connect session
313 */
314 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
315 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
316 {
317 std::lock_guard<std::mutex> autoLock(distributedLock);
318 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(1));
319 }
320
321 /**
322 * @tc.steps: step2. process connect died and then check the map
323 * @tc.expected: step2. the connect session is removed
324 */
325 DistributedSchedService::GetInstance().ProcessConnectDied(connect);
326 {
327 std::lock_guard<std::mutex> autoLock(distributedLock);
328 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(0));
329 }
330
331 RemoveSession(connect);
332 }
333
334 /**
335 * @tc.name: ProcessConnectDied002
336 * @tc.desc: process connect died which is not exist
337 * @tc.type: FUNC
338 */
339 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied002, TestSize.Level0)
340 {
341 DTEST_LOG << "DistributedSchedServiceTest ProcessConnectDied002 start" << std::endl;
342 OHOS::AAFwk::Want want;
343 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
344 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
345 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
346
347 /**
348 * @tc.steps: step1. add one session
349 * @tc.expected: step1. can find the newly-added connect session
350 */
351 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
352 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
353 {
354 std::lock_guard<std::mutex> autoLock(distributedLock);
355 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(1));
356 }
357
358 /**
359 * @tc.steps: step2. process connect died which is not exist
360 * @tc.expected: step2. still can find the newly-added connect session
361 */
362 DistributedSchedService::GetInstance().ProcessConnectDied(new AbilityConnectCallbackTest());
363 {
364 std::lock_guard<std::mutex> autoLock(distributedLock);
365 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(1));
366 }
367
368 RemoveSession(connect);
369 }
370
371 /**
372 * @tc.name: ProcessConnectDied003
373 * @tc.desc: process connect died and check the trackingUidMap_
374 * @tc.type: FUNC
375 */
376 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied003, TestSize.Level1)
377 {
378 DTEST_LOG << "DistributedSchedServiceTest ProcessConnectDied003 start" << std::endl;
379 OHOS::AAFwk::Want want;
380 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
381 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
382 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
383
384 auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
385 /**
386 * @tc.steps: step1. Increase connect count
387 * @tc.expected: step1. connect count increase one
388 */
389
390 int32_t uid = IPCSkeleton::GetCallingUid();
391 uint32_t oldCount = trackingUidMap[uid];
392 AddConnectCount(uid);
393 EXPECT_EQ(trackingUidMap[uid] - oldCount, static_cast<uint32_t>(1));
394
395 /**
396 * @tc.steps: step2. process connect died and then check the trackingUidMap_
397 * @tc.expected: step2. the connect count is decrease
398 */
399 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessConnectDied(connect));
400
401
402 RemoveConnectInfo(connect);
403 }
404
405 /**
406 * @tc.name: ProcessConnectDied004
407 * @tc.desc: process connect died and check the connectAbilityMap_
408 * @tc.type: FUNC
409 */
410 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied004, TestSize.Level1)
411 {
412 DTEST_LOG << "DistributedSchedServiceTest ProcessConnectDied004 start" << std::endl;
413 auto& connectAbilityMap = DistributedSchedService::GetInstance().connectAbilityMap_;
414 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
415
416 /**
417 * @tc.steps: step1. add one connectInfo
418 * @tc.expected: step1. can find the newly-added connectInfo
419 */
420 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
421 AddConnectInfo(connect, "123_local_device_id", "123_remote_device_id");
422 {
423 std::lock_guard<std::mutex> autoLock(distributedLock);
424 EXPECT_EQ(connectAbilityMap.size(), static_cast<size_t>(1));
425 }
426
427 /**
428 * @tc.steps: step2. process connect died and then check the connectAbilityMap_
429 * @tc.expected: step2. the connectInfo is removed
430 */
431 DistributedSchedService::GetInstance().DisconnectAbilityFromRemote(connect,
432 IPCSkeleton::GetCallingUid(), "123_local_device_id");
433 {
434 std::lock_guard<std::mutex> autoLock(distributedLock);
435 EXPECT_EQ(connectAbilityMap.size(), static_cast<size_t>(0));
436 }
437
438 RemoveConnectInfo(connect);
439 }
440
441 /**
442 * @tc.name: ProcessDeviceOffline001
443 * @tc.desc: process device offline with only one connection
444 * @tc.type: FUNC
445 */
446 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline001, TestSize.Level0)
447 {
448 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline001 start" << std::endl;
449 OHOS::AAFwk::Want want;
450 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
451 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
452 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
453
454 /**
455 * @tc.steps: step1. add one session
456 */
457 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
458 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
459 /**
460 * @tc.steps: step2. process device offline and check the map
461 * @tc.expected: step2. the connect session is removed
462 */
463 DistributedSchedService::GetInstance().ProcessDeviceOffline("123_remote_device_id");
464 {
465 std::lock_guard<std::mutex> autoLock(distributedLock);
466 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(0));
467 }
468
469 RemoveSession(connect);
470 }
471
472 /**
473 * @tc.name: ProcessDeviceOffline002
474 * @tc.desc: process device offline with multiple connections
475 * @tc.type: FUNC
476 */
477 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline002, TestSize.Level0)
478 {
479 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline002 start" << std::endl;
480 OHOS::AAFwk::Want want;
481 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
482 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
483 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
484
485 /**
486 * @tc.steps: step1. add one session
487 * @tc.expected: step1. can find the two newly-added connect sessions
488 */
489 sptr<AbilityConnectCallbackTest> connect1(new AbilityConnectCallbackTest());
490 AddSession(connect1, "123_local_device_id", "123_remote_device_id", want);
491 sptr<AbilityConnectCallbackTest> connect2(new AbilityConnectCallbackTest());
492 AddSession(connect2, "123_local_device_id", "123_remote_device_id", want);
493 {
494 std::lock_guard<std::mutex> autoLock(distributedLock);
495 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(2));
496 }
497
498 /**
499 * @tc.steps: step2. process device offline
500 * @tc.expected: step2. the connect sessions are removed
501 */
502 DistributedSchedService::GetInstance().ProcessDeviceOffline("123_remote_device_id");
503 {
504 std::lock_guard<std::mutex> autoLock(distributedLock);
505 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(0));
506 }
507
508 RemoveSession(connect1);
509 RemoveSession(connect2);
510 }
511
512 /**
513 * @tc.name: ProcessDeviceOffline003
514 * @tc.desc: process device offline with multiple online devices
515 * @tc.type: FUNC
516 */
517 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline003, TestSize.Level0)
518 {
519 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline003 start" << std::endl;
520 OHOS::AAFwk::Want want;
521 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
522 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
523 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
524
525 /**
526 * @tc.steps: step1. add one session
527 */
528 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
529 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
530 /**
531 * @tc.steps: step2. process other device offline and check the map
532 * @tc.expected: step2. still can find the newly-added connect session
533 */
534 DistributedSchedService::GetInstance().ProcessDeviceOffline("456_remote_device_id");
535 {
536 std::lock_guard<std::mutex> autoLock(distributedLock);
537 auto iter = connectionMap.find(connect);
538 EXPECT_NE(iter, connectionMap.end());
539 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(1));
540 }
541
542 RemoveSession(connect);
543 }
544
545 /**
546 * @tc.name: ProcessDeviceOffline004
547 * @tc.desc: process device offline and check the trackingUidMap_
548 * @tc.type: FUNC
549 */
550 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline004, TestSize.Level1)
551 {
552 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline004 start" << std::endl;
553 OHOS::AAFwk::Want want;
554 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
555 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
556 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
557
558 auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
559 /**
560 * @tc.steps: step1. Increase connect count
561 * @tc.expected: step1. connect count increase one
562 */
563 int32_t uid = IPCSkeleton::GetCallingUid();
564 uint32_t oldCount = trackingUidMap[uid];
565 AddConnectCount(uid);
566 EXPECT_EQ(trackingUidMap[uid] - oldCount, static_cast<uint32_t>(1));
567
568 /**
569 * @tc.steps: step2. process device offline and then check the trackingUidMap_
570 * @tc.expected: step2. the connect count is decrease
571 */
572 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessDeviceOffline("123_remote_device_id"));
573
574 RemoveConnectInfo(connect);
575 }
576
577 /**
578 * @tc.name: ProcessDeviceOffline005
579 * @tc.desc: process device offline and check the connectAbilityMap_
580 * @tc.type: FUNC
581 */
582 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline005, TestSize.Level1)
583 {
584 DTEST_LOG << "DistributedSchedServiceTest ProcessDeviceOffline005 start" << std::endl;
585 auto& connectAbilityMap = DistributedSchedService::GetInstance().connectAbilityMap_;
586 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
587
588 /**
589 * @tc.steps: step1. add one connectInfo
590 * @tc.expected: step1. can find the newly-added connectInfo
591 */
592 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
593 AddConnectInfo(connect, "123_local_device_id", "123_remote_device_id");
594 {
595 std::lock_guard<std::mutex> autoLock(distributedLock);
596 EXPECT_EQ(connectAbilityMap.size(), static_cast<size_t>(1));
597 }
598
599 /**
600 * @tc.steps: step2. process device offline and then check the connectAbilityMap_
601 * @tc.expected: step2. the connectInfo is removed
602 */
603 DistributedSchedService::GetInstance().ProcessDeviceOffline("123_local_device_id");
604 {
605 std::lock_guard<std::mutex> autoLock(distributedLock);
606 EXPECT_EQ(connectAbilityMap.size(), static_cast<size_t>(0));
607 }
608
609 RemoveConnectInfo(connect);
610 }
611
612 /**
613 * @tc.name: DisconnectRemoteAbility001
614 * @tc.desc: disconnect remote ability
615 * @tc.type: FUNC
616 */
617 HWTEST_F(DistributedSchedConnectTest, DisconnectRemoteAbility001, TestSize.Level0)
618 {
619 DTEST_LOG << "DistributedSchedServiceTest DisconnectRemoteAbility001 start" << std::endl;
620 OHOS::AAFwk::Want want;
621 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
622 auto& connectionMap = DistributedSchedService::GetInstance().distributedConnectAbilityMap_;
623 auto& distributedLock = DistributedSchedService::GetInstance().distributedLock_;
624
625 /**
626 * @tc.steps: step1. add one session
627 */
628 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
629 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
630 /**
631 * @tc.steps: step2. disconnect the ability and check the map
632 * @tc.expected: step2. the connect session is removed
633 */
634 DistributedSchedService::GetInstance().DisconnectRemoteAbility(connect, 0, 0);
635 {
636 std::lock_guard<std::mutex> autoLock(distributedLock);
637 EXPECT_EQ(connectionMap.size(), static_cast<size_t>(0));
638 }
639
640 RemoveSession(connect);
641 }
642
643 /**
644 * @tc.name: DisconnectRemoteAbility002
645 * @tc.desc: disconnect remote ability and check the trackingUidMap_
646 * @tc.type: FUNC
647 */
648 HWTEST_F(DistributedSchedConnectTest, DisconnectRemoteAbility002, TestSize.Level1)
649 {
650 DTEST_LOG << "DistributedSchedServiceTest DisconnectRemoteAbility002 start" << std::endl;
651 OHOS::AAFwk::Want want;
652 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
653 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
654 AddSession(connect, "123_local_device_id", "123_remote_device_id", want);
655
656 auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
657 /**
658 * @tc.steps: step1. Increase connect count
659 * @tc.expected: step1. connect count increase one
660 */
661 int32_t uid = IPCSkeleton::GetCallingUid();
662 uint32_t oldCount = trackingUidMap[uid];
663 AddConnectCount(uid);
664 uint32_t newCount = trackingUidMap[uid];
665 EXPECT_EQ(newCount - oldCount, static_cast<uint32_t>(1));
666
667 /**
668 * @tc.steps: step2. disconnect remote ability and then check the trackingUidMap_
669 * @tc.expected: step2. the connect count is decrease
670 */
671 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().DisconnectRemoteAbility(connect, 0, 0));
672
673 RemoveConnectInfo(connect);
674 }
675
676 /**
677 * @tc.name: DisconnectRemoteAbility003
678 * @tc.desc: disconnect remote ability whith error callback
679 * @tc.type: FUNC
680 * @tc.require: I5OOKG
681 */
682 HWTEST_F(DistributedSchedConnectTest, DisconnectRemoteAbility003, TestSize.Level4)
683 {
684 DTEST_LOG << "DistributedSchedServiceTest DisconnectRemoteAbility003 start" << std::endl;
685 int32_t ret = DistributedSchedService::GetInstance().DisconnectRemoteAbility(nullptr, 0, 0);
686 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
687 DTEST_LOG << "DistributedSchedServiceTest DisconnectRemoteAbility003 end" << std::endl;
688 }
689
690 /**
691 * @tc.name: ConnectRemoteAbility
692 * @tc.desc: connect remote ability whith error uid and pid
693 * @tc.type: FUNC
694 * @tc.require: I5OOKG
695 */
696 HWTEST_F(DistributedSchedConnectTest, ConnectRemoteAbility001, TestSize.Level4)
697 {
698 DTEST_LOG << "DistributedSchedServiceTest ConnectRemoteAbility001 start" << std::endl;
699 OHOS::AAFwk::Want want;
700 want.SetElementName("123_remote_device_id", "ohos.demo.bundleName", "abilityName");
701 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
702 EXPECT_CALL(*multiUserMgrMock_, IsCallerForeground(_)).WillRepeatedly(Return(true));
703 int32_t ret = DistributedSchedService::GetInstance().ConnectRemoteAbility(want, connect, -1, -1, -1);
704 EXPECT_EQ(ret, BIND_ABILITY_UID_INVALID_ERR);
705 DTEST_LOG << "DistributedSchedServiceTest ConnectRemoteAbility001 end" << std::endl;
706 }
707
708 /**
709 * @tc.name: ConnectRemoteAbility
710 * @tc.desc: connect remote ability whith empty deviceId.
711 * @tc.type: FUNC
712 * @tc.require: I5OOKG
713 */
714 HWTEST_F(DistributedSchedConnectTest, ConnectRemoteAbility002, TestSize.Level4)
715 {
716 DTEST_LOG << "DistributedSchedServiceTest ConnectRemoteAbility002 start" << std::endl;
717 OHOS::AAFwk::Want want;
718 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
719 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
720 int32_t ret = DistributedSchedService::GetInstance().ConnectRemoteAbility(want, connect, -1, -1, -1);
721 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
722 DTEST_LOG << "DistributedSchedServiceTest ConnectRemoteAbility002 end" << std::endl;
723 }
724
725 /**
726 * @tc.name: ConnectRemoteAbility
727 * @tc.desc: user is not foreground
728 * @tc.type: FUNC
729 * @tc.require: I5OOKG
730 */
731 HWTEST_F(DistributedSchedConnectTest, ConnectRemoteAbility006, TestSize.Level4)
732 {
733 DTEST_LOG << "DistributedSchedServiceTest ConnectRemoteAbility006 start" << std::endl;
734 OHOS::AAFwk::Want want;
735 want.SetElementName("123_remote_device_id", "ohos.demo.bundleName", "abilityName");
736 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
737 EXPECT_CALL(*multiUserMgrMock_, IsCallerForeground(_)).WillOnce(Return(false));
738 int32_t ret = DistributedSchedService::GetInstance().ConnectRemoteAbility(want, connect, -1, -1, -1);
739 EXPECT_EQ(ret, DMS_NOT_FOREGROUND_USER);
740 DTEST_LOG << "DistributedSchedServiceTest ConnectRemoteAbility006 end" << std::endl;
741 }
742
743 /**
744 * @tc.name: ConnectAbilityFromRemote
745 * @tc.desc: connect remote ability whith fake deviceId.
746 * @tc.type: FUNC
747 * @tc.require: I5OOKG
748 */
749 HWTEST_F(DistributedSchedConnectTest, ConnectAbilityFromRemote001, TestSize.Level3)
750 {
751 DTEST_LOG << "DistributedSchedConnectTest ConnectAbilityFromRemote001 start" << std::endl;
752 OHOS::AAFwk::Want want;
753 want.SetElementName("123_remote_device_id", "ohos.demo.bundleName", "abilityName");
754 AppExecFwk::AbilityInfo abilityInfo;
755 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
756 CallerInfo callerInfo;
757 IDistributedSched::AccountInfo accountInfo;
758 int32_t ret = DistributedSchedService::GetInstance().ConnectAbilityFromRemote(want,
759 abilityInfo, connect, callerInfo, accountInfo);
760 EXPECT_EQ(ret, INVALID_REMOTE_PARAMETERS_ERR);
761 DTEST_LOG << "DistributedSchedConnectTest ConnectAbilityFromRemote001 end" << std::endl;
762 }
763
764 /**
765 * @tc.name: ConnectAbilityFromRemote
766 * @tc.desc: connect remote ability whith error callback.
767 * @tc.type: FUNC
768 * @tc.require: I5OOKG
769 */
770 HWTEST_F(DistributedSchedConnectTest, ConnectAbilityFromRemote002, TestSize.Level4)
771 {
772 DTEST_LOG << "DistributedSchedConnectTest ConnectAbilityFromRemote002 start" << std::endl;
773 OHOS::AAFwk::Want want;
774 want.SetElementName("123_remote_device_id", "ohos.demo.bundleName", "abilityName");
775 AppExecFwk::AbilityInfo abilityInfo;
776 CallerInfo callerInfo;
777 IDistributedSched::AccountInfo accountInfo;
778 int32_t ret = DistributedSchedService::GetInstance().ConnectAbilityFromRemote(want,
779 abilityInfo, nullptr, callerInfo, accountInfo);
780 EXPECT_EQ(ret, INVALID_REMOTE_PARAMETERS_ERR);
781 DTEST_LOG << "DistributedSchedConnectTest ConnectAbilityFromRemote002 end" << std::endl;
782 }
783
784 /**
785 * @tc.name: ConnectAbilityFromRemote
786 * @tc.desc: connect remote ability whith error param
787 * @tc.type: FUNC
788 * @tc.require: I5OOKG
789 */
790 HWTEST_F(DistributedSchedConnectTest, ConnectAbilityFromRemote003, TestSize.Level4)
791 {
792 DTEST_LOG << "DistributedSchedConnectTest ConnectAbilityFromRemote003 start" << std::endl;
793 OHOS::AAFwk::Want want;
794 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
795 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
796 AppExecFwk::AbilityInfo abilityInfo;
797 CallerInfo callerInfo;
798 IDistributedSched::AccountInfo accountInfo;
799 int32_t ret = DistributedSchedService::GetInstance().ConnectAbilityFromRemote(want,
800 abilityInfo, connect, callerInfo, accountInfo);
801 EXPECT_EQ(ret, INVALID_REMOTE_PARAMETERS_ERR);
802 DTEST_LOG << "DistributedSchedConnectTest ConnectAbilityFromRemote003 end" << std::endl;
803 }
804
805 /**
806 * @tc.name: ConnectAbilityFromRemote
807 * @tc.desc: connect remote ability whith null callback
808 * @tc.type: FUNC
809 * @tc.require: I5OOKG
810 */
811 HWTEST_F(DistributedSchedConnectTest, ConnectAbilityFromRemote004, TestSize.Level4)
812 {
813 DTEST_LOG << "DistributedSchedConnectTest ConnectAbilityFromRemote004 start" << std::endl;
814 OHOS::AAFwk::Want want;
815 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
816 AppExecFwk::AbilityInfo abilityInfo;
817 CallerInfo callerInfo;
818 IDistributedSched::AccountInfo accountInfo;
819 int32_t ret = DistributedSchedService::GetInstance().ConnectAbilityFromRemote(want,
820 abilityInfo, nullptr, callerInfo, accountInfo);
821 EXPECT_EQ(ret, INVALID_REMOTE_PARAMETERS_ERR);
822 DTEST_LOG << "DistributedSchedConnectTest ConnectAbilityFromRemote004 end" << std::endl;
823 }
824
825 /**
826 * @tc.name: DisconnectEachRemoteAbilityLocked
827 * @tc.desc: disconnect remote ability.
828 * @tc.type: FUNC
829 * @tc.require: I5OOKG
830 */
831 HWTEST_F(DistributedSchedConnectTest, DisconnectEachRemoteAbilityLocked001, TestSize.Level4)
832 {
833 DTEST_LOG << "DistributedSchedConnectTest DisconnectEachRemoteAbilityLocked001 start" << std::endl;
834 int32_t ret = DistributedSchedService::GetInstance().DisconnectEachRemoteAbilityLocked("", "", nullptr);
835 EXPECT_NE(ret, ERR_OK);
836 DTEST_LOG << "DistributedSchedConnectTest DisconnectEachRemoteAbilityLocked001 end" << std::endl;
837 }
838
839 /**
840 * @tc.name: DisconnectEachRemoteAbilityLocked
841 * @tc.desc: disconnect remote ability.
842 * @tc.type: FUNC
843 * @tc.require: I5OOKG
844 */
845 HWTEST_F(DistributedSchedConnectTest, DisconnectEachRemoteAbilityLocked002, TestSize.Level4)
846 {
847 DTEST_LOG << "DistributedSchedConnectTest DisconnectEachRemoteAbilityLocked002 start" << std::endl;
848 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
849 int32_t ret = DistributedSchedService::GetInstance().DisconnectEachRemoteAbilityLocked("", "", connect);
850 EXPECT_NE(ret, ERR_OK);
851 DTEST_LOG << "DistributedSchedConnectTest DisconnectEachRemoteAbilityLocked002 end" << std::endl;
852 }
853
854 /**
855 * @tc.name: DisconnectEachRemoteAbilityLocked
856 * @tc.desc: disconnect remote ability.
857 * @tc.type: FUNC
858 * @tc.require: I5OOKG
859 */
860 HWTEST_F(DistributedSchedConnectTest, DisconnectEachRemoteAbilityLocked003, TestSize.Level3)
861 {
862 DTEST_LOG << "DistributedSchedConnectTest DisconnectEachRemoteAbilityLocked003 start" << std::endl;
863 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
864 int32_t ret = DistributedSchedService::GetInstance().DisconnectEachRemoteAbilityLocked(
865 "123_remote_device_id", "123_remote_device_id", connect);
866 EXPECT_NE(ret, ERR_OK);
867 DTEST_LOG << "DistributedSchedConnectTest DisconnectEachRemoteAbilityLocked003 end" << std::endl;
868 }
869
870 /**
871 * @tc.name: DisconnectEachRemoteAbilityLocked
872 * @tc.desc: disconnect remote ability.
873 * @tc.type: FUNC
874 * @tc.require: I5OOKG
875 */
876 HWTEST_F(DistributedSchedConnectTest, DisconnectEachRemoteAbilityLocked004, TestSize.Level3)
877 {
878 DTEST_LOG << "DistributedSchedConnectTest DisconnectEachRemoteAbilityLocked004 start" << std::endl;
879 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
880 std::string deviceId;
881 DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(deviceId);
882 int32_t ret = DistributedSchedService::GetInstance().DisconnectEachRemoteAbilityLocked(
883 deviceId, deviceId, connect);
884 EXPECT_NE(ret, ERR_OK);
885 DTEST_LOG << "DistributedSchedConnectTest DisconnectEachRemoteAbilityLocked004 end" << std::endl;
886 }
887
888 /**
889 * @tc.name: DisconnectRemoteAbility
890 * @tc.desc: disconnect remote ability.
891 * @tc.type: FUNC
892 * @tc.require: I5OOKG
893 */
894 HWTEST_F(DistributedSchedConnectTest, DisconnectRemoteAbility004, TestSize.Level4)
895 {
896 DTEST_LOG << "DistributedSchedConnectTest DisconnectRemoteAbility004 start" << std::endl;
897 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
898 int32_t ret = DistributedSchedService::GetInstance().DisconnectRemoteAbility(connect, 0, 0);
899 EXPECT_NE(ret, ERR_OK);
900 DTEST_LOG << "DistributedSchedConnectTest DisconnectRemoteAbility004 end" << std::endl;
901 }
902
903 /**
904 * @tc.name: DisconnectRemoteAbility
905 * @tc.desc: disconnect remote ability.
906 * @tc.type: FUNC
907 * @tc.require: I5OOKG
908 */
909 HWTEST_F(DistributedSchedConnectTest, DisconnectRemoteAbility005, TestSize.Level4)
910 {
911 DTEST_LOG << "DistributedSchedConnectTest DisconnectRemoteAbility005 start" << std::endl;
912 /**
913 * @tc.steps: step1. call RemoveCallerComponent
914 */
915 DTEST_LOG << "DistributedSchedServiceTest RemoveCallerComponent001 start" << std::endl;
916 OHOS::AAFwk::Want want;
917 std::string localDeviceId = "123_local_device_id";
918 std::string remoteDeviceId = "123_remote_device_id";
919 want.SetElementName(remoteDeviceId, "ohos.demo.bundleName", "abilityName");
920 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
921 CallerInfo callerInfo;
922 callerInfo.uid = IPCSkeleton::GetCallingUid();
923 callerInfo.pid = IPCSkeleton::GetCallingRealPid();
924 callerInfo.sourceDeviceId = localDeviceId;
925 callerInfo.callerType = CALLER_TYPE_HARMONY;
926 DistributedSchedService::GetInstance().SaveCallerComponent(want, connect, callerInfo);
927 DistributedSchedService::GetInstance().RemoveCallerComponent(connect);
928 DTEST_LOG << "DistributedSchedServiceTest RemoveCallerComponent001 end" << std::endl;
929
930 int32_t ret = DistributedSchedService::GetInstance().DisconnectRemoteAbility(nullptr, 0, 0);
931 EXPECT_NE(ret, ERR_OK);
932 DTEST_LOG << "DistributedSchedConnectTest DisconnectRemoteAbility005 end" << std::endl;
933 }
934
935 /**
936 * @tc.name: DisconnectAbilityFromRemote
937 * @tc.desc: disconnect ability from remote.
938 * @tc.type: FUNC
939 * @tc.require: I5OOKG
940 */
941 HWTEST_F(DistributedSchedConnectTest, DisconnectAbilityFromRemote001, TestSize.Level4)
942 {
943 DTEST_LOG << "DistributedSchedConnectTest DisconnectAbilityFromRemote001 start" << std::endl;
944 /**
945 * @tc.steps: step1. call RemoveCallerComponent
946 */
947 DTEST_LOG << "DistributedSchedServiceTest SaveCallerComponent002 start" << std::endl;
948 std::string localDeviceId = "123_local_device_id";
949 std::string remoteDeviceId = "123_remote_device_id";
950 OHOS::AAFwk::Want want1;
951 want1.SetElementName(remoteDeviceId, "ohos.demo.bundleName1", "abilityName1");
952 OHOS::AAFwk::Want want2;
953 want2.SetElementName(remoteDeviceId, "ohos.demo.bundleName2", "abilityName2");
954 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
955 CallerInfo callerInfo;
956 callerInfo.uid = IPCSkeleton::GetCallingUid();
957 callerInfo.pid = IPCSkeleton::GetCallingRealPid();
958 callerInfo.sourceDeviceId = localDeviceId;
959 callerInfo.callerType = CALLER_TYPE_HARMONY;
960 DistributedSchedService::GetInstance().SaveCallerComponent(want1, connect, callerInfo);
961 DistributedSchedService::GetInstance().SaveCallerComponent(want2, connect, callerInfo);
962 DTEST_LOG << "DistributedSchedServiceTest SaveCallerComponent002 end" << std::endl;
963
964 int32_t ret = DistributedSchedService::GetInstance().DisconnectAbilityFromRemote(nullptr, 0, "");
965 EXPECT_NE(ret, ERR_OK);
966 DTEST_LOG << "DistributedSchedConnectTest DisconnectAbilityFromRemote001 end" << std::endl;
967 }
968
969 /**
970 * @tc.name: DisconnectAbilityFromRemote
971 * @tc.desc: disconnect ability from remote.
972 * @tc.type: FUNC
973 * @tc.require: I5OOKG
974 */
975 HWTEST_F(DistributedSchedConnectTest, DisconnectAbilityFromRemote002, TestSize.Level4)
976 {
977 DTEST_LOG << "DistributedSchedConnectTest DisconnectAbilityFromRemote002 start" << std::endl;
978 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
979 int32_t ret = DistributedSchedService::GetInstance().DisconnectAbilityFromRemote(connect, 0, "");
980 EXPECT_NE(ret, ERR_OK);
981 DTEST_LOG << "DistributedSchedConnectTest DisconnectAbilityFromRemote002 end" << std::endl;
982 }
983
984 /**
985 * @tc.name: DisconnectAbilityFromRemote
986 * @tc.desc: disconnect ability from remote.
987 * @tc.type: FUNC
988 * @tc.require: I5OOKG
989 */
990 HWTEST_F(DistributedSchedConnectTest, DisconnectAbilityFromRemote003, TestSize.Level4)
991 {
992 DTEST_LOG << "DistributedSchedConnectTest DisconnectAbilityFromRemote003 start" << std::endl;
993 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
994 std::string deviceId;
995 DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(deviceId);
996 int32_t ret = DistributedSchedService::GetInstance().DisconnectAbilityFromRemote(connect, 0, deviceId);
997 EXPECT_NE(ret, ERR_OK);
998 DTEST_LOG << "DistributedSchedConnectTest DisconnectAbilityFromRemote003 end" << std::endl;
999 }
1000
1001 /**
1002 * @tc.name: DisconnectAbilityFromRemote
1003 * @tc.desc: disconnect ability from remote.
1004 * @tc.type: FUNC
1005 * @tc.require: I5OOKG
1006 */
1007 HWTEST_F(DistributedSchedConnectTest, DisconnectAbilityFromRemote004, TestSize.Level4)
1008 {
1009 DTEST_LOG << "DistributedSchedConnectTest DisconnectAbilityFromRemote004 start" << std::endl;
1010 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1011 /**
1012 * @tc.steps: step1. call SaveCallerComponent
1013 */
1014 DTEST_LOG << "DistributedSchedServiceTest SaveCallerComponent001 start" << std::endl;
1015 OHOS::AAFwk::Want want;
1016 std::string localDeviceId = "123_local_device_id";
1017 std::string remoteDeviceId = "123_remote_device_id";
1018 want.SetElementName(remoteDeviceId, "ohos.demo.bundleName", "abilityName");
1019 CallerInfo callerInfo;
1020 callerInfo.uid = IPCSkeleton::GetCallingUid();
1021 callerInfo.pid = IPCSkeleton::GetCallingRealPid();
1022 callerInfo.sourceDeviceId = localDeviceId;
1023 callerInfo.callerType = CALLER_TYPE_HARMONY;
1024 DistributedSchedService::GetInstance().SaveCallerComponent(want, connect, callerInfo);
1025 DTEST_LOG << "DistributedSchedServiceTest SaveCallerComponent001 end" << std::endl;
1026
1027 int32_t ret = DistributedSchedService::GetInstance().DisconnectAbilityFromRemote(
1028 connect, 0, "123_remote_device_id");
1029 EXPECT_NE(ret, ERR_OK);
1030 DTEST_LOG << "DistributedSchedConnectTest DisconnectAbilityFromRemote004 end" << std::endl;
1031 }
1032
1033 /**
1034 * @tc.name: ProcessDeviceOffline
1035 * @tc.desc: device offline, clear the connect info using fake deviceId.
1036 * @tc.type: FUNC
1037 * @tc.require: I5OOKG
1038 */
1039 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline006, TestSize.Level3)
1040 {
1041 DTEST_LOG << "DistributedSchedConnectTest ProcessDeviceOffline006 start" << std::endl;
1042 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessDeviceOffline("123_remote_device_id"));
1043 DTEST_LOG << "DistributedSchedConnectTest ProcessDeviceOffline006 end" << std::endl;
1044 }
1045
1046 /**
1047 * @tc.name: ProcessDeviceOffline
1048 * @tc.desc: device offline, clear connect info using empty deviceId.
1049 * @tc.type: FUNC
1050 * @tc.require: I5OOKG
1051 */
1052 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline007, TestSize.Level4)
1053 {
1054 DTEST_LOG << "DistributedSchedConnectTest ProcessDeviceOffline007 start" << std::endl;
1055 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessDeviceOffline(""));
1056 DTEST_LOG << "DistributedSchedConnectTest ProcessDeviceOffline007 end" << std::endl;
1057 }
1058
1059 /**
1060 * @tc.name: ProcessDeviceOffline
1061 * @tc.desc: device offline, clear connect info using local deviceId.
1062 * @tc.type: FUNC
1063 * @tc.require: I5OOKG
1064 */
1065 HWTEST_F(DistributedSchedConnectTest, ProcessDeviceOffline008, TestSize.Level3)
1066 {
1067 DTEST_LOG << "DistributedSchedConnectTest ProcessDeviceOffline008 start" << std::endl;
1068 std::string deviceId;
1069 DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(deviceId);
1070 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessDeviceOffline(deviceId));
1071 DTEST_LOG << "DistributedSchedConnectTest ProcessDeviceOffline008 end" << std::endl;
1072 }
1073
1074 /**
1075 * @tc.name: NotifyApp
1076 * @tc.desc: notify app to dealwith the offline message.
1077 * @tc.type: FUNC
1078 * @tc.require: I5OOKG
1079 */
1080 HWTEST_F(DistributedSchedConnectTest, NotifyApp001, TestSize.Level3)
1081 {
1082 DTEST_LOG << "DistributedSchedConnectTest NotifyApp001 start" << std::endl;
1083 AppExecFwk::ElementName element;
1084 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1085 /**
1086 * @tc.steps: step1. call ProcessCalleeDied
1087 */
1088 DTEST_LOG << "DistributedSchedServiceTest ProcessCalleeDied002 start" << std::endl;
1089 sptr<IRemoteObject> callbackWrapper(new AbilityConnectionWrapperStubTest(connect));
1090 CallerInfo callerInfo;
1091 ConnectInfo connectInfo {callerInfo, callbackWrapper};
1092 DistributedSchedService::GetInstance().calleeMap_.emplace(connect, connectInfo);
1093 DistributedSchedService::GetInstance().ProcessCalleeDied(connect);
1094 DTEST_LOG << "DistributedSchedServiceTest ProcessCalleeDied002 end" << std::endl;
1095
1096 int32_t ret = DistributedSchedService::GetInstance().NotifyApp(connect, element, 0);
1097 EXPECT_EQ(ret, ERR_OK);
1098 DTEST_LOG << "DistributedSchedConnectTest NotifyApp001 end" << std::endl;
1099 }
1100
1101 /**
1102 * @tc.name: NotifyApp
1103 * @tc.desc: notify app to dealwith the offline message.
1104 * @tc.type: FUNC
1105 * @tc.require: I5OOKG
1106 */
1107 HWTEST_F(DistributedSchedConnectTest, NotifyApp002, TestSize.Level4)
1108 {
1109 DTEST_LOG << "DistributedSchedConnectTest NotifyApp002 start" << std::endl;
1110 AppExecFwk::ElementName element;
1111 /**
1112 * @tc.steps: step1. call ProcessCalleeDied
1113 */
1114 DTEST_LOG << "DistributedSchedServiceTest ProcessCalleeDied001 start" << std::endl;
1115 DistributedSchedService::GetInstance().ProcessCalleeDied(nullptr);
1116 DTEST_LOG << "DistributedSchedServiceTest ProcessCalleeDied001 end" << std::endl;
1117
1118 int32_t ret = DistributedSchedService::GetInstance().NotifyApp(nullptr, element, 0);
1119 EXPECT_NE(ret, ERR_OK);
1120 DTEST_LOG << "DistributedSchedConnectTest NotifyApp002 end" << std::endl;
1121 }
1122
1123 /**
1124 * @tc.name: ProcessConnectDied
1125 * @tc.desc: dealwith the app died message.
1126 * @tc.type: FUNC
1127 * @tc.require: I5OOKG
1128 */
1129 HWTEST_F(DistributedSchedConnectTest, ProcessConnectDied005, TestSize.Level4)
1130 {
1131 DTEST_LOG << "DistributedSchedConnectTest ProcessConnectDied005 start" << std::endl;
1132 DistributedSchedService::GetInstance().ProcessConnectDied(nullptr);
1133 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1134 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessConnectDied(connect));
1135 DTEST_LOG << "DistributedSchedConnectTest ProcessConnectDied005 end" << std::endl;
1136 }
1137
1138 /**
1139 * @tc.name: NotifyProcessDied001
1140 * @tc.desc: notify process died message to remote.
1141 * @tc.type: FUNC
1142 * @tc.require: I5OOKG
1143 */
1144 HWTEST_F(DistributedSchedConnectTest, NotifyProcessDied001, TestSize.Level4)
1145 {
1146 DTEST_LOG << "DistributedSchedConnectTest NotifyProcessDied001 start" << std::endl;
1147 TargetComponent targetComponent {TargetComponent::HARMONY_COMPONENT};
1148 CallerInfo callerInfo;
1149 DistributedSchedService::GetInstance().NotifyProcessDied("", callerInfo, targetComponent);
1150 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().NotifyProcessDied("123_remote_device_id",
1151 callerInfo, targetComponent));
1152 DTEST_LOG << "DistributedSchedConnectTest NotifyProcessDied001 end" << std::endl;
1153 }
1154
1155 /**
1156 * @tc.name: ProxyCallDisconnectRemoteAbility001
1157 * @tc.desc: call dms proxy DisconnectRemoteAbility
1158 * @tc.type: FUNC
1159 * @tc.require: I5OOKG
1160 */
1161 HWTEST_F(DistributedSchedConnectTest, ProxyCallDisconnectRemoteAbility001, TestSize.Level3)
1162 {
1163 DTEST_LOG << "DistributedSchedServiceTest ProxyCallDisconnectRemoteAbility001 start" << std::endl;
1164 sptr<IDistributedSched> proxy = GetDms();
1165 ASSERT_NE(nullptr, proxy);
1166 /**
1167 * @tc.steps: step1. call HandleLocalCallerDied
1168 */
1169 DTEST_LOG << "DistributedSchedServiceTest HandleLocalCallerDied002 start" << std::endl;
1170 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1171 DistributedSchedService::GetInstance().RemoveCallerComponent(connect);
1172 DistributedSchedService::GetInstance().HandleLocalCallerDied(connect);
1173 DTEST_LOG << "DistributedSchedServiceTest HandleLocalCallerDied002 end" << std::endl;
1174
1175 int32_t ret = proxy->DisconnectRemoteAbility(nullptr, 0, 0);
1176 EXPECT_EQ(ret, ERR_NULL_OBJECT);
1177 DTEST_LOG << "DistributedSchedServiceTest ProxyCallDisconnectRemoteAbility001 end" << std::endl;
1178 }
1179
1180 /**
1181 * @tc.name: ProxyCallDisconnectRemoteAbility002
1182 * @tc.desc: call dms proxy DisconnectRemoteAbility
1183 * @tc.type: FUNC
1184 * @tc.require: I5OOKG
1185 */
1186 HWTEST_F(DistributedSchedConnectTest, ProxyCallDisconnectRemoteAbility002, TestSize.Level3)
1187 {
1188 DTEST_LOG << "DistributedSchedServiceTest ProxyCallDisconnectRemoteAbility002 start" << std::endl;
1189 sptr<IDistributedSched> proxy = GetDms();
1190 ASSERT_NE(nullptr, proxy);
1191 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1192 /**
1193 * @tc.steps: step1. call HandleLocalCallerDied
1194 */
1195 DTEST_LOG << "DistributedSchedServiceTest HandleLocalCallerDied001 start" << std::endl;
1196 OHOS::AAFwk::Want want;
1197 std::string localDeviceId = "123_local_device_id";
1198 std::string remoteDeviceId = "123_remote_device_id";
1199 want.SetElementName(remoteDeviceId, "ohos.demo.bundleName", "abilityName");
1200 CallerInfo callerInfo;
1201 callerInfo.uid = IPCSkeleton::GetCallingUid();
1202 callerInfo.pid = IPCSkeleton::GetCallingRealPid();
1203 callerInfo.sourceDeviceId = localDeviceId;
1204 callerInfo.callerType = CALLER_TYPE_HARMONY;
1205 DistributedSchedService::GetInstance().SaveCallerComponent(want, connect, callerInfo);
1206 DistributedSchedService::GetInstance().HandleLocalCallerDied(connect);
1207 DTEST_LOG << "DistributedSchedServiceTest HandleLocalCallerDied001 end" << std::endl;
1208
1209 int32_t ret = proxy->DisconnectRemoteAbility(connect, 0, 0);
1210 EXPECT_EQ(ret, DMS_PERMISSION_DENIED);
1211 DTEST_LOG << "DistributedSchedServiceTest ProxyCallDisconnectRemoteAbility002 end" << std::endl;
1212 }
1213
1214 /**
1215 * @tc.name: ProxyCallConnectRemoteAbility001
1216 * @tc.desc: call dms proxy ConnectRemoteAbility
1217 * @tc.type: FUNC
1218 * @tc.require: I5OOKG
1219 */
1220 HWTEST_F(DistributedSchedConnectTest, ProxyCallConnectRemoteAbility001, TestSize.Level3)
1221 {
1222 DTEST_LOG << "DistributedSchedServiceTest ProxyCallConnectRemoteAbility001 start" << std::endl;
1223 sptr<IDistributedSched> proxy = GetDms();
1224 ASSERT_NE(nullptr, proxy);
1225 OHOS::AAFwk::Want want;
1226 want.SetElementName("123_remote_device_id", "ohos.demo.bundleName", "abilityName");
1227 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1228 /**
1229 * @tc.steps: step1. call GetUidLocked
1230 */
1231 DTEST_LOG << "DistributedSchedConnectTest GetUidLocked001 start" << std::endl;
1232 std::list<ConnectAbilitySession> sessionsList;
1233 DistributedSchedService::GetInstance().GetUidLocked(sessionsList);
1234 DTEST_LOG << "DistributedSchedConnectTest GetUidLocked001 end" << std::endl;
1235
1236 EXPECT_CALL(*multiUserMgrMock_, IsCallerForeground(_)).WillRepeatedly(Return(true));
1237 int32_t ret = proxy->ConnectRemoteAbility(want, connect, 0, 0, 0);
1238 EXPECT_EQ(ret, DMS_PERMISSION_DENIED);
1239 DTEST_LOG << "DistributedSchedServiceTest ProxyCallConnectRemoteAbility001 end" << std::endl;
1240 }
1241
1242 /**
1243 * @tc.name: ProxyCallConnectRemoteAbility002
1244 * @tc.desc: call dms proxy ConnectRemoteAbility
1245 * @tc.type: FUNC
1246 * @tc.require: I5OOKG
1247 */
1248 HWTEST_F(DistributedSchedConnectTest, ProxyCallConnectRemoteAbility002, TestSize.Level3)
1249 {
1250 DTEST_LOG << "DistributedSchedServiceTest ProxyCallConnectRemoteAbility002 start" << std::endl;
1251 sptr<IDistributedSched> proxy = GetDms();
1252 ASSERT_NE(nullptr, proxy);
1253 OHOS::AAFwk::Want want;
1254 want.SetElementName("123_remote_device_id", "ohos.demo.bundleName", "abilityName");
1255 /**
1256 * @tc.steps: step1. call DecreaseConnectLocked
1257 */
1258 DTEST_LOG << "DistributedSchedConnectTest DecreaseConnectLocked002 start" << std::endl;
1259 int32_t uid = 1000;
1260 AddConnectCount(uid);
1261 DistributedSchedService::GetInstance().DecreaseConnectLocked(uid);
1262 DTEST_LOG << "DistributedSchedConnectTest DecreaseConnectLocked002 end" << std::endl;
1263
1264 EXPECT_CALL(*multiUserMgrMock_, IsCallerForeground(_)).WillRepeatedly(Return(true));
1265 int32_t ret = proxy->ConnectRemoteAbility(want, nullptr, 0, 0, 0);
1266 EXPECT_EQ(ret, ERR_NULL_OBJECT);
1267 DTEST_LOG << "DistributedSchedServiceTest ProxyCallConnectRemoteAbility002 end" << std::endl;
1268 }
1269
1270 /**
1271 * @tc.name: ProxyCallConnectAbilityFromRemote001
1272 * @tc.desc: call dms proxy ConnectAbilityFromRemote
1273 * @tc.type: FUNC
1274 * @tc.require: I5OOKG
1275 */
1276 HWTEST_F(DistributedSchedConnectTest, ProxyCallConnectAbilityFromRemote001, TestSize.Level3)
1277 {
1278 DTEST_LOG << "DistributedSchedServiceTest ProxyCallConnectAbilityFromRemote001 start" << std::endl;
1279 sptr<IDistributedSched> proxy = GetDms();
1280 ASSERT_NE(nullptr, proxy);
1281 OHOS::AAFwk::Want want;
1282 want.SetElementName("123_remote_device_id", "ohos.demo.bundleName", "abilityName");
1283 AppExecFwk::AbilityInfo abilityInfo;
1284 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1285 CallerInfo callerInfo;
1286 IDistributedSched::AccountInfo accountInfo;
1287 /**
1288 * @tc.steps: step1. call DecreaseConnectLocked
1289 */
1290 DTEST_LOG << "DistributedSchedConnectTest DecreaseConnectLocked001 start" << std::endl;
1291 int32_t uid = -1;
1292 DistributedSchedService::GetInstance().DecreaseConnectLocked(uid);
1293 DTEST_LOG << "DistributedSchedConnectTest DecreaseConnectLocked001 end" << std::endl;
1294
1295 int32_t ret = proxy->ConnectAbilityFromRemote(want, abilityInfo,
1296 connect, callerInfo, accountInfo);
1297 EXPECT_EQ(ret, REQUEST_CODE_ERR);
1298 DTEST_LOG << "DistributedSchedServiceTest ProxyCallConnectAbilityFromRemote001 end" << std::endl;
1299 }
1300
1301 /**
1302 * @tc.name: ProxyCallConnectAbilityFromRemote002
1303 * @tc.desc: call dms proxy ConnectAbilityFromRemote
1304 * @tc.type: FUNC
1305 * @tc.require: I5OOKG
1306 */
1307 HWTEST_F(DistributedSchedConnectTest, ProxyCallConnectAbilityFromRemote002, TestSize.Level3)
1308 {
1309 DTEST_LOG << "DistributedSchedServiceTest ProxyCallConnectAbilityFromRemote002 start" << std::endl;
1310 sptr<IDistributedSched> proxy = GetDms();
1311 ASSERT_NE(nullptr, proxy);
1312 OHOS::AAFwk::Want want;
1313 want.SetElementName("123_remote_device_id", "ohos.demo.bundleName", "abilityName");
1314 AppExecFwk::AbilityInfo abilityInfo;
1315 CallerInfo callerInfo;
1316 IDistributedSched::AccountInfo accountInfo;
1317 /**
1318 * @tc.steps: step1. call CheckDistributedConnectLocked
1319 */
1320 DTEST_LOG << "DistributedSchedConnectTest CheckDistributedConnectLocked002 start" << std::endl;
1321 int32_t uid = IPCSkeleton::GetCallingUid();
1322 callerInfo.uid = uid;
1323 DistributedSchedService::GetInstance().CheckDistributedConnectLocked(callerInfo);
1324 DTEST_LOG << "DistributedSchedConnectTest CheckDistributedConnectLocked002 end" << std::endl;
1325
1326 int32_t ret = proxy->ConnectAbilityFromRemote(want, abilityInfo,
1327 nullptr, callerInfo, accountInfo);
1328 EXPECT_EQ(ret, ERR_NULL_OBJECT);
1329 DTEST_LOG << "DistributedSchedServiceTest ProxyCallConnectAbilityFromRemote002 end" << std::endl;
1330 }
1331
1332 /**
1333 * @tc.name: ProxyCallDisconnectAbilityFromRemote001
1334 * @tc.desc: call dms proxy DisconnectAbilityFromRemote
1335 * @tc.type: FUNC
1336 * @tc.require: I5OOKG
1337 */
1338 HWTEST_F(DistributedSchedConnectTest, ProxyCallDisconnectAbilityFromRemote001, TestSize.Level3)
1339 {
1340 DTEST_LOG << "DistributedSchedServiceTest ProxyCallDisconnectAbilityFromRemote001 start" << std::endl;
1341 sptr<IDistributedSched> proxy = GetDms();
1342 ASSERT_NE(nullptr, proxy);
1343 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1344 /**
1345 * @tc.steps: step1. call CheckDistributedConnectLocked
1346 */
1347 DTEST_LOG << "DistributedSchedConnectTest CheckDistributedConnectLocked001 start" << std::endl;
1348 int32_t uid = IPCSkeleton::GetCallingUid();
1349 CallerInfo callerInfo;
1350 callerInfo.uid = uid;
1351 AddConnectCount(uid);
1352 DistributedSchedService::GetInstance().CheckDistributedConnectLocked(callerInfo);
1353 DTEST_LOG << "DistributedSchedConnectTest CheckDistributedConnectLocked001 end" << std::endl;
1354
1355 int32_t ret = proxy->DisconnectAbilityFromRemote(connect, 0, "");
1356 EXPECT_EQ(ret, REQUEST_CODE_ERR);
1357 DTEST_LOG << "DistributedSchedServiceTest ProxyCallDisconnectAbilityFromRemote001 end" << std::endl;
1358 }
1359
1360 /**
1361 * @tc.name: ProxyCallDisconnectAbilityFromRemote002
1362 * @tc.desc: call dms proxy DisconnectAbilityFromRemote
1363 * @tc.type: FUNC
1364 * @tc.require: I5OOKG
1365 */
1366 HWTEST_F(DistributedSchedConnectTest, ProxyCallDisconnectAbilityFromRemote002, TestSize.Level3)
1367 {
1368 DTEST_LOG << "DistributedSchedServiceTest ProxyCallDisconnectAbilityFromRemote002 start" << std::endl;
1369 sptr<IDistributedSched> proxy = GetDms();
1370 ASSERT_NE(nullptr, proxy);
1371 /**
1372 * @tc.steps: step1. call RemoteConnectAbilityMappingLocked
1373 */
1374 DTEST_LOG << "DistributedSchedConnectTest CheckDistributedConnectLocked001 start" << std::endl;
1375 OHOS::AAFwk::Want want;
1376 want.SetElementName("", "ohos.demo.bundleName", "abilityName");
1377 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1378 std::string localDeviceId = "123_local_device_id";
1379 std::string remoteDeviceId = "123_remote_device_id";
1380 CallerInfo callerInfo;
1381 AddSession(connect, localDeviceId, remoteDeviceId, want);
1382 DistributedSchedService::GetInstance().RemoteConnectAbilityMappingLocked(connect, localDeviceId,
1383 remoteDeviceId, want.GetElement(), callerInfo, TargetComponent::HARMONY_COMPONENT);
1384 DTEST_LOG << "DistributedSchedConnectTest RemoteConnectAbilityMappingLocked001 end" << std::endl;
1385
1386 int32_t ret = proxy->DisconnectAbilityFromRemote(nullptr, 0, "");
1387 EXPECT_EQ(ret, ERR_NULL_OBJECT);
1388 DTEST_LOG << "DistributedSchedServiceTest ProxyCallDisconnectAbilityFromRemote002 end" << std::endl;
1389 }
1390
1391 /**
1392 * @tc.name: ConnectRemoteAbility003
1393 * @tc.desc: call ConnectRemoteAbility
1394 * @tc.type: FUNC
1395 */
1396 HWTEST_F(DistributedSchedConnectTest, ConnectRemoteAbility003, TestSize.Level4)
1397 {
1398 DTEST_LOG << "DistributedSchedServiceTest ConnectRemoteAbility003 start" << std::endl;
1399 std::string remoteDeviceId = "remoteDeviceId";
1400 OHOS::AAFwk::Want want;
1401 want.SetElementName(remoteDeviceId, "ohos.demo.bundleName", "abilityName");
1402 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1403 int32_t uid = IPCSkeleton::GetCallingUid();
1404 int32_t pid = IPCSkeleton::GetCallingRealPid();
1405 int32_t accessToken = IPCSkeleton::GetCallingTokenID();
1406
1407 EXPECT_CALL(*multiUserMgrMock_, IsCallerForeground(_)).WillRepeatedly(Return(true));
1408 int32_t ret = DistributedSchedService::GetInstance().ConnectRemoteAbility(want, connect, uid, pid, accessToken);
1409 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
1410 DTEST_LOG << "DistributedSchedServiceTest ConnectRemoteAbility003 end" << std::endl;
1411 }
1412
1413 /**
1414 * @tc.name: TryConnectRemoteAbility001
1415 * @tc.desc: call TryConnectRemoteAbility
1416 * @tc.type: FUNC
1417 */
1418 HWTEST_F(DistributedSchedConnectTest, TryConnectRemoteAbility001, TestSize.Level4)
1419 {
1420 DTEST_LOG << "DistributedSchedServiceTest TryConnectRemoteAbility001 start" << std::endl;
1421 std::string remoteDeviceId = "remoteDeviceId";
1422 OHOS::AAFwk::Want want;
1423 want.SetElementName(remoteDeviceId, "ohos.demo.bundleName", "abilityName");
1424 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1425 CallerInfo callerInfo;
1426 int32_t ret = DistributedSchedService::GetInstance().TryConnectRemoteAbility(want, connect, callerInfo);
1427 EXPECT_EQ(ret, INVALID_PARAMETERS_ERR);
1428 DTEST_LOG << "DistributedSchedServiceTest TryConnectRemoteAbility001 end" << std::endl;
1429 }
1430
1431 /**
1432 * @tc.name: ProcessCallerDied001
1433 * @tc.desc: call ProcessCallerDied
1434 * @tc.type: FUNC
1435 */
1436 HWTEST_F(DistributedSchedConnectTest, ProcessCallerDied001, TestSize.Level4)
1437 {
1438 DTEST_LOG << "DistributedSchedServiceTest ProcessCallerDied001 start" << std::endl;
1439 int32_t deviceType = IDistributedSched::CALLER;
1440 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessCallerDied(nullptr, deviceType));
1441 DTEST_LOG << "DistributedSchedServiceTest ProcessCallerDied001 end" << std::endl;
1442 }
1443
1444 /**
1445 * @tc.name: ProcessCallerDied002
1446 * @tc.desc: call ProcessCallerDied
1447 * @tc.type: FUNC
1448 */
1449 HWTEST_F(DistributedSchedConnectTest, ProcessCallerDied002, TestSize.Level4)
1450 {
1451 DTEST_LOG << "DistributedSchedServiceTest ProcessCallerDied002 start" << std::endl;
1452 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1453 int32_t deviceType = IDistributedSched::CALLER;
1454 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessCallerDied(connect, deviceType));
1455 DTEST_LOG << "DistributedSchedServiceTest ProcessCallerDied002 end" << std::endl;
1456 }
1457
1458 /**
1459 * @tc.name: ProcessCallerDied003
1460 * @tc.desc: call ProcessCallerDied
1461 * @tc.type: FUNC
1462 */
1463 HWTEST_F(DistributedSchedConnectTest, ProcessCallerDied003, TestSize.Level4)
1464 {
1465 DTEST_LOG << "DistributedSchedServiceTest ProcessCallerDied003 start" << std::endl;
1466 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1467 int32_t deviceType = IDistributedSched::CALLEE;
1468 CallerInfo callerInfo;
1469 sptr<IRemoteObject> callbackWrapper(new AbilityConnectionWrapperStubTest(connect));
1470 ConnectInfo connectInfo {callerInfo, callbackWrapper};
1471 DistributedSchedService::GetInstance().calleeMap_.emplace(connect, connectInfo);
1472 EXPECT_NO_FATAL_FAILURE(DistributedSchedService::GetInstance().ProcessCallerDied(connect, deviceType));
1473 DTEST_LOG << "DistributedSchedServiceTest ProcessCallerDied003 end" << std::endl;
1474 }
1475
1476 /**
1477 * @tc.name: AbilityConnectionWrapperStub001
1478 * @tc.desc: receive connect message
1479 * @tc.type: FUNC
1480 */
1481 HWTEST_F(DistributedSchedConnectTest, AbilityConnectionWrapperStub001, TestSize.Level3)
1482 {
1483 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub001 start" << std::endl;
1484 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1485 ASSERT_NE(connect, nullptr);
1486 sptr<AbilityConnectionWrapperStub> connectStub(new AbilityConnectionWrapperStub(connect));
1487 ASSERT_NE(connectStub, nullptr);
1488 MessageParcel data;
1489 ASSERT_NE(false, data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN));
1490 MessageParcel reply;
1491 MessageOption option;
1492 AppExecFwk::ElementName element;
1493 PARCEL_WRITE_HELPER_NORET(data, Parcelable, &element);
1494 PARCEL_WRITE_HELPER_NORET(data, RemoteObject, connect);
1495 PARCEL_WRITE_HELPER_NORET(data, Int32, 1);
1496 int32_t result = connectStub->OnRemoteRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
1497 EXPECT_EQ(result, ERR_NONE);
1498 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub001 end" << std::endl;
1499 }
1500
1501 /**
1502 * @tc.name: AbilityConnectionWrapperStub002
1503 * @tc.desc: receive oncall message
1504 * @tc.type: FUNC
1505 */
1506 HWTEST_F(DistributedSchedConnectTest, AbilityConnectionWrapperStub002, TestSize.Level3)
1507 {
1508 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub002 start" << std::endl;
1509 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1510 ASSERT_NE(connect, nullptr);
1511 sptr<AbilityConnectionWrapperStub> connectStub(new AbilityConnectionWrapperStub(connect, "localDeviceId"));
1512 ASSERT_NE(connectStub, nullptr);
1513 MessageParcel data;
1514 ASSERT_NE(false, data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN));
1515 MessageParcel reply;
1516 MessageOption option;
1517 AppExecFwk::ElementName element;
1518 PARCEL_WRITE_HELPER_NORET(data, Parcelable, &element);
1519 PARCEL_WRITE_HELPER_NORET(data, RemoteObject, connect);
1520 PARCEL_WRITE_HELPER_NORET(data, Int32, 1);
1521 int32_t result = connectStub->OnRemoteRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
1522 EXPECT_EQ(result, ERR_NONE);
1523 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub002 end" << std::endl;
1524 }
1525
1526 /**
1527 * @tc.name: AbilityConnectionWrapperStub003
1528 * @tc.desc: receive disconnect message
1529 * @tc.type: FUNC
1530 */
1531 HWTEST_F(DistributedSchedConnectTest, AbilityConnectionWrapperStub003, TestSize.Level3)
1532 {
1533 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub003 start" << std::endl;
1534 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1535 ASSERT_NE(connect, nullptr);
1536 sptr<AbilityConnectionWrapperStub> connectStub(new AbilityConnectionWrapperStub(connect));
1537 ASSERT_NE(connectStub, nullptr);
1538 MessageParcel data;
1539 ASSERT_NE(false, data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN));
1540 MessageParcel reply;
1541 MessageOption option;
1542 AppExecFwk::ElementName element;
1543 PARCEL_WRITE_HELPER_NORET(data, Parcelable, &element);
1544 PARCEL_WRITE_HELPER_NORET(data, Int32, 1);
1545 int32_t result = connectStub->OnRemoteRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
1546 EXPECT_EQ(result, ERR_NONE);
1547 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub003 end" << std::endl;
1548 }
1549
1550 /**
1551 * @tc.name: AbilityConnectionWrapperStub004
1552 * @tc.desc: receive oncall disconnect message
1553 * @tc.type: FUNC
1554 */
1555 HWTEST_F(DistributedSchedConnectTest, AbilityConnectionWrapperStub004, TestSize.Level3)
1556 {
1557 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub004 start" << std::endl;
1558 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1559 ASSERT_NE(connect, nullptr);
1560 sptr<AbilityConnectionWrapperStub> connectStub(new AbilityConnectionWrapperStub(connect, "localDeviceId"));
1561 ASSERT_NE(connectStub, nullptr);
1562 MessageParcel data;
1563 ASSERT_NE(false, data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN));
1564 MessageParcel reply;
1565 MessageOption option;
1566 AppExecFwk::ElementName element;
1567 PARCEL_WRITE_HELPER_NORET(data, Parcelable, &element);
1568 PARCEL_WRITE_HELPER_NORET(data, Int32, 1);
1569 int32_t result = connectStub->OnRemoteRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
1570 EXPECT_EQ(result, ERR_NONE);
1571 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub004 end" << std::endl;
1572 }
1573
1574 /**
1575 * @tc.name: AbilityConnectionWrapperStub005
1576 * @tc.desc: receive error connect message
1577 * @tc.type: FUNC
1578 */
1579 HWTEST_F(DistributedSchedConnectTest, AbilityConnectionWrapperStub005, TestSize.Level3)
1580 {
1581 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub005 start" << std::endl;
1582 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1583 ASSERT_NE(connect, nullptr);
1584 sptr<AbilityConnectionWrapperStub> connectStub(new AbilityConnectionWrapperStub(connect, "localDeviceId"));
1585 ASSERT_NE(connectStub, nullptr);
1586 MessageParcel data;
1587 MessageParcel reply;
1588 MessageOption option;
1589 // no interface token
1590 AppExecFwk::ElementName element;
1591 PARCEL_WRITE_HELPER_NORET(data, Parcelable, &element);
1592 PARCEL_WRITE_HELPER_NORET(data, Int32, 1);
1593 int32_t result = connectStub->OnRemoteRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
1594 EXPECT_EQ(result, ERR_INVALID_STATE);
1595 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub005 end" << std::endl;
1596 }
1597
1598 /**
1599 * @tc.name: AbilityConnectionWrapperStub006
1600 * @tc.desc: receive error connect message
1601 * @tc.type: FUNC
1602 */
1603 HWTEST_F(DistributedSchedConnectTest, AbilityConnectionWrapperStub006, TestSize.Level3)
1604 {
1605 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub006 start" << std::endl;
1606 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1607 ASSERT_NE(connect, nullptr);
1608 sptr<AbilityConnectionWrapperStub> connectStub(new AbilityConnectionWrapperStub(connect, "localDeviceId"));
1609 ASSERT_NE(connectStub, nullptr);
1610 MessageParcel data;
1611 MessageParcel reply;
1612 MessageOption option;
1613 ASSERT_NE(false, data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN));
1614 // no element
1615 int32_t result = connectStub->OnRemoteRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
1616 EXPECT_EQ(result, ERR_INVALID_VALUE);
1617 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub006 end" << std::endl;
1618 }
1619
1620 /**
1621 * @tc.name: AbilityConnectionWrapperStub007
1622 * @tc.desc: receive disconnect message
1623 * @tc.type: FUNC
1624 */
1625 HWTEST_F(DistributedSchedConnectTest, AbilityConnectionWrapperStub007, TestSize.Level3)
1626 {
1627 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub007 start" << std::endl;
1628 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1629 ASSERT_NE(connect, nullptr);
1630 sptr<AbilityConnectionWrapperStub> connectStub(new AbilityConnectionWrapperStub(connect, "localDeviceId"));
1631 ASSERT_NE(connectStub, nullptr);
1632 MessageParcel data;
1633 ASSERT_NE(false, data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN));
1634 MessageParcel reply;
1635 MessageOption option;
1636 AppExecFwk::ElementName element;
1637 PARCEL_WRITE_HELPER_NORET(data, Parcelable, &element);
1638 PARCEL_WRITE_HELPER_NORET(data, Int32, 1);
1639 // using error code
1640 int32_t result = connectStub->OnRemoteRequest(ERROR_CONNECT_CODE, data, reply, option);
1641 EXPECT_NE(result, ERR_NONE);
1642 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub007 end" << std::endl;
1643 }
1644
1645 /**
1646 * @tc.name: AbilityConnectionWrapperStub008
1647 * @tc.desc: receive error connect message
1648 * @tc.type: FUNC
1649 */
1650 HWTEST_F(DistributedSchedConnectTest, AbilityConnectionWrapperStub008, TestSize.Level3)
1651 {
1652 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub008 start" << std::endl;
1653 sptr<AbilityConnectCallbackTest> connect(new AbilityConnectCallbackTest());
1654 ASSERT_NE(connect, nullptr);
1655 sptr<AbilityConnectionWrapperStub> connectStub(new AbilityConnectionWrapperStub(connect, "localDeviceId"));
1656 ASSERT_NE(connectStub, nullptr);
1657 MessageParcel data;
1658 ASSERT_NE(false, data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN));
1659 MessageParcel reply;
1660 MessageOption option;
1661 // no remoteObject
1662 AppExecFwk::ElementName element;
1663 PARCEL_WRITE_HELPER_NORET(data, Parcelable, &element);
1664 PARCEL_WRITE_HELPER_NORET(data, Int32, 1);
1665 int32_t result = connectStub->OnRemoteRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
1666 EXPECT_NE(result, ERR_NONE);
1667 DTEST_LOG << "DistributedSchedServiceTest AbilityConnectionWrapperStub008 end" << std::endl;
1668 }
1669 }
1670 }
1671