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