1 /*
2 * Copyright (c) 2024 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 #include <gtest/gtest.h>
16 #include <map>
17 #include <unistd.h>
18 #include <vector>
19 #include "executor/memory/dma_info.h"
20 #include "executor/memory/get_hardware_info.h"
21 #include "executor/memory/get_process_info.h"
22 #include "executor/memory/get_kernel_info.h"
23 #include "executor/memory/memory_info.h"
24 #include "executor/memory/memory_filter.h"
25 #include "executor/memory/memory_util.h"
26 #include "executor/memory/parse/parse_meminfo.h"
27 #include "executor/memory/parse/parse_smaps_info.h"
28 #include "executor/memory/parse/parse_smaps_rollup_info.h"
29 #include "executor/memory/smaps_memory_info.h"
30
31 using namespace std;
32 using namespace testing::ext;
33 namespace OHOS {
34 namespace HiviewDFX {
35 const int INIT_PID = 1;
36 const uint64_t INVALID_PID = 0;
37 string NULL_STR = "";
38
39 using ValueMap = std::map<std::string, uint64_t>;
40 using GroupMap = std::map<std::string, ValueMap>;
41 using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
42
43 class HidumperMemoryTest : public testing::Test {
44 public:
45 static void SetUpTestCase(void);
46 static void TearDownTestCase(void);
47 void SetUp();
48 void TearDown();
49 };
50
SetUpTestCase(void)51 void HidumperMemoryTest::SetUpTestCase(void)
52 {
53 }
TearDownTestCase(void)54 void HidumperMemoryTest::TearDownTestCase(void)
55 {
56 }
SetUp(void)57 void HidumperMemoryTest::SetUp(void)
58 {
59 }
TearDown(void)60 void HidumperMemoryTest::TearDown(void)
61 {
62 }
63
64 /**
65 * @tc.name: MemoryParse001
66 * @tc.desc: Test ParseMeminfo invalid ret.
67 * @tc.type: FUNC
68 */
69 HWTEST_F(HidumperMemoryTest, MemoryParse001, TestSize.Level1)
70 {
71 unique_ptr<OHOS::HiviewDFX::ParseMeminfo> parseMeminfo = make_unique<OHOS::HiviewDFX::ParseMeminfo>();
72 ValueMap result;
73 parseMeminfo->SetData("", result);
74 ASSERT_EQ(result.size(), 0);
75 }
76
77 /**
78 * @tc.name: ParseSmapsInfo001
79 * @tc.desc: Test parseSmapsInfo invalid ret.
80 * @tc.type: FUNC
81 */
82 HWTEST_F(HidumperMemoryTest, ParseSmapsInfo001, TestSize.Level1)
83 {
84 unique_ptr<OHOS::HiviewDFX::ParseSmapsInfo> parseSmapsInfo = make_unique<OHOS::HiviewDFX::ParseSmapsInfo>();
85 uint64_t pid = 0;
86 ASSERT_FALSE(parseSmapsInfo->GetHasPidValue("RSS", NULL_STR, pid));
87 ASSERT_FALSE(parseSmapsInfo->GetHasPidValue("PSS", NULL_STR, pid));
88 ASSERT_FALSE(parseSmapsInfo->GetHasPidValue("Size", NULL_STR, pid));
89 ASSERT_FALSE(parseSmapsInfo->GetSmapsValue(MemoryFilter::MemoryType::NOT_SPECIFIED_PID, NULL_STR,
90 NULL_STR, pid));
91 GroupMap groupMapResult;
92 std::vector<std::map<std::string, std::string>> vectMap;
93 parseSmapsInfo->memMap_.clear();
94 ASSERT_FALSE(parseSmapsInfo->ShowSmapsData(OHOS::HiviewDFX::MemoryFilter::MemoryType::NOT_SPECIFIED_PID,
95 0, groupMapResult, false, vectMap));
96 ASSERT_TRUE(parseSmapsInfo->ShowSmapsData(OHOS::HiviewDFX::MemoryFilter::MemoryType::NOT_SPECIFIED_PID,
97 INIT_PID, groupMapResult, true, vectMap));
98 }
99
100 /**
101 * @tc.name: ParseSmapsRollupInfo001
102 * @tc.desc: Test ParseSmapsRollupInfo invalid ret.
103 * @tc.type: FUNC
104 */
105 HWTEST_F(HidumperMemoryTest, ParseSmapsRollupInfo001, TestSize.Level1)
106 {
107 unique_ptr<OHOS::HiviewDFX::ParseSmapsRollupInfo> parseSmapsRollup =
108 make_unique<OHOS::HiviewDFX::ParseSmapsRollupInfo>();
109 MemInfoData::MemInfo memInfo;
110 parseSmapsRollup->GetValue("RSS", memInfo);
111 parseSmapsRollup->GetValue("PSS", memInfo);
112 parseSmapsRollup->GetValue("Size", memInfo);
113 ASSERT_TRUE(memInfo.rss == 0);
114 }
115
116 /**
117 * @tc.name: SmapsMemoryInfo001
118 * @tc.desc: Test SmapsMemoryInfo ret.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(HidumperMemoryTest, SmapsMemoryInfo001, TestSize.Level1)
122 {
123 shared_ptr<OHOS::HiviewDFX::SmapsMemoryInfo> smapsMemoryInfo =
124 make_shared<OHOS::HiviewDFX::SmapsMemoryInfo>();
125 shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
126 ASSERT_TRUE(smapsMemoryInfo->ShowMemorySmapsByPid(INIT_PID, result, true));
127 ASSERT_FALSE(smapsMemoryInfo->ShowMemorySmapsByPid(INVALID_PID, result, true));
128 }
129
130 /**
131 * @tc.name: DmaInfo001
132 * @tc.desc: Test DmaInfo ret.
133 * @tc.type: FUNC
134 */
135 HWTEST_F(HidumperMemoryTest, DmaInfo001, TestSize.Level1)
136 {
137 unique_ptr<OHOS::HiviewDFX::DmaInfo> dmaInfo = make_unique<OHOS::HiviewDFX::DmaInfo>();
138 ASSERT_TRUE(dmaInfo->ParseDmaInfo());
139 dmaInfo->GetDmaByPid(INVALID_PID);
140 ASSERT_TRUE(dmaInfo->ParseDmaInfo());
141 }
142
143 /**
144 * @tc.name: GetHardwareInfo001
145 * @tc.desc: Test GetHardwareInfo ret.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(HidumperMemoryTest, GetHardwareInfo001, TestSize.Level1)
149 {
150 unique_ptr<OHOS::HiviewDFX::GetHardwareInfo> getHardwareInfo =
151 make_unique<OHOS::HiviewDFX::GetHardwareInfo>();
152 getHardwareInfo->GetResverRegPath(NULL_STR);
153 vector<string> paths;
154 uint64_t value = getHardwareInfo->CalcHardware(paths);
155 ASSERT_TRUE(value == 0);
156 size_t groupSize = 0;
157 getHardwareInfo->GetGroupOfPaths(groupSize, groupSize, paths, paths);
158 ASSERT_TRUE(paths.size() == 0);
159 }
160
161 /**
162 * @tc.name: MemoryInfo001
163 * @tc.desc: Test MemoryInfo ret.
164 * @tc.type: FUNC
165 */
166 HWTEST_F(HidumperMemoryTest, MemoryInfo001, TestSize.Level1)
167 {
168 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
169 make_unique<OHOS::HiviewDFX::MemoryInfo>();
170 int value = static_cast<int>(memoryInfo->GetProcValue(INVALID_PID, NULL_STR));
171 ASSERT_EQ(value, 0);
172 value = static_cast<int>(memoryInfo->GetVss(INVALID_PID));
173 ASSERT_EQ(value, 0);
174 MemInfoData::MemUsage usage;
175 OHOS::HiviewDFX::DmaInfo dmaInfo;
176 ASSERT_FALSE(memoryInfo->GetMemByProcessPid(INVALID_PID, dmaInfo, usage));
177 memoryInfo->GetProcessAdjLabel(INVALID_PID);
178 memoryInfo->GetReclaimPriorityString(RECLAIM_PRIORITY_UNKNOWN + 1);
179 memoryInfo->GetReclaimPriorityString(RECLAIM_PRIORITY_BACKGROUND - 1);
180 }
181
182 /**
183 * @tc.name: MemoryInfo002
184 * @tc.desc: Test MemoryInfo ret.
185 * @tc.type: FUNC
186 */
187 HWTEST_F(HidumperMemoryTest, MemoryInfo002, TestSize.Level1)
188 {
189 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
190 make_unique<OHOS::HiviewDFX::MemoryInfo>();
191 shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
192 ValueMap memInfo;
193 memoryInfo->GetPurgTotal(memInfo, result);
194 ASSERT_TRUE(memInfo.size() == 0);
195 }
196
197 /**
198 * @tc.name: GetProcessInfo001
199 * @tc.desc: Test GetProcessInfo ret.
200 * @tc.type: FUNC
201 */
202 HWTEST_F(HidumperMemoryTest, GetProcessInfo001, TestSize.Level1)
203 {
204 unique_ptr<OHOS::HiviewDFX::GetProcessInfo> getProcessInfo = make_unique<OHOS::HiviewDFX::GetProcessInfo>();
205 GroupMap groupMap;
206 ValueMap memInfo;
207 uint64_t pid = 0;
208 memInfo.insert(pair<string, uint64_t>("Name", pid));
209 groupMap.insert(pair<string, ValueMap>("test", memInfo));
210 int value = static_cast<int>(getProcessInfo->GetProcess(groupMap));
211 ASSERT_EQ(value, 0);
212 }
213
214 /**
215 * @tc.name: GetKernelInfo001
216 * @tc.desc: Test GetKernelInfo ret.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(HidumperMemoryTest, GetKernelInfo001, TestSize.Level1)
220 {
221 unique_ptr<OHOS::HiviewDFX::GetKernelInfo> getGetKernelInfo = make_unique<OHOS::HiviewDFX::GetKernelInfo>();
222 ValueMap memInfo;
223 uint64_t value = 0;
224 ASSERT_TRUE(getGetKernelInfo->GetKernel(memInfo, value));
225 }
226 } // namespace HiviewDFX
227 } // namespace OHOS