• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include <fstream>
17 #include "gtest/gtest.h"
18 #include "iservice_registry.h"
19 #include "string_ex.h"
20 #include "test_log.h"
21 
22 #define private public
23 #include "local_ability_manager.h"
24 #include "mock_sa_realize.h"
25 #undef private
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace SAFWK {
31 namespace {
32     const std::string TEST_RESOURCE_PATH = "/data/test/resource/safwk/profile/";
33     const std::u16string LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN = u"ohos.localabilitymanager.accessToken";
34     constexpr int VAILD_SAID = 401;
35     constexpr int SAID = 1499;
36     constexpr int MUT_SAID = 9999;
37     constexpr int INVALID_SAID = -1;
38 }
39 
40 class LocalAbilityManagerTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46 };
47 
SetUpTestCase()48 void LocalAbilityManagerTest::SetUpTestCase()
49 {
50     DTEST_LOG << "SetUpTestCase" << std::endl;
51 }
52 
TearDownTestCase()53 void LocalAbilityManagerTest::TearDownTestCase()
54 {
55     DTEST_LOG << "TearDownTestCase" << std::endl;
56 }
57 
SetUp()58 void LocalAbilityManagerTest::SetUp()
59 {
60     DTEST_LOG << "SetUp" << std::endl;
61 }
62 
TearDown()63 void LocalAbilityManagerTest::TearDown()
64 {
65     DTEST_LOG << "TearDown" << std::endl;
66 }
67 
68 /**
69  * @tc.name: CheckTrustSa001
70  * @tc.desc:  CheckTrustSa with not all allow
71  * @tc.type: FUNC
72  */
73 HWTEST_F(LocalAbilityManagerTest, CheckTrustSa001, TestSize.Level1)
74 {
75     /**
76      * @tc.steps: step1. parse multi-sa profile
77      * @tc.expected: step1. return true when load multi-sa profile
78      */
79     bool ret = LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles
80         (TEST_RESOURCE_PATH + "multi_sa_profile.xml");
81     EXPECT_TRUE(ret);
82     /**
83      * @tc.steps: step2. CheckTrustSa with not all allow
84      * @tc.expected: step2. load allowed sa
85      */
86     auto profiles = LocalAbilityManager::GetInstance().profileParser_->GetAllSaProfiles();
87     auto path = TEST_RESOURCE_PATH + "test_trust_not_all_allow.xml";
88     auto process = "test";
89     LocalAbilityManager::GetInstance().CheckTrustSa(path, process, profiles);
90     profiles = LocalAbilityManager::GetInstance().profileParser_->GetAllSaProfiles();
91     EXPECT_EQ(profiles.size(), 2);
92 }
93 
94 /**
95  * @tc.name: CheckTrustSa002
96  * @tc.desc:  CheckTrustSa with all allow
97  * @tc.type: FUNC
98  */
99 HWTEST_F(LocalAbilityManagerTest, CheckTrustSa002, TestSize.Level1)
100 {
101     LocalAbilityManager::GetInstance().profileParser_->ClearResource();
102     /**
103      * @tc.steps: step1. parse multi-sa profile
104      * @tc.expected: step1. return true when load multi-sa profile
105      */
106     bool ret = LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles
107         (TEST_RESOURCE_PATH + "multi_sa_profile.xml");
108     EXPECT_TRUE(ret);
109     /**
110      * @tc.steps: step2. CheckTrustSa with all allow
111      * @tc.expected: step2. load all sa
112      */
113     auto profiles = LocalAbilityManager::GetInstance().profileParser_->GetAllSaProfiles();
114     auto path = TEST_RESOURCE_PATH + "test_trust_all_allow.xml";
115     auto process = "test";
116     LocalAbilityManager::GetInstance().CheckTrustSa(path, process, profiles);
117     auto result = LocalAbilityManager::GetInstance().profileParser_->GetAllSaProfiles();
118     EXPECT_EQ(result.size(), 4);
119 }
120 
121 /**
122  * @tc.name: DoStartSAProcess001
123  * @tc.desc:  DoStartSAProcess001
124  * @tc.type: FUNC
125  */
126 HWTEST_F(LocalAbilityManagerTest, DoStartSAProcess001, TestSize.Level2)
127 {
128     LocalAbilityManager::GetInstance().DoStartSAProcess("profile_audio.xml", SAID);
129     sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
130     EXPECT_TRUE(sm != nullptr);
131     auto ability = sm->GetSystemAbility(SAID);
132     EXPECT_TRUE(ability == nullptr);
133 }
134 
135 /**
136  * @tc.name: DoStartSAProcess002
137  * @tc.desc:  DoStartSAProcess002
138  * @tc.type: FUNC
139  */
140 HWTEST_F(LocalAbilityManagerTest, DoStartSAProcess002, TestSize.Level2)
141 {
142     string profilePath = "/system/usr/profile_audio.xml";
143     LocalAbilityManager::GetInstance().DoStartSAProcess(profilePath, SAID);
144     sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
145     auto ability = sm->GetSystemAbility(SAID);
146     EXPECT_EQ(ability, nullptr);
147 }
148 
149 /**
150  * @tc.name: DoStartSAProcess003
151  * @tc.desc:  DoStartSAProcess, InitializeSaProfiles failed!
152  * @tc.type: FUNC
153  */
154 HWTEST_F(LocalAbilityManagerTest, DoStartSAProcess003, TestSize.Level2)
155 {
156     string profilePath = "/system/usr/profile_audio.xml";
157     int32_t invalidSaid = -2;
158     LocalAbilityManager::GetInstance().DoStartSAProcess(profilePath, invalidSaid);
159     sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
160     auto ability = sm->GetSystemAbility(invalidSaid);
161     EXPECT_EQ(ability, nullptr);
162 }
163 
164 /**
165  * @tc.name: GetTraceTag001
166  * @tc.desc:  GetTraceTag
167  * @tc.type: FUNC
168  */
169 HWTEST_F(LocalAbilityManagerTest, GetTraceTag001, TestSize.Level1)
170 {
171     string profilePath = "/system/usr/profile_audio.xml";
172     string traceTag = LocalAbilityManager::GetInstance().GetTraceTag(profilePath);
173     EXPECT_EQ(traceTag, "profile_audio");
174 }
175 
176 /**
177  * @tc.name: GetTraceTag002
178  * @tc.desc:  GetTraceTag
179  * @tc.type: FUNC
180  */
181 HWTEST_F(LocalAbilityManagerTest, GetTraceTag002, TestSize.Level1)
182 {
183     string profilePath = "";
184     string traceTag = LocalAbilityManager::GetInstance().GetTraceTag(profilePath);
185     EXPECT_EQ(traceTag, "default_proc");
186 }
187 
188 /**
189  * @tc.name: GetTraceTag003
190  * @tc.desc:  GetTraceTag
191  * @tc.type: FUNC
192  */
193 HWTEST_F(LocalAbilityManagerTest, GetTraceTag003, TestSize.Level1)
194 {
195     string profilePath = "/system/usr/test";
196     string traceTag = LocalAbilityManager::GetInstance().GetTraceTag(profilePath);
197     EXPECT_EQ(traceTag, "test");
198 }
199 
200 /**
201  * @tc.name: CheckAndGetProfilePath001
202  * @tc.desc:  CheckAndGetProfilePath
203  * @tc.type: FUNC
204  */
205 HWTEST_F(LocalAbilityManagerTest, CheckAndGetProfilePath001, TestSize.Level3)
206 {
207     string profilePath = "/system/usr/profile_audio.xml";
208     string realProfilePath = "";
209     bool res = LocalAbilityManager::GetInstance().CheckAndGetProfilePath(profilePath, realProfilePath);
210     EXPECT_TRUE(res);
211 }
212 
213 /**
214  * @tc.name: CheckAndGetProfilePath002
215  * @tc.desc:  CheckAndGetProfilePath, xmlDoc dir is not matched!
216  * @tc.type: FUNC
217  */
218 HWTEST_F(LocalAbilityManagerTest, CheckAndGetProfilePath002, TestSize.Level1)
219 {
220     string profilePath = TEST_RESOURCE_PATH + "test_trust_all_allow.xml";
221     string realProfilePath = "";
222     bool res = LocalAbilityManager::GetInstance().CheckAndGetProfilePath(profilePath, realProfilePath);
223     EXPECT_FALSE(res);
224 }
225 
226 /**
227  * @tc.name: CheckSystemAbilityManagerReady001
228  * @tc.desc:  CheckSystemAbilityManagerReady, return true!
229  * @tc.type: FUNC
230  */
231 HWTEST_F(LocalAbilityManagerTest, CheckSystemAbilityManagerReady001, TestSize.Level3)
232 {
233     string profilePath = TEST_RESOURCE_PATH + "test_trust_all_allow.xml";
234     string realProfilePath = "";
235     bool res = LocalAbilityManager::GetInstance().CheckSystemAbilityManagerReady();
236     EXPECT_TRUE(res);
237 }
238 
239 /**
240  * @tc.name: InitSystemAbilityProfiles001
241  * @tc.desc:  InitSystemAbilityProfiles, ParseSaProfiles failed!
242  * @tc.type: FUNC
243  */
244 HWTEST_F(LocalAbilityManagerTest, InitSystemAbilityProfiles001, TestSize.Level1)
245 {
246     string profilePath = "";
247     bool res = LocalAbilityManager::GetInstance().InitSystemAbilityProfiles(profilePath, SAID);
248     EXPECT_FALSE(res);
249 }
250 
251 /**
252  * @tc.name: InitSystemAbilityProfiles002
253  * @tc.desc:  InitSystemAbilityProfiles, return true!
254  * @tc.type: FUNC
255  */
256 HWTEST_F(LocalAbilityManagerTest, InitSystemAbilityProfiles002, TestSize.Level3)
257 {
258     string profilePath = "/system/usr/profile_audio.xml";
259     bool res = LocalAbilityManager::GetInstance().InitSystemAbilityProfiles(profilePath, SAID);
260     EXPECT_TRUE(res);
261 }
262 
263 /**
264  * @tc.name: InitSystemAbilityProfiles003
265  * @tc.desc:  InitSystemAbilityProfiles, return true!
266  * @tc.type: FUNC
267  */
268 HWTEST_F(LocalAbilityManagerTest, InitSystemAbilityProfiles003, TestSize.Level3)
269 {
270     string profilePath = "/system/usr/profile_audio.xml";
271     int32_t defaultId = -1;
272     bool res = LocalAbilityManager::GetInstance().InitSystemAbilityProfiles(profilePath, defaultId);
273     EXPECT_TRUE(res);
274 }
275 
276 /**
277  * @tc.name: AddAbility001
278  * @tc.desc: AddAbility, try to add null ability!
279  * @tc.type: FUNC
280  */
281 HWTEST_F(LocalAbilityManagerTest, AddAbility001, TestSize.Level3)
282 {
283     string path = "";
284     string process = "process";
285     std::list<SaProfile> saInfos;
286     LocalAbilityManager::GetInstance().CheckTrustSa(path, process, saInfos);
287     bool res = LocalAbilityManager::GetInstance().AddAbility(nullptr);
288     EXPECT_FALSE(res);
289 }
290 
291 /**
292  * @tc.name: AddAbility002
293  * @tc.desc: AddAbility, try to add existed ability
294  * @tc.type: FUNC
295  */
296 HWTEST_F(LocalAbilityManagerTest, AddAbility002, TestSize.Level3)
297 {
298     MockSaRealize *sysAby = new MockSaRealize(MUT_SAID, false);
299     bool ret = LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles
300         (TEST_RESOURCE_PATH + "multi_sa_profile.xml");
301     EXPECT_TRUE(ret);
302     LocalAbilityManager::GetInstance().abilityMap_[MUT_SAID] = sysAby;
303     bool res = LocalAbilityManager::GetInstance().AddAbility(sysAby);
304     LocalAbilityManager::GetInstance().abilityMap_.clear();
305     delete sysAby;
306     EXPECT_FALSE(res);
307 }
308 
309 /**
310  * @tc.name: RemoveAbility001
311  * @tc.desc: RemoveAbility, invalid systemAbilityId
312  * @tc.type: FUNC
313  */
314 HWTEST_F(LocalAbilityManagerTest, RemoveAbility001, TestSize.Level1)
315 {
316     bool res = LocalAbilityManager::GetInstance().RemoveAbility(INVALID_SAID);
317     EXPECT_FALSE(res);
318 }
319 
320 /**
321  * @tc.name: RemoveAbility002
322  * @tc.desc: RemoveAbility, invalid systemAbilityId
323  * @tc.type: FUNC
324  */
325 HWTEST_F(LocalAbilityManagerTest, RemoveAbility002, TestSize.Level3)
326 {
327     bool res = LocalAbilityManager::GetInstance().RemoveAbility(SAID);
328     EXPECT_TRUE(res);
329 }
330 
331 /**
332  * @tc.name: AddSystemAbilityListener001
333  * @tc.desc: AddSystemAbilityListener, SA or listenerSA invalid
334  * @tc.type: FUNC
335  */
336 HWTEST_F(LocalAbilityManagerTest, AddSystemAbilityListener001, TestSize.Level1)
337 {
338     bool res = LocalAbilityManager::GetInstance().AddSystemAbilityListener(SAID, INVALID_SAID);
339     EXPECT_FALSE(res);
340 }
341 
342 /**
343  * @tc.name: AddSystemAbilityListener002
344  * @tc.desc: AddSystemAbilityListener, SA or listenerSA invalid
345  * @tc.type: FUNC
346  */
347 HWTEST_F(LocalAbilityManagerTest, AddSystemAbilityListener002, TestSize.Level1)
348 {
349     bool res = LocalAbilityManager::GetInstance().AddSystemAbilityListener(INVALID_SAID, SAID);
350     EXPECT_FALSE(res);
351 }
352 
353 /**
354  * @tc.name: AddSystemAbilityListener003
355  * @tc.desc: AddSystemAbilityListener, return true
356  * @tc.type: FUNC
357  */
358 HWTEST_F(LocalAbilityManagerTest, AddSystemAbilityListener003, TestSize.Level1)
359 {
360     LocalAbilityManager::GetInstance().listenerMap_[SAID].push_back(MUT_SAID);
361     bool res = LocalAbilityManager::GetInstance().AddSystemAbilityListener(SAID, SAID);
362     EXPECT_TRUE(res);
363 }
364 
365 /**
366  * @tc.name: AddSystemAbilityListener004
367  * @tc.desc: AddSystemAbilityListener, return true
368  * @tc.type: FUNC
369  */
370 HWTEST_F(LocalAbilityManagerTest, AddSystemAbilityListener004, TestSize.Level1)
371 {
372     LocalAbilityManager::GetInstance().listenerMap_[VAILD_SAID].push_back(VAILD_SAID);
373     LocalAbilityManager::GetInstance().listenerMap_[VAILD_SAID].push_back(SAID);
374     sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
375     bool res = LocalAbilityManager::GetInstance().AddSystemAbilityListener(VAILD_SAID, VAILD_SAID);
376     EXPECT_TRUE(res);
377 }
378 
379 /**
380  * @tc.name: RemoveSystemAbilityListener001
381  * @tc.desc: RemoveSystemAbilityListener, SA or listenerSA invalid
382  * @tc.type: FUNC
383  */
384 HWTEST_F(LocalAbilityManagerTest, RemoveSystemAbilityListener001, TestSize.Level1)
385 {
386     bool res = LocalAbilityManager::GetInstance().RemoveSystemAbilityListener(INVALID_SAID, SAID);
387     EXPECT_FALSE(res);
388 }
389 
390 /**
391  * @tc.name: RemoveSystemAbilityListener002
392  * @tc.desc: RemoveSystemAbilityListener, SA or listenerSA invalid
393  * @tc.type: FUNC
394  */
395 HWTEST_F(LocalAbilityManagerTest, RemoveSystemAbilityListener002, TestSize.Level1)
396 {
397     bool res = LocalAbilityManager::GetInstance().RemoveSystemAbilityListener(SAID, INVALID_SAID);
398     EXPECT_FALSE(res);
399 }
400 
401 /**
402  * @tc.name: RemoveSystemAbilityListener003
403  * @tc.desc: RemoveSystemAbilityListener, return true
404  * @tc.type: FUNC
405  */
406 HWTEST_F(LocalAbilityManagerTest, RemoveSystemAbilityListener003, TestSize.Level1)
407 {
408     LocalAbilityManager::GetInstance().listenerMap_.clear();
409     bool res = LocalAbilityManager::GetInstance().RemoveSystemAbilityListener(SAID, SAID);
410     EXPECT_TRUE(res);
411 }
412 
413 /**
414  * @tc.name: RemoveSystemAbilityListener004
415  * @tc.desc: RemoveSystemAbilityListener, return true
416  * @tc.type: FUNC
417  */
418 HWTEST_F(LocalAbilityManagerTest, RemoveSystemAbilityListener004, TestSize.Level3)
419 {
420     LocalAbilityManager::GetInstance().listenerMap_[SAID].push_back(MUT_SAID);
421     bool res = LocalAbilityManager::GetInstance().RemoveSystemAbilityListener(SAID, SAID);
422     EXPECT_TRUE(res);
423 }
424 
425 /**
426  * @tc.name: OnStartAbility001
427  * @tc.desc: OnStartAbility, return true
428  * @tc.type: FUNC
429  */
430 HWTEST_F(LocalAbilityManagerTest, OnStartAbility001, TestSize.Level1)
431 {
432     std::string deviceId = "";
433     int32_t addAbility = 1;
434     MockSaRealize *sysAby = new MockSaRealize(MUT_SAID, false);
435     LocalAbilityManager::GetInstance().abilityMap_[MUT_SAID] = sysAby;
436     LocalAbilityManager::GetInstance().NotifyAbilityListener(SAID, MUT_SAID, deviceId, addAbility);
437     LocalAbilityManager::GetInstance().abilityMap_[SAID] = sysAby;
438     LocalAbilityManager::GetInstance().NotifyAbilityListener(SAID, MUT_SAID, deviceId, addAbility);
439     bool res = LocalAbilityManager::GetInstance().OnStartAbility(SAID);
440     LocalAbilityManager::GetInstance().abilityMap_.clear();
441     delete sysAby;
442     EXPECT_TRUE(res);
443 }
444 
445 /**
446  * @tc.name: OnStartAbility002
447  * @tc.desc: OnStartAbility, return false
448  * @tc.type: FUNC
449  */
450 HWTEST_F(LocalAbilityManagerTest, OnStartAbility002, TestSize.Level1)
451 {
452     std::string deviceId = "";
453     int32_t removeAbility = 2;
454     int32_t otherAbility = 3;
455     MockSaRealize *sysAby = new MockSaRealize(MUT_SAID, false);
456     LocalAbilityManager::GetInstance().abilityMap_[MUT_SAID] = sysAby;
457     LocalAbilityManager::GetInstance().abilityMap_[SAID] = sysAby;
458     LocalAbilityManager::GetInstance().NotifyAbilityListener(SAID, MUT_SAID, deviceId, removeAbility);
459     LocalAbilityManager::GetInstance().NotifyAbilityListener(SAID, MUT_SAID, deviceId, otherAbility);
460     LocalAbilityManager::GetInstance().abilityMap_.clear();
461     bool res = LocalAbilityManager::GetInstance().OnStartAbility(SAID);
462     EXPECT_FALSE(res);
463 }
464 
465 /**
466  * @tc.name: GetAbility001
467  * @tc.desc: GetAbility, SA not register
468  * @tc.type: FUNC
469  */
470 HWTEST_F(LocalAbilityManagerTest, GetAbility001, TestSize.Level1)
471 {
472     std::string deviceId = "";
473     int32_t removeAbility = 2;
474     LocalAbilityManager::GetInstance().abilityMap_.clear();
475     SystemAbility* res = LocalAbilityManager::GetInstance().GetAbility(SAID);
476     LocalAbilityManager::GetInstance().FindAndNotifyAbilityListeners(SAID, deviceId, removeAbility);
477     EXPECT_EQ(res, nullptr);
478 }
479 
480 /**
481  * @tc.name: GetAbility002
482  * @tc.desc: GetAbility, SA not register
483  * @tc.type: FUNC
484  */
485 HWTEST_F(LocalAbilityManagerTest, GetAbility002, TestSize.Level1)
486 {
487     std::string deviceId = "";
488     int32_t removeAbility = 2;
489     MockSaRealize *sysAby = new MockSaRealize(SAID, false);
490     LocalAbilityManager::GetInstance().abilityMap_[SAID] = sysAby;
491     SystemAbility* res = LocalAbilityManager::GetInstance().GetAbility(SAID);
492     LocalAbilityManager::GetInstance().listenerMap_[SAID].push_back(MUT_SAID);
493     LocalAbilityManager::GetInstance().FindAndNotifyAbilityListeners(SAID, deviceId, removeAbility);
494     LocalAbilityManager::GetInstance().abilityMap_.clear();
495     delete sysAby;
496     EXPECT_NE(res, nullptr);
497 }
498 
499 /**
500  * @tc.name: GetRunningStatus001
501  * @tc.desc: GetRunningStatus, return false
502  * @tc.type: FUNC
503  */
504 HWTEST_F(LocalAbilityManagerTest, GetRunningStatus001, TestSize.Level1)
505 {
506     std::string deviceId = "";
507     int32_t removeAbility = 2;
508     bool res = LocalAbilityManager::GetInstance().GetRunningStatus(SAID);
509     LocalAbilityManager::GetInstance().listenerMap_[SAID].clear();
510     LocalAbilityManager::GetInstance().FindAndNotifyAbilityListeners(SAID, deviceId, removeAbility);
511     EXPECT_FALSE(res);
512 }
513 
514 /**
515  * @tc.name: GetRunningStatus002
516  * @tc.desc: GetRunningStatus, return true
517  * @tc.type: FUNC
518  */
519 HWTEST_F(LocalAbilityManagerTest, GetRunningStatus002, TestSize.Level1)
520 {
521     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
522     LocalAbilityManager::GetInstance().abilityMap_[SAID] = mockSa;
523     bool res = LocalAbilityManager::GetInstance().GetRunningStatus(SAID);
524     LocalAbilityManager::GetInstance().abilityMap_.clear();
525     delete mockSa;
526     EXPECT_FALSE(res);
527 }
528 
529 /**
530  * @tc.name: StartOndemandSystemAbility001
531  * @tc.desc: StartOndemandSystemAbility
532  * @tc.type: FUNC
533  */
534 HWTEST_F(LocalAbilityManagerTest, StartOndemandSystemAbility001, TestSize.Level1)
535 {
536     std::string profilePath = "/system/usr/profile_audio.xml";
537     LocalAbilityManager::GetInstance().profileParser_->saProfiles_.clear();
538     LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles(profilePath);
539     LocalAbilityManager::GetInstance().StartOndemandSystemAbility(SAID);
540     EXPECT_EQ(LocalAbilityManager::GetInstance().profileParser_->saProfiles_.size(), 3);
541 }
542 
543 /**
544  * @tc.name: StartOndemandSystemAbility002
545  * @tc.desc: StartOndemandSystemAbility
546  * @tc.type: FUNC
547  */
548 HWTEST_F(LocalAbilityManagerTest, StartOndemandSystemAbility002, TestSize.Level1)
549 {
550     std::string profilePath = "/system/usr/profile_audio.xml";
551     LocalAbilityManager::GetInstance().abilityMap_.clear();
552     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
553     LocalAbilityManager::GetInstance().profileParser_->saProfiles_.clear();
554     LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles(profilePath);
555     LocalAbilityManager::GetInstance().abilityMap_[SAID] = mockSa;
556     LocalAbilityManager::GetInstance().StartOndemandSystemAbility(SAID);
557     delete mockSa;
558     EXPECT_EQ(LocalAbilityManager::GetInstance().profileParser_->saProfiles_.size(), 3);
559 }
560 
561 /**
562  * @tc.name: InitializeSaProfiles001
563  * @tc.desc: InitializeSaProfiles, sa profile is empty
564  * @tc.type: FUNC
565  */
566 HWTEST_F(LocalAbilityManagerTest, InitializeSaProfiles001, TestSize.Level1)
567 {
568     LocalAbilityManager::GetInstance().profileParser_->saProfiles_.clear();
569     bool res = LocalAbilityManager::GetInstance().InitializeSaProfiles(INVALID_SAID);
570     EXPECT_FALSE(res);
571 }
572 
573 /**
574  * @tc.name: InitializeSaProfiles002
575  * @tc.desc: InitializeSaProfiles
576  * @tc.type: FUNC
577  */
578 HWTEST_F(LocalAbilityManagerTest, InitializeSaProfiles002, TestSize.Level1)
579 {
580     LocalAbilityManager::GetInstance().abilityMap_.clear();
581     bool res = LocalAbilityManager::GetInstance().InitializeSaProfiles(SAID);
582     EXPECT_FALSE(res);
583 }
584 
585 /**
586  * @tc.name: InitializeRunOnCreateSaProfiles001
587  * @tc.desc: InitializeSaProfiles
588  * @tc.type: FUNC
589  */
590 HWTEST_F(LocalAbilityManagerTest, InitializeRunOnCreateSaProfiles001, TestSize.Level1)
591 {
592     LocalAbilityManager::GetInstance().profileParser_->saProfiles_.clear();
593     bool res = LocalAbilityManager::GetInstance().InitializeRunOnCreateSaProfiles();
594     EXPECT_FALSE(res);
595 }
596 
597 /**
598  * @tc.name: InitializeRunOnCreateSaProfiles002
599  * @tc.desc: InitializeSaProfiles
600  * @tc.type: FUNC
601  */
602 HWTEST_F(LocalAbilityManagerTest, InitializeRunOnCreateSaProfiles002, TestSize.Level1)
603 {
604     std::string profilePath = "/system/usr/profile_audio.xml";
605     LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles(profilePath);
606     bool res = LocalAbilityManager::GetInstance().InitializeRunOnCreateSaProfiles();
607     EXPECT_TRUE(res);
608 }
609 
610 /**
611  * @tc.name: InitializeRunOnCreateSaProfiles003
612  * @tc.desc: InitializeSaProfiles
613  * @tc.type: FUNC
614  */
615 HWTEST_F(LocalAbilityManagerTest, InitializeRunOnCreateSaProfiles003, TestSize.Level1)
616 {
617     std::string profilePath = "/system/usr/profile_audio.xml";
618     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
619     LocalAbilityManager::GetInstance().abilityMap_[SAID] = mockSa;
620     LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles(profilePath);
621     bool res = LocalAbilityManager::GetInstance().InitializeRunOnCreateSaProfiles();
622     delete mockSa;
623     EXPECT_TRUE(res);
624 }
625 
626 /**
627  * @tc.name: InitializeOnDemandSaProfile001
628  * @tc.desc: InitializeOnDemandSaProfile
629  * @tc.type: FUNC
630  */
631 HWTEST_F(LocalAbilityManagerTest, InitializeOnDemandSaProfile001, TestSize.Level1)
632 {
633     std::string profilePath = "/system/usr/profile_audio.xml";
634     LocalAbilityManager::GetInstance().abilityMap_.clear();
635     LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles(profilePath);
636     bool res = LocalAbilityManager::GetInstance().InitializeOnDemandSaProfile(SAID);
637     EXPECT_FALSE(res);
638 }
639 
640 /**
641  * @tc.name: InitializeOnDemandSaProfile002
642  * @tc.desc: InitializeOnDemandSaProfile
643  * @tc.type: FUNC
644  */
645 HWTEST_F(LocalAbilityManagerTest, InitializeOnDemandSaProfile002, TestSize.Level1)
646 {
647     LocalAbilityManager::GetInstance().profileParser_->saProfiles_.clear();
648     bool res = LocalAbilityManager::GetInstance().InitializeOnDemandSaProfile(SAID);
649     EXPECT_FALSE(res);
650 }
651 
652 /**
653  * @tc.name: InitializeSaProfilesInnerLocked001
654  * @tc.desc: InitializeSaProfilesInnerLocked, SA not found!
655  * @tc.type: FUNC
656  */
657 HWTEST_F(LocalAbilityManagerTest, InitializeSaProfilesInnerLocked001, TestSize.Level1)
658 {
659     SaProfile saProfile;
660     saProfile.saId = SAID;
661     LocalAbilityManager::GetInstance().abilityMap_.clear();
662     bool res = LocalAbilityManager::GetInstance().InitializeSaProfilesInnerLocked(saProfile);
663     EXPECT_FALSE(res);
664 }
665 
666 /**
667  * @tc.name: InitializeSaProfilesInnerLocked002
668  * @tc.desc: InitializeSaProfilesInnerLocked, SA is null!
669  * @tc.type: FUNC
670  */
671 HWTEST_F(LocalAbilityManagerTest, InitializeSaProfilesInnerLocked002, TestSize.Level1)
672 {
673     SaProfile saProfile;
674     saProfile.saId = SAID;
675     LocalAbilityManager::GetInstance().abilityMap_[SAID] = nullptr;
676     bool res = LocalAbilityManager::GetInstance().InitializeSaProfilesInnerLocked(saProfile);
677     EXPECT_FALSE(res);
678 }
679 
680 /**
681  * @tc.name: InitializeSaProfilesInnerLocked003
682  * @tc.desc: InitializeSaProfilesInnerLocked, return true!
683  * @tc.type: FUNC
684  */
685 HWTEST_F(LocalAbilityManagerTest, InitializeSaProfilesInnerLocked003, TestSize.Level1)
686 {
687     SaProfile saProfile;
688     saProfile.saId = SAID;
689     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
690     LocalAbilityManager::GetInstance().abilityMap_[SAID] = mockSa;
691     bool res = LocalAbilityManager::GetInstance().InitializeSaProfilesInnerLocked(saProfile);
692     LocalAbilityManager::GetInstance().abilityMap_.clear();
693     delete mockSa;
694     EXPECT_TRUE(res);
695 }
696 
697 /**
698  * @tc.name: InitializeSaProfilesInnerLocked004
699  * @tc.desc: InitializeSaProfilesInnerLocked, return true!
700  * @tc.type: FUNC
701  */
702 HWTEST_F(LocalAbilityManagerTest, InitializeSaProfilesInnerLocked004, TestSize.Level1)
703 {
704     SaProfile saProfile;
705     saProfile.saId = SAID;
706     const std::u16string bootStart = u"BootStartPhase";
707     saProfile.bootPhase = bootStart;
708     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
709     LocalAbilityManager::GetInstance().abilityMap_[SAID] = mockSa;
710     bool res = LocalAbilityManager::GetInstance().InitializeSaProfilesInnerLocked(saProfile);
711     LocalAbilityManager::GetInstance().abilityMap_.clear();
712     delete mockSa;
713     EXPECT_TRUE(res);
714 }
715 
716 /**
717  * @tc.name: InitializeSaProfilesInnerLocked005
718  * @tc.desc: InitializeSaProfilesInnerLocked, return true!
719  * @tc.type: FUNC
720  */
721 HWTEST_F(LocalAbilityManagerTest, InitializeSaProfilesInnerLocked005, TestSize.Level1)
722 {
723     SaProfile saProfile;
724     saProfile.saId = SAID;
725     const std::u16string coreStart = u"CoreStartPhase";
726     saProfile.bootPhase = coreStart;
727     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
728     LocalAbilityManager::GetInstance().abilityMap_[SAID] = mockSa;
729     bool res = LocalAbilityManager::GetInstance().InitializeSaProfilesInnerLocked(saProfile);
730     LocalAbilityManager::GetInstance().abilityMap_.clear();
731     delete mockSa;
732     EXPECT_TRUE(res);
733 }
734 
735 /**
736  * @tc.name: CheckDependencyStatus001
737  * @tc.desc: CheckDependencyStatus, return size is 1!
738  * @tc.type: FUNC
739  */
740 HWTEST_F(LocalAbilityManagerTest, CheckDependencyStatus001, TestSize.Level1)
741 {
742     std::string profilePath = "/system/usr/profile_audio.xml";
743     vector<std::u16string> dependSa;
744     dependSa.push_back(u"1499");
745     bool ret = LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles(profilePath);
746     EXPECT_TRUE(ret);
747     LocalAbilityManager::GetInstance().RegisterOnDemandSystemAbility(SAID);
748     vector<std::u16string> res = LocalAbilityManager::GetInstance().CheckDependencyStatus(dependSa);
749     EXPECT_EQ(res.size(), 1);
750 }
751 
752 /**
753  * @tc.name: CheckDependencyStatus002
754  * @tc.desc: CheckDependencyStatus, return size is 0!
755  * @tc.type: FUNC
756  */
757 HWTEST_F(LocalAbilityManagerTest, CheckDependencyStatus002, TestSize.Level1)
758 {
759     vector<std::u16string> dependSa;
760     dependSa.push_back(u"-1");
761     vector<std::u16string> res = LocalAbilityManager::GetInstance().CheckDependencyStatus(dependSa);
762     EXPECT_EQ(res.size(), 0);
763 }
764 
765 /**
766  * @tc.name: CheckDependencyStatus003
767  * @tc.desc: CheckDependencyStatus, return size is 0!
768  * @tc.type: FUNC
769  */
770 HWTEST_F(LocalAbilityManagerTest, CheckDependencyStatus003, TestSize.Level1)
771 {
772     vector<std::u16string> dependSa;
773     dependSa.push_back(u"401");
774     vector<std::u16string> res = LocalAbilityManager::GetInstance().CheckDependencyStatus(dependSa);
775     EXPECT_EQ(res.size(), 0);
776 }
777 
778 /**
779  * @tc.name: NeedRegisterOnDemand001
780  * @tc.desc: NeedRegisterOnDemand, return false!
781  * @tc.type: FUNC
782  */
783 HWTEST_F(LocalAbilityManagerTest, NeedRegisterOnDemand001, TestSize.Level1)
784 {
785     SaProfile saProfile;
786     saProfile.runOnCreate = true;
787     LocalAbilityManager::GetInstance().StartSystemAbilityTask(nullptr);
788     bool res = LocalAbilityManager::GetInstance().NeedRegisterOnDemand(saProfile, INVALID_SAID);
789     EXPECT_FALSE(res);
790 }
791 
792 /**
793  * @tc.name: NeedRegisterOnDemand002
794  * @tc.desc: NeedRegisterOnDemand, return true!
795  * @tc.type: FUNC
796  */
797 HWTEST_F(LocalAbilityManagerTest, NeedRegisterOnDemand002, TestSize.Level1)
798 {
799     SaProfile saProfile;
800     saProfile.runOnCreate = false;
801     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
802     LocalAbilityManager::GetInstance().StartSystemAbilityTask(mockSa);
803     bool res = LocalAbilityManager::GetInstance().NeedRegisterOnDemand(saProfile, INVALID_SAID);
804     delete mockSa;
805     EXPECT_TRUE(res);
806 }
807 
808 /**
809  * @tc.name: NeedRegisterOnDemand003
810  * @tc.desc: NeedRegisterOnDemand, return true!
811  * @tc.type: FUNC
812  */
813 HWTEST_F(LocalAbilityManagerTest, NeedRegisterOnDemand003, TestSize.Level3)
814 {
815     SaProfile saProfile;
816     saProfile.saId = INVALID_SAID;
817     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
818     vector<std::u16string> dependSa;
819     dependSa.push_back(u"1499");
820     mockSa->SetDependSa(dependSa);
821     mockSa->SetDependTimeout(200);
822     LocalAbilityManager::GetInstance().StartSystemAbilityTask(mockSa);
823     LocalAbilityManager::GetInstance().RegisterOnDemandSystemAbility(SAID);
824     bool res = LocalAbilityManager::GetInstance().NeedRegisterOnDemand(saProfile, SAID);
825     delete mockSa;
826     EXPECT_TRUE(res);
827 }
828 
829 /**
830  * @tc.name: Run001
831  * @tc.desc: Run, return true!
832  * @tc.type: FUNC
833  */
834 HWTEST_F(LocalAbilityManagerTest, Run001, TestSize.Level3)
835 {
836     std::list<SystemAbility*> systemAbilityList;
837     MockSaRealize *mockSa = new MockSaRealize(SAID, false);
838     vector<std::u16string> dependSa;
839     dependSa.push_back(u"-1");
840     mockSa->SetDependSa(dependSa);
841     mockSa->SetDependTimeout(200);
842     LocalAbilityManager::GetInstance().startTaskNum_ = 1;
843     LocalAbilityManager::GetInstance().StartSystemAbilityTask(mockSa);
844     LocalAbilityManager::GetInstance().StartPhaseTasks(systemAbilityList);
845     bool res = LocalAbilityManager::GetInstance().Run(SAID);
846     delete mockSa;
847     EXPECT_FALSE(res);
848 }
849 
850 /**
851  * @tc.name: AddLocalAbilityManager001
852  * @tc.desc: AddLocalAbilityManager, return false!
853  * @tc.type: FUNC
854  */
855 HWTEST_F(LocalAbilityManagerTest, AddLocalAbilityManager001, TestSize.Level3)
856 {
857     std::list<SystemAbility*> systemAbilityList;
858     systemAbilityList.push_back(nullptr);
859     LocalAbilityManager::GetInstance().StartPhaseTasks(systemAbilityList);
860     LocalAbilityManager::GetInstance().procName_ = u"";
861     bool res = LocalAbilityManager::GetInstance().AddLocalAbilityManager();
862     EXPECT_FALSE(res);
863 }
864 
865 /**
866  * @tc.name: AddLocalAbilityManager002
867  * @tc.desc: AddLocalAbilityManager, return true!
868  * @tc.type: FUNC
869  */
870 HWTEST_F(LocalAbilityManagerTest, AddLocalAbilityManager002, TestSize.Level3)
871 {
872     LocalAbilityManager::GetInstance().abilityPhaseMap_.clear();
873     LocalAbilityManager::GetInstance().FindAndStartPhaseTasks();
874     LocalAbilityManager::GetInstance().procName_ = u"test";
875     bool res = LocalAbilityManager::GetInstance().AddLocalAbilityManager();
876     EXPECT_FALSE(res);
877 }
878 
879 /**
880  * @tc.name: FoundationRestart001
881  * @tc.desc:  FoundationRestart001
882  * @tc.type: FUNC
883  * @tc.require: I5N9IY
884  */
885 HWTEST_F(LocalAbilityManagerTest, FoundationRestart001, TestSize.Level3)
886 {
887     std::ifstream foundationCfg;
888     foundationCfg.open("/etc/init/foundation.cfg", std::ios::in);
889     ASSERT_TRUE(foundationCfg.is_open());
890     std::string cfg = "";
891     char ch;
892     while (foundationCfg.get(ch)) {
893         cfg.push_back(ch);
894     }
895     foundationCfg.close();
896     EXPECT_TRUE(cfg.find("critical") == std::string::npos);
897 }
898 
899 /**
900  * @tc.name: OnRemoteRequest001
901  * @tc.desc: OnRemoteRequest001
902  * @tc.type: FUNC
903  */
904 HWTEST_F(LocalAbilityManagerTest, OnRemoteRequest001, TestSize.Level2)
905 {
906     MessageParcel data;
907     MessageParcel reply;
908     MessageOption option;
909     std::string deviceId = "";
910     LocalAbilityManager::SystemAbilityListener *sysListener = new LocalAbilityManager::SystemAbilityListener();
911     sysListener->OnAddSystemAbility(SAID, deviceId);
912     int32_t result = LocalAbilityManager::GetInstance().OnRemoteRequest(0, data, reply, option);
913     delete sysListener;
914     EXPECT_NE(result, ERR_NONE);
915 }
916 
917 /**
918  * @tc.name: OnRemoteRequest002
919  * @tc.desc: OnRemoteRequest002
920  * @tc.type: FUNC
921  */
922 HWTEST_F(LocalAbilityManagerTest, OnRemoteRequest002, TestSize.Level2)
923 {
924     MessageParcel data;
925     data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN);
926     MessageParcel reply;
927     MessageOption option;
928     std::string deviceId = "";
929     LocalAbilityManager::SystemAbilityListener *sysListener = new LocalAbilityManager::SystemAbilityListener();
930     sysListener->OnAddSystemAbility(INVALID_SAID, deviceId);
931     int32_t result = LocalAbilityManager::GetInstance().OnRemoteRequest(0, data, reply, option);
932     delete sysListener;
933     EXPECT_NE(result, ERR_NONE);
934 }
935 
936 /**
937  * @tc.name: OnRemoteRequest003
938  * @tc.desc: OnRemoteRequest003
939  * @tc.type: FUNC
940  */
941 HWTEST_F(LocalAbilityManagerTest, OnRemoteRequest003, TestSize.Level2)
942 {
943     MessageParcel data;
944     data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN);
945     MessageParcel reply;
946     MessageOption option;
947     std::string deviceId = "";
948     LocalAbilityManager::SystemAbilityListener *sysListener = new LocalAbilityManager::SystemAbilityListener();
949     sysListener->OnRemoveSystemAbility(INVALID_SAID, deviceId);
950     int32_t result = LocalAbilityManager::GetInstance().OnRemoteRequest(1, data, reply, option);
951     delete sysListener;
952     EXPECT_NE(result, ERR_NONE);
953 }
954 /**
955  * @tc.name: OnRemoteRequest004
956  * @tc.desc: OnRemoteRequest004
957  * @tc.type: FUNC
958  */
959 HWTEST_F(LocalAbilityManagerTest, OnRemoteRequest004, TestSize.Level2)
960 {
961     MessageParcel data;
962     data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN);
963     data.WriteInt32(1);
964     MessageParcel reply;
965     MessageOption option;
966     std::string deviceId = "";
967     LocalAbilityManager::SystemAbilityListener *sysListener = new LocalAbilityManager::SystemAbilityListener();
968     sysListener->OnRemoveSystemAbility(SAID, deviceId);
969     int32_t result = LocalAbilityManager::GetInstance().OnRemoteRequest(1, data, reply, option);
970     delete sysListener;
971     EXPECT_EQ(result, ERR_NONE);
972 }
973 } // namespace SAFWK
974 } // namespace OHOS
975