1 /*
2 * Copyright (c) 2025 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 "el5_filekey_memory_manager_unittest.h"
17
18 #include "el5_memory_manager.h"
19 #include "system_ability_definition.h"
20
21 using namespace testing::ext;
22 using namespace OHOS::Security::AccessToken;
23 namespace {
24 constexpr int32_t MAX_RUNNING_NUM = 256;
25 constexpr int64_t EXTRA_PARAM = 3;
26 constexpr int32_t SA_READY_TO_UNLOAD = 0;
27 constexpr int32_t SA_REFUSE_TO_UNLOAD = -1;
28 const std::string LOW_MEMORY_PARAM = "resourceschedule.memmgr.low.memory.prepare";
29 } // namespace
30
SetUp()31 void El5FilekeyMemoryManagerTest::SetUp()
32 {
33 el5FilekeyManagerServiceAbility_ =
34 std::make_shared<El5FilekeyManagerServiceAbility>(EL5_FILEKEY_MANAGER_SERVICE_ID, false);
35 }
36
37 /**
38 * @tc.name: MemoryManagerTest001
39 * @tc.desc: test OnIdle
40 * @tc.type: FUNC
41 * @tc.require: issueICIZZE
42 */
43 HWTEST_F(El5FilekeyMemoryManagerTest, MemoryManagerTest001, TestSize.Level1)
44 {
45 OHOS::SystemAbilityOnDemandReason reason1(OHOS::OnDemandReasonId::PARAM, "test", "true", EXTRA_PARAM);
46 EXPECT_EQ(SA_READY_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason1));
47
48 El5MemoryManager::GetInstance().SetIsAllowUnloadService(false);
49 OHOS::SystemAbilityOnDemandReason reason2(OHOS::OnDemandReasonId::PARAM, LOW_MEMORY_PARAM, "true", EXTRA_PARAM);
50 EXPECT_EQ(SA_REFUSE_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason2));
51
52 for (int32_t i = 0; i <= MAX_RUNNING_NUM + 1; i++) {
53 El5MemoryManager::GetInstance().AddFunctionRuningNum();
54 }
55 EXPECT_EQ(SA_REFUSE_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason2));
56
57 El5MemoryManager::GetInstance().SetIsAllowUnloadService(true);
58 EXPECT_EQ(SA_REFUSE_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason2));
59
60 for (int32_t i = 0; i <= MAX_RUNNING_NUM + 1; i++) {
61 El5MemoryManager::GetInstance().DecreaseFunctionRuningNum();
62 }
63 EXPECT_EQ(SA_READY_TO_UNLOAD, el5FilekeyManagerServiceAbility_->OnIdle(reason2));
64 }
65