• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gtest/gtest.h>
16 #include <map>
17 #include <unistd.h>
18 #include <vector>
19 #include <v1_0/imemory_tracker_interface.h>
20 
21 #define private public
22 #include "executor/memory_dumper.h"
23 #undef private
24 #include "dump_client_main.h"
25 #include "hdf_base.h"
26 #include "executor/memory/memory_filter.h"
27 #include "executor/memory/memory_util.h"
28 
29 using namespace testing::ext;
30 using namespace OHOS::HDI::Memorytracker::V1_0;
31 namespace OHOS {
32 namespace HiviewDFX {
33 class MemoryDumperTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39     bool IsExistInCmdResult(const std::string &cmd, const std::string &str);
40 };
41 
SetUpTestCase(void)42 void MemoryDumperTest::SetUpTestCase(void)
43 {
44 }
TearDownTestCase(void)45 void MemoryDumperTest::TearDownTestCase(void)
46 {
47 }
SetUp(void)48 void MemoryDumperTest::SetUp(void)
49 {
50 }
TearDown(void)51 void MemoryDumperTest::TearDown(void)
52 {
53 }
54 
IsExistInCmdResult(const std::string & cmd,const std::string & str)55 bool MemoryDumperTest::IsExistInCmdResult(const std::string &cmd, const std::string &str)
56 {
57     bool ret = false;
58     char* buffer = nullptr;
59     size_t len = 0;
60     FILE *fp = popen(cmd.c_str(), "r");
61     while (getline(&buffer, &len, fp) != -1) {
62         std::string line = buffer;
63         if (line.find(str) != string::npos) {
64             ret = true;
65             break;
66         }
67     }
68     pclose(fp);
69     return ret;
70 }
71 
72 /**
73  * @tc.name: MemoryDumperTest001
74  * @tc.desc: Test MemoryDumper has correct group.
75  * @tc.type: FUNC
76  * @tc.require: issueI5NWZQ
77  */
78 HWTEST_F(MemoryDumperTest, MemoryDumperTest001, TestSize.Level3)
79 {
80     std::string cmd = "hidumper --mem";
81     std::string str = "Anonymous Page";
82     ASSERT_TRUE(IsExistInCmdResult(cmd, str));
83 }
84 
85 /**
86  * @tc.name: MemoryDumperTest002
87  * @tc.desc: Test MemoryDumper has DMA group.
88  * @tc.type: FUNC
89  * @tc.require: issueI5NX04
90  */
91 HWTEST_F(MemoryDumperTest, MemoryDumperTest002, TestSize.Level3)
92 {
93     sptr<IMemoryTrackerInterface> memtrack = IMemoryTrackerInterface::Get();
94     if (memtrack == nullptr) {
95         return;
96     }
97     std::string cmd = "hidumper --mem";
98     std::string str = "DMA";
99     ASSERT_TRUE(IsExistInCmdResult(cmd, str));
100 }
101 
102 /**
103  * @tc.name: MemoryUtilTest001
104  * @tc.desc: Test IsNameLine has correct ret.
105  * @tc.type: FUNC
106  */
107 HWTEST_F(MemoryDumperTest, MemoryUtilTest001, TestSize.Level1)
108 {
109     const std::string valueLine = "Rss:                  24 kB";
110     std::string name;
111     uint64_t iNode = 0;
112     ASSERT_FALSE(MemoryUtil::GetInstance().IsNameLine(valueLine, name, iNode));
113     ASSERT_EQ(name, "");
114     const std::string nameLine = "ffb84000-ffba5000 rw-p 00000000 00:00 0                                  [stack]";
115     ASSERT_TRUE(MemoryUtil::GetInstance().IsNameLine(nameLine, name, iNode));
116     ASSERT_EQ(name, "[stack]");
117 }
118 
119 /**
120  * @tc.name: MemoryUtilTest002
121  * @tc.desc: Test GetTypeAndValue has correct ret.
122  * @tc.type: FUNC
123  */
124 HWTEST_F(MemoryDumperTest, MemoryUtilTest002, TestSize.Level1)
125 {
126     std::string type;
127     uint64_t value = 0;
128     const std::string illegalStr = "aaaaaa";
129     ASSERT_FALSE(MemoryUtil::GetInstance().GetTypeAndValue(illegalStr, type, value));
130     const std::string valueStr = "MemTotal:        2010244 kB";
131     ASSERT_TRUE(MemoryUtil::GetInstance().GetTypeAndValue(valueStr, type, value));
132     ASSERT_EQ(type, "MemTotal");
133     ASSERT_EQ(value, 2010244);
134 }
135 
136 /**
137  * @tc.name: MemoryUtilTest003
138  * @tc.desc: Test RunCMD has correct ret.
139  * @tc.type: FUNC
140  */
141 HWTEST_F(MemoryDumperTest, MemoryUtilTest003, TestSize.Level1)
142 {
143     const std::string cmd = "ps -ef";
144     std::vector<std::string> vec;
145     ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
146     ASSERT_GT(vec.size(), 0);
147 }
148 } // namespace HiviewDFX
149 } // namespace OHOS
150 
151 
152