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 #include "bbox_detectors_mock.h"
19 #include "common_defines.h"
20 #include "event.h"
21 #include "hisysevent_util_mock.h"
22 #include "log_util.h"
23 #include "sys_event.h"
24 #include "string_util.h"
25 #include "time_util.h"
26
27 using namespace std;
28 namespace OHOS {
29 namespace HiviewDFX {
30 using namespace testing;
31 using namespace testing::ext;
SetUpTestCase(void)32 void BBoxDetectorModuleTest::SetUpTestCase(void) {}
33
TearDownTestCase(void)34 void BBoxDetectorModuleTest::TearDownTestCase(void) {}
35
SetUp(void)36 void BBoxDetectorModuleTest::SetUp(void)
37 {
38 EXPECT_CALL(MockHisyseventUtil::GetInstance(), IsEventProcessed).WillRepeatedly(Return(false));
39 }
40
TearDown(void)41 void BBoxDetectorModuleTest::TearDown(void) {}
42
43 /**
44 * @tc.name: BBoxDetectorModuleTest001
45 * @tc.desc: check whether fault is processed.
46 * 1. check whether event is valid;
47 * 2. check whether category and reason is ignored;
48 * @tc.type: FUNC
49 * @tc.require:
50 * @tc.author: liuwei
51 */
52 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest001, TestSize.Level1)
53 {
54 /**
55 * @tc.steps: step1. construct PANIC SysEvent
56 * @tc.steps: step2. construct BBOXDetectorPlugin
57 * @tc.steps: step3. OnEvent
58 * @tc.steps: step4. check result
59 */
60 SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
61 sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
62 sysEventCreator.SetKeyValue("name_", "PANIC");
63 sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
64 sysEventCreator.SetKeyValue("LOG_PATH", "/data/hisi_logs/");
65 sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
66 sysEventCreator.SetKeyValue("MODULE", "AP");
67 sysEventCreator.SetKeyValue("REASON", "AP_S_PANIC");
68 auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
69 auto testPlugin = make_shared<BBoxDetectorPlugin>();
70 MockHiviewContext hiviewContext;
71 auto eventLoop = std::make_shared<MockEventLoop>();
72 EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
73 EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
74 testPlugin->SetHiviewContext(&hiviewContext);
75 shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
76 testPlugin->OnLoad();
77 testPlugin->OnEvent(event);
78 ASSERT_EQ(sysEvent->GetEventValue("MODULE"), "AP");
79 ASSERT_EQ(sysEvent->GetEventValue("REASON"), "AP_S_PANIC");
80 ASSERT_EQ(sysEvent->GetEventValue("LOG_PATH"), "/data/hisi_logs/19700106031950-00001111");
81 }
82
83 /**
84 * @tc.name: BBoxDetectorModuleTest002
85 * @tc.desc: check whether fault is processed.
86 * 1. check whether event is valid;
87 * 2. check whether category and reason is ignored;
88 * @tc.type: FUNC
89 * @tc.require:
90 * @tc.author: liuwei
91 */
92 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest002, TestSize.Level1)
93 {
94 /**
95 * @tc.steps: step1. construct HWWATCHDOG SysEvent
96 * @tc.steps: step2. construct BBOXDetectorPlugin
97 * @tc.steps: step3. OnEvent
98 * @tc.steps: step4. check result
99 */
100 SysEventCreator sysEventCreator("KERNEL_VENDOR", "HWWATCHDOG", SysEventCreator::FAULT);
101 sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
102 sysEventCreator.SetKeyValue("name_", "HWWATCHDOG");
103 sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
104 sysEventCreator.SetKeyValue("LOG_PATH", "/data/hisi_logs/");
105 sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
106 sysEventCreator.SetKeyValue("MODULE", "AP");
107 sysEventCreator.SetKeyValue("REASON", "AP_S_HWWATCHDOG");
108 auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
109 auto testPlugin = make_shared<BBoxDetectorPlugin>();
110 MockHiviewContext hiviewContext;
111 auto eventLoop = std::make_shared<MockEventLoop>();
112 EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
113 EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
114 testPlugin->SetHiviewContext(&hiviewContext);
115 shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
116 testPlugin->OnLoad();
117 testPlugin->OnEvent(event);
118 ASSERT_EQ(sysEvent->GetEventValue("MODULE"), "AP");
119 ASSERT_EQ(sysEvent->GetEventValue("REASON"), "AP_S_HWWATCHDOG");
120 ASSERT_EQ(sysEvent->GetEventValue("LOG_PATH"), "/data/hisi_logs/19700106031950-00001111");
121 }
122
123 /**
124 * @tc.name: BBoxDetectorModuleTest003
125 * @tc.desc: check whether fault is processed.
126 * 1. check whether event is valid;
127 * 2. check whether category and reason is ignored;
128 * @tc.type: FUNC
129 * @tc.require:
130 * @tc.author: liuwei
131 */
132 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest003, TestSize.Level1)
133 {
134 /**
135 * @tc.steps: step1. construct HWWATCHDOG SysEvent
136 * @tc.steps: step2. construct BBOXDetectorPlugin
137 * @tc.steps: step3. OnEvent
138 * @tc.steps: step4. check result
139 */
140 SysEventCreator sysEventCreator("KERNEL_VENDOR", "MODEMCRASH", SysEventCreator::FAULT);
141 sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
142 sysEventCreator.SetKeyValue("name_", "MODEMCRASH");
143 sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
144 sysEventCreator.SetKeyValue("LOG_PATH", "/data/hisi_logs/");
145 sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
146 sysEventCreator.SetKeyValue("MODULE", "AP");
147 sysEventCreator.SetKeyValue("REASON", "MODEMCRASH");
148 auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
149 auto testPlugin = make_shared<BBoxDetectorPlugin>();
150 MockHiviewContext hiviewContext;
151 auto eventLoop = std::make_shared<MockEventLoop>();
152 EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
153 EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
154 testPlugin->SetHiviewContext(&hiviewContext);
155 shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
156 testPlugin->OnLoad();
157 testPlugin->OnEvent(event);
158 ASSERT_EQ(sysEvent->GetEventValue("MODULE"), "AP");
159 ASSERT_EQ(sysEvent->GetEventValue("REASON"), "MODEMCRASH");
160 ASSERT_EQ(sysEvent->GetEventValue("LOG_PATH"), "/data/hisi_logs/19700106031950-00001111");
161 }
162
163 /**
164 * @tc.name: BBoxDetectorModuleTest004
165 * @tc.desc: check whether fault is processed.
166 * 1. check whether event is invalid;
167 * @tc.type: FUNC
168 * @tc.require:
169 * @tc.author: liuwei
170 */
171 HWTEST_F(BBoxDetectorModuleTest, BBoxDetectorModuleTest004, TestSize.Level1)
172 {
173 /**
174 * @tc.steps: step1. construct PANIC SysEvent
175 * @tc.steps: step2. construct BBOXDetectorPlugin
176 * @tc.steps: step3. OnEvent
177 * @tc.steps: step4. check result should return true
178 */
179 SysEventCreator sysEventCreator("KERNEL_VENDOR", "PANIC", SysEventCreator::FAULT);
180 sysEventCreator.SetKeyValue("SUMMARY", "bootup_keypoint:97");
181 sysEventCreator.SetKeyValue("name_", "PANIC");
182 sysEventCreator.SetKeyValue("HAPPEN_TIME", "443990995");
183 sysEventCreator.SetKeyValue("LOG_PATH", "/data/hisi_logs/");
184 sysEventCreator.SetKeyValue("SUB_LOG_PATH", "19700106031950-00001111");
185 sysEventCreator.SetKeyValue("MODULE", "AP");
186 sysEventCreator.SetKeyValue("REASON", "MODEMCRASH");
187 auto sysEvent = make_shared<SysEvent>("test", nullptr, sysEventCreator);
188 auto testPlugin = make_shared<BBoxDetectorPlugin>();
189 MockHiviewContext hiviewContext;
190 auto eventLoop = std::make_shared<MockEventLoop>();
191 EXPECT_CALL(*(eventLoop.get()), GetMockInterval()).WillRepeatedly(Return(1));
192 EXPECT_CALL(hiviewContext, GetSharedWorkLoop()).WillRepeatedly(Return(eventLoop));
193 testPlugin->SetHiviewContext(&hiviewContext);
194 shared_ptr<Event> event = dynamic_pointer_cast<Event>(sysEvent);
195 testPlugin->OnLoad();
196 ASSERT_EQ(testPlugin->OnEvent(event), true);
197 }
198 } // namespace HiviewDFX
199 } // namespace OHOS
200