1 /*
2  * Copyright (c) 2022 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_stub.h"
18 #include "if_system_ability_manager.h"
19 #include "ipc_skeleton.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 
23 #define private public
24 #define protected public
25 #include "distributed_sched_adapter.h"
26 #include "distributed_sched_service.h"
27 #include "dtbschedmgr_device_info_storage.h"
28 #include "test_log.h"
29 #undef private
30 #undef protected
31 
32 namespace OHOS {
33 namespace DistributedSchedule {
34 using namespace testing;
35 using namespace testing::ext;
36 
37 namespace {
38 constexpr int32_t INVALID_PARAMETERS_ERR_CODE = 29360128;
39 constexpr int32_t INVALID_REMOTE_PARAMETERS_ERR_CODE = 29360131;
40 constexpr int32_t MOCK_UID = 1000;
41 constexpr int32_t MOCK_PID = 1000;
42 const std::string MOCK_DEVICE_ID = "1234567890";
43 }
44 
45 class AbilityCallCallbackTest : public AAFwk::AbilityConnectionStub {
46 public:
47     AbilityCallCallbackTest() = default;
48     ~AbilityCallCallbackTest() = default;
49 
50     void OnAbilityConnectDone(const AppExecFwk::ElementName& element,
51         const sptr<IRemoteObject>& remoteObject, int32_t resultCode) override;
52     void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
53         int32_t resultCode) override;
54 };
55 
56 class AbilityCallWrapperStubTest : public AAFwk::AbilityConnectionStub {
57 public:
AbilityCallWrapperStubTest(sptr<IRemoteObject> connection)58     explicit AbilityCallWrapperStubTest(sptr<IRemoteObject> connection) : distributedCall_(connection) {}
59     ~AbilityCallWrapperStubTest() = default;
60 
61     void OnAbilityConnectDone(const AppExecFwk::ElementName& element,
62         const sptr<IRemoteObject>& remoteObject, int32_t resultCode) override;
63     void OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
64         int32_t resultCode) override;
65 
66 private:
67     sptr<IRemoteObject> distributedCall_;
68 };
69 
70 class DistributedSchedCallTest : public testing::Test {
71 public:
72     static void SetUpTestCase();
73     static void TearDownTestCase();
74     void SetUp();
75     void TearDown();
76 
77     void AddSession(const sptr<IRemoteObject>& connect, const std::string& localDeviceId,
78         const std::string& remoteDeviceId, const AAFwk::Want& want) const;
79     void RemoveSession(const sptr<IRemoteObject>& connect) const;
80 
81     void AddConnectInfo(const sptr<IRemoteObject>& connect, const std::string& localDeviceId,
82         const std::string& remoteDeviceId) const;
83     void RemoveConnectInfo(const sptr<IRemoteObject>& connect) const;
84 
85     void AddConnectCount(int32_t uid) const;
86     void DecreaseConnectCount(int32_t uid) const;
87 };
88 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)89 void AbilityCallCallbackTest::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
90     const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
91 {
92     DTEST_LOG << "DistributedSchedServiceTest call OnAbilityConnectDone " << std::endl;
93 }
94 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)95 void AbilityCallCallbackTest::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
96     int32_t resultCode)
97 {
98     DTEST_LOG << "DistributedSchedServiceTest call OnAbilityDisconnectDone " << std::endl;
99 }
100 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)101 void AbilityCallWrapperStubTest::OnAbilityConnectDone(const AppExecFwk::ElementName& element,
102     const sptr<IRemoteObject>& remoteObject, int32_t resultCode)
103 {
104     DTEST_LOG << "DistributedSchedServiceTest call OnAbilityConnectDone " << std::endl;
105 }
106 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)107 void AbilityCallWrapperStubTest::OnAbilityDisconnectDone(const AppExecFwk::ElementName& element,
108     int32_t resultCode)
109 {
110     DTEST_LOG << "DistributedSchedServiceTest call OnAbilityDisconnectDone " << std::endl;
111 }
112 
SetUpTestCase()113 void DistributedSchedCallTest::SetUpTestCase()
114 {
115     DTEST_LOG << "DistributedSchedServiceTest call SetUpTestCase " << std::endl;
116 }
117 
TearDownTestCase()118 void DistributedSchedCallTest::TearDownTestCase()
119 {
120     DTEST_LOG << "DistributedSchedServiceTest call TearDownTestCase " << std::endl;
121 }
122 
SetUp()123 void DistributedSchedCallTest::SetUp()
124 {
125     DTEST_LOG << "DistributedSchedServiceTest call SetUp " << std::endl;
126 }
127 
TearDown()128 void DistributedSchedCallTest::TearDown()
129 {
130     DTEST_LOG << "DistributedSchedServiceTest call TearDown " << std::endl;
131 }
132 
133 /**
134  * @tc.name: CallAbility_001
135  * @tc.desc: Call StartRemoteAbilityByCall with illegal want
136  * @tc.type: FUNC
137  */
138 HWTEST_F(DistributedSchedCallTest, CallAbility_001, TestSize.Level1)
139 {
140     DTEST_LOG << "DistributedSchedServiceTest CallAbility_001 start " << std::endl;
141     OHOS::AAFwk::Want want;
142     want.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
143     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
144     int32_t callerUid = MOCK_UID;
145     int32_t callerPid = MOCK_PID;
146     uint32_t accessToken = 0;
147 
148     DTEST_LOG << "DistributedSchedServiceTest mock illegal want " << std::endl;
149     OHOS::AAFwk::Want illegalWant;
150     illegalWant.SetElementName("", "ohos.demo.test", "abilityTest");
151     int32_t result = DistributedSchedService::GetInstance().StartRemoteAbilityByCall(illegalWant,
152         callback, callerUid, callerPid, accessToken);
153     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
154 
155     DTEST_LOG << "DistributedSchedServiceTest CallAbility_001 end " << std::endl;
156 }
157 
158 /**
159  * @tc.name: CallAbility_002
160  * @tc.desc: Call TryStartRemoteAbilityByCall with illegal parameter
161  * @tc.type: FUNC
162  */
163 HWTEST_F(DistributedSchedCallTest, CallAbility_002, TestSize.Level1)
164 {
165     DTEST_LOG << "DistributedSchedServiceTest CallAbility_002 start " << std::endl;
166     OHOS::AAFwk::Want mockWant;
167     mockWant.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
168     CallerInfo callerInfo;
169     callerInfo.uid = MOCK_UID;
170     callerInfo.pid = MOCK_PID;
171     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
172     callerInfo.callerType = CALLER_TYPE_NONE;
173     callerInfo.duid = 0;
174     callerInfo.dmsVersion = 0;
175     callerInfo.accessToken = 0;
176     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
177 
178     DTEST_LOG << "DistributedSchedServiceTest mock illegal want " << std::endl;
179     int32_t result = DistributedSchedService::GetInstance().TryStartRemoteAbilityByCall(mockWant,
180         callback, callerInfo);
181     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
182     DTEST_LOG << "DistributedSchedServiceTest CallAbility_002 end " << std::endl;
183 }
184 
185 /**
186  * @tc.name: CallAbility_003
187  * @tc.desc: Call StartAbilityByCallFromRemote with illegal parameter
188  * @tc.type: FUNC
189  */
190 HWTEST_F(DistributedSchedCallTest, CallAbility_003, TestSize.Level1)
191 {
192     DTEST_LOG << "DistributedSchedServiceTest CallAbility_003 start " << std::endl;
193     OHOS::AAFwk::Want mockWant;
194     mockWant.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
195     CallerInfo callerInfo;
196     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
197     callerInfo.callerType = CALLER_TYPE_NONE;
198     callerInfo.duid = 0;
199     callerInfo.uid = MOCK_UID;
200     callerInfo.pid = MOCK_PID;
201     callerInfo.dmsVersion = 1000;
202     callerInfo.accessToken = 0;
203     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
204     IDistributedSched::AccountInfo accountInfo;
205 
206     DTEST_LOG << "DistributedSchedServiceTest mock illegal want " << std::endl;
207     int32_t result = DistributedSchedService::GetInstance().StartAbilityByCallFromRemote(mockWant,
208         callback, callerInfo, accountInfo);
209     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
210 
211     DTEST_LOG << "DistributedSchedServiceTest CallAbility_003 end " << std::endl;
212 }
213 
214 /**
215  * @tc.name: CallAbility_004
216  * @tc.desc: Call StartAbilityByCall with illegal parameter
217  * @tc.type: FUNC
218  */
219 HWTEST_F(DistributedSchedCallTest, CallAbility_004, TestSize.Level1)
220 {
221     DTEST_LOG << "DistributedSchedServiceTest CallAbility_004 start " << std::endl;
222     OHOS::AAFwk::Want want;
223     want.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
224     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
225     sptr<IRemoteObject> callbackWrapper = new AbilityCallWrapperStubTest(callback);
226     sptr<IRemoteObject> callerToken = new AbilityCallCallbackTest();
227 
228     DTEST_LOG << "DistributedSchedServiceTest mock illegal want " << std::endl;
229     OHOS::AAFwk::Want mockWant;
230     mockWant.SetElementName(MOCK_DEVICE_ID, "ohos.demo.xxx", "xxx");
231     int32_t result = DistributedSchedAdapter::GetInstance().StartAbilityByCall(mockWant,
232         callbackWrapper, new AbilityCallCallbackTest());
233     EXPECT_NE(result, 0);
234 
235     DTEST_LOG << "DistributedSchedServiceTest CallAbility_004 end " << std::endl;
236 }
237 
238 /**
239  * @tc.name: CallAbility_005
240  * @tc.desc: Call ReleaseRemoteAbility with illegal parameter
241  * @tc.type: FUNC
242  */
243 HWTEST_F(DistributedSchedCallTest, CallAbility_005, TestSize.Level1)
244 {
245     DTEST_LOG << "DistributedSchedServiceTest CallAbility_005 start " << std::endl;
246     AppExecFwk::ElementName element(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
247     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
248 
249     DTEST_LOG << "DistributedSchedServiceTest mock illegal element " << std::endl;
250     AppExecFwk::ElementName mockElement("", "ohos.demo.test", "abilityTest");
251     int32_t result = DistributedSchedService::GetInstance().ReleaseRemoteAbility(callback,
252         mockElement);
253     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
254 
255     DTEST_LOG << "DistributedSchedServiceTest CallAbility_005 end " << std::endl;
256 }
257 
258 /**
259  * @tc.name: CallAbility_006
260  * @tc.desc: Call ReleaseAbilityFromRemote with illegal parameter
261  * @tc.type: FUNC
262  */
263 HWTEST_F(DistributedSchedCallTest, CallAbility_006, TestSize.Level1)
264 {
265     DTEST_LOG << "DistributedSchedServiceTest CallAbility_006 start " << std::endl;
266     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
267     AppExecFwk::ElementName element(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
268     CallerInfo callerInfo;
269     callerInfo.callerType = CALLER_TYPE_NONE;
270     callerInfo.duid = 1000;
271     callerInfo.dmsVersion = 1000;
272     callerInfo.uid = MOCK_UID;
273     callerInfo.accessToken = 1000;
274     callerInfo.pid = MOCK_PID;
275     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
276 
277     DTEST_LOG << "DistributedSchedServiceTest mock illegal element " << std::endl;
278     AppExecFwk::ElementName mockElement("", "ohos.demo.test", "abilityTest");
279     int32_t result = DistributedSchedService::GetInstance().ReleaseAbilityFromRemote(callback,
280         mockElement, callerInfo);
281     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
282 
283     DTEST_LOG << "DistributedSchedServiceTest get illegal callerInfo" << std::endl;
284     CallerInfo mockCallerInfo = callerInfo;
285     mockCallerInfo.uid = -1;
286     result = DistributedSchedService::GetInstance().ReleaseAbilityFromRemote(callback,
287         element, mockCallerInfo);
288     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
289 
290     DTEST_LOG << "DistributedSchedServiceTest CallAbility_006 end " << std::endl;
291 }
292 
293 /**
294  * @tc.name: CallAbility_007
295  * @tc.desc: Call ReleaseAbilityFromRemote with illegal callerInfo
296  * @tc.type: FUNC
297  */
298 HWTEST_F(DistributedSchedCallTest, CallAbility_007, TestSize.Level1)
299 {
300     DTEST_LOG << "DistributedSchedServiceTest CallAbility_007 start " << std::endl;
301     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
302     AppExecFwk::ElementName element(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
303     CallerInfo callerInfo;
304     callerInfo.uid = MOCK_UID;
305     callerInfo.pid = MOCK_PID;
306     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
307     callerInfo.callerType = CALLER_TYPE_NONE;
308     callerInfo.duid = 0;
309     callerInfo.dmsVersion = 0;
310     callerInfo.accessToken = 0;
311 
312     DTEST_LOG << "DistributedSchedServiceTest get illegal uid" << std::endl;
313     CallerInfo mockCallerInfo = callerInfo;
314     mockCallerInfo.uid = -1;
315     int32_t result = DistributedSchedService::GetInstance().ReleaseAbilityFromRemote(callback,
316         element, mockCallerInfo);
317     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
318 
319     DTEST_LOG << "DistributedSchedServiceTest get illegal sourceDeviceId" << std::endl;
320     mockCallerInfo = callerInfo;
321     mockCallerInfo.sourceDeviceId = "";
322     result = DistributedSchedService::GetInstance().ReleaseAbilityFromRemote(callback,
323         element, mockCallerInfo);
324     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
325 
326     DTEST_LOG << "DistributedSchedServiceTest get illegal duid" << std::endl;
327     mockCallerInfo = callerInfo;
328     mockCallerInfo.duid = -1;
329     result = DistributedSchedService::GetInstance().ReleaseAbilityFromRemote(callback,
330         element, mockCallerInfo);
331     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
332 
333     DTEST_LOG << "DistributedSchedServiceTest get illegal accessToken" << std::endl;
334     mockCallerInfo = callerInfo;
335     mockCallerInfo.accessToken = -1;
336     result = DistributedSchedService::GetInstance().ReleaseAbilityFromRemote(callback,
337         element, mockCallerInfo);
338     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
339 
340         DTEST_LOG << "DistributedSchedServiceTest get illegal dmsVersion" << std::endl;
341     mockCallerInfo = callerInfo;
342     mockCallerInfo.dmsVersion = -1;
343     result = DistributedSchedService::GetInstance().ReleaseAbilityFromRemote(callback,
344         element, mockCallerInfo);
345     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
346     DTEST_LOG << "DistributedSchedServiceTest CallAbility_007 end " << std::endl;
347 }
348 
349 /**
350  * @tc.name: CallAbility_008
351  * @tc.desc: Call StartAbilityByCall with illegal parameter
352  * @tc.type: FUNC
353  */
354 HWTEST_F(DistributedSchedCallTest, CallAbility_008, TestSize.Level1)
355 {
356     DTEST_LOG << "DistributedSchedServiceTest CallAbility_008 start " << std::endl;
357     OHOS::AAFwk::Want want;
358     want.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
359     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
360     sptr<IRemoteObject> callbackWrapper = new AbilityCallWrapperStubTest(callback);
361 
362     DTEST_LOG << "DistributedSchedServiceTest mock illegal ElementName " << std::endl;
363     OHOS::AAFwk::Want mockWant;
364     mockWant.SetElementName(MOCK_DEVICE_ID, "", "abilityTest");
365     int32_t result = DistributedSchedAdapter::GetInstance().ReleaseAbility(callbackWrapper,
366         mockWant.GetElement());
367     EXPECT_NE(result, 0);
368 
369     DTEST_LOG << "DistributedSchedServiceTest mock illegal callback " << std::endl;
370     sptr<IRemoteObject> mockCallbackWrapper = nullptr;
371     result = DistributedSchedAdapter::GetInstance().ReleaseAbility(mockCallbackWrapper,
372         want.GetElement());
373     EXPECT_NE(result, 0);
374 
375     DTEST_LOG << "DistributedSchedServiceTest mock illegal ElementName " << std::endl;
376     mockWant.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "");
377     result = DistributedSchedAdapter::GetInstance().ReleaseAbility(callbackWrapper,
378         mockWant.GetElement());
379     EXPECT_NE(result, 0);
380 
381     DTEST_LOG << "DistributedSchedServiceTest mock illegal ElementName " << std::endl;
382     mockWant.SetElementName("", "ohos.demo.test", "abilityTest");
383     result = DistributedSchedAdapter::GetInstance().ReleaseAbility(callbackWrapper,
384         mockWant.GetElement());
385     EXPECT_NE(result, 0);
386     DTEST_LOG << "DistributedSchedServiceTest CallAbility_008 end " << std::endl;
387 }
388 
389 /**
390  * @tc.name: CallAbility_011
391  * @tc.desc: Call StartRemoteAbilityByCall with illegal callback
392  * @tc.type: FUNC
393  */
394 HWTEST_F(DistributedSchedCallTest, CallAbility_011, TestSize.Level1)
395 {
396     DTEST_LOG << "DistributedSchedServiceTest CallAbility_011 start " << std::endl;
397     OHOS::AAFwk::Want want;
398     want.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
399     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
400     int32_t callerUid = MOCK_UID;
401     int32_t callerPid = MOCK_PID;
402     uint32_t accessToken = 0;
403 
404     DTEST_LOG << "DistributedSchedServiceTest mock illegal callback " << std::endl;
405     sptr<IRemoteObject> illegalCallback = nullptr;
406     int32_t result = DistributedSchedService::GetInstance().StartRemoteAbilityByCall(want,
407         illegalCallback, callerUid, callerPid, accessToken);
408     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
409 
410     DTEST_LOG << "DistributedSchedServiceTest CallAbility_011 end " << std::endl;
411 }
412 
413 /**
414  * @tc.name: CallAbility_012
415  * @tc.desc: Call StartRemoteAbilityByCall with illegal uid
416  * @tc.type: FUNC
417  */
418 HWTEST_F(DistributedSchedCallTest, CallAbility_012, TestSize.Level1)
419 {
420     DTEST_LOG << "DistributedSchedServiceTest CallAbility_012 start " << std::endl;
421     OHOS::AAFwk::Want want;
422     want.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
423     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
424     int32_t callerPid = MOCK_PID;
425     uint32_t accessToken = 0;
426 
427     DTEST_LOG << "DistributedSchedServiceTest mock illegal uid " << std::endl;
428     int32_t illegalUid = -1;
429     int32_t result = DistributedSchedService::GetInstance().StartRemoteAbilityByCall(want,
430         callback, illegalUid, callerPid, accessToken);
431     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
432 
433     DTEST_LOG << "DistributedSchedServiceTest CallAbility_012 end " << std::endl;
434 }
435 
436 /**
437  * @tc.name: CallAbility_013
438  * @tc.desc: Call TryStartRemoteAbilityByCall with illegal callback
439  * @tc.type: FUNC
440  */
441 HWTEST_F(DistributedSchedCallTest, CallAbility_013, TestSize.Level1)
442 {
443     DTEST_LOG << "DistributedSchedServiceTest CallAbility_002 start " << std::endl;
444     OHOS::AAFwk::Want mockWant;
445     mockWant.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
446     CallerInfo callerInfo;
447     callerInfo.uid = MOCK_UID;
448     callerInfo.pid = MOCK_PID;
449     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
450     callerInfo.callerType = CALLER_TYPE_NONE;
451     callerInfo.duid = 0;
452     callerInfo.dmsVersion = 0;
453     callerInfo.accessToken = 0;
454     DTEST_LOG << "DistributedSchedServiceTest mock illegal want " << std::endl;
455     int32_t result = DistributedSchedService::GetInstance().TryStartRemoteAbilityByCall(mockWant,
456         nullptr, callerInfo);
457     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
458     DTEST_LOG << "DistributedSchedServiceTest CallAbility_013 end " << std::endl;
459 }
460 
461 /**
462  * @tc.name: CallAbility_014
463  * @tc.desc: Call TryStartRemoteAbilityByCall with illegal callerInfo
464  * @tc.type: FUNC
465  */
466 HWTEST_F(DistributedSchedCallTest, CallAbility_014, TestSize.Level1)
467 {
468     DTEST_LOG << "DistributedSchedServiceTest CallAbility_002 start " << std::endl;
469     OHOS::AAFwk::Want mockWant;
470     mockWant.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
471     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
472     CallerInfo callerInfo;
473     callerInfo.uid = MOCK_UID;
474     callerInfo.pid = MOCK_PID;
475     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
476     callerInfo.callerType = CALLER_TYPE_NONE;
477     callerInfo.duid = 0;
478     callerInfo.dmsVersion = 0;
479     callerInfo.accessToken = 0;
480 
481     DTEST_LOG << "DistributedSchedServiceTest mock illegal uid " << std::endl;
482     callerInfo.uid = -1;
483     int32_t result = DistributedSchedService::GetInstance().TryStartRemoteAbilityByCall(mockWant,
484         callback, callerInfo);
485     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
486 
487     callerInfo.uid = MOCK_UID;
488     DTEST_LOG << "DistributedSchedServiceTest mock illegal sourceDeviceId " << std::endl;
489     callerInfo.sourceDeviceId = "";
490     result = DistributedSchedService::GetInstance().TryStartRemoteAbilityByCall(mockWant,
491         callback, callerInfo);
492     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
493 
494     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
495     DTEST_LOG << "DistributedSchedServiceTest mock illegal duid " << std::endl;
496     callerInfo.duid = -1;
497     result = DistributedSchedService::GetInstance().TryStartRemoteAbilityByCall(mockWant,
498         callback, callerInfo);
499     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
500 
501     DTEST_LOG << "DistributedSchedServiceTest CallAbility_014 end " << std::endl;
502 }
503 
504 /**
505  * @tc.name: CallAbility_015
506  * @tc.desc: Call StartAbilityByCallFromRemote with illegal callback
507  * @tc.type: FUNC
508  */
509 HWTEST_F(DistributedSchedCallTest, CallAbility_015, TestSize.Level1)
510 {
511     DTEST_LOG << "DistributedSchedServiceTest CallAbility_015 start " << std::endl;
512     OHOS::AAFwk::Want mockWant;
513     mockWant.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
514     CallerInfo callerInfo;
515     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
516     callerInfo.callerType = CALLER_TYPE_NONE;
517     callerInfo.duid = 0;
518     callerInfo.uid = MOCK_UID;
519     callerInfo.pid = MOCK_PID;
520     callerInfo.dmsVersion = 1000;
521     callerInfo.accessToken = 0;
522     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
523     IDistributedSched::AccountInfo accountInfo;
524 
525     DTEST_LOG << "DistributedSchedServiceTest mock illegal callback " << std::endl;
526     std::string srcDeviceId;
527     DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(srcDeviceId);
528     OHOS::AAFwk::Want want;
529     want.SetElementName(srcDeviceId, "ohos.demo.test", "abilityTest");
530     sptr<IRemoteObject> mockCallback = nullptr;
531 
532     int32_t result = DistributedSchedService::GetInstance().StartAbilityByCallFromRemote(want,
533         mockCallback, callerInfo, accountInfo);
534     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
535     DTEST_LOG << "DistributedSchedServiceTest CallAbility_015 end " << std::endl;
536 }
537 
538 /**
539  * @tc.name: CallAbility_016
540  * @tc.desc: Call StartAbilityByCall with illegal callback
541  * @tc.type: FUNC
542  */
543 HWTEST_F(DistributedSchedCallTest, CallAbility_016, TestSize.Level1)
544 {
545     DTEST_LOG << "DistributedSchedServiceTest CallAbility_016 start " << std::endl;
546     OHOS::AAFwk::Want want;
547     want.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
548     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
549     sptr<IRemoteObject> callbackWrapper = new AbilityCallWrapperStubTest(callback);
550     sptr<IRemoteObject> callerToken = new AbilityCallCallbackTest();
551 
552     DTEST_LOG << "DistributedSchedServiceTest mock illegal callback " << std::endl;
553     sptr<IRemoteObject> mockCallbackWrapper = nullptr;
554     int32_t result = DistributedSchedAdapter::GetInstance().StartAbilityByCall(want,
555         mockCallbackWrapper, new AbilityCallCallbackTest());
556     EXPECT_NE(result, 0);
557 
558     DTEST_LOG << "DistributedSchedServiceTest mock illegal token " << std::endl;
559     sptr<IRemoteObject> mockToken = nullptr;
560     result = DistributedSchedAdapter::GetInstance().StartAbilityByCall(want,
561         callbackWrapper, mockToken);
562     EXPECT_NE(result, 0);
563 
564     DTEST_LOG << "DistributedSchedServiceTest CallAbility_016 end " << std::endl;
565 }
566 
567 /**
568  * @tc.name: CallAbility_017
569  * @tc.desc: Call StartAbilityByCall with illegal token
570  * @tc.type: FUNC
571  */
572 HWTEST_F(DistributedSchedCallTest, CallAbility_017, TestSize.Level1)
573 {
574     DTEST_LOG << "DistributedSchedServiceTest CallAbility_017 start " << std::endl;
575     OHOS::AAFwk::Want want;
576     want.SetElementName(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
577     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
578     sptr<IRemoteObject> callbackWrapper = new AbilityCallWrapperStubTest(callback);
579     sptr<IRemoteObject> callerToken = new AbilityCallCallbackTest();
580 
581     DTEST_LOG << "DistributedSchedServiceTest mock illegal token " << std::endl;
582     sptr<IRemoteObject> mockToken = nullptr;
583     int32_t result = DistributedSchedAdapter::GetInstance().StartAbilityByCall(want,
584         callbackWrapper, mockToken);
585     EXPECT_NE(result, 0);
586 
587     DTEST_LOG << "DistributedSchedServiceTest CallAbility_017 end " << std::endl;
588 }
589 
590 /**
591  * @tc.name: CallAbility_018
592  * @tc.desc: Call ReleaseRemoteAbility with illegal callback
593  * @tc.type: FUNC
594  */
595 HWTEST_F(DistributedSchedCallTest, CallAbility_018, TestSize.Level1)
596 {
597     DTEST_LOG << "DistributedSchedServiceTest CallAbility_005 start " << std::endl;
598     AppExecFwk::ElementName element(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
599     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
600 
601     DTEST_LOG << "DistributedSchedServiceTest mock illegal callback " << std::endl;
602     sptr<IRemoteObject> mockCallbackWrapper = nullptr;
603     int32_t result = DistributedSchedService::GetInstance().ReleaseRemoteAbility(mockCallbackWrapper,
604         element);
605     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
606 
607     DTEST_LOG << "DistributedSchedServiceTest CallAbility_018 end " << std::endl;
608 }
609 
610 /**
611  * @tc.name: CallAbility_019
612  * @tc.desc: Call ReleaseRemoteAbility with illegal remote deviceId
613  * @tc.type: FUNC
614  */
615 HWTEST_F(DistributedSchedCallTest, CallAbility_019, TestSize.Level1)
616 {
617     DTEST_LOG << "DistributedSchedServiceTest CallAbility_005 start " << std::endl;
618     AppExecFwk::ElementName element(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
619     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
620 
621     DTEST_LOG << "DistributedSchedServiceTest get remote dms failed " << std::endl;
622     int32_t result = DistributedSchedService::GetInstance().ReleaseRemoteAbility(callback,
623         element);
624     EXPECT_EQ(result, INVALID_PARAMETERS_ERR_CODE);
625 
626     DTEST_LOG << "DistributedSchedServiceTest CallAbility_019 end " << std::endl;
627 }
628 
629 /**
630  * @tc.name: CallAbility_020
631  * @tc.desc: Call ReleaseAbilityFromRemote with illegal callback
632  * @tc.type: FUNC
633  */
634 HWTEST_F(DistributedSchedCallTest, CallAbility_020, TestSize.Level1)
635 {
636     DTEST_LOG << "DistributedSchedServiceTest CallAbility_020 start " << std::endl;
637     sptr<IRemoteObject> callback = new AbilityCallCallbackTest();
638     AppExecFwk::ElementName element(MOCK_DEVICE_ID, "ohos.demo.test", "abilityTest");
639     CallerInfo callerInfo;
640     callerInfo.callerType = CALLER_TYPE_NONE;
641     callerInfo.duid = 1000;
642     callerInfo.dmsVersion = 1000;
643     callerInfo.uid = MOCK_UID;
644     callerInfo.accessToken = 1000;
645     callerInfo.pid = MOCK_PID;
646     callerInfo.sourceDeviceId = MOCK_DEVICE_ID;
647 
648     DTEST_LOG << "DistributedSchedServiceTest mock illegal callback " << std::endl;
649     sptr<IRemoteObject> mockCallbackWrapper = nullptr;
650     int32_t result = DistributedSchedService::GetInstance().ReleaseAbilityFromRemote(mockCallbackWrapper,
651         element, callerInfo);
652     EXPECT_EQ(result, INVALID_REMOTE_PARAMETERS_ERR_CODE);
653 
654     DTEST_LOG << "DistributedSchedServiceTest CallAbility_020 end " << std::endl;
655 }
656 
AddSession(const sptr<IRemoteObject> & connect,const std::string & localDeviceId,const std::string & remoteDeviceId,const AAFwk::Want & want) const657 void DistributedSchedCallTest::AddSession(const sptr<IRemoteObject>& connect,
658     const std::string& localDeviceId, const std::string& remoteDeviceId, const AAFwk::Want& want) const
659 {
660     DTEST_LOG << "DistributedSchedServiceTest call AddSession " << std::endl;
661     if (connect == nullptr) {
662         return;
663     }
664 
665     std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
666     CallerInfo callerInfo;
667     callerInfo.uid = IPCSkeleton::GetCallingUid();
668     callerInfo.pid = IPCSkeleton::GetCallingRealPid();
669     callerInfo.sourceDeviceId = localDeviceId;
670     callerInfo.callerType = CALLER_TYPE_HARMONY;
671     DistributedSchedService::GetInstance().RemoteConnectAbilityMappingLocked(connect, localDeviceId,
672         remoteDeviceId, want.GetElement(), callerInfo, TargetComponent::HARMONY_COMPONENT);
673 }
674 
RemoveSession(const sptr<IRemoteObject> & connect) const675 void DistributedSchedCallTest::RemoveSession(const sptr<IRemoteObject>& connect) const
676 {
677     DTEST_LOG << "DistributedSchedServiceTest call RemoveSession " << std::endl;
678     if (connect == nullptr) {
679         DTEST_LOG << "DistributedSchedServiceTest RemoveSession connect nullptr " << std::endl;
680         return;
681     }
682 
683     std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
684     DistributedSchedService::GetInstance().distributedConnectAbilityMap_.erase(connect);
685 }
686 
AddConnectInfo(const sptr<IRemoteObject> & connect,const std::string & localDeviceId,const std::string & remoteDeviceId) const687 void DistributedSchedCallTest::AddConnectInfo(const sptr<IRemoteObject>& connect,
688     const std::string& localDeviceId, const std::string& remoteDeviceId) const
689 {
690     DTEST_LOG << "DistributedSchedServiceTest call AddConnectInfo " << std::endl;
691     if (connect == nullptr) {
692         DTEST_LOG << "DistributedSchedServiceTest AddConnectInfo connect nullptr " << std::endl;
693         return;
694     }
695 
696     std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
697     CallerInfo callerInfo;
698     callerInfo.uid = IPCSkeleton::GetCallingUid();
699     callerInfo.pid = IPCSkeleton::GetCallingRealPid();
700     callerInfo.sourceDeviceId = localDeviceId;
701     callerInfo.callerType = CALLER_TYPE_HARMONY;
702 
703     sptr<IRemoteObject> callbackWrapper = new AbilityCallWrapperStubTest(connect);
704     ConnectInfo connectInfo {callerInfo, callbackWrapper};
705     DistributedSchedService::GetInstance().connectAbilityMap_.emplace(connect, connectInfo);
706 }
707 
RemoveConnectInfo(const sptr<IRemoteObject> & connect) const708 void DistributedSchedCallTest::RemoveConnectInfo(const sptr<IRemoteObject>& connect) const
709 {
710     DTEST_LOG << "DistributedSchedServiceTest call RemoveConnectInfo " << std::endl;
711     if (connect == nullptr) {
712         DTEST_LOG << "DistributedSchedServiceTest RemoveConnectInfo connect nullptr " << std::endl;
713         return;
714     }
715 
716     std::lock_guard<std::mutex> autoLock(DistributedSchedService::GetInstance().distributedLock_);
717     DistributedSchedService::GetInstance().connectAbilityMap_.erase(connect);
718 }
719 
AddConnectCount(int32_t uid) const720 void DistributedSchedCallTest::AddConnectCount(int32_t uid) const
721 {
722     DTEST_LOG << "DistributedSchedServiceTest call AddConnectCount " << std::endl;
723     if (uid < 0) {
724         DTEST_LOG << "DistributedSchedServiceTest AddConnectCount uid < 0 " << std::endl;
725         return;
726     }
727 
728     auto& trackingUidMap = DistributedSchedService::GetInstance().trackingUidMap_;
729     ++trackingUidMap[uid];
730 }
731 
DecreaseConnectCount(int32_t uid) const732 void DistributedSchedCallTest::DecreaseConnectCount(int32_t uid) const
733 {
734     DTEST_LOG << "DistributedSchedServiceTest call DecreaseConnectCount " << std::endl;
735     if (uid < 0) {
736         DTEST_LOG << "DistributedSchedServiceTest DecreaseConnectCount uid < 0 " << std::endl;
737         return;
738     }
739 
740     DistributedSchedService::GetInstance().DecreaseConnectLocked(uid);
741 }
742 }
743 }