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/get_hardware_info.h"
20 #include "executor/memory/get_process_info.h"
21 #include "executor/memory/get_kernel_info.h"
22 #include "executor/memory/memory_info.h"
23 #include "executor/memory/memory_filter.h"
24 #include "executor/memory/memory_util.h"
25 #include "executor/memory/parse/parse_meminfo.h"
26 #include "executor/memory/parse/parse_smaps_info.h"
27 #include "executor/memory/parse/parse_smaps_rollup_info.h"
28 #include "executor/memory/smaps_memory_info.h"
29 
30 using namespace std;
31 using namespace testing::ext;
32 namespace OHOS {
33 namespace HiviewDFX {
34 const int INIT_PID = 1;
35 const uint64_t INVALID_PID = 0;
36 const int BUFFER_SIZE = 1024;
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     parseSmapsInfo->memMap_.clear();
99     parseSmapsInfo->SetMapByNameLine("", "");
100     ASSERT_TRUE(parseSmapsInfo->memMap_.size() == 1);
101 }
102 
103 /**
104  * @tc.name: ParseSmapsRollupInfo001
105  * @tc.desc: Test ParseSmapsRollupInfo invalid ret.
106  * @tc.type: FUNC
107  */
108 HWTEST_F(HidumperMemoryTest, ParseSmapsRollupInfo001, TestSize.Level1)
109 {
110     unique_ptr<OHOS::HiviewDFX::ParseSmapsRollupInfo> parseSmapsRollup =
111         make_unique<OHOS::HiviewDFX::ParseSmapsRollupInfo>();
112     MemInfoData::MemInfo memInfo;
113     parseSmapsRollup->GetValue("RSS", memInfo);
114     parseSmapsRollup->GetValue("PSS", memInfo);
115     parseSmapsRollup->GetValue("Size", memInfo);
116     ASSERT_TRUE(memInfo.rss == 0);
117 }
118 
119 /**
120  * @tc.name: SmapsMemoryInfo001
121  * @tc.desc: Test SmapsMemoryInfo ret.
122  * @tc.type: FUNC
123  */
124 HWTEST_F(HidumperMemoryTest, SmapsMemoryInfo001, TestSize.Level1)
125 {
126     shared_ptr<OHOS::HiviewDFX::SmapsMemoryInfo> smapsMemoryInfo =
127         make_shared<OHOS::HiviewDFX::SmapsMemoryInfo>();
128     shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
129     ASSERT_TRUE(smapsMemoryInfo->ShowMemorySmapsByPid(INIT_PID, result, true));
130     ASSERT_FALSE(smapsMemoryInfo->ShowMemorySmapsByPid(INVALID_PID, result, true));
131 }
132 
133 /**
134  * @tc.name: GetHardwareInfo001
135  * @tc.desc: Test GetHardwareInfo ret.
136  * @tc.type: FUNC
137  */
138 HWTEST_F(HidumperMemoryTest, GetHardwareInfo001, TestSize.Level1)
139 {
140     unique_ptr<OHOS::HiviewDFX::GetHardwareInfo> getHardwareInfo =
141         make_unique<OHOS::HiviewDFX::GetHardwareInfo>();
142     getHardwareInfo->GetResverRegPath(NULL_STR);
143     vector<string> paths;
144     uint64_t value = getHardwareInfo->CalcHardware(paths);
145     ASSERT_TRUE(value == 0);
146     size_t groupSize = 0;
147     getHardwareInfo->GetGroupOfPaths(groupSize, groupSize, paths, paths);
148     ASSERT_TRUE(paths.size() == 0);
149 }
150 
151 /**
152  * @tc.name: MemoryInfo001
153  * @tc.desc: Test MemoryInfo ret.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(HidumperMemoryTest, MemoryInfo001, TestSize.Level1)
157 {
158     unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
159         make_unique<OHOS::HiviewDFX::MemoryInfo>();
160     int value = static_cast<int>(memoryInfo->GetProcValue(INVALID_PID, NULL_STR));
161     ASSERT_EQ(value, 0);
162     value = static_cast<int>(memoryInfo->GetVss(INVALID_PID));
163     ASSERT_EQ(value, 0);
164     MemInfoData::MemUsage usage;
165     OHOS::HiviewDFX::DmaInfo dmaInfo;
166     ASSERT_FALSE(memoryInfo->GetMemByProcessPid(INVALID_PID, usage));
167     memoryInfo->GetProcessAdjLabel(INVALID_PID);
168     memoryInfo->GetReclaimPriorityString(RECLAIM_PRIORITY_UNKNOWN + 1);
169     memoryInfo->GetReclaimPriorityString(RECLAIM_PRIORITY_BACKGROUND - 1);
170 }
171 
172 /**
173  * @tc.name: MemoryInfo002
174  * @tc.desc: Test MemoryInfo ret.
175  * @tc.type: FUNC
176  */
177 HWTEST_F(HidumperMemoryTest, MemoryInfo002, TestSize.Level1)
178 {
179     unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
180         make_unique<OHOS::HiviewDFX::MemoryInfo>();
181     shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
182     ValueMap memInfo;
183     memoryInfo->GetPurgTotal(memInfo, result);
184     ASSERT_TRUE(memInfo.size() == 0);
185 }
186 
187 /**
188  * @tc.name: GetProcessInfo001
189  * @tc.desc: Test GetProcessInfo ret.
190  * @tc.type: FUNC
191  */
192 HWTEST_F(HidumperMemoryTest, GetProcessInfo001, TestSize.Level1)
193 {
194     unique_ptr<OHOS::HiviewDFX::GetProcessInfo> getProcessInfo = make_unique<OHOS::HiviewDFX::GetProcessInfo>();
195     GroupMap groupMap;
196     ValueMap memInfo;
197     uint64_t pid = 0;
198     memInfo.insert(pair<string, uint64_t>("Name", pid));
199     groupMap.insert(pair<string, ValueMap>("test", memInfo));
200     int value = static_cast<int>(getProcessInfo->GetProcess(groupMap));
201     ASSERT_EQ(value, 0);
202 }
203 
204 /**
205  * @tc.name: GetKernelInfo001
206  * @tc.desc: Test GetKernelInfo ret.
207  * @tc.type: FUNC
208  */
209 HWTEST_F(HidumperMemoryTest, GetKernelInfo001, TestSize.Level1)
210 {
211     unique_ptr<OHOS::HiviewDFX::GetKernelInfo> getGetKernelInfo = make_unique<OHOS::HiviewDFX::GetKernelInfo>();
212     ValueMap memInfo;
213     uint64_t value = 0;
214     ASSERT_TRUE(getGetKernelInfo->GetKernel(memInfo, value));
215 }
216 
217 
218 /**
219  * @tc.name: GraphicMemory001
220  * @tc.desc: Test GetGraphicMemory.
221  * @tc.type: FUNC
222  */
223 HWTEST_F(HidumperMemoryTest, GraphicMemory001, TestSize.Level1)
224 {
225     shared_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
226         make_shared<OHOS::HiviewDFX::MemoryInfo>();
227     MemInfoData::GraphicsMemory graphicsMemory;
228     FILE* file = popen("pidof render_service", "r");
229     char buffer[BUFFER_SIZE];
230     if (file) {
231         if (fgets(buffer, sizeof(buffer), file) != nullptr) {};
232         pclose(file);
233     }
234     int pid = strtol(buffer, nullptr, 10);
235     int ret = memoryInfo->GetGraphicsMemory(pid, graphicsMemory, GraphicType::GRAPH);
236     ASSERT_TRUE(ret);
237     memoryInfo->GetGraphicsMemory(pid, graphicsMemory, GraphicType::GL);
238     ASSERT_TRUE(ret);
239 }
240 } // namespace HiviewDFX
241 } // namespace OHOS