• 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 "bbox_detector_module_test.h"
16 
17 #include "bbox_detector_plugin.h"
18 
19 #include "bbox_detectors_mock.h"
20 #include "common_defines.h"
21 #include "event.h"
22 #include "file_util.h"
23 #include "hisysevent_util_mock.h"
24 #include "log_util.h"
25 #include "string_util.h"
26 #include "sys_event.h"
27 #include "time_util.h"
28 
29 using namespace std;
30 namespace OHOS {
31 namespace HiviewDFX {
32 using namespace testing;
33 using namespace testing::ext;
SetUpTestCase(void)34 void BBoxDetectorModuleTest::SetUpTestCase(void) {}
35 
TearDownTestCase(void)36 void BBoxDetectorModuleTest::TearDownTestCase(void) {}
37 
SetUp(void)38 void BBoxDetectorModuleTest::SetUp(void)
39 {
40     EXPECT_CALL(MockHisyseventUtil::GetInstance(), IsEventProcessed).WillRepeatedly(Return(false));
41 }
42 
TearDown(void)43 void BBoxDetectorModuleTest::TearDown(void) {}
44 
45 /**
46  * @tc.name: BBoxDetectorModuleTest001
47  * @tc.desc: check whether fault is processed.
48  *           1. check whether event is valid;
49  *           2. check whether category and reason is ignored;
50  * @tc.type: FUNC
51  * @tc.require:
52  * @tc.author: liuwei
53  */
54 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest001, TestSize.Level0)
55 {
56     /**
57      * @tc.steps: step1. construct PANIC SysEvent
58      * @tc.steps: step2. construct BBOXDetectorPlugin
59      * @tc.steps: step3. OnEvent
60      * @tc.steps: step4. check result
61      */
62     SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
63     sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
64     sysEventCreator.SetKeyValue("name_", "PANIC");
65     sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
66     sysEventCreator.SetKeyValue("LOG_PATH", "/data/test/hisi_logs/");
67     sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
68     sysEventCreator.SetKeyValue("MODULE", "AP");
69     sysEventCreator.SetKeyValue("REASON", "AP_S_PANIC");
70 
71     FileUtil::ForceCreateDirectory("/data/test/hisi_logs/19700106031950-00001111/");
72     FileUtil::SaveStringToFile("/data/test/hisi_logs/19700106031950-00001111/DONE", "done", true);
73     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
74     auto testPlugin = make_shared<BBoxDetectorPlugin>();
75     MockHiviewContext hiviewContext;
76     auto eventLoop = std::make_shared<MockEventLoop>();
77     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
78     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
79     testPlugin->SetHiviewContext(&hiviewContext);
80     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
81     testPlugin->OnLoad();
82     testPlugin->OnEvent(event);
83     ASSERT_EQ(sysEvent->GetEventValue("MODULE"), "AP");
84     ASSERT_EQ(sysEvent->GetEventValue("REASON"), "AP_S_PANIC");
85     ASSERT_EQ(sysEvent->GetEventValue("LOG_PATH"), "/data/test/hisi_logs/19700106031950-00001111");
86 }
87 
88 /**
89  * @tc.name: BBoxDetectorModuleTest002
90  * @tc.desc: check whether fault is processed.
91  *           1. check whether event is valid;
92  *           2. check whether category and reason is ignored;
93  * @tc.type: FUNC
94  * @tc.require:
95  * @tc.author: liuwei
96  */
97 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest002, TestSize.Level1)
98 {
99     /**
100      * @tc.steps: step1. construct HWWATCHDOG SysEvent
101      * @tc.steps: step2. construct BBOXDetectorPlugin
102      * @tc.steps: step3. OnEvent
103      * @tc.steps: step4. check result
104      */
105     SysEventCreator sysEventCreator("KERNEL_VENDOR", "HWWATCHDOG", SysEventCreator::FAULT);
106     sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
107     sysEventCreator.SetKeyValue("name_", "HWWATCHDOG");
108     sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
109     sysEventCreator.SetKeyValue("LOG_PATH", "/data/test/hisi_logs/");
110     sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
111     sysEventCreator.SetKeyValue("MODULE", "AP");
112     sysEventCreator.SetKeyValue("REASON", "AP_S_HWWATCHDOG");
113 
114     FileUtil::ForceCreateDirectory("/data/test/hisi_logs/19700106031950-00001111/");
115     FileUtil::SaveStringToFile("/data/test/hisi_logs/19700106031950-00001111/DONE", "done", true);
116     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
117     auto testPlugin = make_shared<BBoxDetectorPlugin>();
118     MockHiviewContext hiviewContext;
119     auto eventLoop = std::make_shared<MockEventLoop>();
120     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
121     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
122     testPlugin->SetHiviewContext(&hiviewContext);
123     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
124     testPlugin->OnLoad();
125     testPlugin->OnEvent(event);
126     ASSERT_EQ(sysEvent->GetEventValue("MODULE"), "AP");
127     ASSERT_EQ(sysEvent->GetEventValue("REASON"), "AP_S_HWWATCHDOG");
128     ASSERT_EQ(sysEvent->GetEventValue("LOG_PATH"), "/data/test/hisi_logs/19700106031950-00001111");
129 }
130 
131 /**
132  * @tc.name: BBoxDetectorModuleTest003
133  * @tc.desc: check whether fault is processed.
134  *           1. check whether event is valid;
135  *           2. check whether category and reason is ignored;
136  * @tc.type: FUNC
137  * @tc.require:
138  * @tc.author: liuwei
139  */
140 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest003, TestSize.Level1)
141 {
142     /**
143      * @tc.steps: step1. construct HWWATCHDOG SysEvent
144      * @tc.steps: step2. construct BBOXDetectorPlugin
145      * @tc.steps: step3. OnEvent
146      * @tc.steps: step4. check result
147      */
148     SysEventCreator sysEventCreator("KERNEL_VENDOR", "MODEMCRASH", SysEventCreator::FAULT);
149     sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
150     sysEventCreator.SetKeyValue("name_", "MODEMCRASH");
151     sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
152     sysEventCreator.SetKeyValue("LOG_PATH", "/data/test/hisi_logs/");
153     sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
154     sysEventCreator.SetKeyValue("MODULE", "AP");
155     sysEventCreator.SetKeyValue("REASON", "MODEMCRASH");
156 
157     FileUtil::ForceCreateDirectory("/data/test/hisi_logs/19700106031950-00001111/");
158     FileUtil::SaveStringToFile("/data/test/hisi_logs/19700106031950-00001111/DONE", "done", true);
159     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
160     auto testPlugin = make_shared<BBoxDetectorPlugin>();
161     MockHiviewContext hiviewContext;
162     auto eventLoop = std::make_shared<MockEventLoop>();
163     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
164     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
165     testPlugin->SetHiviewContext(&hiviewContext);
166     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
167     testPlugin->OnLoad();
168     testPlugin->OnEvent(event);
169     ASSERT_EQ(sysEvent->GetEventValue("MODULE"), "AP");
170     ASSERT_EQ(sysEvent->GetEventValue("REASON"), "MODEMCRASH");
171     ASSERT_EQ(sysEvent->GetEventValue("LOG_PATH"), "/data/test/hisi_logs/19700106031950-00001111");
172 }
173 
174 /**
175  * @tc.name: BBoxDetectorModuleTest004
176  * @tc.desc: check whether fault is processed.
177  *           1. check whether event is invalid;
178  * @tc.type: FUNC
179  * @tc.require:
180  * @tc.author: liuwei
181  */
182 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest004, TestSize.Level1)
183 {
184     /**
185      * @tc.steps: step1. construct PANIC SysEvent
186      * @tc.steps: step2. construct BBOXDetectorPlugin
187      * @tc.steps: step3. OnEvent
188      * @tc.steps: step4. check result should return true
189      */
190     SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
191     sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
192     sysEventCreator.SetKeyValue("name_", "PANIC");
193     sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
194     sysEventCreator.SetKeyValue("LOG_PATH", "/data/test/hisi_logs/");
195     sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
196     sysEventCreator.SetKeyValue("MODULE", "AP");
197     sysEventCreator.SetKeyValue("REASON", "MODEMCRASH");
198 
199     FileUtil::ForceCreateDirectory("/data/test/hisi_logs/19700106031950-00001111/");
200     FileUtil::SaveStringToFile("/data/test/hisi_logs/19700106031950-00001111/DONE", "done", true);
201     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
202     auto testPlugin = make_shared<BBoxDetectorPlugin>();
203     MockHiviewContext hiviewContext;
204     auto eventLoop = std::make_shared<MockEventLoop>();
205     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
206     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
207     testPlugin->SetHiviewContext(&hiviewContext);
208     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
209     testPlugin->OnLoad();
210     ASSERT_EQ(testPlugin->OnEvent(event), true);
211 }
212 
213 /**
214  * @tc.name: BBoxDetectorModuleTest005
215  * @tc.desc: check event is null.
216  * @tc.type: FUNC
217  * @tc.require:
218  */
219 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest005, TestSize.Level1)
220 {
221     auto testPlugin = make_shared<BBoxDetectorPlugin>();
222     MockHiviewContext hiviewContext;
223     auto eventLoop = std::make_shared<MockEventLoop>();
224     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
225     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
226     testPlugin->SetHiviewContext(&hiviewContext);
227     shared_ptr<Event> event = nullptr;
228     testPlugin->OnLoad();
229     ASSERT_EQ(testPlugin->OnEvent(event), false);
230 }
231 
232 /**
233  * @tc.name: BBoxDetectorModuleTest006
234  * @tc.desc: check domain is not KERNEL_VENDOR
235  * @tc.type: FUNC
236  * @tc.require:
237  */
238 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest006, TestSize.Level1)
239 {
240     /**
241      * @tc.steps: step1. construct PANIC SysEvent
242      * @tc.steps: step2. construct BBOXDetectorPlugin
243      * @tc.steps: step3. OnEvent
244      * @tc.steps: step4. check result should return true
245      */
246     SysEventCreator sysEventCreator("TEST", "PANIC", SysEventCreator::FAULT);
247     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
248     auto testPlugin = make_shared<BBoxDetectorPlugin>();
249     MockHiviewContext hiviewContext;
250     auto eventLoop = std::make_shared<MockEventLoop>();
251     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
252     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
253     testPlugin->SetHiviewContext(&hiviewContext);
254     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
255     testPlugin->OnLoad();
256     ASSERT_EQ(testPlugin->OnEvent(event), false);
257     testPlugin->OnUnload();
258 }
259 
260 /**
261  * @tc.name: BBoxDetectorModuleTest007
262  * @tc.desc: test CUSTOM
263  * @tc.type: FUNC
264  * @tc.require:
265  */
266 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest007, TestSize.Level1)
267 {
268     /**
269      * @tc.steps: step1. construct CUSTOM SysEvent
270      * @tc.steps: step2. construct BBOXDetectorPlugin
271      * @tc.steps: step3. OnEvent
272      * @tc.steps: step4. check result should return true
273      */
274     SysEventCreator sysEventCreator("KERNEL_VENDOR", "CUSTOM", SysEventCreator::FAULT);
275     sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
276     sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
277     sysEventCreator.SetKeyValue("BBOX_TIME", "443990995");
278     sysEventCreator.SetKeyValue("BBOX_SYSRESET", "443990995");
279     sysEventCreator.SetKeyValue("LOG_PATH", "/data/test/hisi_logs/");
280     sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
281     sysEventCreator.SetKeyValue("MODULE", "AP");
282     sysEventCreator.SetKeyValue("REASON", "MODEMCRASH");
283 
284     FileUtil::ForceCreateDirectory("/data/test/hisi_logs/19700106031950-00001111/");
285     FileUtil::SaveStringToFile("/data/test/hisi_logs/19700106031950-00001111/DONE", "done", true);
286     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
287     auto testPlugin = make_shared<BBoxDetectorPlugin>();
288     MockHiviewContext hiviewContext;
289     auto eventLoop = std::make_shared<MockEventLoop>();
290     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
291     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
292     testPlugin->SetHiviewContext(&hiviewContext);
293     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
294     testPlugin->OnLoad();
295 
296     ASSERT_EQ(testPlugin->OnEvent(event), true);
297     testPlugin->OnUnload();
298 }
299 
300 /**
301  * @tc.name: BBoxDetectorModuleTest008
302  * @tc.desc: check event is recovery panic
303  * @tc.type: FUNC
304  * @tc.require:
305  */
306 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest008, TestSize.Level1)
307 {
308     /**
309      * @tc.steps: step1. construct PANIC" SysEvent
310      * @tc.steps: step2. construct BBOXDetectorPlugin
311      * @tc.steps: step3. OnEvent
312      * @tc.steps: step4. check result should return true
313      */
314     SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
315     sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
316     sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
317     sysEventCreator.SetKeyValue("LOG_PATH", "/data/test/bbox/panic_log/");
318     sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
319     sysEventCreator.SetKeyValue("MODULE", "AP");
320     sysEventCreator.SetKeyValue("REASON", "HM_PANIC:HM_PANIC_SYSMGR");
321 
322     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
323     auto testPlugin = make_shared<BBoxDetectorPlugin>();
324     MockHiviewContext hiviewContext;
325     auto eventLoop = std::make_shared<MockEventLoop>();
326     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
327     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
328     testPlugin->SetHiviewContext(&hiviewContext);
329     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
330     testPlugin->OnLoad();
331 
332     ASSERT_EQ(testPlugin->OnEvent(event), true);
333     testPlugin->OnUnload();
334     ASSERT_TRUE(sysEvent->GetEventValue("FIRST_FRAME").empty());
335     ASSERT_TRUE(sysEvent->GetEventValue("SECOND_FRAME").empty());
336     ASSERT_TRUE(sysEvent->GetEventValue("LAST_FRAME").empty());
337 }
338 
339 /**
340  * @tc.name: BBoxDetectorModuleTest009
341  * @tc.desc: The event has been processed and completed
342  * @tc.type: FUNC
343  * @tc.require:
344  */
345 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest009, TestSize.Level1)
346 {
347     /**
348      * @tc.steps: step1. construct PANIC SysEvent
349      * @tc.steps: step2. construct BBOXDetectorPlugin
350      * @tc.steps: step3. OnEvent
351      * @tc.steps: step4. check result should return true
352      */
353     SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
354     sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
355     sysEventCreator.SetKeyValue("LOG_PATH", "/data/test/hisi_logs/");
356     sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
357     sysEventCreator.SetKeyValue("MODULE", "AP");
358     sysEventCreator.SetKeyValue("REASON", "HM_PANIC:HM_PANIC_SYSMGR");
359 
360     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
361     auto testPlugin = make_shared<BBoxDetectorPlugin>();
362     MockHiviewContext hiviewContext;
363     auto eventLoop = std::make_shared<MockEventLoop>();
364     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
365     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
366     EXPECT_CALL(MockHisyseventUtil::GetInstance(), IsEventProcessed).WillRepeatedly(Return(true));
367     testPlugin->SetHiviewContext(&hiviewContext);
368     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
369     testPlugin->OnLoad();
370 
371     ASSERT_EQ(testPlugin->OnEvent(event), true);
372     testPlugin->OnUnload();
373     ASSERT_TRUE(sysEvent->GetEventValue("FIRST_FRAME").empty());
374     ASSERT_TRUE(sysEvent->GetEventValue("SECOND_FRAME").empty());
375     ASSERT_TRUE(sysEvent->GetEventValue("LAST_FRAME").empty());
376 }
377 
378 /**
379  * @tc.name: BBoxDetectorModuleTest010
380  * @tc.desc: check BFM_S_NATIVE_DATA_FAIL
381  * @tc.type: FUNC
382  * @tc.require:
383  */
384 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest010, TestSize.Level1)
385 {
386     /**
387      * @tc.steps: step1. construct PANIC SysEvent
388      * @tc.steps: step2. construct BBOXDetectorPlugin
389      * @tc.steps: step3. OnEvent
390      * @tc.steps: step4. check result should return true
391      */
392     SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
393     sysEventCreator.SetKeyValue("MODULE", "AP");
394     sysEventCreator.SetKeyValue("REASON", "BFM_S_NATIVE_DATA_FAIL");
395 
396     auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
397     auto testPlugin = make_shared<BBoxDetectorPlugin>();
398     MockHiviewContext hiviewContext;
399     auto eventLoop = std::make_shared<MockEventLoop>();
400     EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
401     EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
402     testPlugin->SetHiviewContext(&hiviewContext);
403     shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
404     testPlugin->OnLoad();
405 
406     ASSERT_EQ(testPlugin->OnEvent(event), true);
407     ASSERT_TRUE(event->HasFinish());
408 }
409 }  // namespace HiviewDFX
410 }  // namespace OHOS
411