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
16 #include <gtest/gtest.h>
17 #include <vector>
18 #include <unistd.h>
19
20 #include "directory_ex.h"
21 #include "executor/zip_output.h"
22 #include "executor/fd_output.h"
23 #define private public
24 #include "raw_param.h"
25 #undef private
26
27 using namespace std;
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::HiviewDFX;
31 namespace OHOS {
32 namespace HiviewDFX {
33 class HidumperOutputTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39
40 protected:
41 static const std::string FILE_ROOT;
42 };
43
SetUpTestCase(void)44 void HidumperOutputTest::SetUpTestCase(void)
45 {
46 ForceCreateDirectory(FILE_ROOT);
47 }
TearDownTestCase(void)48 void HidumperOutputTest::TearDownTestCase(void)
49 {
50 ForceRemoveDirectory(FILE_ROOT);
51 }
SetUp(void)52 void HidumperOutputTest::SetUp(void)
53 {
54 }
TearDown(void)55 void HidumperOutputTest::TearDown(void)
56 {
57 }
58
59 const std::string HidumperOutputTest::FILE_ROOT = "/data/local/tmp/hidumper_test/";
60
61 /**
62 * @tc.name: HidumperOutputTest001
63 * @tc.desc: Test ZipOutpu with multibytes content.
64 * @tc.type: FUNC
65 */
66 HWTEST_F(HidumperOutputTest, HidumperOutputTest001, TestSize.Level3)
67 {
68 auto parameter = std::make_shared<DumperParameter>();
69 auto dump_datas = std::make_shared<std::vector<std::vector<std::string>>>();
70 auto zip_output = make_shared<ZipOutput>();
71
72 // multibytes content
73 {
74 std::string line_content;
75 line_content = "根据输出结果(win32 + vc 编译器)可以看到,"
76 "strShort 对象的地址和 c_str() 的地址很接近,"
77 "都在 strShort 对象的栈控件内,"
78 "而 strLong 的 c_str() 地址很明显与 strLong 对象地址不同,是在 heap 中分配的.";
79 std::vector<std::string> line_vector;
80 line_vector.push_back(line_content);
81 dump_datas->push_back(line_vector);
82 }
83
84 DumperOpts opts;
85 opts.path_ = FILE_ROOT + "GZ_HidumperOutputTest001.gz";
86 parameter->SetOpts(opts);
87
88 auto config = std::make_shared<DumpCfg>();
89 zip_output->SetDumpConfig(config);
90
91 DumpStatus ret = zip_output->PreExecute(parameter, dump_datas);
92 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "PreExecute failed.";
93
94 ret = zip_output->Execute();
95 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "Execute failed.";
96
97 ret = zip_output->AfterExecute();
98 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "AfterExecute failed.";
99 }
100
101 /**
102 * @tc.name: HidumperOutputTest002
103 * @tc.desc: Test ZipOutpu with content size touch buffer size.
104 * @tc.type: FUNC
105 */
106 HWTEST_F(HidumperOutputTest, HidumperOutputTest002, TestSize.Level3)
107 {
108 auto parameter = std::make_shared<DumperParameter>();
109 auto dump_datas = std::make_shared<std::vector<std::vector<std::string>>>();
110 auto zip_output = make_shared<ZipOutput>();
111
112 // Touch Buffer Size
113 {
114 std::string line_content;
115 line_content = "HidumperOutputTest001 big one!HidumperOutputTest001 big one!"
116 "HidumperOutputTest001 big one!HidumperOutputTest001 big one!"
117 "HidumperOutputTest001 big one!HidumperOutputTest001 big one!"
118 "HidumperOutputTest001 big one!HidumperOutputTest001 big one!"
119 "HidumperOut123456";
120 std::vector<std::string> line_vector;
121 line_vector.push_back(line_content);
122 dump_datas->push_back(line_vector);
123 }
124
125 DumperOpts opts;
126 opts.path_ = FILE_ROOT + "GZ_HidumperOutputTest002.gz";
127 parameter->SetOpts(opts);
128
129 DumpStatus ret = zip_output->PreExecute(parameter, dump_datas);
130 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "PreExecute failed.";
131
132 ret = zip_output->Execute();
133 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "Execute failed.";
134
135 ret = zip_output->AfterExecute();
136 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "AfterExecute failed.";
137 }
138
139 /**
140 * @tc.name: HidumperOutputTest003
141 * @tc.desc: Test ZipOutpu with content size touch buffer size - 1.
142 * @tc.type: FUNC
143 */
144 HWTEST_F(HidumperOutputTest, HidumperOutputTest003, TestSize.Level3)
145 {
146 auto parameter = std::make_shared<DumperParameter>();
147 auto dump_datas = std::make_shared<std::vector<std::vector<std::string>>>();
148 auto zip_output = make_shared<ZipOutput>();
149
150 // Touch Buffer Size - 1
151 {
152 std::string line_content;
153 line_content = "HidumperOutputTest001 big one!"
154 "HidumperOutputTest001 big one!"
155 "HidumperOutputTest001 big one!"
156 "HidumperOutputTest001 big one!"
157 "HidumperOutputTest001 big one!"
158 "HidumperOutputTest001 big one!"
159 "HidumperOutputTest001 big one!"
160 "HidumperOutputTest001 big one!"
161 "HidumperOu12345";
162 std::vector<std::string> line_vector;
163 line_vector.push_back(line_content);
164 dump_datas->push_back(line_vector);
165 }
166
167 DumperOpts opts;
168 opts.path_ = FILE_ROOT + "GZ_HidumperOutputTest003.gz";
169 parameter->SetOpts(opts);
170
171 DumpStatus ret = zip_output->PreExecute(parameter, dump_datas);
172 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "PreExecute failed.";
173
174 ret = zip_output->Execute();
175 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "Execute failed.";
176
177 ret = zip_output->AfterExecute();
178 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "AfterExecute failed.";
179 }
180
181 /**
182 * @tc.name: HidumperOutputTest004
183 * @tc.desc: Test ZipOutpu with content size touch buffer size + 1.
184 * @tc.type: FUNC
185 */
186 HWTEST_F(HidumperOutputTest, HidumperOutputTest004, TestSize.Level3)
187 {
188 auto parameter = std::make_shared<DumperParameter>();
189 auto dump_datas = std::make_shared<std::vector<std::vector<std::string>>>();
190 auto zip_output = make_shared<ZipOutput>();
191
192 // Touch Buffer Size + 1
193 {
194 std::string line_content;
195 line_content = "HidumperOutputTest001 big one!"
196 "HidumperOutputTest001 big one!"
197 "HidumperOutputTest001 big one!"
198 "HidumperOutputTest001 big one!"
199 "HidumperOutputTest001 big one!"
200 "HidumperOutputTest001 big one!"
201 "HidumperOutputTest001 big one!"
202 "HidumperOutputTest001 big one!"
203 "HidumperOu1234567";
204 std::vector<std::string> line_vector;
205 line_vector.push_back(line_content);
206 dump_datas->push_back(line_vector);
207 }
208
209 DumperOpts opts;
210 opts.path_ = FILE_ROOT + "GZ_HidumperOutputTest004.gz";
211 parameter->SetOpts(opts);
212
213 DumpStatus ret = zip_output->PreExecute(parameter, dump_datas);
214 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "PreExecute failed.";
215
216 ret = zip_output->Execute();
217 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "Execute failed.";
218
219 ret = zip_output->AfterExecute();
220 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "AfterExecute failed.";
221 }
222
223 /**
224 * @tc.name: HidumperOutputTest005
225 * @tc.desc: Test ZipOutpu with multi lines content.
226 * @tc.type: FUNC
227 */
228 HWTEST_F(HidumperOutputTest, HidumperOutputTest005, TestSize.Level3)
229 {
230 auto parameter = std::make_shared<DumperParameter>();
231 auto dump_datas = std::make_shared<std::vector<std::vector<std::string>>>();
232 auto zip_output = make_shared<ZipOutput>();
233
234 // Multi lines
235 for (int i = 1; i < 1000; i++) {
236 std::string line_content;
237 line_content = "HidumperOutputTest001 big one!"
238 "HidumperOutputTest001 big one!"
239 "HidumperOutputTest001 big one!"
240 "HidumperOutputTest001 big one!"
241 "HidumperOutputTest001 big one!"
242 "HidumperOutputTest001 big one!"
243 "HidumperOutputTest001 big one!"
244 "HidumperOutputTest001 big one!"
245 "HidumperOutputTest001 big one!"
246 "HidumperOutputTest001 big one!"
247 "HidumperOutputTest001 big one!"
248 "HidumperOutputTest001 big one!"
249 "HidumperOutputTest001 big one!"
250 "HidumperOutputTest001 big one!";
251 std::vector<std::string> line_vector;
252 line_vector.push_back(line_content);
253 dump_datas->push_back(line_vector);
254 }
255
256 DumperOpts opts;
257 opts.path_ = FILE_ROOT + "GZ_HidumperOutputTest005.gz";
258 parameter->SetOpts(opts);
259
260 DumpStatus ret = zip_output->PreExecute(parameter, dump_datas);
261 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "PreExecute failed.";
262
263 ret = zip_output->Execute();
264 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "Execute failed.";
265
266 ret = zip_output->AfterExecute();
267 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "AfterExecute failed.";
268 }
269
270 /**
271 * @tc.name: HidumperOutputTest006
272 * @tc.desc: Test ZipOutpu with short string.
273 * @tc.type: FUNC
274 */
275 HWTEST_F(HidumperOutputTest, HidumperOutputTest006, TestSize.Level3)
276 {
277 auto parameter = std::make_shared<DumperParameter>();
278 auto dump_datas = std::make_shared<std::vector<std::vector<std::string>>>();
279 auto zip_output = make_shared<ZipOutput>();
280
281 std::string line_content;
282 line_content = "cmd is:";
283 std::vector<std::string> line_vector;
284 line_vector.push_back(line_content);
285 dump_datas->push_back(line_vector);
286
287 DumperOpts opts;
288 opts.path_ = FILE_ROOT + "GZ_HidumperOutputTest006.gz";
289 parameter->SetOpts(opts);
290
291 DumpStatus ret = zip_output->PreExecute(parameter, dump_datas);
292 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "PreExecute failed.";
293
294 ret = zip_output->Execute();
295 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "Execute failed.";
296
297 ret = zip_output->AfterExecute();
298 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "AfterExecute failed.";
299 }
300
301 /**
302 * @tc.name: HidumperOutputTest007
303 * @tc.desc: Test FdOutput.
304 * @tc.type: FUNC
305 */
306 HWTEST_F(HidumperOutputTest, HidumperOutputTest007, TestSize.Level3)
307 {
308 auto parameter = std::make_shared<DumperParameter>();
309 std::vector<std::u16string> args;
310 auto reqCtl = std::make_shared<RawParam>(0, 0, 0, args, STDOUT_FILENO);
311 parameter->setClientCallback(reqCtl);
312 auto dumpDatas = std::make_shared<std::vector<std::vector<std::string>>>();
313 auto fdOutput = make_shared<FDOutput>();
314
315 std::string lineContent;
316 lineContent = "this is FdOutputTest";
317 std::vector<std::string> lineVector;
318 lineVector.push_back(lineContent);
319 dumpDatas->push_back(lineVector);
320
321 DumpStatus ret = fdOutput->PreExecute(parameter, dumpDatas);
322 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "PreExecute failed.";
323
324 ret = fdOutput->Execute();
325 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "Execute failed.";
326
327 ret = fdOutput->AfterExecute();
328 ASSERT_TRUE(ret == DumpStatus::DUMP_OK) << "AfterExecute failed.";
329 }
330 } // namespace HiviewDFX
331 } // namespace OHOS
332