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 #include "dumper_opts.h"
19 #include "raw_param.h"
20 using namespace std;
21 using namespace testing::ext;
22 using namespace OHOS;
23 using namespace OHOS::HiviewDFX;
24 namespace OHOS {
25 namespace HiviewDFX {
26 const int HidumperConfigUtilsTest::PID_EMPTY = -1;
27 const int HidumperConfigUtilsTest::UID_EMPTY = -1;
28 const int HidumperConfigUtilsTest::PID_TEST = 100;
29 const std::string HidumperConfigUtilsTest::DUMPER_NAME = "dumper_kernel_version";
30 const OHOS::HiviewDFX::DumpCommonUtils DUMP_COMMON_UTILS = *(std::make_shared<OHOS::HiviewDFX::DumpCommonUtils>());
SetUpTestCase(void)31 void HidumperConfigUtilsTest::SetUpTestCase(void)
32 {
33 }
34 
TearDownTestCase(void)35 void HidumperConfigUtilsTest::TearDownTestCase(void)
36 {
37 }
38 
SetUp(void)39 void HidumperConfigUtilsTest::SetUp(void)
40 {
41 }
42 
TearDown(void)43 void HidumperConfigUtilsTest::TearDown(void)
44 {
45 }
46 
47 /**
48  * @tc.name: HidumperConfigUtils001
49  * @tc.desc: Test GetDumper by index
50  * @tc.type: FUNC
51  */
52 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils001, TestSize.Level3)
53 {
54     int index = -1;
55     std::vector<std::shared_ptr<DumpCfg>> result;
56     ConfigUtils configUtils(nullptr);
57     auto args = OptionArgs::Create();
58     args->SetPid(PID_EMPTY, UID_EMPTY);
59     DumpStatus ret = configUtils.GetDumper(index, result, args);
60     EXPECT_NE(ret, DumpStatus::DUMP_OK);
61 
62     index = 100000;
63     result.clear();
64     ret = configUtils.GetDumper(index, result, args);
65     EXPECT_NE(ret, DumpStatus::DUMP_OK);
66 
67     index = 0;
68     result.clear();
69     ret = configUtils.GetDumper(index, result, args);
70     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
71 
72     ASSERT_TRUE(!result.empty());
73     ASSERT_TRUE(!result[0]->name_.empty());
74     ASSERT_TRUE(result[0]->target_.empty());
75     ASSERT_TRUE(result[0]->args_ == args);
76 }
77 
78 /**
79  * @tc.name: HidumperConfigUtils002
80  * @tc.desc: Test GetDumper by index.
81  * @tc.type: FUNC
82  */
83 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils002, TestSize.Level3)
84 {
85     int index = 1;
86     std::vector<std::shared_ptr<DumpCfg>> result;
87     ConfigUtils configUtils(nullptr);
88     auto args = OptionArgs::Create();
89     args->SetPid(PID_EMPTY, UID_EMPTY);
90     DumpStatus ret = configUtils.GetDumper(index, result, args);
91     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
92 
93     std::size_t allSum = result.size();
94     index = 1;
95     result.clear();
96     ret = configUtils.GetDumper(index, result, args);
97     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
98     std::size_t highSum = result.size();
99 
100     index = 1;
101     result.clear();
102     ret = configUtils.GetDumper(index, result, args);
103     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
104     std::size_t middleSum = result.size();
105 
106     index = 1;
107     result.clear();
108     ret = configUtils.GetDumper(index, result, args);
109     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
110     std::size_t lowSum = result.size();
111 
112     index = 1;
113     result.clear();
114     ret = configUtils.GetDumper(index, result, args);
115     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
116     std::size_t noneSum = result.size();
117 
118     ASSERT_TRUE(highSum <= allSum);
119     ASSERT_TRUE(middleSum <= allSum);
120     ASSERT_TRUE(lowSum <= allSum);
121     ASSERT_TRUE(noneSum <= allSum);
122 }
123 
124 /**
125  * @tc.name: HidumperConfigUtils003
126  * @tc.desc: Test GetDumper by name.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils003, TestSize.Level3)
130 {
131     std::string name;
132     std::vector<std::shared_ptr<DumpCfg>> result;
133     ConfigUtils configUtils(nullptr);
134     auto args = OptionArgs::Create();
135     args->SetPid(PID_EMPTY, UID_EMPTY);
136     DumpStatus ret = configUtils.GetDumper(name, result, args);
137     EXPECT_NE(ret, DumpStatus::DUMP_OK);
138 
139     name = DUMPER_NAME;
140     result.clear();
141     ret = configUtils.GetDumper(name, result, args);
142     EXPECT_EQ(ret, DumpStatus::DUMP_OK);
143 
144     ASSERT_TRUE(!result.empty());
145     ASSERT_TRUE(result[0]->name_ == name);
146 }
147 
148 /**
149  * @tc.name: HidumperConfigUtils004
150  * @tc.desc: Test configUtil HandleDumpLog.
151  * @tc.type: FUNC
152  */
153 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils004, TestSize.Level3)
154 {
155     std::vector<shared_ptr<DumpCfg>> dumpCfgs;
156     auto parameter = std::make_shared<DumperParameter>();
157     DumperOpts opts;
158     opts.isDumpLog_ = true;
159     opts.logArgs_.push_back("hidumper");
160     parameter->SetOpts(opts);
161     ConfigUtils configUtils(parameter);
162     bool ret = configUtils.HandleDumpLog(dumpCfgs);
163     ASSERT_TRUE(ret);
164 }
165 
166 /**
167  * @tc.name: HidumperConfigUtils005
168  * @tc.desc: Test configUtil HandleDumpLog.
169  * @tc.type: FUNC
170  */
171 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils005, TestSize.Level3)
172 {
173     std::vector<shared_ptr<DumpCfg>> dumpCfgs;
174     auto parameter = std::make_shared<DumperParameter>();
175     DumperOpts opts;
176     opts.isDumpLog_ = true;
177     opts.logArgs_.push_back("hidumper");
178     parameter->SetOpts(opts);
179     ConfigUtils configUtils(parameter);
180     bool ret = configUtils.HandleDumpLog(dumpCfgs);
181     ASSERT_TRUE(ret);
182 }
183 
184 /**
185  * @tc.name: HidumperConfigUtils006
186  * @tc.desc: Test configUtil isDumpSystemSystem.
187  * @tc.type: FUNC
188  */
189 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils006, TestSize.Level3)
190 {
191     std::vector<shared_ptr<DumpCfg>> dumpCfgs;
192     auto parameter = std::make_shared<DumperParameter>();
193     DumperOpts opts;
194     opts.isDumpSystem_ = true;
195     opts.isDumpService_ = true;
196     opts.isDumpList_ = false;
197     opts.isDumpCpuFreq_ = true;
198     opts.isDumpCpuUsage_ = true;
199     opts.isDumpMem_ = true;
200     opts.isShowSmaps_ = true;
201     opts.isDumpStorage_ = true;
202     opts.isDumpNet_ = true;
203     opts.isAppendix_ = true;
204     opts.logArgs_.push_back("hidumper");
205     parameter->SetOpts(opts);
206     ConfigUtils configUtils(parameter);
207     configUtils.HandleDumpSystem(dumpCfgs);
208     bool ret = true;
209     ret = configUtils.HandleDumpCpuFreq(dumpCfgs);
210     ASSERT_FALSE(ret);
211     ret = configUtils.HandleDumpCpuUsage(dumpCfgs);
212     ASSERT_FALSE(ret);
213     ret = configUtils.HandleDumpMem(dumpCfgs);
214     ASSERT_FALSE(ret);
215     ret = configUtils.HandleDumpMemShowMaps(dumpCfgs);
216     ASSERT_FALSE(ret);
217     ret = configUtils.HandleDumpStorage(dumpCfgs);
218     ASSERT_FALSE(ret);
219     ret = configUtils.HandleDumpNet(dumpCfgs);
220     ASSERT_FALSE(ret);
221     ret = configUtils.HandleDumpAppendix(dumpCfgs);
222     ASSERT_TRUE(ret);
223 }
224 
225 /**
226  * @tc.name: HidumperConfigUtils007
227  * @tc.desc: Test configUtil HandleDumpService.
228  * @tc.type: FUNC
229  */
230 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils007, TestSize.Level3)
231 {
232     std::vector<shared_ptr<DumpCfg>> dumpCfgs;
233     auto parameter = std::make_shared<DumperParameter>();
234     DumperOpts opts;
235     opts.isDumpList_ = false;
236     opts.isDumpService_ = true;
237     opts.logArgs_.push_back("hidumper");
238     parameter->SetOpts(opts);
239     ConfigUtils configUtils(parameter);
240     bool ret = configUtils.HandleDumpService(dumpCfgs);
241     ASSERT_TRUE(ret);
242 }
243 
244 /**
245  * @tc.name: HidumperConfigUtils008
246  * @tc.desc: Test configUtil HandleDumpList.
247  * @tc.type: FUNC
248  */
249 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils008, TestSize.Level3)
250 {
251     std::vector<shared_ptr<DumpCfg>> dumpCfgs;
252     auto parameter = std::make_shared<DumperParameter>();
253     DumperOpts opts;
254     opts.isDumpSystemAbility_ = false;
255     opts.isDumpList_ = true;
256     opts.isDumpService_ = true;
257     opts.logArgs_.push_back("hidumper");
258     parameter->SetOpts(opts);
259     ConfigUtils configUtils(parameter);
260     bool ret = configUtils.HandleDumpList(dumpCfgs);
261     ASSERT_TRUE(ret);
262 }
263 
264 /**
265  * @tc.name: HidumperConfigUtils009
266  * @tc.desc: Test configUtil HandleDumpJsHeapMem.
267  * @tc.type: FUNC
268  */
269 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils009, TestSize.Level3)
270 {
271     std::vector<shared_ptr<DumpCfg>> dumpCfgs;
272     auto parameter = std::make_shared<DumperParameter>();
273     DumperOpts opts;
274     opts.isDumpJsHeapMem_ = true;
275     opts.dumpJsHeapMemPid_ = PID_EMPTY;
276     parameter->SetOpts(opts);
277     ConfigUtils configUtils(parameter);
278     bool ret = configUtils.HandleDumpJsHeapMem(dumpCfgs);
279     ASSERT_FALSE(ret);
280 }
281 
282 /**
283  * @tc.name: HidumperConfigUtils010
284  * @tc.desc: Test configUtil HandleDumpStorage.
285  * @tc.type: FUNC
286  */
287 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils010, TestSize.Level3)
288 {
289     std::vector<shared_ptr<DumpCfg>> dumpCfgs;
290     auto parameter = std::make_shared<DumperParameter>();
291     DumperOpts opts;
292     opts.isDumpStorage_ = true;
293     opts.storagePid_ = PID_EMPTY;
294     parameter->SetOpts(opts);
295     ConfigUtils configUtils(parameter);
296     bool ret = configUtils.HandleDumpStorage(dumpCfgs);
297     ASSERT_TRUE(ret);
298 }
299 
300 /**
301  * @tc.name: HidumperConfigUtils011
302  * @tc.desc: Test configUtil Nest overceed NEST_MAX.
303  * @tc.type: FUNC
304  */
305 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils011, TestSize.Level3)
306 {
307     std::string name = "configUtil test";
308     std::vector<shared_ptr<DumpCfg>> result;
309     std::shared_ptr<OptionArgs> args;
310     ConfigUtils configUtils(nullptr);
311     DumpStatus ret = configUtils.GetGroup(name, result, args, DumperConstant::LEVEL_HIGH, 11);
312     ASSERT_EQ(ret, DumpStatus::DUMP_INVALID_ARG);
313     ret = configUtils.GetGroup(0, result, args, DumperConstant::LEVEL_HIGH, 11);
314     ASSERT_EQ(ret, DumpStatus::DUMP_INVALID_ARG);
315 }
316 
317 /**
318  * @tc.name: HidumperConfigUtils012
319  * @tc.desc: Test configUtil CopySmaps.
320  * @tc.type: FUNC
321  */
322 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils012, TestSize.Level3)
323 {
324     DumperOpts opts;
325     auto parameter = std::make_shared<DumperParameter>();
326     parameter->SetOpts(opts);
327     ConfigUtils configUtils(parameter);
328     bool ret = configUtils.CopySmaps();
329     ASSERT_TRUE(ret == false);
330 }
331 
332 HWTEST_F(HidumperConfigUtilsTest, HidumperZipWriter001, TestSize.Level3)
333 {
334     string testfile = "/data/log/hidumpertest.txt";
335     string testzipfile = "/data/log/hidumpertest.zip";
336     system("touch /data/log/hidumpertest.txt");
337     system("echo hidumpertest > /data/log/hidumpertest.txt");
338     auto zipwriter = std::make_shared<ZipWriter>(testzipfile);
339     ASSERT_TRUE(zipwriter->Open());
340     ASSERT_TRUE(zipwriter->Close());
341     auto testzip = zipwriter->OpenForZipping(testfile, APPEND_STATUS_CREATE);
342     zipwriter->AddFileEntryToZip(testzip, testzipfile, testzipfile);
343     system("rm -rf /data/log/hidumpertest.txt");
344     system("rm -rf /data/log/hidumpertest.zip");
345 }
346 
347 HWTEST_F(HidumperConfigUtilsTest, HidumperFileUtils001, TestSize.Level3)
348 {
349     auto fileutils = std::make_shared<FileUtils>();
350     string testpath = "/data";
351     ASSERT_TRUE(fileutils->CreateFolder(testpath));
352     testpath = "";
353     ASSERT_TRUE(fileutils->CreateFolder(testpath));
354     testpath = "test";
355     ASSERT_TRUE(!(fileutils->CreateFolder(testpath)));
356     testpath = "/data/log/testhidumper";
357     ASSERT_TRUE(fileutils->CreateFolder(testpath));
358     ASSERT_TRUE(access(testpath.c_str(), F_OK) == 0);
359     system("rm -rf /data/log/testhidumper");
360 }
361 
362 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils001, TestSize.Level3)
363 {
364     system("mkdir /data/log/hidumpertest/");
365     system("touch /data/log/hidumpertest/1.log");
366     system("touch /data/log/hidumpertest/a.log");
367     const std::string path = "/data/log/hidumpertest";
368     bool digit = true;
369     std::vector<std::string> strs = DUMP_COMMON_UTILS.GetSubNodes(path, digit);
370     ASSERT_TRUE(strs.size() == 1);
371 
372     digit = false;
373     strs = DUMP_COMMON_UTILS.GetSubNodes(path, digit);
374     ASSERT_TRUE(strs.size() == 2);
375     system("rm -rf /data/log/hidumpertest");
376 }
377 
378 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils002, TestSize.Level3)
379 {
380     const std::string pathTest = "/data";
381     ASSERT_TRUE(DUMP_COMMON_UTILS.IsDirectory(pathTest));
382 
383     system("touch /data/log/hidumpertest.txt");
384     const std::string pathTest2 = "/data/log/hidumpertest.txt";
385     ASSERT_FALSE(DUMP_COMMON_UTILS.IsDirectory(pathTest2));
386     system("rm -rf /data/log/hidumpertest.txt");
387 }
388 
389 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils003, TestSize.Level3)
390 {
391     std::vector<int32_t> pids = DUMP_COMMON_UTILS.GetAllPids();
392     ASSERT_FALSE(pids.empty()) << "GetAllPids result is empty.";
393 }
394 
395 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils004, TestSize.Level3)
396 {
397     std::vector<DumpCommonUtils::CpuInfo> infos;
398     ASSERT_TRUE(DUMP_COMMON_UTILS.GetCpuInfos(infos));
399 }
400 
401 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils005, TestSize.Level3)
402 {
403     std::vector<DumpCommonUtils::PidInfo> infos;
404     ASSERT_TRUE(DUMP_COMMON_UTILS.GetPidInfos(infos));
405     ASSERT_FALSE(infos.empty()) << "GetPidInfos result is empty.";
406 
407     std::vector<DumpCommonUtils::PidInfo> infosAll;
408     ASSERT_TRUE(DUMP_COMMON_UTILS.GetPidInfos(infosAll, true));
409 }
410 
411 HWTEST_F(HidumperConfigUtilsTest, HidumpCommonUtils006, TestSize.Level3)
412 {
413     std::vector<int> pids;
414     ASSERT_TRUE(DUMP_COMMON_UTILS.GetUserPids(pids));
415 }
416 } // namespace HiviewDFX
417 } // namespace OHOS