1 /*
2 * Copyright (c) 2025 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 <iostream>
16 #include <unistd.h>
17
18 #include <gtest/gtest.h>
19
20 #include "event_json_parser.h"
21 #include "event_validator.h"
22 #include "hiview_global.h"
23 #include "sys_event.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::HiviewDFX;
27
28 class TestHiviewContext : public HiviewContext {
29 public:
GetHiViewDirectory(DirectoryType type)30 std::string GetHiViewDirectory(DirectoryType type) override
31 {
32 return "/data/test/hiview/event_validator";
33 }
34 };
35
36 namespace {
37 const std::string TEST_DOMAIN = "HIVIEWDFX";
38 const std::string TEST_NAME = "PLUGIN_LOAD";
39 constexpr uint64_t HAPPEN_TIME_ONE_SECOND = 1000; // ms
40 constexpr uint64_t CREATE_TIME_ONE_SECOND = 1000 * 1000; // us
41 constexpr uint64_t CREATE_TIME_SIX_SECOND = 6 * 1000 * 1000; // us
42 TestHiviewContext g_context;
43
InitPlugin(std::shared_ptr<EventValidator> plugin)44 void InitPlugin(std::shared_ptr<EventValidator> plugin)
45 {
46 TestHiviewContext context;
47 plugin->SetHiviewContext(&context);
48 plugin->OnLoad();
49 }
50
CreateSysEvent(const std::string & domain=TEST_DOMAIN,const std::string & name=TEST_NAME,SysEventCreator::EventType type=SysEventCreator::BEHAVIOR)51 std::shared_ptr<SysEvent> CreateSysEvent(
52 const std::string& domain = TEST_DOMAIN,
53 const std::string& name = TEST_NAME,
54 SysEventCreator::EventType type = SysEventCreator::BEHAVIOR)
55 {
56 SysEventCreator sysEventCreator(domain, name, type);
57 return std::make_shared<SysEvent>("", nullptr, sysEventCreator);
58 }
59 }
60
61 class EventValidatorTest : public testing::Test {
62 public:
SetUp()63 void SetUp()
64 {
65 HiviewGlobal::CreateInstance(g_context);
66 EventJsonParser::GetInstance()->ReadDefFile();
67 }
68
TearDown()69 void TearDown()
70 {
71 HiviewGlobal::ReleaseInstance();
72 }
73 };
74
75 /**
76 * @tc.name: EventValidatorTest001
77 * @tc.desc: invalid OnLoad test.
78 * @tc.type: FUNC
79 * @tc.require: issueICAH56
80 */
81 HWTEST_F(EventValidatorTest, EventValidatorTest001, TestSize.Level1)
82 {
83 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
84 plugin->OnLoad();
85 ASSERT_TRUE(plugin->GetHiviewContext() == nullptr);
86 plugin->OnUnload();
87 }
88
89 /**
90 * @tc.name: EventValidatorTest002
91 * @tc.desc: valid OnLoad test.
92 * @tc.type: FUNC
93 * @tc.require: issueICAH56
94 */
95 HWTEST_F(EventValidatorTest, EventValidatorTest002, TestSize.Level1)
96 {
97 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
98 InitPlugin(plugin);
99 ASSERT_TRUE(plugin->GetHiviewContext() != nullptr);
100 plugin->OnUnload();
101 }
102
103 /**
104 * @tc.name: EventValidatorTest003
105 * @tc.desc: OnEvent test, null event.
106 * @tc.type: FUNC
107 * @tc.require: issueICAH56
108 */
109 HWTEST_F(EventValidatorTest, EventValidatorTest003, TestSize.Level1)
110 {
111 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
112 InitPlugin(plugin);
113 std::shared_ptr<Event> event = nullptr;
114 ASSERT_FALSE(plugin->OnEvent(event));
115 }
116
117 /**
118 * @tc.name: EventValidatorTest004
119 * @tc.desc: OnEvent test, non-sys event.
120 * @tc.type: FUNC
121 * @tc.require: issueICAH56
122 */
123 HWTEST_F(EventValidatorTest, EventValidatorTest004, TestSize.Level1)
124 {
125 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
126 InitPlugin(plugin);
127 std::shared_ptr<Event> event = std::make_shared<Event>("");
128 ASSERT_FALSE(plugin->OnEvent(event));
129 }
130
131 /**
132 * @tc.name: EventValidatorTest005
133 * @tc.desc: OnEvent test, non-sys event.
134 * @tc.type: FUNC
135 * @tc.require: issueICAH56
136 */
137 HWTEST_F(EventValidatorTest, EventValidatorTest005, TestSize.Level1)
138 {
139 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
140 InitPlugin(plugin);
141 std::shared_ptr<Event> event = std::make_shared<Event>("");
142 event->messageType_ = Event::MessageType::SYS_EVENT;
143 ASSERT_FALSE(plugin->OnEvent(event));
144 }
145
146 /**
147 * @tc.name: EventValidatorTest006
148 * @tc.desc: OnEvent test, normal event.
149 * @tc.type: FUNC
150 * @tc.require: issueICAH56
151 */
152 HWTEST_F(EventValidatorTest, EventValidatorTest006, TestSize.Level1)
153 {
154 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
155 InitPlugin(plugin);
156 std::shared_ptr<Event> event = CreateSysEvent();
157 ASSERT_TRUE(plugin->OnEvent(event));
158 }
159
160 /**
161 * @tc.name: EventValidatorTest007
162 * @tc.desc: OnEvent test, delayed event, for time jump scene.
163 * @tc.type: FUNC
164 * @tc.require: issueICAH56
165 */
166 HWTEST_F(EventValidatorTest, EventValidatorTest007, TestSize.Level1)
167 {
168 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
169 InitPlugin(plugin);
170
171 std::shared_ptr<Event> event = CreateSysEvent();
172 event->happenTime_ = 1000; // 1000ms
173 event->createTime_ = 0; // for time jump scene
174 ASSERT_TRUE(plugin->OnEvent(event));
175 }
176
177 /**
178 * @tc.name: EventValidatorTest008
179 * @tc.desc: OnEvent test, delayed event, delayed 5s.
180 * @tc.type: FUNC
181 * @tc.require: issueICAH56
182 */
183 HWTEST_F(EventValidatorTest, EventValidatorTest008, TestSize.Level1)
184 {
185 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
186 InitPlugin(plugin);
187
188 // first event, delayed 5s
189 std::shared_ptr<Event> event1 = CreateSysEvent();
190 event1->happenTime_ = HAPPEN_TIME_ONE_SECOND;
191 event1->createTime_ = CREATE_TIME_SIX_SECOND;
192 ASSERT_TRUE(plugin->OnEvent(event1));
193
194 // second event, delayed 5s
195 ASSERT_TRUE(plugin->OnEvent(event1));
196
197 // third event, not delayed
198 usleep(10000); // 10000: sleep for 10ms to prevent repetition
199 std::shared_ptr<Event> event2 = CreateSysEvent();
200 event2->happenTime_ = HAPPEN_TIME_ONE_SECOND;
201 event2->createTime_ = CREATE_TIME_ONE_SECOND;
202 ASSERT_TRUE(plugin->OnEvent(event2));
203
204 // fourth event, not delayed
205 ASSERT_TRUE(plugin->OnEvent(event2));
206 }
207
208 /**
209 * @tc.name: EventValidatorTest009
210 * @tc.desc: OnEvent test, invalid domain.
211 * @tc.type: FUNC
212 * @tc.require: issueICAH56
213 */
214 HWTEST_F(EventValidatorTest, EventValidatorTest009, TestSize.Level1)
215 {
216 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
217 InitPlugin(plugin);
218 std::shared_ptr<Event> event = CreateSysEvent("");
219 ASSERT_FALSE(plugin->OnEvent(event));
220 }
221
222 /**
223 * @tc.name: EventValidatorTest010
224 * @tc.desc: OnEvent test, invalid name.
225 * @tc.type: FUNC
226 * @tc.require: issueICAH56
227 */
228 HWTEST_F(EventValidatorTest, EventValidatorTest010, TestSize.Level1)
229 {
230 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
231 InitPlugin(plugin);
232 std::shared_ptr<Event> event = CreateSysEvent(TEST_DOMAIN, "");
233 ASSERT_FALSE(plugin->OnEvent(event));
234 }
235
236 /**
237 * @tc.name: EventValidatorTest011
238 * @tc.desc: OnEvent test, non-existent event.
239 * @tc.type: FUNC
240 * @tc.require: issueICAH56
241 */
242 HWTEST_F(EventValidatorTest, EventValidatorTest011, TestSize.Level1)
243 {
244 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
245 InitPlugin(plugin);
246 std::shared_ptr<Event> event = CreateSysEvent("N_EXIST_DOMAIN", "N_EXIST_NAME");
247 ASSERT_FALSE(plugin->OnEvent(event));
248 }
249
250 /**
251 * @tc.name: EventValidatorTest012
252 * @tc.desc: OnEvent test, invalid type.
253 * @tc.type: FUNC
254 * @tc.require: issueICAH56
255 */
256 HWTEST_F(EventValidatorTest, EventValidatorTest012, TestSize.Level1)
257 {
258 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
259 InitPlugin(plugin);
260 std::shared_ptr<Event> event = CreateSysEvent(TEST_DOMAIN, TEST_NAME, SysEventCreator::FAULT);
261 ASSERT_FALSE(plugin->OnEvent(event));
262 }
263
264 /**
265 * @tc.name: EventValidatorTest013
266 * @tc.desc: OnEvent test, duplicate event.
267 * @tc.type: FUNC
268 * @tc.require: issueICAH56
269 */
270 HWTEST_F(EventValidatorTest, EventValidatorTest013, TestSize.Level1)
271 {
272 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
273 InitPlugin(plugin);
274
275 // create same events
276 SysEventCreator sysEventCreator(TEST_DOMAIN, TEST_NAME, SysEventCreator::BEHAVIOR);
277 std::shared_ptr<Event> event1 = std::make_shared<SysEvent>("", nullptr, sysEventCreator);
278 ASSERT_TRUE(plugin->OnEvent(event1));
279 std::shared_ptr<Event> event2 = std::make_shared<SysEvent>("", nullptr, sysEventCreator);
280 ASSERT_FALSE(plugin->OnEvent(event2));
281 }
282
283 /**
284 * @tc.name: EventValidatorTest014
285 * @tc.desc: OnConfigUpdate test.
286 * @tc.type: FUNC
287 * @tc.require: issueICAH56
288 */
289 HWTEST_F(EventValidatorTest, EventValidatorTest014, TestSize.Level1)
290 {
291 std::shared_ptr<EventValidator> plugin = std::make_shared<EventValidator>();
292 InitPlugin(plugin);
293 plugin->OnConfigUpdate("", "");
294 std::shared_ptr<Event> event = CreateSysEvent();
295 ASSERT_TRUE(plugin->OnEvent(event));
296 }
297