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