1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gtest/gtest.h"
17 
18 #include "utils.h"
19 
20 #define private public
21 #define protected public
22 #include "kernel_interface.h"
23 #include "system_memory_level_config.h"
24 #include "memmgr_config_manager.h"
25 #include "xml_helper.h"
26 #undef private
27 #undef protected
28 
29 namespace OHOS {
30 namespace Memory {
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace {
35     const std::string TAG = "MemmgrConfigManager";
36     const std::string XML_PATH = "/etc/memmgr/";
37     const std::string MEMCG_PATH = KernelInterface::MEMCG_BASE_PATH;
38 } // namespace
39 
40 class SystemMemoryLevelConfigTest : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp();
45     void TearDown();
46 };
47 
SetUpTestCase()48 void SystemMemoryLevelConfigTest::SetUpTestCase()
49 {
50 }
51 
TearDownTestCase()52 void SystemMemoryLevelConfigTest::TearDownTestCase()
53 {
54 }
55 
SetUp()56 void SystemMemoryLevelConfigTest::SetUp()
57 {
58 }
59 
TearDown()60 void SystemMemoryLevelConfigTest::TearDown()
61 {
62 }
63 
64 /**
65  * @tc.name: SetModerate and GetModerate
66  * @tc.desc: Test set value of moderate_ equals to parameter
67  * @tc.desc: Test get value of moderate_ equals to parameter
68  * @tc.type: FUNC
69  */
70 HWTEST_F(SystemMemoryLevelConfigTest, SetModerate, TestSize.Level1)
71 {
72     unsigned int moderate = 0;
73     SystemMemoryLevelConfig setMod;
74     setMod.SetModerate(moderate);
75     EXPECT_EQ(setMod.GetModerate(), moderate);
76 }
77 
78 /**
79  * @tc.name: SetLow and GetLow
80  * @tc.desc: Test set value of low_ equals to parameter
81  * @tc.desc: Test get value of low_ equals to parameter
82  * @tc.type: FUNC
83  */
84 HWTEST_F(SystemMemoryLevelConfigTest, SetLow, TestSize.Level1)
85 {
86     unsigned int low = 0;
87     SystemMemoryLevelConfig setLow;
88     setLow.SetLow(low);
89     EXPECT_EQ(setLow.GetLow(), low);
90 }
91 
92 /**
93  * @tc.name: SetCritical and GetCritical
94  * @tc.desc: Test set value of critical_ equals to parameter
95  * @tc.desc: Test get value of moderate_ and low_ and critical_
96  * @tc.type: FUNC
97  */
98 HWTEST_F(SystemMemoryLevelConfigTest, SetCritical, TestSize.Level1)
99 {
100     unsigned int critical = 0;
101     SystemMemoryLevelConfig setCri;
102     setCri.SetCritical(critical);
103     EXPECT_EQ(setCri.GetCritical(), critical);
104 }
105 
106 /**
107  * @tc.name: ParseConfig
108  * @tc.desc: Test the branch of if
109  * @tc.desc: Test set value of critical_ equals to parameter
110  * @tc.type: FUNC
111  */
112 HWTEST_F(SystemMemoryLevelConfigTest, ParseConfig1, TestSize.Level1)
113 {
114     const xmlNodePtr rootNodePtr = nullptr;
115     SystemMemoryLevelConfig parCon;
116     EXPECT_EQ(XmlHelper::CheckNode(rootNodePtr), false);
117     EXPECT_EQ(XmlHelper::HasChild(rootNodePtr), false);
118     parCon.ParseConfig(rootNodePtr);
119 }
120 
121 /**
122  * @tc.name: ParseConfig
123  * @tc.desc: Test set value of critical_ equals to parameter
124  * @tc.type: FUNC
125  */
126 HWTEST_F(SystemMemoryLevelConfigTest, ParseConfig2, TestSize.Level1)
127 {
128     std::string path = KernelInterface::GetInstance().JoinPath(XML_PATH, "memmgr_config.xml");
129     std::unique_ptr<xmlDoc, decltype(&xmlFreeDoc)> docPtr(
130         xmlReadFile(path.c_str(), nullptr, XML_PARSE_NOBLANKS), xmlFreeDoc);
131     const xmlNodePtr rootNodePtr = xmlDocGetRootElement(docPtr.get());
132     std::map<std::string, std::string> param;
133     SystemMemoryLevelConfig parCon;
134     EXPECT_EQ(XmlHelper::CheckNode(rootNodePtr), true);
135     EXPECT_EQ(XmlHelper::HasChild(rootNodePtr), true);
136     EXPECT_EQ(XmlHelper::CheckNode(rootNodePtr->xmlChildrenNode), true);
137     parCon.ParseConfig(rootNodePtr);
138     EXPECT_NE(parCon.GetModerate(), 0);
139     EXPECT_NE(parCon.GetLow(), 0);
140     EXPECT_NE(parCon.GetCritical(), 0);
141 }
142 
143 }
144 }
145 
146