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