1 /*
2  * Copyright (c) 2021-2023 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 "system_ability_mgr_test.h"
17 #include "samgr_err_code.h"
18 #include "hisysevent_adapter.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "itest_transaction_service.h"
22 #include "sam_mock_permission.h"
23 #include "parameter.h"
24 #include "parameters.h"
25 #include "sa_profiles.h"
26 #include "sa_status_change_mock.h"
27 #include "string_ex.h"
28 #include "system_ability_definition.h"
29 #include "if_local_ability_manager.h"
30 #include "system_process_status_change_proxy.h"
31 #include "test_log.h"
32 #define private public
33 #include "system_ability_manager.h"
34 #ifdef SUPPORT_COMMON_EVENT
35 #include "common_event_collect.h"
36 #include "ipc_skeleton.h"
37 #include "accesstoken_kit.h"
38 #endif
39 
40 using namespace std;
41 using namespace testing;
42 using namespace testing::ext;
43 using namespace OHOS;
44 
45 namespace OHOS {
46 namespace system {
47     /*
48     * Returns true if the system parameter `key` has the value "1", "y", "yes", "on", or "true",
49     * false for "0", "n", "no", "off", or "false", or `def` otherwise.
50     */
GetBoolParameter(const std::string & key,bool def)51     bool GetBoolParameter(const std::string &key, bool def)
52     {
53         return true;
54     }
55 
GetParameter(const std::string & key,const std::string & def)56     std::string GetParameter(const std::string &key, const std::string &def)
57     {
58         return "";
59     }
60 
SetParameter(const std::string & key,const std::string & value)61     bool SetParameter(const std::string &key, const std::string &value)
62     {
63         return true;
64     }
65 }
66 namespace {
67 constexpr int32_t SAID = 1234;
68 constexpr int32_t OTHER_ON_DEMAND = 3;
69 constexpr int32_t TEST_VALUE = 2021;
70 constexpr int32_t TEST_REVERSE_VALUE = 1202;
71 constexpr int32_t REPEAT = 10;
72 constexpr int32_t OVERFLOW_TIME = 257;
73 constexpr int32_t TEST_OVERFLOW_SAID = 99999;
74 constexpr int32_t TEST_EXCEPTION_HIGH_SA_ID = LAST_SYS_ABILITY_ID + 1;
75 constexpr int32_t TEST_EXCEPTION_LOW_SA_ID = -1;
76 constexpr int32_t TEST_SYSTEM_ABILITY1 = 1491;
77 constexpr int32_t TEST_SYSTEM_ABILITY2 = 1492;
78 constexpr int32_t SHFIT_BIT = 32;
79 constexpr int32_t ONDEMAND_SLEEP_TIME = 600 * 1000; // us
80 constexpr int32_t MAX_COUNT = INT32_MAX - 1000000;
81 constexpr int64_t ONDEMAND_EXTRA_DATA_ID = 1;
82 
SaProfileStore(sptr<SystemAbilityManager> & saMgr,map<int32_t,SaProfile> & saProfileMapTmp,int32_t maxLoop)83 void SaProfileStore(sptr<SystemAbilityManager>& saMgr,
84     map<int32_t, SaProfile>& saProfileMapTmp, int32_t maxLoop)
85 {
86     for (int32_t loop = 0; loop < maxLoop; ++loop) {
87         if (saMgr->saProfileMap_.count(SAID + loop) > 0) {
88             saProfileMapTmp[SAID + loop] = saMgr->saProfileMap_[SAID + loop];
89         }
90     }
91     return;
92 }
93 
SaProfileRecover(sptr<SystemAbilityManager> & saMgr,map<int32_t,SaProfile> saProfileMapTmp,int32_t maxLoop)94 void SaProfileRecover(sptr<SystemAbilityManager>& saMgr,
95     map<int32_t, SaProfile> saProfileMapTmp, int32_t maxLoop)
96 {
97     for (int32_t loop = 0; loop < maxLoop; ++loop) {
98         if (saProfileMapTmp.count(SAID + loop) > 0) {
99             saMgr->saProfileMap_[SAID + loop] = saProfileMapTmp[SAID + loop];
100         } else {
101             saMgr->saProfileMap_.erase(SAID + loop);
102         }
103     }
104     return;
105 }
106 
SaProfileExtensionTestPrevSet(sptr<SystemAbilityManager> & saMgr,int32_t maxLoop)107 void SaProfileExtensionTestPrevSet(sptr<SystemAbilityManager>& saMgr, int32_t maxLoop)
108 {
109     const int32_t mod_num = 2;
110     std::vector<std::string> extensionVec = { "backup_test", "restore_test", "alpha", "beta" };
111     for (int32_t loop = 0; loop < maxLoop; ++loop) {
112         SaProfile saProfile;
113         saProfile.process = Str8ToStr16(extensionVec[loop]);
114         saProfile.extension.push_back(extensionVec[loop % mod_num]);
115         saProfile.cacheCommonEvent = true;
116         if (loop >= (maxLoop - mod_num)) {
117             saProfile.extension.push_back(extensionVec[(loop + 1) % mod_num]);
118         }
119         saMgr->saProfileMap_[SAID + loop] = saProfile;
120     }
121     return;
122 }
123 
SaAbilityMapObjTestPrevSet(sptr<SystemAbilityManager> & saMgr,int32_t maxLoop)124 void SaAbilityMapObjTestPrevSet(sptr<SystemAbilityManager>& saMgr, int32_t maxLoop)
125 {
126     for (int32_t loop = 0; loop < maxLoop; ++loop) {
127         if (loop == 0) {
128             continue;
129         }
130         sptr<IRemoteObject> testAbility(new SaStatusChangeMock());
131         SAInfo saInfo;
132         saInfo.remoteObj = testAbility;
133         saMgr->abilityMap_[SAID + loop] = saInfo;
134     }
135 
136     return;
137 }
138 
ProcMapObjTestPrevSet(sptr<SystemAbilityManager> & saMgr,int32_t maxLoop)139 void ProcMapObjTestPrevSet(sptr<SystemAbilityManager>& saMgr, int32_t maxLoop)
140 {
141     std::vector<std::string> extensionVec = { "backup_test", "restore_test", "alpha", "beta" };
142     for (int32_t loop = 0; loop < maxLoop; ++loop) {
143         if (loop == 0) {
144             continue;
145         }
146         sptr<IRemoteObject> testAbility(new SaStatusChangeMock());
147         saMgr->systemProcessMap_[Str8ToStr16(extensionVec[loop])] = testAbility;
148     }
149     return;
150 }
151 }
152 
153 /**
154  * @tc.name: GetLocalNodeId001
155  * @tc.desc: test GetLocalNodeId
156  * @tc.type: FUNC
157  * @tc.require: I6NKWX
158  */
159 HWTEST_F(SystemAbilityMgrTest, GetLocalNodeId001, TestSize.Level3)
160 {
161     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
162     string ret = saMgr->GetLocalNodeId();
163     EXPECT_EQ(ret, "");
164 }
165 
166 /**
167  * @tc.name: ReportGetSAPeriodically001
168  * @tc.desc: test ReportGetSAPeriodically with saFrequencyMap_ is not empty
169  * @tc.type: FUNC
170  * @tc.require: I6NKWX
171  */
172 HWTEST_F(SystemAbilityMgrTest, ReportGetSAPeriodically001, TestSize.Level3)
173 {
174     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
175     uint64_t pid_said = 123;
176     int32_t count = 1;
177     saMgr->saFrequencyMap_.clear();
178     saMgr->saFrequencyMap_[pid_said] = count;
179     saMgr->ReportGetSAPeriodically();
180     EXPECT_TRUE(saMgr->saFrequencyMap_.empty());
181 }
182 
183 /**
184  * @tc.name: UnloadSystemAbility004
185  * @tc.desc: test UnloadSystemAbility004, abilityStateScheduler_ is nullptr
186  * @tc.type: FUNC
187  * @tc.require: I6J4T7
188  */
189 HWTEST_F(SystemAbilityMgrTest, UnloadSystemAbility004, TestSize.Level3)
190 {
191     DTEST_LOG << " UnloadSystemAbility004 " << std::endl;
192     SamMockPermission::MockProcess("memmgrservice");
193     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
194     SaProfile saProfile;
195     saProfile.process = u"memmgrservice";
196     saMgr->saProfileMap_[1] = saProfile;
197     saMgr->abilityStateScheduler_ = nullptr;
198     int32_t systemAbilityId = 1;
199     int32_t ret = saMgr->UnloadSystemAbility(systemAbilityId);
200     saMgr->abilityStateScheduler_ = make_shared<SystemAbilityStateScheduler>();
201     EXPECT_EQ(ret, STATE_SCHEDULER_NULL);
202 }
203 
204 /**
205  * @tc.name: StartDynamicSystemProcess
206  * @tc.desc: start invalid process, return fail.
207  * @tc.type: FUNC
208  */
209 HWTEST_F(SystemAbilityMgrTest, StartDynamicSystemProcess001, TestSize.Level3)
210 {
211     cout << "begin StartDynamicSystemProcess001 "<< endl;
212     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
213     EXPECT_TRUE(saMgr != nullptr);
214     std::u16string invalidProcess = u"1234567890123456789012345678901234567890123456789"
215         "01234567890123456789012345678901234567890123456";
216     OnDemandEvent event;
217     int result = saMgr->StartDynamicSystemProcess(invalidProcess, 100, event);
218     cout << "begin StartDynamicSystemProcess001 result is "<< result << endl;
219     EXPECT_EQ(result, 102);
220 }
221 
222 /**
223  * @tc.desc: test GetOnDemandPolicy with OnDemandPolicyType is valid
224  * @tc.type: FUNC
225  * @tc.require: I6V4AX
226  */
227 HWTEST_F(SystemAbilityMgrTest, GetOnDemandPolicy001, TestSize.Level3)
228 {
229     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
230     int32_t systemAbilityId = 1;
231     OnDemandPolicyType type = OnDemandPolicyType::START_POLICY;
232     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
233     int32_t ret = saMgr->GetOnDemandPolicy(systemAbilityId, type, abilityOnDemandEvents);
234     EXPECT_EQ(ERR_INVALID_VALUE, ret);
235 }
236 
237 /**
238  * @tc.desc: test GetOnDemandPolicy with OnDemandPolicyType is valid
239  * @tc.type: FUNC
240  * @tc.require: I6V4AX
241  */
242 HWTEST_F(SystemAbilityMgrTest, GetOnDemandPolicy002, TestSize.Level3)
243 {
244     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
245     int32_t systemAbilityId = 1;
246     OnDemandPolicyType type = OnDemandPolicyType::STOP_POLICY;
247     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
248     sptr<DeviceStatusCollectManager> collectManager_ = nullptr;
249     int32_t ret = saMgr->GetOnDemandPolicy(systemAbilityId, type, abilityOnDemandEvents);
250     EXPECT_EQ(ERR_INVALID_VALUE, ret);
251 }
252 
253 HWTEST_F(SystemAbilityMgrTest, GetOnDemandPolicy003, TestSize.Level3)
254 {
255     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
256     int32_t systemAbilityId = 1;
257     OnDemandPolicyType type = OnDemandPolicyType::START_POLICY;
258     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
259     Security::AccessToken::NativeTokenInfo nativeTokenInfo;
260     int32_t tokenInfoResult = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
261     SaProfile saProfile;
262     saProfile.process = Str8ToStr16(nativeTokenInfo.processName);
263     saProfile.startOnDemand.allowUpdate = false;
264     saMgr->saProfileMap_[1] = saProfile;
265     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
266     int32_t ret = saMgr->GetOnDemandPolicy(systemAbilityId, type, abilityOnDemandEvents);
267     EXPECT_EQ(ERR_PERMISSION_DENIED, ret);
268 }
269 
270 HWTEST_F(SystemAbilityMgrTest, GetOnDemandPolicy004, TestSize.Level3)
271 {
272     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
273     int32_t systemAbilityId = 1;
274     OnDemandPolicyType type = OnDemandPolicyType::START_POLICY;
275     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
276     Security::AccessToken::NativeTokenInfo nativeTokenInfo;
277     int32_t tokenInfoResult = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
278     SaProfile saProfile;
279     saProfile.process = Str8ToStr16(nativeTokenInfo.processName);
280     saProfile.startOnDemand.allowUpdate = true;
281     saMgr->saProfileMap_[1] = saProfile;
282     sptr<DeviceStatusCollectManager> collectManager = nullptr;
283     saMgr->collectManager_ = collectManager;
284     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
285     int32_t ret = saMgr->GetOnDemandPolicy(systemAbilityId, type, abilityOnDemandEvents);
286     EXPECT_EQ(ERR_INVALID_VALUE, ret);
287 }
288 
289 
290 HWTEST_F(SystemAbilityMgrTest, GetOnDemandPolicy005, TestSize.Level3)
291 {
292     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
293     int32_t systemAbilityId = 1;
294     OnDemandPolicyType type = OnDemandPolicyType::START_POLICY;
295     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
296     Security::AccessToken::NativeTokenInfo nativeTokenInfo;
297     int32_t tokenInfoResult = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
298     SaProfile saProfile;
299     saProfile.process = Str8ToStr16(nativeTokenInfo.processName);
300     saProfile.startOnDemand.allowUpdate = true;
301     saMgr->saProfileMap_[1] = saProfile;
302     sptr<DeviceStatusCollectManager> collectManager = new DeviceStatusCollectManager();
303     saMgr->collectManager_ = collectManager;
304     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
305     int32_t ret = saMgr->GetOnDemandPolicy(systemAbilityId, type, abilityOnDemandEvents);
306     EXPECT_EQ(ERR_INVALID_VALUE, ret);
307 }
308 
309 HWTEST_F(SystemAbilityMgrTest, GetOnDemandPolicy006, TestSize.Level3)
310 {
311     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
312     int32_t systemAbilityId = 1;
313     OnDemandPolicyType type = OnDemandPolicyType::START_POLICY;
314     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
315     Security::AccessToken::NativeTokenInfo nativeTokenInfo;
316     int32_t tokenInfoResult = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
317     SaProfile saProfile;
318     saProfile.process = Str8ToStr16(nativeTokenInfo.processName);
319     saProfile.saId = 1;
320     saProfile.startOnDemand.allowUpdate = true;
321     vector<OnDemandEvent> onDemandEvents;
322     onDemandEvents.push_back({ 1, "test" });
323     saProfile.startOnDemand.onDemandEvents = onDemandEvents;
324     saMgr->saProfileMap_[1] = saProfile;
325     sptr<DeviceStatusCollectManager> collectManager = new DeviceStatusCollectManager();
326     collectManager->onDemandSaProfiles_.emplace_back(saProfile);
327     saMgr->collectManager_ = collectManager;
328     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
329     int32_t ret = saMgr->GetOnDemandPolicy(systemAbilityId, type, abilityOnDemandEvents);
330     EXPECT_EQ(ERR_OK, ret);
331 }
332 
333 
334 /**
335  * @tc.name: UpdateOnDemandPolicy001
336  * @tc.desc: test UpdateOnDemandPolicy with OnDemandPolicyType is valid
337  * @tc.type: FUNC
338  * @tc.require: I6V4AX
339  */
340 HWTEST_F(SystemAbilityMgrTest, UpdateOnDemandPolicy001, TestSize.Level3)
341 {
342     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
343     OnDemandPolicyType type = OnDemandPolicyType::START_POLICY;
344     SystemAbilityOnDemandEvent event = {OnDemandEventId::COMMON_EVENT, "TEST", "TEST"};
345     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
346     abilityOnDemandEvents.emplace_back(event);
347     SaProfile saProfile = {u"test", TEST_OVERFLOW_SAID};
348     saMgr->saProfileMap_[TEST_OVERFLOW_SAID] = saProfile;
349     int32_t ret = saMgr->UpdateOnDemandPolicy(TEST_OVERFLOW_SAID, type, abilityOnDemandEvents);
350     EXPECT_EQ(ret, ERR_INVALID_VALUE);
351 
352     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
353     Security::AccessToken::NativeTokenInfo nativeTokenInfo;
354     int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
355     EXPECT_TRUE(result == ERR_OK);
356     saMgr->saProfileMap_[TEST_OVERFLOW_SAID].process = Str8ToStr16(nativeTokenInfo.processName);
357     saMgr->saProfileMap_[TEST_OVERFLOW_SAID].startOnDemand.allowUpdate = false;
358     ret = saMgr->UpdateOnDemandPolicy(TEST_OVERFLOW_SAID, type, abilityOnDemandEvents);
359     EXPECT_EQ(ret, ERR_PERMISSION_DENIED);
360 
361     saMgr->saProfileMap_[TEST_OVERFLOW_SAID].startOnDemand.allowUpdate = true;
362     sptr<DeviceStatusCollectManager> collectMgr = saMgr->collectManager_;
363     saMgr->collectManager_ = nullptr;
364     ret = saMgr->UpdateOnDemandPolicy(TEST_OVERFLOW_SAID, type, abilityOnDemandEvents);
365     EXPECT_EQ(ret, ERR_INVALID_VALUE);
366 
367     saMgr->collectManager_ = collectMgr;
368     ret = saMgr->UpdateOnDemandPolicy(TEST_OVERFLOW_SAID, type, abilityOnDemandEvents);
369     EXPECT_EQ(ret, ERR_INVALID_VALUE);
370     saMgr->saProfileMap_.erase(TEST_OVERFLOW_SAID);
371 }
372 
373 /**
374  * @tc.name: GetOnDemandReasonExtraData001
375  * @tc.desc: test GetOnDemandReasonExtraData with collectManager_ is nullptr
376  * @tc.type: FUNC
377  * @tc.require: I6XB42
378  */
379 
380 HWTEST_F(SystemAbilityMgrTest, GetOnDemandReasonExtraData001, TestSize.Level3)
381 {
382     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
383     MessageParcel messageParcel;
384     sptr<DeviceStatusCollectManager> collectMgr = saMgr->collectManager_;
385     saMgr->collectManager_ = nullptr;
386     int32_t ret = saMgr->GetOnDemandReasonExtraData(ONDEMAND_EXTRA_DATA_ID, messageParcel);
387     EXPECT_EQ(ret, ERR_INVALID_VALUE);
388 
389     saMgr->collectManager_ = collectMgr;
390     ret = saMgr->GetOnDemandReasonExtraData(ONDEMAND_EXTRA_DATA_ID, messageParcel);
391     EXPECT_EQ(ret, ERR_INVALID_VALUE);
392 }
393 
394 /**
395  * @tc.name: GetOnDemandReasonExtraData002
396  * @tc.desc: test GetOnDemandReasonExtraData with extraDataId is not exist
397  * @tc.type: FUNC
398  * @tc.require: I6XB42
399  */
400 
401 HWTEST_F(SystemAbilityMgrTest, GetOnDemandReasonExtraData002, TestSize.Level3)
402 {
403     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
404     sptr<DeviceStatusCollectManager> collectManager = new DeviceStatusCollectManager();
405     collectManager->collectPluginMap_.clear();
406     saMgr->collectManager_ = collectManager;
407     MessageParcel messageParcel;
408     int32_t ret = saMgr->GetOnDemandReasonExtraData(ONDEMAND_EXTRA_DATA_ID, messageParcel);
409     EXPECT_EQ(ret, ERR_INVALID_VALUE);
410 }
411 
412 /**
413  * @tc.name: GetOnDemandReasonExtraData003
414  * @tc.desc: call GetOnDemandReasonExtraData, get extraData
415  * @tc.type: FUNC
416  * @tc.require: I6XB42
417  */
418 
419 HWTEST_F(SystemAbilityMgrTest, GetOnDemandReasonExtraData003, TestSize.Level3)
420 {
421     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
422     sptr<DeviceStatusCollectManager> collectManager = new DeviceStatusCollectManager();
423     saMgr->collectManager_ = collectManager;
424     sptr<CommonEventCollect> commonEventCollect = new CommonEventCollect(collectManager);
425     commonEventCollect->workHandler_ = std::make_shared<CommonHandler>(commonEventCollect);
426     collectManager->collectPluginMap_.clear();
427     collectManager->collectPluginMap_[COMMON_EVENT] = commonEventCollect;
428     EventFwk::CommonEventData eventData;
429     commonEventCollect->SaveOnDemandReasonExtraData(eventData);
430     MessageParcel messageParcel;
431     int32_t ret = saMgr->GetOnDemandReasonExtraData(ONDEMAND_EXTRA_DATA_ID, messageParcel);
432     EXPECT_EQ(ret, ERR_OK);
433 }
434 
435 /**
436  * @tc.name: GetSystemAbilityWithDevice001
437  * @tc.desc: get invalid system ability.
438  * @tc.type: FUNC
439  */
440 HWTEST_F(SystemAbilityMgrTest, GetSystemAbilityWithDevice001, TestSize.Level3)
441 {
442     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
443     EXPECT_TRUE(saMgr != nullptr);
444     std::string deviceId = "";
445     auto ability = saMgr->GetSystemAbility(TEST_EXCEPTION_LOW_SA_ID, deviceId);
446     EXPECT_EQ(ability, nullptr);
447 
448     SaProfile saProfile = {u"test", TEST_OVERFLOW_SAID};
449     saMgr->saProfileMap_[TEST_OVERFLOW_SAID] = saProfile;
450     ability = saMgr->GetSystemAbility(TEST_OVERFLOW_SAID, deviceId);
451     EXPECT_EQ(ability, nullptr);
452 
453     saMgr->saProfileMap_[TEST_OVERFLOW_SAID].distributed = true;
454     ability = saMgr->GetSystemAbility(TEST_OVERFLOW_SAID, deviceId);
455     EXPECT_EQ(ability, nullptr);
456     saMgr->saProfileMap_.erase(TEST_OVERFLOW_SAID);
457 }
458 
459 /**
460  * @tc.name: GetSystemAbilityFromRemote001
461  * @tc.desc: get invalid system ability.
462  * @tc.type: FUNC
463  */
464 HWTEST_F(SystemAbilityMgrTest, GetSystemAbilityFromRemote001, TestSize.Level3)
465 {
466     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
467     EXPECT_TRUE(saMgr != nullptr);
468     auto ability = saMgr->GetSystemAbilityFromRemote(TEST_EXCEPTION_LOW_SA_ID);
469     EXPECT_EQ(ability, nullptr);
470 }
471 
472 /**
473  * @tc.name: GetSystemAbilityFromRemote002
474  * @tc.desc: get not exist system ability.
475  * @tc.type: FUNC
476  */
477 HWTEST_F(SystemAbilityMgrTest, GetSystemAbilityFromRemote002, TestSize.Level3)
478 {
479     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
480     EXPECT_TRUE(saMgr != nullptr);
481     auto ability = saMgr->GetSystemAbilityFromRemote(TEST_SYSTEM_ABILITY1);
482     EXPECT_EQ(ability, nullptr);
483 }
484 
485 /**
486  * @tc.name: GetSystemAbilityFromRemote003
487  * @tc.desc: get exist system ability.
488  * @tc.type: FUNC
489  */
490 HWTEST_F(SystemAbilityMgrTest, GetSystemAbilityFromRemote003, TestSize.Level3)
491 {
492     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
493     EXPECT_TRUE(saMgr != nullptr);
494     SAInfo saInfo;
495     saMgr->abilityMap_[1] = saInfo;
496     auto ability = saMgr->GetSystemAbilityFromRemote(1);
497     EXPECT_EQ(ability, nullptr);
498 }
499 
500 /**
501  * @tc.name: GetSystemAbilityFromRemote004
502  * @tc.desc: get exist system ability, isDistributed is true.
503  * @tc.type: FUNC
504  */
505 HWTEST_F(SystemAbilityMgrTest, GetSystemAbilityFromRemote004, TestSize.Level3)
506 {
507     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
508     EXPECT_TRUE(saMgr != nullptr);
509     SAInfo saInfo;
510     saInfo.isDistributed = true;
511     saMgr->abilityMap_[1] = saInfo;
512     auto ability = saMgr->GetSystemAbilityFromRemote(1);
513     EXPECT_EQ(ability, nullptr);
514 }
515 
516 /**
517  * @tc.name: GetDBinder001
518  * @tc.desc: GetDBinder, return null
519  * @tc.type: FUNC
520  * @tc.require: I7VEPG
521  */
522 
523 HWTEST_F(SystemAbilityMgrTest, GetDBinder001, TestSize.Level3)
524 {
525     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
526     EXPECT_TRUE(saMgr != nullptr);
527     sptr<DBinderService> result = saMgr->GetDBinder();
528     EXPECT_TRUE(saMgr != nullptr);
529 }
530 
531 /**
532  * @tc.name: NotifyRpcLoadCompleted001
533  * @tc.desc: test NotifyRpcLoadCompleted, workHandler_ is nullptr
534  * @tc.type: FUNC
535  * @tc.require: I6MO6A
536  */
537 
538 HWTEST_F(SystemAbilityMgrTest, NotifyRpcLoadCompleted001, TestSize.Level3)
539 {
540     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
541     EXPECT_TRUE(saMgr != nullptr);
542     sptr<IRemoteObject> testAbility = new TestTransactionService();
543     saMgr->workHandler_ = nullptr;
544     saMgr->NotifyRpcLoadCompleted("", 1, testAbility);
545     EXPECT_TRUE(saMgr != nullptr);
546 }
547 
548 /**
549  * @tc.name: NotifyRpcLoadCompleted003
550  * @tc.desc: test NotifyRpcLoadCompleted, dBinderService_ is null
551  * @tc.type: FUNC
552  * @tc.require: I6MO6A
553  */
554 
555 HWTEST_F(SystemAbilityMgrTest, NotifyRpcLoadCompleted003, TestSize.Level3)
556 {
557     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
558     EXPECT_TRUE(saMgr != nullptr);
559     sptr<IRemoteObject> testAbility = new TestTransactionService();
560     saMgr->dBinderService_ = nullptr;
561     saMgr->NotifyRpcLoadCompleted("", 1, testAbility);
562     EXPECT_TRUE(saMgr != nullptr);
563 }
564 
565 /**
566  * @tc.name: NotifyRpcLoadCompleted004
567  * @tc.desc: test NotifyRpcLoadCompleted
568  * @tc.type: FUNC
569  * @tc.require: I6MO6A
570  */
571 HWTEST_F(SystemAbilityMgrTest, NotifyRpcLoadCompleted004, TestSize.Level3)
572 {
573     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
574     EXPECT_TRUE(saMgr != nullptr);
575     sptr<IRemoteObject> testAbility = new TestTransactionService();
576     saMgr->NotifyRpcLoadCompleted("", 1, testAbility);
577     EXPECT_TRUE(saMgr != nullptr);
578 }
579 
580 /**
581  * @tc.name: UnloadAllIdleSystemAbility001
582  * @tc.desc: UnloadAllIdleSystemAbility process is memmgrservice
583  * @tc.type: FUNC
584  * @tc.require: I6NKWX
585  */
586 HWTEST_F(SystemAbilityMgrTest, UnloadAllIdleSystemAbility001, TestSize.Level3)
587 {
588     SamMockPermission::MockProcess("memmgrservice");
589     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
590     saMgr->abilityStateScheduler_ = make_shared<SystemAbilityStateScheduler>();
591     int32_t ret = saMgr->UnloadAllIdleSystemAbility();
592     EXPECT_EQ(ret, ERR_OK);
593 }
594 
595 /**
596  * @tc.name: UnloadAllIdleSystemAbility002
597  * @tc.desc: UnloadAllIdleSystemAbility abilityStateScheduler_ is null
598  * @tc.type: FUNC
599  * @tc.require: I6NKWX
600  */
601 HWTEST_F(SystemAbilityMgrTest, UnloadAllIdleSystemAbility002, TestSize.Level3)
602 {
603     SamMockPermission::MockProcess("memmgrservice");
604     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
605     saMgr->abilityStateScheduler_ = nullptr;
606     int32_t ret = saMgr->UnloadAllIdleSystemAbility();
607     saMgr->abilityStateScheduler_ = make_shared<SystemAbilityStateScheduler>();
608     EXPECT_EQ(ret, ERR_INVALID_VALUE);
609 }
610 
611 /**
612  * @tc.name: UnloadAllIdleSystemAbility003
613  * @tc.desc: UnloadAllIdleSystemAbility process is not memmgrservice
614  * @tc.type: FUNC
615  * @tc.require: I6NKWX
616  */
617 HWTEST_F(SystemAbilityMgrTest, UnloadAllIdleSystemAbility003, TestSize.Level3)
618 {
619     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
620     int32_t ret = saMgr->UnloadAllIdleSystemAbility();
621     EXPECT_EQ(ret, ERR_PERMISSION_DENIED);
622 }
623 
624 /**
625  * @tc.name: Dump001
626  * @tc.desc: call Dump, return ERR_OK
627  * @tc.type: FUNC
628  * @tc.require: I7VEPG
629  */
630 
631 HWTEST_F(SystemAbilityMgrTest, Dump001, TestSize.Level3)
632 {
633     SamMockPermission::MockProcess("hidumper_service");
634     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
635     vector<std::u16string> args;
636     args.push_back(u"test_name");
637     int32_t result = saMgr->Dump(1, args);
638     EXPECT_EQ(result, ERR_OK);
639 }
640 
641 /**
642  * @tc.name: Dump002
643  * @tc.desc: call Dump, return ERR_OK
644  * @tc.type: FUNC
645  * @tc.require: I7VEPG
646  */
647 
648 HWTEST_F(SystemAbilityMgrTest, Dump002, TestSize.Level3)
649 {
650     SamMockPermission::MockProcess("hidumper_service");
651     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
652     vector<std::u16string> args;
653     vector<std::string> argsWithStr8;
654     args.push_back(u"test_name");
655     argsWithStr8.push_back("--ffrt");
656     int32_t result = saMgr->Dump(1, args);
657     EXPECT_EQ(result, ERR_OK);
658 }
659 
660 /**
661  * @tc.name: Dump003
662  * @tc.desc: call Dump.
663  * @tc.type: FUNC
664  * @tc.require: I7VEPG
665  */
666 
667 HWTEST_F(SystemAbilityMgrTest, Dump003, TestSize.Level3)
668 {
669     SamMockPermission::MockProcess("hidumper_service");
670     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
671     vector<std::u16string> args;
672     args.push_back(u"--ipc");
673     args.push_back(u"all");
674     int32_t result = saMgr->Dump(1, args);
675     EXPECT_EQ(result, ERR_INVALID_VALUE);
676 }
677 
678 /**
679  * @tc.name: Dump004
680  * @tc.desc: call Dump.
681  * @tc.type: FUNC
682  * @tc.require: I7VEPG
683  */
684 
685 HWTEST_F(SystemAbilityMgrTest, Dump004, TestSize.Level3)
686 {
687     SamMockPermission::MockProcess("hidumper_service");
688     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
689     vector<std::u16string> args;
690     args.push_back(u"--ffrt");
691     args.push_back(u"99999");
692     int32_t result = saMgr->Dump(1, args);
693     EXPECT_EQ(result, ERR_OK);
694 }
695 
696 /**
697  * @tc.name: AddSamgrToAbilityMap001
698  * @tc.desc: call AddSamgrToAbilityMap, return ERR_OK
699  * @tc.type: FUNC
700  * @tc.require: I7VEPG
701  */
702 
703 HWTEST_F(SystemAbilityMgrTest, AddSamgrToAbilityMap001, TestSize.Level3)
704 {
705     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
706     saMgr->AddSamgrToAbilityMap();
707     vector<std::u16string> args;
708     args.push_back(u"test_name");
709     int32_t result = saMgr->Dump(1, args);
710     EXPECT_EQ(result, ERR_OK);
711 }
712 
713 /**
714  * @tc.name: GetCommonEventExtraIdList001
715  * @tc.desc: call GetCommonEventExtraIdList001, return ERR_OK
716  * @tc.type: FUNC
717  * @tc.require: I7VEPG
718  */
719 HWTEST_F(SystemAbilityMgrTest, GetCommonEventExtraIdList001, TestSize.Level3)
720 {
721     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
722 
723     const int32_t maxLoop = 4;
724     map<int32_t, SaProfile> saProfileMapTmp;
725     SaProfileStore(saMgr, saProfileMapTmp, maxLoop);
726     SaProfileExtensionTestPrevSet(saMgr, maxLoop);
727     OnDemandEvent event;
728     std::list<SaControlInfo> saControlList;
729     saMgr->collectManager_->SaveCacheCommonEventSaExtraId(event, saControlList);
730 
731     event.eventId = COMMON_EVENT;
732     event.extraDataId = 1000000;
733     SaControlInfo info;
734     info.saId = SAID;
735     info.cacheCommonEvent = true;
736     saControlList.push_back(info);
737     saMgr->collectManager_->SaveCacheCommonEventSaExtraId(event, saControlList);
738 
739     std::vector<int64_t> extraDataIdList;
740     int32_t ret = saMgr->GetCommonEventExtraDataIdlist(SAID, extraDataIdList);
741     EXPECT_EQ(ret, ERR_OK);
742     EXPECT_EQ(extraDataIdList.size(), 1);
743     extraDataIdList.clear();
744 
745     ret = saMgr->GetCommonEventExtraDataIdlist(SAID, extraDataIdList, "test");
746     EXPECT_EQ(ret, ERR_OK);
747     EXPECT_EQ(extraDataIdList.size(), 0);
748     extraDataIdList.clear();
749 
750     saMgr->collectManager_->ClearSaExtraDataId(SAID);
751     ret = saMgr->GetCommonEventExtraDataIdlist(SAID, extraDataIdList);
752     EXPECT_EQ(ret, ERR_OK);
753     EXPECT_EQ(extraDataIdList.size(), 0);
754 
755     SaProfileRecover(saMgr, saProfileMapTmp, maxLoop);
756 }
757 
758 /**
759  * @tc.name: RemoveWhiteCommonEvent001
760  * @tc.desc: test RemoveWhiteCommonEvent
761  * @tc.type: FUNC
762  * @tc.require: I6NKWX
763  */
764 HWTEST_F(SystemAbilityMgrTest, RemoveWhiteCommonEvent001, TestSize.Level3)
765 {
766     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
767     saMgr->RemoveWhiteCommonEvent();
768     EXPECT_TRUE(saMgr->collectManager_ != nullptr);
769 }
770 
771 /**
772  * @tc.name: CleanFfrt001
773  * @tc.desc: test CleanFfrt, workHandler_ true.
774  * @tc.type: FUNC
775  * @tc.require: I6NKWX
776  */
777 HWTEST_F(SystemAbilityMgrTest, CleanFfrt001, TestSize.Level3)
778 {
779     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
780     std::shared_ptr<FFRTHandler> workHandler_ = make_shared<FFRTHandler>("workHandler");
781     int ret = true;
782     saMgr->CleanFfrt();
783     EXPECT_EQ(ret, true);
784 }
785 
786 /**
787  * @tc.name: CleanFfrt002
788  * @tc.desc: test CleanFfrt, collectManager_ true.
789  * @tc.type: FUNC
790  * @tc.require: I6NKWX
791  */
792 HWTEST_F(SystemAbilityMgrTest, CleanFfrt002, TestSize.Level3)
793 {
794     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
795     sptr<DeviceStatusCollectManager> collectManager_ =
796         sptr<DeviceStatusCollectManager>(new DeviceStatusCollectManager());
797     int ret = true;
798     saMgr->CleanFfrt();
799     EXPECT_EQ(ret, true);
800 }
801 
802 /**
803  * @tc.name: CleanFfrt003
804  * @tc.desc: test CleanFfrt, abilityStateScheduler_ true.
805  * @tc.type: FUNC
806  * @tc.require: I6NKWX
807  */
808 HWTEST_F(SystemAbilityMgrTest, CleanFfrt003, TestSize.Level3)
809 {
810     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
811     std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler_ =
812         std::make_shared<SystemAbilityStateScheduler>();
813     int ret = true;
814     saMgr->CleanFfrt();
815     EXPECT_EQ(ret, true);
816 }
817 
818 /**
819  * @tc.name: SetFfrt001
820  * @tc.desc: test SetFfrt, workHandler_  true.
821  * @tc.type: FUNC
822  * @tc.require: I6NKWX
823  */
824 HWTEST_F(SystemAbilityMgrTest, SetFfrt001, TestSize.Level3)
825 {
826     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
827     saMgr->workHandler_ = make_shared<FFRTHandler>("workHandler");
828     saMgr->SetFfrt();
829     EXPECT_NE(saMgr->collectManager_, nullptr);
830     EXPECT_NE(saMgr->abilityStateScheduler_, nullptr);
831 }
832 
833 /**
834  * @tc.name: GetRunningSaExtensionInfoList001
835  * @tc.desc: test GetRunningSaExtensionInfoList, not exist obj
836  * @tc.type: FUNC
837  */
838 HWTEST_F(SystemAbilityMgrTest, GetRunningSaExtensionInfoList001, TestSize.Level3)
839 {
840     DTEST_LOG << __func__ << std::endl;
841     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
842     EXPECT_TRUE(saMgr != nullptr);
843     const int32_t maxLoop = 4;
844     map<int32_t, SaProfile> saProfileMapTmp;
845 
846     SaProfileStore(saMgr, saProfileMapTmp, maxLoop);
847     SaProfileExtensionTestPrevSet(saMgr, maxLoop);
848 
849     MessageParcel data;
850     MessageParcel reply;
851     data.WriteString("restore_test");
852     int32_t result = saMgr->GetRunningSaExtensionInfoListInner(data, reply);
853     EXPECT_EQ(result, ERR_NONE);
854     bool ret = reply.ReadInt32(result);
855     EXPECT_EQ(ret, true);
856     int32_t size = -1;
857     ret = reply.ReadInt32(size);
858     EXPECT_EQ(ret, true);
859     EXPECT_EQ(size, 0);
860 
861     saMgr->abilityMap_.clear();
862     SaProfileRecover(saMgr, saProfileMapTmp, maxLoop);
863 }
864 
865 /**
866  * @tc.name: GetRunningSaExtensionInfoList002
867  * @tc.desc: test GetRunningSaExtensionInfoList, get extension success with restore!
868  * @tc.type: FUNC
869  */
870 HWTEST_F(SystemAbilityMgrTest, GetRunningSaExtensionInfoList002, TestSize.Level3)
871 {
872     DTEST_LOG << __func__ << std::endl;
873     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
874     EXPECT_TRUE(saMgr != nullptr);
875     const int32_t maxLoop = 4;
876     map<int32_t, SaProfile> saProfileMapTmp;
877 
878     SaProfileStore(saMgr, saProfileMapTmp, maxLoop);
879     SaProfileExtensionTestPrevSet(saMgr, maxLoop);
880     ProcMapObjTestPrevSet(saMgr, maxLoop);
881     SaAbilityMapObjTestPrevSet(saMgr, maxLoop);
882 
883     MessageParcel data;
884     MessageParcel reply;
885     data.WriteString("restore_test");
886     int32_t result = saMgr->GetRunningSaExtensionInfoListInner(data, reply);
887     EXPECT_EQ(result, ERR_NONE);
888     bool ret = reply.ReadInt32(result);
889     EXPECT_EQ(ret, true);
890 
891     int32_t size = -1;
892     ret = reply.ReadInt32(size);
893     EXPECT_EQ(ret, true);
894     EXPECT_EQ(size, 3);
895     for (int32_t i = 0; i < size; ++i) {
896         int id = -1;
897         ret = reply.ReadInt32(id);
898         EXPECT_EQ(ret, true);
899         EXPECT_EQ(id, SAID + 1 + i);
900         sptr<IRemoteObject> obj = reply.ReadRemoteObject();
901         EXPECT_NE(obj, nullptr);
902     }
903 
904     saMgr->abilityMap_.clear();
905     SaProfileRecover(saMgr, saProfileMapTmp, maxLoop);
906 }
907 
908 /**
909  * @tc.name: StartOnDemandAbilityInner001
910  * @tc.desc: test StartOnDemandAbilityInner, ERR_INVALID_VALUE.
911  * @tc.type: FUNC
912  * @tc.require: I6NKWX
913  */
914 HWTEST_F(SystemAbilityMgrTest, StartOnDemandAbilityInner001, TestSize.Level3)
915 {
916     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
917     const std::u16string procName;
918     int32_t systemAbilityId = 0;
919     SystemAbilityManager::AbilityItem abilityItem;
920     abilityItem.state = SystemAbilityManager::AbilityState::STARTING;
921     int32_t ret = saMgr->StartOnDemandAbilityInner(procName, systemAbilityId, abilityItem);
922     EXPECT_EQ(ret, ERR_INVALID_VALUE);
923 }
924 
925 /**
926  * @tc.name: CheckSystemAbility006
927  * @tc.desc: test CheckSystemAbility, nullptr.
928  * @tc.type: FUNC
929  * @tc.require: I6NKWX
930  */
931 HWTEST_F(SystemAbilityMgrTest, CheckSystemAbility006, TestSize.Level3)
932 {
933     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
934     int32_t systemAbilityId = -1;
935     bool isExist;
936     sptr<IRemoteObject> ret = saMgr->CheckSystemAbility(systemAbilityId, isExist);
937     EXPECT_EQ(ret, nullptr);
938 }
939 
940 /**
941  * @tc.name: RemoveSystemAbility005
942  * @tc.desc: test RemoveSystemAbility, ERR_INVALID_VALUE.
943  * @tc.type: FUNC
944  * @tc.require: I6NKWX
945  */
946 HWTEST_F(SystemAbilityMgrTest, RemoveSystemAbility005, TestSize.Level3)
947 {
948     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
949     const sptr<IRemoteObject> ability = nullptr;
950     int32_t ret = saMgr->RemoveSystemAbility(ability);
951     EXPECT_EQ(ret, ERR_INVALID_VALUE);
952 }
953 
954 /**
955  * @tc.name: SubscribeSystemAbility001
956  * @tc.desc: test SubscribeSystemAbility, ERR_INVALID_VALUE.
957  * @tc.type: FUNC
958  * @tc.require: I6NKWX
959  */
960 HWTEST_F(SystemAbilityMgrTest, SubscribeSystemAbility001, TestSize.Level3)
961 {
962     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
963     int32_t systemAbilityId = -1;
964     const sptr<ISystemAbilityStatusChange> listener;
965     int32_t ret = saMgr->SubscribeSystemAbility(systemAbilityId, listener);
966     EXPECT_EQ(ret, ERR_INVALID_VALUE);
967 }
968 
969 /**
970  * @tc.name: UnSubscribeSystemAbility001
971  * @tc.desc: test UnSubscribeSystemAbility, ERR_INVALID_VALUE.
972  * @tc.type: FUNC
973  * @tc.require: I6NKWX
974  */
975 HWTEST_F(SystemAbilityMgrTest, UnSubscribeSystemAbility001, TestSize.Level3)
976 {
977     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
978     int32_t systemAbilityId = -1;
979     const sptr<ISystemAbilityStatusChange> listener;
980     int32_t ret = saMgr->UnSubscribeSystemAbility(systemAbilityId, listener);
981     EXPECT_EQ(ret, ERR_INVALID_VALUE);
982 }
983 
984 /**
985  * @tc.name: NotifyRemoteSaDied002
986  * @tc.desc: test NotifyRemoteSaDied, true.
987  * @tc.type: FUNC
988  * @tc.require: I6NKWX
989  */
990 HWTEST_F(SystemAbilityMgrTest, NotifyRemoteSaDied002, TestSize.Level3)
991 {
992     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
993     const std::u16string name;
994     sptr<DBinderService> dBinderService_;
995     int ret = true;
996     saMgr->NotifyRemoteSaDied(name);
997     EXPECT_EQ(ret, true);
998 }
999 
1000 /**
1001  * @tc.name: AddSystemAbility006
1002  * @tc.desc: add system ability with empty capability.
1003  * @tc.type: FUNC
1004  */
1005 HWTEST_F(SystemAbilityMgrTest, AddSystemAbility006, TestSize.Level1)
1006 {
1007     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1008     int32_t systemAbilityId = -1;
1009     const sptr<IRemoteObject> ability;
1010     const ISystemAbilityManager::SAExtraProp extraProp;
1011     int32_t ret = saMgr->AddSystemAbility(systemAbilityId, ability, extraProp);
1012     EXPECT_EQ(ret, ERR_INVALID_VALUE);
1013 }
1014 
1015 /**
1016  * @tc.name: AddSystemAbility007
1017  * @tc.desc: test AddSystemAbility.
1018  * @tc.type: FUNC
1019  */
1020 HWTEST_F(SystemAbilityMgrTest, AddSystemAbility007, TestSize.Level1)
1021 {
1022     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1023     int32_t systemAbilityId = -1;
1024     const sptr<IRemoteObject> ability;
1025     const ISystemAbilityManager::SAExtraProp extraProp;
1026     std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler_ = nullptr;
1027     int32_t ret = saMgr->AddSystemAbility(systemAbilityId, ability, extraProp);
1028     EXPECT_EQ(ret, ERR_INVALID_VALUE);
1029 }
1030 
1031 /**
1032  * @tc.name: GetSystemProcess001
1033  * @tc.desc: test GetSystemProcess.
1034  * @tc.type: FUNC
1035  */
1036 HWTEST_F(SystemAbilityMgrTest, GetSystemProcess001, TestSize.Level1)
1037 {
1038     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1039     std::u16string procName;
1040     sptr<IRemoteObject> ret = saMgr->GetSystemProcess(procName);
1041     EXPECT_EQ(ret, nullptr);
1042 }
1043 
1044 /**
1045  * @tc.name: GetSystemProcessInfo003
1046  * @tc.desc: test GetSystemProcessInfo.
1047  * @tc.type: FUNC
1048  */
1049 HWTEST_F(SystemAbilityMgrTest, GetSystemProcessInfo003, TestSize.Level1)
1050 {
1051     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1052     saMgr->abilityStateScheduler_ = nullptr;
1053     int32_t systemAbilityId = 0;
1054     SystemProcessInfo systemProcessInfo;
1055     int32_t ret = saMgr->GetSystemProcessInfo(systemAbilityId, systemProcessInfo);
1056     EXPECT_EQ(ret, ERR_INVALID_VALUE);
1057 }
1058 
1059 /**
1060  * @tc.name: LoadSystemAbilityFromRpc009
1061  * @tc.desc: test LoadSystemAbilityFromRpc.
1062  * @tc.type: FUNC
1063  */
1064 HWTEST_F(SystemAbilityMgrTest, LoadSystemAbilityFromRpc009, TestSize.Level1)
1065 {
1066     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1067     const std::string srcDeviceId;
1068     SaProfile saProfile = {u"test", TEST_OVERFLOW_SAID};
1069     saProfile.distributed = true;
1070     saMgr->saProfileMap_[TEST_OVERFLOW_SAID] = saProfile;
1071     sptr<SystemAbilityLoadCallbackMock> callback = new SystemAbilityLoadCallbackMock();
1072     std::shared_ptr<SystemAbilityStateScheduler> saScheduler = saMgr->abilityStateScheduler_;
1073     saMgr->abilityStateScheduler_ = nullptr;
1074     int32_t ret = saMgr->LoadSystemAbilityFromRpc(srcDeviceId, TEST_OVERFLOW_SAID, callback);
1075     EXPECT_FALSE(ret);
1076     saMgr->abilityStateScheduler_ = saScheduler;
1077     saMgr->saProfileMap_.erase(TEST_OVERFLOW_SAID);
1078 }
1079 
1080 /**
1081  * @tc.name: IsModuleUpdate001
1082  * @tc.desc: test IsModuleUpdate with saprofile is not exist.
1083  * @tc.type: FUNC
1084  */
1085 HWTEST_F(SystemAbilityMgrTest, IsModuleUpdate001, TestSize.Level2)
1086 {
1087     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1088     saMgr->saProfileMap_.clear();
1089     bool ret = saMgr->IsModuleUpdate(SAID);
1090     EXPECT_FALSE(ret);
1091 }
1092 
1093 /**
1094  * @tc.name: IsModuleUpdate002
1095  * @tc.desc: test IsModuleUpdate with default.
1096  * @tc.type: FUNC
1097  */
1098 HWTEST_F(SystemAbilityMgrTest, IsModuleUpdate002, TestSize.Level2)
1099 {
1100     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1101     saMgr->saProfileMap_.clear();
1102     SaProfile saprofile;
1103     saMgr->saProfileMap_[saprofile.saId] = saprofile;
1104     bool ret = saMgr->IsModuleUpdate(saprofile.saId);
1105     EXPECT_FALSE(ret);
1106 }
1107 
1108 /**
1109  * @tc.name: IsModuleUpdate003
1110  * @tc.desc: test IsModuleUpdate with moduleUpdate is true.
1111  * @tc.type: FUNC
1112  */
1113 HWTEST_F(SystemAbilityMgrTest, IsModuleUpdate003, TestSize.Level2)
1114 {
1115     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1116     saMgr->saProfileMap_.clear();
1117     SaProfile saprofile;
1118     saprofile.moduleUpdate = true;
1119     saMgr->saProfileMap_[saprofile.saId] = saprofile;
1120     bool ret = saMgr->IsModuleUpdate(saprofile.saId);
1121     EXPECT_TRUE(ret);
1122 }
1123 
1124 /**
1125  * @tc.name: GetExtensionSaIdsInner001
1126  * @tc.desc: test GetExtensionSaIdsInner, read extension failed!
1127  * @tc.type: FUNC
1128  */
1129 HWTEST_F(SystemAbilityMgrTest, GetExtensionSaIdsInner001, TestSize.Level3)
1130 {
1131     DTEST_LOG << __func__ << std::endl;
1132     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1133     EXPECT_TRUE(saMgr != nullptr);
1134     MessageParcel data;
1135     MessageParcel reply;
1136     int32_t result = saMgr->GetExtensionSaIdsInner(data, reply);
1137     EXPECT_EQ(result, ERR_FLATTEN_OBJECT);
1138 }
1139 
1140 /**
1141  * @tc.name: GetExtensionSaIdsInner002
1142  * @tc.desc: test GetExtensionSaIdsInner, read null extension!
1143  * @tc.type: FUNC
1144  */
1145 HWTEST_F(SystemAbilityMgrTest, GetExtensionSaIdsInner002, TestSize.Level3)
1146 {
1147     DTEST_LOG << __func__ << std::endl;
1148     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1149     EXPECT_TRUE(saMgr != nullptr);
1150     MessageParcel data;
1151     MessageParcel reply;
1152     data.WriteString("backup_test");
1153     int32_t result = saMgr->GetExtensionSaIdsInner(data, reply);
1154     EXPECT_EQ(result, ERR_NONE);
1155     int32_t ret = reply.ReadInt32(result);
1156     EXPECT_EQ(ret, true);
1157 
1158     vector<int32_t> saIds;
1159     ret = reply.ReadInt32Vector(&saIds);
1160     EXPECT_EQ(ret, true);
1161     EXPECT_EQ(saIds.size(), 0);
1162 }
1163 
1164 /**
1165  * @tc.name: GetExtensionSaIdsInner003
1166  * @tc.desc: test GetExtensionSaIdsInner, get extension success with backup!
1167  * @tc.type: FUNC
1168  */
1169 HWTEST_F(SystemAbilityMgrTest, GetExtensionSaIdsInner003, TestSize.Level3)
1170 {
1171     DTEST_LOG << __func__ << std::endl;
1172     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1173     EXPECT_TRUE(saMgr != nullptr);
1174     const int32_t maxLoop = 4;
1175     map<int32_t, SaProfile> saProfileMapTmp;
1176     SaProfileStore(saMgr, saProfileMapTmp, maxLoop);
1177     SaProfileExtensionTestPrevSet(saMgr, maxLoop);
1178 
1179     MessageParcel data;
1180     MessageParcel reply;
1181     data.WriteString("backup_test");
1182     int32_t result = saMgr->GetExtensionSaIdsInner(data, reply);
1183     EXPECT_EQ(result, ERR_NONE);
1184     int32_t ret = reply.ReadInt32(result);
1185     EXPECT_EQ(ret, true);
1186 
1187     vector<int32_t> saIds;
1188     ret = reply.ReadInt32Vector(&saIds);
1189     EXPECT_EQ(ret, true);
1190     EXPECT_EQ(saIds.size(), 3);
1191     EXPECT_EQ(saIds[0], SAID);
1192     EXPECT_EQ(saIds[1], SAID + 2);
1193     EXPECT_EQ(saIds[2], SAID + 3);
1194 
1195     SaProfileRecover(saMgr, saProfileMapTmp, maxLoop);
1196 }
1197 
1198 /**
1199  * @tc.name: GetExtensionRunningSaListInner001
1200  * @tc.desc: test GetExtensionRunningSaListInner, read extension failed!
1201  * @tc.type: FUNC
1202  */
1203 HWTEST_F(SystemAbilityMgrTest, GetExtensionRunningSaListInner001, TestSize.Level3)
1204 {
1205     DTEST_LOG << __func__ << std::endl;
1206     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1207     EXPECT_TRUE(saMgr != nullptr);
1208     MessageParcel data;
1209     MessageParcel reply;
1210     int32_t result = saMgr->GetExtensionRunningSaListInner(data, reply);
1211     EXPECT_EQ(result, ERR_FLATTEN_OBJECT);
1212 }
1213 
1214 /**
1215  * @tc.name: GetExtensionRunningSaListInner002
1216  * @tc.desc: test GetExtensionRunningSaListInner, read null extension!
1217  * @tc.type: FUNC
1218  */
1219 HWTEST_F(SystemAbilityMgrTest, GetExtensionRunningSaListInner002, TestSize.Level3)
1220 {
1221     DTEST_LOG << __func__ << std::endl;
1222     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1223     EXPECT_TRUE(saMgr != nullptr);
1224     MessageParcel data;
1225     MessageParcel reply;
1226     data.WriteString("backup_test");
1227     int32_t result = saMgr->GetExtensionRunningSaListInner(data, reply);
1228     EXPECT_EQ(result, ERR_NONE);
1229     int32_t ret = reply.ReadInt32(result);
1230     EXPECT_EQ(ret, true);
1231 
1232     int32_t size;
1233     ret = reply.ReadInt32(size);
1234     EXPECT_EQ(ret, true);
1235     EXPECT_EQ(size, 0);
1236 }
1237 
1238 /**
1239  * @tc.name: GetExtensionRunningSaListInner003
1240  * @tc.desc: test GetExtensionRunningSaListInner, get extension success with restore!
1241  * @tc.type: FUNC
1242  */
1243 HWTEST_F(SystemAbilityMgrTest, GetExtensionRunningSaListInner003, TestSize.Level3)
1244 {
1245     DTEST_LOG << __func__ << std::endl;
1246     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1247     EXPECT_TRUE(saMgr != nullptr);
1248     const int32_t maxLoop = 4;
1249     map<int32_t, SaProfile> saProfileMapTmp;
1250 
1251     SaProfileStore(saMgr, saProfileMapTmp, maxLoop);
1252     SaProfileExtensionTestPrevSet(saMgr, maxLoop);
1253     SaAbilityMapObjTestPrevSet(saMgr, maxLoop);
1254 
1255     MessageParcel data;
1256     MessageParcel reply;
1257     data.WriteString("restore_test");
1258     int32_t result = saMgr->GetExtensionRunningSaListInner(data, reply);
1259     EXPECT_EQ(result, ERR_NONE);
1260     int32_t ret = reply.ReadInt32(result);
1261     EXPECT_EQ(ret, true);
1262 
1263     int32_t size;
1264     ret = reply.ReadInt32(size);
1265     EXPECT_EQ(ret, true);
1266     EXPECT_EQ(size, 3);
1267     for (int32_t i = 0; i < size; ++i) {
1268         sptr<IRemoteObject> obj = reply.ReadRemoteObject();
1269         EXPECT_NE(obj, nullptr);
1270     }
1271 
1272     saMgr->abilityMap_.clear();
1273     SaProfileRecover(saMgr, saProfileMapTmp, maxLoop);
1274 }
1275 
1276 /**
1277  * @tc.name: IpcDumpProc001
1278  * @tc.desc: test IpcDumpProc.
1279  * @tc.type: FUNC
1280  */
1281 HWTEST_F(SystemAbilityMgrTest, IpcDumpProc001, TestSize.Level2)
1282 {
1283     SamMockPermission::MockProcess("hidumper_service");
1284     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1285     int32_t cmd = -1;
1286     int32_t fd = 1;
1287     std::vector<std::string> args;
1288     args.push_back("abc");
1289     args.push_back("abcd");
1290     int ret = saMgr->IpcDumpProc(fd, args);
1291     EXPECT_EQ(ret, ERR_INVALID_VALUE);
1292 }
1293 
1294 /**
1295  * @tc.name: IpcDumpProc002
1296  * @tc.desc: test IpcDumpProc.
1297  * @tc.type: FUNC
1298  */
1299 HWTEST_F(SystemAbilityMgrTest, IpcDumpProc002, TestSize.Level2)
1300 {
1301     SamMockPermission::MockProcess("hidumper_service");
1302     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1303     int32_t cmd = -1;
1304     int32_t fd = 1;
1305     std::vector<std::string> args;
1306     args.push_back("abc");
1307     args.push_back("abcd");
1308     args.push_back("--start-stat");
1309     int ret = saMgr->IpcDumpProc(fd, args);
1310     EXPECT_EQ(ret, ERR_OK);
1311 }
1312 
1313 /**
1314  * @tc.name: IpcDumpProc003
1315  * @tc.desc: test IpcDumpProc.
1316  * @tc.type: FUNC
1317  */
1318 HWTEST_F(SystemAbilityMgrTest, IpcDumpProc003, TestSize.Level2)
1319 {
1320     SamMockPermission::MockProcess("hidumper_service");
1321     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1322     int32_t cmd = -1;
1323     int32_t fd = 1;
1324     std::vector<std::string> args;
1325     args.push_back("test");
1326     args.push_back("samgr");
1327     args.push_back("--start-stat");
1328     int ret = saMgr->IpcDumpProc(fd, args);
1329     EXPECT_EQ(ret, ERR_OK);
1330 }
1331 
1332 /**
1333  * @tc.name: IpcDumpProc004
1334  * @tc.desc: test IpcDumpProc.
1335  * @tc.type: FUNC
1336  */
1337 HWTEST_F(SystemAbilityMgrTest, IpcDumpProc004, TestSize.Level2)
1338 {
1339     SamMockPermission::MockProcess("hidumper_service");
1340     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1341     int32_t cmd = -1;
1342     int32_t fd = 1;
1343     std::vector<std::string> args;
1344     args.push_back("test");
1345     args.push_back("all");
1346     args.push_back("--start-stat");
1347     int ret = saMgr->IpcDumpProc(fd, args);
1348     EXPECT_EQ(ret, ERR_OK);
1349 }
1350 
1351 /**
1352  * @tc.name: IpcStatSamgrProc001
1353  * @tc.desc: test IpcStatSamgrProc.
1354  * @tc.type: FUNC
1355  */
1356 HWTEST_F(SystemAbilityMgrTest, IpcStatSamgrProc001, TestSize.Level2)
1357 {
1358     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1359     int32_t cmd = IPC_STAT_CMD_START - 1;
1360     int32_t fd = 1;
1361     bool ret = saMgr->IpcStatSamgrProc(fd, cmd);
1362     EXPECT_FALSE(ret);
1363     cmd = IPC_STAT_CMD_MAX;
1364     bool result = saMgr->IpcStatSamgrProc(fd, cmd);
1365     EXPECT_FALSE(result);
1366 }
1367 
1368 /**
1369  * @tc.name: IpcStatSamgrProc002
1370  * @tc.desc: test IpcStatSamgrProc.
1371  * @tc.type: FUNC
1372  */
1373 HWTEST_F(SystemAbilityMgrTest, IpcStatSamgrProc002, TestSize.Level2)
1374 {
1375     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1376     int32_t cmd = IPC_STAT_CMD_START;
1377     int32_t dmd = IPC_STAT_CMD_STOP;
1378     int32_t emd = IPC_STAT_CMD_GET;
1379     int32_t fd = 1;
1380     int ret = true;
1381     saMgr->IpcStatSamgrProc(fd, cmd);
1382     saMgr->IpcStatSamgrProc(fd, dmd);
1383     saMgr->IpcStatSamgrProc(fd, emd);
1384     EXPECT_TRUE(ret);
1385     int32_t fmd = 4;
1386     bool result = saMgr->IpcStatSamgrProc(fd, fmd);
1387     EXPECT_FALSE(result);
1388 }
1389 
1390 /**
1391  * @tc.name: IpcDumpAllProcess001
1392  * @tc.desc: test IpcDumpAllProcess.
1393  * @tc.type: FUNC
1394  */
1395 HWTEST_F(SystemAbilityMgrTest, IpcDumpAllProcess001, TestSize.Level2)
1396 {
1397     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1398     int32_t cmd = IPC_STAT_CMD_START;
1399     int32_t fd = 1;
1400     bool ret = true;
1401     saMgr->IpcDumpAllProcess(fd, cmd);
1402     EXPECT_TRUE(ret);
1403 }
1404 
1405 /**
1406  * @tc.name: IpcDumpSamgrProcess001
1407  * @tc.desc: test IpcDumpSamgrProcess.
1408  * @tc.type: FUNC
1409  */
1410 HWTEST_F(SystemAbilityMgrTest, IpcDumpSamgrProcess001, TestSize.Level2)
1411 {
1412     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1413     int32_t cmd = 4;
1414     int32_t fd = 1;
1415     bool ret = true;
1416     saMgr->IpcDumpSamgrProcess(fd, cmd);
1417     EXPECT_TRUE(ret);
1418 }
1419 
1420 /**
1421  * @tc.name: IpcDumpSingleProcess001
1422  * @tc.desc: test IpcDumpSingleProcess.
1423  * @tc.type: FUNC
1424  */
1425 HWTEST_F(SystemAbilityMgrTest, IpcDumpSingleProcess001, TestSize.Level2)
1426 {
1427     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1428     int32_t cmd = IPC_STAT_CMD_START;
1429     int32_t fd = 1;
1430     bool ret = true;
1431 
1432     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
1433     Security::AccessToken::NativeTokenInfo nativeTokenInfo;
1434     int32_t tokenInfoResult = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
1435     saMgr->IpcDumpSingleProcess(fd, cmd, nativeTokenInfo.processName);
1436     EXPECT_TRUE(ret);
1437 }
1438 
1439 /**
1440  * @tc.name: Test DoLoadForPerf
1441  * @tc.desc: DoLoadForPerf001
1442  * @tc.type: FUNC
1443  * @tc.require: I5KMF7
1444  */
1445 HWTEST_F(SystemAbilityMgrTest, DoLoadForPerf001, TestSize.Level2)
1446 {
1447     sptr<SystemAbilityManager> saMgr1 = new SystemAbilityManager;
1448     SaProfile saProfile;
1449     saProfile.process = u"memmgrservice";
1450     saMgr1->saProfileMap_.clear();
1451     saMgr1->saProfileMap_[-1] = saProfile;
1452     saMgr1->abilityMap_.clear();
1453     bool ret = true;
1454     saMgr1->DoLoadForPerf();
1455     EXPECT_TRUE(ret);
1456 }
1457 /**
1458  * @tc.name: Test IsDistributedSystemAbility
1459  * @tc.desc: IsDistributedSystemAbility001
1460  * @tc.type: FUNC
1461  * @tc.require: I5KMF7
1462  */
1463 HWTEST_F(SystemAbilityMgrTest, IsDistributedSystemAbility001, TestSize.Level2)
1464 {
1465     sptr<SystemAbilityManager> saMgr = SystemAbilityManager::GetInstance();
1466     bool res = saMgr->IsDistributedSystemAbility(-1);
1467     EXPECT_FALSE(res);
1468     SaProfile saProfile;
1469     saProfile.distributed = true;
1470     saProfile.process = u"memmgrservice";
1471     saMgr->saProfileMap_.clear();
1472     saMgr->saProfileMap_[1] = saProfile;
1473     res = saMgr->IsDistributedSystemAbility(1);
1474     EXPECT_TRUE(res);
1475     res = saMgr->IsDistributedSystemAbility(2);
1476     EXPECT_FALSE(res);
1477     saProfile.distributed = false;
1478     saMgr->saProfileMap_[2] = saProfile;
1479     res = saMgr->IsDistributedSystemAbility(2);
1480     EXPECT_FALSE(res);
1481 }
1482 } // namespace OHOS