• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <fstream>
16 #include "bbox_detector_unit_test.h"
17 
18 #include "bbox_detector_plugin.h"
19 
20 #include "bbox_detectors_mock.h"
21 #include "bbox_event_recorder.h"
22 #include "hisysevent_util_mock.h"
23 #include "panic_report_recovery.h"
24 #include "smart_parser.h"
25 #include "sys_event.h"
26 #include "sys_event_dao.h"
27 #include "tbox.h"
28 using namespace std;
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 using namespace testing;
33 using namespace testing::ext;
SetUpTestCase(void)34 void BBoxDetectorUnitTest::SetUpTestCase(void) {}
35 
TearDownTestCase(void)36 void BBoxDetectorUnitTest::TearDownTestCase(void) {}
37 
SetUp(void)38 void BBoxDetectorUnitTest::SetUp(void)
39 {
40     FileUtil::ForceCreateDirectory("/data/test/bbox/panic_log/");
41     FileUtil::ForceCreateDirectory("/data/test/bbox/ap_log/");
42 }
43 
TearDown(void)44 void BBoxDetectorUnitTest::TearDown(void)
45 {
46     FileUtil::ForceRemoveDirectory("/data/test/bbox/");
47 }
48 
GenerateFile(const std::string & path,unsigned int size)49 void GenerateFile(const std::string &path, unsigned int size)
50 {
51     constexpr int bufferSize = 1024;
52     constexpr int charSize = 26;
53     std::ofstream ofs;
54     ofs.open(path, std::ios::out | std::ios::trunc);
55     for (unsigned int i = 0; i < size; i++) {
56         for (int j = 0; j < bufferSize; ++j) {
57             ofs << static_cast<char>(rand() % charSize + 'a');
58         }
59     }
60     ofs << std::endl;
61     ofs.close();
62 }
63 
64 /**
65  * @tc.name: BBoxDetectorUnitTest001
66  * @tc.desc: check bbox config parser whether it is passed.
67  *           1.parse bbox config;
68  *           2.check result;
69  * @tc.type: FUNC
70  * @tc.require:
71  * @tc.author: liuwei
72  */
73 HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest001, TestSize.Level1)
74 {
75     /**
76      * @tc.steps: step1. init bboxDetector and parse hisysevent.
77      */
78 
79     /**
80      * @tc.steps: step2. check func.
81      * @tc.expect: get right result for checking
82      */
83     SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
84     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
85     auto testPlugin = make_shared<BBoxDetectorPlugin>();
86     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
87     EXPECT_EQ(testPlugin->CanProcessEvent(event), true);
88 }
89 
90 /**
91  * @tc.name: BBoxDetectorUnitTest002
92  * @tc.desc: check whether fault is processed,and check whether fault file is pasered
93  *           1. check whether fault file is pasered;
94  * @tc.type: FUNC
95  * @tc.require:
96  * @tc.author: liuwei
97  */
98 HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest002, TestSize.Level0)
99 {
100     /**
101      * @tc.steps: step1. construct panic file path
102      * @tc.steps: step2. Analysis panic event
103      * @tc.steps: step3. check result
104      */
105     string stack = R"("dump_backtrace+0x0/0x184"
106                      "show_stack+0x2c/0x3c",
107                      "dump_stack+0xc0/0x11c",
108                      "panic+0x1cc/0x3dc",
109                      "sysrq_handle_term+0x0/0x94",
110                      "__handle_sysrq+0x15c/0x184",
111                      "write_sysrq_trigger+0xb0/0xf4",
112                      "proc_reg_write+0x94/0x120",
113                      "vfs_write+0x184/0x380",
114                      "ksys_write+0x74/0xc8",
115                      "__arm64_sys_write+0x24/0x34",
116                      "el0_svc_common+0x104/0x180",
117                      "do_el0_svc+0x2c/0x3c",
118                      "el0_svc+0x10/0x1c",
119                      "vks_write+0x123/0xa6",
120                      "el0_sync+0x180/0x1c0"
121                     )";
122 
123     std::map<std::string, std::string> eventInfos;
124     eventInfos.insert(std::pair("END_STACK", stack));
125     eventInfos.insert(std::pair("PNAME", "PANIC"));
126     eventInfos.insert(std::pair("Eventid", "901000002"));
127     Tbox::FilterTrace(eventInfos);
128 
129     EXPECT_STREQ(eventInfos["FIRST_FRAME"].c_str(), "sysrq_handle_term+0x0/0x94");
130     EXPECT_STREQ(eventInfos["SECOND_FRAME"].c_str(), "__handle_sysrq+0x15c/0x184");
131 }
132 
133 /**
134  * @tc.name: BBoxDetectorUnitTest003
135  * @tc.desc: check the interface IsRecoveryPanicEvent.
136  * @tc.type: FUNC
137  * @tc.require:
138  * @tc.author: liuwei
139  */
140 HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest003, TestSize.Level1)
141 {
142     SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
143     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
144     sysEvent->SetEventValue("LOG_PATH", "/data/test/bbox/panic_log/test");
145     ASSERT_TRUE(PanicReport::IsRecoveryPanicEvent(sysEvent));
146     sysEvent->SetEventValue("LOG_PATH", "OTHERS");
147     ASSERT_FALSE(PanicReport::IsRecoveryPanicEvent(sysEvent));
148 }
149 
150 /**
151  * @tc.name: BBoxDetectorUnitTest004
152  * @tc.desc: check whether the param of isShortStartUp work.
153  * @tc.type: FUNC
154  * @tc.require:
155  * @tc.author: liuwei
156  */
157 HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest004, TestSize.Level1)
158 {
159     ASSERT_TRUE(PanicReport::InitPanicReport());
160     PanicReport::IsLastShortStartUp();
161     ASSERT_TRUE(PanicReport::InitPanicReport());
162     ASSERT_TRUE(PanicReport::IsLastShortStartUp());
163 }
164 
165 /**
166  * @tc.name: BBoxDetectorUnitTest005
167  * @tc.desc: check whether the file will be deleted when it is over limit.
168  * @tc.type: FUNC
169  * @tc.require:
170  * @tc.author: liuwei
171  */
172 HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest005, TestSize.Level1)
173 {
174     constexpr const char* testFile = "/data/test/bbox/panic_log/test";
175     constexpr const unsigned int testFileSize = 5 * 1024 + 1;
176     GenerateFile(testFile, testFileSize);
177     ASSERT_TRUE(FileUtil::FileExists(testFile));
178     ASSERT_TRUE(PanicReport::InitPanicReport());
179     ASSERT_FALSE(FileUtil::FileExists(testFile));
180 }
181 
182 /**
183  * @tc.name: BBoxDetectorUnitTest006
184  * @tc.desc: check whether the process of compress is work.
185  * @tc.type: FUNC
186  * @tc.require:
187  * @tc.author: liuwei
188  */
189 HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest006, TestSize.Level1)
190 {
191     constexpr const char* testHmKLog = "/data/test/bbox/ap_log/hm_klog.txt";
192     GenerateFile(testHmKLog, 0);
193     constexpr const char* testLastFastBootLog = "/data/test/bbox/ap_log/last_fastboot_log";
194     GenerateFile(testLastFastBootLog, 0);
195 
196     constexpr const char* testHmSnap = "/data/test/bbox/ap_log/hm_snapshot.txt";
197     GenerateFile(testHmSnap, 0);
198     constexpr const char* testHappenTimeStr1 = "20170805171207-00000001";
199     PanicReport::CompressAndCopyLogFiles("/data/test/bbox/", testHappenTimeStr1);
200     auto bboxSaveLogFlags = PanicReport::LoadBboxSaveFlagFromFile();
201     ASSERT_EQ(bboxSaveLogFlags.happenTime, testHappenTimeStr1);
202     ASSERT_TRUE(FileUtil::FileExists(PanicReport::GetBackupFilePath(testHappenTimeStr1)));
203 
204     GenerateFile(testHmSnap, 40 * 1024);
205     constexpr const char* testHappenTimeStr2 = "20170805171207-00000002";
206     PanicReport::CompressAndCopyLogFiles("/data/test/bbox/", testHappenTimeStr2);
207     ASSERT_EQ(FileUtil::GetFolderSize("/data/test/bbox/panic_log/"), 0);
208 
209     GenerateFile(testHmSnap, 6 * 1024);
210     constexpr const char* testHappenTimeStr3 = "20170805171207-00000003";
211     PanicReport::CompressAndCopyLogFiles("/data/test/bbox/", testHappenTimeStr3);
212     bboxSaveLogFlags = PanicReport::LoadBboxSaveFlagFromFile();
213     ASSERT_EQ(bboxSaveLogFlags.happenTime, testHappenTimeStr3);
214     ASSERT_TRUE(FileUtil::FileExists(PanicReport::GetBackupFilePath(testHappenTimeStr3)));
215 }
216 
217 /**
218  * @tc.name: BBoxDetectorUnitTest007
219  * @tc.desc: check whether the plugin initialize success.
220  * @tc.type: FUNC
221  * @tc.require:
222  * @tc.author: liuwei
223  */
224 HWTEST_F(BBoxDetectorUnitTest, BBoxDetectorUnitTest007, TestSize.Level1)
225 {
226     auto bboxSaveFlags = PanicReport::LoadBboxSaveFlagFromFile();
227     bboxSaveFlags.factoryRecoveryTime = "testTime";
228     PanicReport::SaveBboxLogFlagsToFile(bboxSaveFlags);
229     MockHiviewContext hiviewContext;
230     auto eventLoop = std::make_shared<MockEventLoop>();
231     eventLoop->StartLoop();
232     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
233     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
234     EXPECT_CALL(MockHisyseventUtil::GetInstance(), IsEventProcessed).WillRepeatedly(Return(true));
235     auto testPlugin = make_shared<BBoxDetectorPlugin>();
236     testPlugin->SetHiviewContext(&hiviewContext);
237     testPlugin->OnLoad();
238     std::this_thread::sleep_for(std::chrono::seconds(5));
239     ASSERT_TRUE(PanicReport::LoadBboxSaveFlagFromFile().isPanicUploaded);
240 }
241 
242 /**
243  * @tc.name: BboxEventRecorder001
244  * @tc.desc: check BboxEventRecorder.
245  * @tc.type: FUNC
246  */
247 HWTEST(BboxEventRecorderTets, BboxEventRecorder001, TestSize.Level1)
248 {
249     BboxEventRecorder recorder;
250     std::string event = "MODEMCRASH";
251     time_t now = time(nullptr);
252     std::string logPath = "/root/testDir";
253 
254     ASSERT_FALSE(recorder.IsExistEvent(event, now, logPath));
255     ASSERT_TRUE(recorder.AddEventToMaps(event, now, logPath));
256     ASSERT_TRUE(recorder.IsExistEvent(event, now, logPath));
257 
258     logPath = "/root/testDir2";
259     ASSERT_FALSE(recorder.IsExistEvent(event, now, logPath));
260 }
261 
LocalTimeFormat()262 std::string LocalTimeFormat()
263 {
264     time_t t = time(nullptr);
265     struct tm *tm_info = localtime(&t);
266     const size_t bufferLen = 16;
267     const size_t resultLen = 14;
268     char buffer[bufferLen] = {0};
269     if (strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S", tm_info) != resultLen) {
270         return "";
271     }
272     return std::string(buffer);
273 }
274 
275 /**
276  * @tc.name: StartBootScan001
277  * @tc.desc: coverage StartBootScan.
278  * @tc.type: FUNC
279  */
280 HWTEST(StartBootScanTest, StartBootScan001, TestSize.Level1)
281 {
282     BBoxDetectorPlugin plugin;
283     plugin.OnLoad();
284     FileUtil::ForceCreateDirectory("/data/hisi_logs/");
285     FileUtil::ForceCreateDirectory("/data/log/bbox/");
286     /* FileUtil::SaveStringToFile("/data/hisi_logs/history.log", "done", true); */
287     std::string bboxContent = "[system exception],module[AP],category[NORMALBOOT],event[COLDBOOT]," \
288                                "time[19700106031950-00001111],sysreboot[true],errdesc[boot_up_keypoint:46]," \
289                                "logpath[/data/test/hisi_logs/]\n" \
290                                "[system exception],module[AP],category[],event[COLDBOOT]," \
291                                "time[19700106031950-00001111],sysreboot[true],errdesc[boot_up_keypoint:46]," \
292                                "logpath[/data/test/hisi_logs/]\n" \
293                                "[system exception],module[AP],category[TEST:123],event[COLDBOOT]," \
294                                "time[19700106031950-00001111],sysreboot[true],errdesc[boot_up_keypoint:46]," \
295                                "logpath[/data/test/hisi_logs/]";
296     bboxContent += "\n[system exception],module[AP],category[TEST:123],event[COLDBOOT],time[" + LocalTimeFormat() +
297         "-00001111],sysreboot[true],errdesc[boot_up_keypoint:46],logpath[/data/test/hisi_logs/]\n";
298     EXPECT_CALL(MockHisyseventUtil::GetInstance(), IsEventProcessed).WillRepeatedly(Return(false));
299     FileUtil::SaveStringToFile("/data/log/bbox/history.log", bboxContent, true);
300     plugin.StartBootScan();
301     ASSERT_TRUE(plugin.eventRecorder_ == nullptr);
302 
303     void SetHiviewContext(HiviewContext* context);
304 }
305 
306 /**
307  * @tc.name: StartBootScan002
308  * @tc.desc: coverage StartBootScan.
309  * @tc.type: FUNC
310  */
311 HWTEST(StartBootScanTest, StartBootScan002, TestSize.Level1)
312 {
313     BBoxDetectorPlugin plugin;
314     HiviewContext context;
315     plugin.SetHiviewContext(&context);
316     plugin.OnLoad();
317     FileUtil::ForceCreateDirectory("/data/hisi_logs/");
318     FileUtil::ForceCreateDirectory("/data/log/bbox/");
319     std::string hisiContent = "system exception core [CP], reason [CP_S_RILD_EXCEPTION], " \
320                                "time [19700106031950-00001111], sysreboot [false], " \
321                                "bootup_keypoint [250], category [MODEMCRASH]";
322     FileUtil::SaveStringToFile("/data/hisi_logs/history.log", hisiContent, true);
323     std::string bboxContent = "[system exception],module[AP],category[NORMALBOOT],event[COLDBOOT]," \
324                                "time[19700106031950-00001111],sysreboot[true],errdesc[boot_up_keypoint:46]," \
325                                "logpath[/data/test/hisi_logs/]\n" \
326                                "[system exception],module[AP],category[],event[COLDBOOT]," \
327                                "time[19700106031950-00001111],sysreboot[true],errdesc[boot_up_keypoint:46]," \
328                                "logpath[/data/test/hisi_logs/]\n" \
329                                "[system exception],module[AP],category[TEST:123],event[COLDBOOT]," \
330                                "time[19700106031950-00001111],sysreboot[true],errdesc[boot_up_keypoint:46]," \
331                                "logpath[/data/test/hisi_logs/]\n";
332     bboxContent += "[system exception],module[AP],category[TEST:123],event[COLDBOOT],time[" + LocalTimeFormat() +
333         "-00001111],sysreboot[true],errdesc[boot_up_keypoint:46],logpath[/data/test/hisi_logs/]\n";
334     EXPECT_CALL(MockHisyseventUtil::GetInstance(), IsEventProcessed).WillRepeatedly(Return(false));
335     FileUtil::SaveStringToFile("/data/log/bbox/history.log", bboxContent, true);
336     plugin.StartBootScan();
337     ASSERT_TRUE(plugin.eventRecorder_ == nullptr);
338 }
339 
340 /**
341  * @tc.name: SaveBboxLogFlagsToFile001
342  * @tc.desc: target dir is not exist, test SaveBboxLogFlagsToFile.
343  * @tc.type: FUNC
344  */
345 HWTEST(SaveBboxLogFlagsToFileTest, SaveBboxLogFlagsToFile001, TestSize.Level1)
346 {
347     FileUtil::ForceRemoveDirectory("/data/test/bbox/");
348 
349     PanicReport::BboxSaveLogFlags bboxSaveLogFlags;
350     ASSERT_FALSE(PanicReport::SaveBboxLogFlagsToFile(bboxSaveLogFlags));
351 }
352 
353 /**
354  * @tc.name: ClearFilesInDir001
355  * @tc.desc: flage file is open, test SaveBboxLogFlagsToFile.
356  * @tc.type: FUNC
357  */
358 HWTEST(ClearFilesInDirTest, ClearFilesInDir001, TestSize.Level1)
359 {
360     /**
361      * test case: input is invalid
362      * */
363     ASSERT_FALSE(PanicReport::ClearFilesInDir(""));
364     ASSERT_FALSE(PanicReport::ClearFilesInDir("/data/test/invalid_path"));
365 
366     /**
367      * test case: input is file path
368      * */
369     ASSERT_TRUE(FileUtil::ForceCreateDirectory("/data/test/bbox/"));
370     ASSERT_TRUE(FileUtil::SaveStringToFile("/data/test/bbox/test_file", "test file"));
371     ASSERT_FALSE(PanicReport::ClearFilesInDir("/data/test/bbox/test_file"));
372 }
373 
374 /**
375  * @tc.name: GetParamValueFromFile001
376  * @tc.desc: file path is invalid.
377  * @tc.type: FUNC
378  */
379 HWTEST(GetParamValueFromFileTest, GetParamValueFromFile001, TestSize.Level1)
380 {
381     /**
382      * test case: input is invalid
383      * */
384     ASSERT_EQ(PanicReport::GetParamValueFromFile("", "last_bootup_keypoint"), "");
385     ASSERT_EQ(PanicReport::GetParamValueFromFile("/data/test/invalid_file_path", "last_bootup_keypoint"), "");
386 }
387 
388 /**
389  * @tc.name: TryToReportRecoveryPanicEvent001
390  * @tc.desc: file path is invalid.
391  * @tc.type: FUNC
392  */
393 HWTEST(TryToReportRecoveryPanicEventTest, TryToReportRecoveryPanicEvent001, TestSize.Level1)
394 {
395     ASSERT_TRUE(FileUtil::ForceCreateDirectory("/data/test/bbox/"));
396 
397     PanicReport::BboxSaveLogFlags bboxSaveLogFlags;
398     bboxSaveLogFlags.isPanicUploaded = true;
399     bboxSaveLogFlags.happenTime = "0";
400     bboxSaveLogFlags.factoryRecoveryTime = PanicReport::GetLastRecoveryTime();
401     bboxSaveLogFlags.softwareVersion = "UT_TEST";
402     ASSERT_TRUE(PanicReport::SaveBboxLogFlagsToFile(bboxSaveLogFlags));
403 
404     ASSERT_FALSE(PanicReport::TryToReportRecoveryPanicEvent());
405 
406     bboxSaveLogFlags.isPanicUploaded = false;
407     ASSERT_TRUE(PanicReport::SaveBboxLogFlagsToFile(bboxSaveLogFlags));
408     ASSERT_FALSE(PanicReport::TryToReportRecoveryPanicEvent());
409 }
410 }  // namespace HiviewDFX
411 }  // namespace OHOS
412