• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <vector>
16 #include "hidumper_configutils_test.h"
17 #include "dump_common_utils.h"
18 using namespace std;
19 using namespace testing::ext;
20 using namespace OHOS;
21 using namespace OHOS::HiviewDFX;
22 namespace OHOS {
23 namespace HiviewDFX {
24 const int HidumperConfigUtilsTest::PID_EMPTY = -1;
25 const int HidumperConfigUtilsTest::UID_EMPTY = -1;
26 const int HidumperConfigUtilsTest::PID_TEST = 100;
27 const std::string HidumperConfigUtilsTest::DUMPER_NAME = "dumper_kernel_version";
28 const OHOS::HiviewDFX::DumpCommonUtils DUMP_COMMON_UTILS = *(std::make_shared<OHOS::HiviewDFX::DumpCommonUtils>());
SetUpTestCase(void)29 void HidumperConfigUtilsTest::SetUpTestCase(void)
30 {
31 }
32 
TearDownTestCase(void)33 void HidumperConfigUtilsTest::TearDownTestCase(void)
34 {
35 }
36 
SetUp(void)37 void HidumperConfigUtilsTest::SetUp(void)
38 {
39 }
40 
TearDown(void)41 void HidumperConfigUtilsTest::TearDown(void)
42 {
43 }
44 
45 /**
46  * @tc.name: HidumperConfigUtils001
47  * @tc.desc: Test GetDumper by index
48  * @tc.type: FUNC
49  */
50 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils001, TestSize.Level3)
51 {
52     int index = -1;
53     std::vector<std::shared_ptr<DumpCfg>> result;
54     ConfigUtils configUtils(nullptr);
55     auto args = OptionArgs::Create();
56     args->SetPid(PID_EMPTY, UID_EMPTY);
57     DumpStatus ret = configUtils.GetDumper(index, result, args);
58     EXPECT_NE(ret, DumpStatus::DUMP_OK);
59 
60     index = 100000;
61     result.clear();
62     ret = configUtils.GetDumper(index, result, args);
63     EXPECT_NE(ret, DumpStatus::DUMP_OK);
64 
65     index = 0;
66     result.clear();
67     ret = configUtils.GetDumper(index, result, args);
68     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
69 
70     ASSERT_TRUE(!result.empty());
71     ASSERT_TRUE(!result[0]->name_.empty());
72     ASSERT_TRUE(result[0]->target_.empty());
73     ASSERT_TRUE(result[0]->args_ == args);
74 }
75 
76 /**
77  * @tc.name: HidumperConfigUtils002
78  * @tc.desc: Test GetDumper by index.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils002, TestSize.Level3)
82 {
83     int index = 1;
84     std::vector<std::shared_ptr<DumpCfg>> result;
85     ConfigUtils configUtils(nullptr);
86     auto args = OptionArgs::Create();
87     args->SetPid(PID_EMPTY, UID_EMPTY);
88     DumpStatus ret = configUtils.GetDumper(index, result, args);
89     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
90 
91     std::size_t allSum = result.size();
92     index = 1;
93     result.clear();
94     ret = configUtils.GetDumper(index, result, args);
95     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
96     std::size_t highSum = result.size();
97 
98     index = 1;
99     result.clear();
100     ret = configUtils.GetDumper(index, result, args);
101     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
102     std::size_t middleSum = result.size();
103 
104     index = 1;
105     result.clear();
106     ret = configUtils.GetDumper(index, result, args);
107     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
108     std::size_t lowSum = result.size();
109 
110     index = 1;
111     result.clear();
112     ret = configUtils.GetDumper(index, result, args);
113     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
114     std::size_t noneSum = result.size();
115 
116     ASSERT_TRUE(highSum <= allSum);
117     ASSERT_TRUE(middleSum <= allSum);
118     ASSERT_TRUE(lowSum <= allSum);
119     ASSERT_TRUE(noneSum <= allSum);
120 }
121 
122 /**
123  * @tc.name: HidumperConfigUtils003
124  * @tc.desc: Test GetDumper by name.
125  * @tc.type: FUNC
126  */
127 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils003, TestSize.Level3)
128 {
129     std::string name;
130     std::vector<std::shared_ptr<DumpCfg>> result;
131     ConfigUtils configUtils(nullptr);
132     auto args = OptionArgs::Create();
133     args->SetPid(PID_EMPTY, UID_EMPTY);
134     DumpStatus ret = configUtils.GetDumper(name, result, args);
135     EXPECT_NE(ret, DumpStatus::DUMP_OK);
136 
137     name = DUMPER_NAME;
138     result.clear();
139     ret = configUtils.GetDumper(name, result, args);
140     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
141 
142     ASSERT_TRUE(!result.empty());
143     ASSERT_TRUE(result[0]->name_ == name);
144 }
145 
146 HWTEST_F(HidumperConfigUtilsTest, HidumperZipWriter001, TestSize.Level3)
147 {
148     string testfile = "/data/log/hidumpertest.txt";
149     string testzipfile = "/data/log/hidumpertest.zip";
150     system("touch /data/log/hidumpertest.txt");
151     system("echo hidumpertest > /data/log/hidumpertest.txt");
152     auto zipwriter = std::make_shared<ZipWriter>(testzipfile);
153     ASSERT_TRUE(zipwriter->Open());
154     ASSERT_TRUE(zipwriter->Close());
155     auto testzip = zipwriter->OpenForZipping(testfile, APPEND_STATUS_CREATE);
156     zipwriter->AddFileEntryToZip(testzip, testzipfile, testzipfile);
157     system("rm -rf /data/log/hidumpertest.txt");
158     system("rm -rf /data/log/hidumpertest.zip");
159 }
160 
161 HWTEST_F(HidumperConfigUtilsTest, HidumperFileUtils001, TestSize.Level3)
162 {
163     auto fileutils = std::make_shared<FileUtils>();
164     string testpath = "/data";
165     ASSERT_TRUE(fileutils->CreateFolder(testpath));
166     testpath = "";
167     ASSERT_TRUE(fileutils->CreateFolder(testpath));
168     testpath = "test";
169     ASSERT_TRUE(!(fileutils->CreateFolder(testpath)));
170     testpath = "/data/log/testhidumper";
171     ASSERT_TRUE(fileutils->CreateFolder(testpath));
172     ASSERT_TRUE(access(testpath.c_str(), F_OK) == 0);
173     system("rm -rf /data/log/testhidumper");
174 }
175 
176 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils001, TestSize.Level3)
177 {
178     system("mkdir /data/log/hidumpertest/");
179     system("touch /data/log/hidumpertest/1.log");
180     system("touch /data/log/hidumpertest/a.log");
181     const std::string path = "/data/log/hidumpertest";
182     bool digit = true;
183     std::vector<std::string> strs = DUMP_COMMON_UTILS.GetSubNodes(path, digit);
184     ASSERT_TRUE(strs.size() == 1);
185 
186     digit = false;
187     strs = DUMP_COMMON_UTILS.GetSubNodes(path, digit);
188     ASSERT_TRUE(strs.size() == 2);
189     system("rm -rf /data/log/hidumpertest");
190 }
191 
192 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils002, TestSize.Level3)
193 {
194     const std::string pathTest = "/data";
195     ASSERT_TRUE(DUMP_COMMON_UTILS.IsDirectory(pathTest));
196 
197     system("touch /data/log/hidumpertest.txt");
198     const std::string pathTest2 = "/data/log/hidumpertest.txt";
199     ASSERT_FALSE(DUMP_COMMON_UTILS.IsDirectory(pathTest2));
200     system("rm -rf /data/log/hidumpertest.txt");
201 }
202 
203 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils003, TestSize.Level3)
204 {
205     std::vector<int32_t> pids = DUMP_COMMON_UTILS.GetAllPids();
206     ASSERT_FALSE(pids.empty()) << "GetAllPids result is empty.";
207 }
208 
209 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils004, TestSize.Level3)
210 {
211     std::vector<DumpCommonUtils::CpuInfo> infos;
212     ASSERT_TRUE(DUMP_COMMON_UTILS.GetCpuInfos(infos));
213 }
214 
215 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils005, TestSize.Level3)
216 {
217     std::vector<DumpCommonUtils::PidInfo> infos;
218     ASSERT_TRUE(DUMP_COMMON_UTILS.GetPidInfos(infos));
219     ASSERT_FALSE(infos.empty()) << "GetPidInfos result is empty.";
220 
221     std::vector<DumpCommonUtils::PidInfo> infosAll;
222     ASSERT_TRUE(DUMP_COMMON_UTILS.GetPidInfos(infosAll, true));
223 }
224 
225 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils006, TestSize.Level3)
226 {
227     std::vector<int> pids;
228     ASSERT_TRUE(DUMP_COMMON_UTILS.GetUserPids(pids));
229 }
230 } // namespace HiviewDFX
231 } // namespace OHOS