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