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 #include "hidumper_test_utils.h"
29
30 using namespace testing::ext;
31 using namespace OHOS::HDI::Memorytracker::V1_0;
32 namespace OHOS {
33 namespace HiviewDFX {
34 class MemoryDumperTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
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
55 /**
56 * @tc.name: MemoryDumperTest001
57 * @tc.desc: Test MemoryDumper has correct group.
58 * @tc.type: FUNC
59 * @tc.require: issueI5NWZQ
60 */
61 HWTEST_F(MemoryDumperTest, MemoryDumperTest001, TestSize.Level3)
62 {
63 std::string cmd = "hidumper --mem";
64 std::string str = "Anonymous Page";
65 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
66 }
67
68 /**
69 * @tc.name: MemoryDumperTest002
70 * @tc.desc: Test MemoryDumper has DMA group.
71 * @tc.type: FUNC
72 * @tc.require: issueI5NX04
73 */
74 HWTEST_F(MemoryDumperTest, MemoryDumperTest002, TestSize.Level3)
75 {
76 sptr<IMemoryTrackerInterface> memtrack = IMemoryTrackerInterface::Get();
77 if (memtrack == nullptr) {
78 return;
79 }
80 std::string cmd = "hidumper --mem";
81 std::string str = "DMA";
82 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
83 }
84
85 /**
86 * @tc.name: MemoryDumperTest003
87 * @tc.desc: Test MemoryDumper has correct group.
88 * @tc.type: FUNC
89 * @tc.require: issueI5NWZQ
90 */
91 HWTEST_F(MemoryDumperTest, MemoryDumperTest003, TestSize.Level3)
92 {
93 std::string cmd = "hidumper --mem 1";
94 std::string str = "Total";
95 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
96 }
97
98 /**
99 * @tc.name: MemoryUtilTest001
100 * @tc.desc: Test IsNameLine has correct ret.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(MemoryDumperTest, MemoryUtilTest001, TestSize.Level1)
104 {
105 const std::string valueLine = "Rss: 24 kB";
106 std::string name;
107 uint64_t iNode = 0;
108 ASSERT_FALSE(MemoryUtil::GetInstance().IsNameLine(valueLine, name, iNode));
109 ASSERT_EQ(name, "");
110 const std::string nameLine = "ffb84000-ffba5000 rw-p 00000000 00:00 0 [stack]";
111 ASSERT_TRUE(MemoryUtil::GetInstance().IsNameLine(nameLine, name, iNode));
112 ASSERT_EQ(name, "[stack]");
113 }
114
115 /**
116 * @tc.name: MemoryUtilTest002
117 * @tc.desc: Test GetTypeAndValue has correct ret.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(MemoryDumperTest, MemoryUtilTest002, TestSize.Level1)
121 {
122 std::string type;
123 uint64_t value = 0;
124 const std::string illegalStr = "aaaaaa";
125 ASSERT_FALSE(MemoryUtil::GetInstance().GetTypeAndValue(illegalStr, type, value));
126 const std::string valueStr = "MemTotal: 2010244 kB";
127 ASSERT_TRUE(MemoryUtil::GetInstance().GetTypeAndValue(valueStr, type, value));
128 ASSERT_EQ(type, "MemTotal");
129 ASSERT_EQ(value, 2010244);
130 }
131
132 /**
133 * @tc.name: MemoryUtilTest003
134 * @tc.desc: Test RunCMD has correct ret.
135 * @tc.type: FUNC
136 */
137 HWTEST_F(MemoryDumperTest, MemoryUtilTest003, TestSize.Level1)
138 {
139 const std::string cmd = "ps -ef";
140 std::vector<std::string> vec;
141 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
142 ASSERT_GT(vec.size(), 0);
143 }
144 } // namespace HiviewDFX
145 } // namespace OHOS