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 "hidumper_configutils_test.h"
16 using namespace std;
17 using namespace testing::ext;
18 using namespace OHOS;
19 using namespace OHOS::HiviewDFX;
20 namespace OHOS {
21 namespace HiviewDFX {
22 const int HidumperConfigUtilsTest::PID_EMPTY = -1;
23 const int HidumperConfigUtilsTest::UID_EMPTY = -1;
24 const int HidumperConfigUtilsTest::PID_TEST = 100;
25 const std::string HidumperConfigUtilsTest::DUMPER_NAME = "dumper_kernel_version";
SetUpTestCase(void)26 void HidumperConfigUtilsTest::SetUpTestCase(void)
27 {
28 }
29
TearDownTestCase(void)30 void HidumperConfigUtilsTest::TearDownTestCase(void)
31 {
32 }
33
SetUp(void)34 void HidumperConfigUtilsTest::SetUp(void)
35 {
36 }
37
TearDown(void)38 void HidumperConfigUtilsTest::TearDown(void)
39 {
40 }
41
42 /**
43 * @tc.name: HidumperConfigUtils001
44 * @tc.desc: Test GetDumper by index
45 * @tc.type: FUNC
46 */
47 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils001, TestSize.Level3)
48 {
49 int index = -1;
50 std::vector<std::shared_ptr<DumpCfg>> result;
51 ConfigUtils configUtils(nullptr);
52 auto args = OptionArgs::Create();
53 args->SetPid(PID_EMPTY, UID_EMPTY);
54 DumpStatus ret = configUtils.GetDumper(index, result, args);
55 EXPECT_NE(ret, DumpStatus::DUMP_OK);
56
57 index = 100000;
58 result.clear();
59 ret = configUtils.GetDumper(index, result, args);
60 EXPECT_NE(ret, DumpStatus::DUMP_OK);
61
62 index = 0;
63 result.clear();
64 ret = configUtils.GetDumper(index, result, args);
65 EXPECT_EQ(ret, DumpStatus::DUMP_OK);
66
67 ASSERT_TRUE(!result.empty());
68 ASSERT_TRUE(!result[0]->name_.empty());
69 ASSERT_TRUE(result[0]->target_.empty());
70 ASSERT_TRUE(result[0]->args_ == args);
71 }
72
73 /**
74 * @tc.name: HidumperConfigUtils002
75 * @tc.desc: Test GetDumper by index.
76 * @tc.type: FUNC
77 */
78 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils002, TestSize.Level3)
79 {
80 int index = 1;
81 std::vector<std::shared_ptr<DumpCfg>> result;
82 ConfigUtils configUtils(nullptr);
83 auto args = OptionArgs::Create();
84 args->SetPid(PID_EMPTY, UID_EMPTY);
85 DumpStatus ret = configUtils.GetDumper(index, result, args);
86 EXPECT_EQ(ret, DumpStatus::DUMP_OK);
87
88 std::size_t allSum = result.size();
89 index = 1;
90 result.clear();
91 ret = configUtils.GetDumper(index, result, args);
92 EXPECT_EQ(ret, DumpStatus::DUMP_OK);
93 std::size_t highSum = result.size();
94
95 index = 1;
96 result.clear();
97 ret = configUtils.GetDumper(index, result, args);
98 EXPECT_EQ(ret, DumpStatus::DUMP_OK);
99 std::size_t middleSum = result.size();
100
101 index = 1;
102 result.clear();
103 ret = configUtils.GetDumper(index, result, args);
104 EXPECT_EQ(ret, DumpStatus::DUMP_OK);
105 std::size_t lowSum = result.size();
106
107 index = 1;
108 result.clear();
109 ret = configUtils.GetDumper(index, result, args);
110 EXPECT_EQ(ret, DumpStatus::DUMP_OK);
111 std::size_t noneSum = result.size();
112
113 ASSERT_TRUE(highSum <= allSum);
114 ASSERT_TRUE(middleSum <= allSum);
115 ASSERT_TRUE(lowSum <= allSum);
116 ASSERT_TRUE(noneSum <= allSum);
117 }
118
119 /**
120 * @tc.name: HidumperConfigUtils003
121 * @tc.desc: Test GetDumper by name.
122 * @tc.type: FUNC
123 */
124 HWTEST_F(HidumperConfigUtilsTest, HidumperConfigUtils003, TestSize.Level3)
125 {
126 std::string name;
127 std::vector<std::shared_ptr<DumpCfg>> result;
128 ConfigUtils configUtils(nullptr);
129 auto args = OptionArgs::Create();
130 args->SetPid(PID_EMPTY, UID_EMPTY);
131 DumpStatus ret = configUtils.GetDumper(name, result, args);
132 EXPECT_NE(ret, DumpStatus::DUMP_OK);
133
134 name = DUMPER_NAME;
135 result.clear();
136 ret = configUtils.GetDumper(name, result, args);
137 EXPECT_EQ(ret, DumpStatus::DUMP_OK);
138
139 ASSERT_TRUE(!result.empty());
140 ASSERT_TRUE(result[0]->name_ == name);
141 }
142
143 HWTEST_F(HidumperConfigUtilsTest, HidumperZipWriter001, TestSize.Level3)
144 {
145 string testfile = "/data/log/hidumpertest.txt";
146 string testzipfile = "/data/log/hidumpertest.zip";
147 system("touch /data/log/hidumpertest.txt");
148 system("echo hidumpertest > /data/log/hidumpertest.txt");
149 auto zipwriter = std::make_shared<ZipWriter>(testfile);
150 ASSERT_TRUE(zipwriter->Open());
151 ASSERT_TRUE(zipwriter->Close());
152 auto testzip = zipwriter->OpenForZipping(testfile, APPEND_STATUS_CREATE);
153 zipwriter->AddFileEntryToZip(testzip, testzipfile, testzipfile);
154 system("rm -rf /data/log/hidumpertest.txt");
155 system("rm -rf /data/log/hidumpertest.zip");
156 }
157
158 HWTEST_F(HidumperConfigUtilsTest, HidumperFileUtils001, TestSize.Level3)
159 {
160 auto fileutils = std::make_shared<FileUtils>();
161 string testpath = "/data";
162 ASSERT_TRUE(fileutils->CreateFolder(testpath));
163 testpath = "";
164 ASSERT_TRUE(fileutils->CreateFolder(testpath));
165 testpath = "test";
166 ASSERT_TRUE(!(fileutils->CreateFolder(testpath)));
167 testpath = "/data/log/testhidumper";
168 ASSERT_TRUE(fileutils->CreateFolder(testpath));
169 ASSERT_TRUE(access(testpath.c_str(), F_OK) == 0);
170 system("rm -rf /data/log/testhidumper");
171 }
172 } // namespace HiviewDFX
173 } // namespace OHOS