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