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 "sys_event_service_ohos_test.h"
17
18 #include <cstdlib>
19 #include <semaphore.h>
20 #include <string>
21 #include <vector>
22
23 #include "ash_mem_utils.h"
24 #include "event.h"
25 #include "event_query_wrapper_builder.h"
26 #include "file_util.h"
27 #include "hiview_global.h"
28 #include "if_system_ability_manager.h"
29 #include "ipc_skeleton.h"
30 #include "iquery_sys_event_callback.h"
31 #include "iservice_registry.h"
32 #include "isys_event_callback.h"
33 #include "isys_event_service.h"
34 #include "query_argument.h"
35 #include "query_sys_event_callback_proxy.h"
36 #include "plugin.h"
37 #include "ret_code.h"
38 #include "running_status_log_util.h"
39 #include "string_ex.h"
40 #include "sys_event.h"
41 #include "sys_event_rule.h"
42 #include "sys_event_service_adapter.h"
43 #include "sys_event_service_ohos.h"
44 #include "system_ability.h"
45 #include "string_ex.h"
46 #include "string_util.h"
47 #include "sys_event_callback_proxy.h"
48 #include "sys_event_service_stub.h"
49 #include "sys_dispatcher.h"
50 #include "time_util.h"
51
52 using namespace std;
53
54 namespace OHOS {
55 namespace HiviewDFX {
56 namespace {
57 constexpr char ASH_MEM_NAME[] = "TestSharedMemory";
58 constexpr int32_t ASH_MEM_SIZE = 1024 * 2; // 2K
59 constexpr char TEST_LOG_DIR[] = "/data/log/hiview/sys_event_test";
60 const std::vector<int> EVENT_TYPES = {1, 2, 3, 4}; // FAULT = 1, STATISTIC = 2 SECURITY = 3, BEHAVIOR = 4
61
GetAshmem()62 sptr<Ashmem> GetAshmem()
63 {
64 auto ashmem = Ashmem::CreateAshmem(ASH_MEM_NAME, ASH_MEM_SIZE);
65 if (ashmem == nullptr) {
66 return nullptr;
67 }
68 if (!ashmem->MapReadAndWriteAshmem()) {
69 return ashmem;
70 }
71 return ashmem;
72 }
73
74 class TestSysEventServiceStub : public SysEventServiceStub {
75 public:
TestSysEventServiceStub()76 TestSysEventServiceStub() {}
~TestSysEventServiceStub()77 virtual ~TestSysEventServiceStub() {}
78
AddListener(const std::vector<SysEventRule> & rules,const sptr<ISysEventCallback> & callback)79 int32_t AddListener(const std::vector<SysEventRule>& rules, const sptr<ISysEventCallback>& callback)
80 {
81 return 0;
82 }
83
RemoveListener(const sptr<ISysEventCallback> & callback)84 int32_t RemoveListener(const sptr<ISysEventCallback>& callback)
85 {
86 return 0;
87 }
88
Query(const QueryArgument & queryArgument,const std::vector<SysEventQueryRule> & rules,const sptr<IQuerySysEventCallback> & callback)89 int32_t Query(const QueryArgument& queryArgument, const std::vector<SysEventQueryRule>& rules,
90 const sptr<IQuerySysEventCallback>& callback)
91 {
92 return 0;
93 }
94
SetDebugMode(const sptr<ISysEventCallback> & callback,bool mode)95 int32_t SetDebugMode(const sptr<ISysEventCallback>& callback, bool mode)
96 {
97 return 0;
98 }
99
AddSubscriber(const std::vector<SysEventQueryRule> & rules)100 int64_t AddSubscriber(const std::vector<SysEventQueryRule> &rules)
101 {
102 return TimeUtil::GetMilliseconds();
103 }
104
RemoveSubscriber()105 int32_t RemoveSubscriber()
106 {
107 return 0;
108 }
109
Export(const QueryArgument & queryArgument,const std::vector<SysEventQueryRule> & rules)110 int64_t Export(const QueryArgument &queryArgument, const std::vector<SysEventQueryRule> &rules)
111 {
112 return TimeUtil::GetMilliseconds();
113 }
114
115 public:
116 enum Code {
117 DEFAULT = -1,
118 ADD_SYS_EVENT_LISTENER = 0,
119 REMOVE_SYS_EVENT_LISTENER,
120 QUERY_SYS_EVENT,
121 SET_DEBUG_MODE,
122 ADD_SYS_EVENT_SUBSCRIBER,
123 REMOVE_SYS_EVENT_SUBSCRIBER,
124 EXPORT_SYS_EVENT
125 };
126 };
127
128 class HiviewTestContext : public HiviewContext {
129 public:
GetHiViewDirectory(DirectoryType type __UNUSED)130 std::string GetHiViewDirectory(DirectoryType type __UNUSED)
131 {
132 return TEST_LOG_DIR;
133 }
134 };
135 }
136
SetUpTestCase()137 void SysEventServiceOhosTest::SetUpTestCase() {}
138
TearDownTestCase()139 void SysEventServiceOhosTest::TearDownTestCase() {}
140
SetUp()141 void SysEventServiceOhosTest::SetUp() {}
142
TearDown()143 void SysEventServiceOhosTest::TearDown()
144 {
145 (void)FileUtil::ForceRemoveDirectory(TEST_LOG_DIR);
146 }
147
GetTestRule(int type,const string & domain,const string & eventName)148 static SysEventRule GetTestRule(int type, const string &domain, const string &eventName)
149 {
150 SysEventRule rule;
151 rule.ruleType = type;
152 rule.domain = domain;
153 rule.eventName = eventName;
154 return rule;
155 }
156
GetTestRules(int type,const string & domain,const string & eventName)157 static vector<SysEventRule> GetTestRules(int type, const string &domain, const string &eventName)
158 {
159 vector<SysEventRule> rules;
160 rules.push_back(GetTestRule(type, domain, eventName));
161 return rules;
162 }
163
164 /**
165 * @tc.name: SysEventServiceAdapterTest
166 * @tc.desc: test apis of SysEventServiceAdapter
167 * @tc.type: FUNC
168 * @tc.require: issueI62BDW
169 */
170 HWTEST_F(SysEventServiceOhosTest, SysEventServiceAdapterTest, testing::ext::TestSize.Level3)
171 {
172 OHOS::HiviewDFX::SysEventServiceAdapter::StartService(nullptr, nullptr);
173 std::shared_ptr<SysEvent> sysEvent = nullptr;
174 OHOS::HiviewDFX::SysEventServiceAdapter::OnSysEvent(sysEvent);
175 SysEventCreator sysEventCreator("DEMO", "EVENT_NAME", SysEventCreator::FAULT);
176 std::vector<int> values = {1, 2, 3};
177 sysEventCreator.SetKeyValue("KEY", values);
178 sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
179 OHOS::HiviewDFX::SysEventServiceAdapter::OnSysEvent(sysEvent);
180 ASSERT_TRUE(true);
181 OHOS::HiviewDFX::SysEventServiceAdapter::UpdateEventSeq(0);
182 ASSERT_TRUE(true);
183 OHOS::HiviewDFX::SysEventServiceAdapter::BindGetTagFunc(nullptr);
184 ASSERT_TRUE(true);
185 OHOS::HiviewDFX::SysEventServiceAdapter::BindGetTypeFunc(nullptr);
186 ASSERT_TRUE(true);
187 }
188
189 /**
190 * @tc.name: TestAshMemory
191 * @tc.desc: Ashmemory test
192 * @tc.type: FUNC
193 * @tc.require: issueI62WJT
194 */
195 HWTEST_F(SysEventServiceOhosTest, TestAshMemory, testing::ext::TestSize.Level1)
196 {
197 MessageParcel msgParcel;
198 std::vector<std::u16string> from = {
199 Str8ToStr16(std::string("11")),
200 Str8ToStr16(std::string("22")),
201 };
202 auto result = AshMemUtils::WriteBulkData(msgParcel, from);
203 ASSERT_TRUE(result != nullptr);
204 std::vector<std::u16string> to;
205 auto result1 = AshMemUtils::ReadBulkData(msgParcel, to);
206 ASSERT_TRUE(result1);
207 ASSERT_TRUE(from.size() == to.size());
208 ASSERT_TRUE(Str16ToStr8(to[0]) == "11" && Str16ToStr8(to[1]) == "22");
209 AshMemUtils::CloseAshmem(nullptr);
210 ASSERT_TRUE(true);
211 AshMemUtils::CloseAshmem(GetAshmem());
212 ASSERT_TRUE(true);
213 }
214
215 /**
216 * @tc.name: TestSysEventService001
217 * @tc.desc: SysEventServiceStub test
218 * @tc.type: FUNC
219 * @tc.require: issueI62WJT
220 */
221 HWTEST_F(SysEventServiceOhosTest, TestSysEventService001, testing::ext::TestSize.Level1)
222 {
223 SysEventServiceStub* sysEventService = new(std::nothrow) TestSysEventServiceStub();
224 MessageParcel data, reply;
225 MessageOption option;
226 sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::DEFAULT, data, reply, option);
227 ASSERT_TRUE(true);
228 sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::ADD_SYS_EVENT_LISTENER, data, reply, option);
229 ASSERT_TRUE(true);
230 sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::REMOVE_SYS_EVENT_LISTENER, data, reply,
231 option);
232 ASSERT_TRUE(true);
233 sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::QUERY_SYS_EVENT, data, reply, option);
234 ASSERT_TRUE(true);
235 sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::SET_DEBUG_MODE, data, reply, option);
236 ASSERT_TRUE(true);
237 }
238
239 /**
240 * @tc.name: TestSysEventService002
241 * @tc.desc: SysEventServiceStub test
242 * @tc.type: FUNC
243 * @tc.require: SR000I1G42
244 */
245 HWTEST_F(SysEventServiceOhosTest, TestSysEventService002, testing::ext::TestSize.Level1)
246 {
247 SysEventServiceStub* sysEventService = new(std::nothrow) TestSysEventServiceStub();
248 MessageParcel data, reply;
249 MessageOption option;
250 sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::ADD_SYS_EVENT_SUBSCRIBER, data, reply, option);
251 ASSERT_TRUE(true);
252 sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::REMOVE_SYS_EVENT_SUBSCRIBER, data, reply, option);
253 ASSERT_TRUE(true);
254 sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::EXPORT_SYS_EVENT, data, reply, option);
255 ASSERT_TRUE(true);
256 }
257
258 /**
259 * @tc.name: MarshallingTAndUnmarshallingTest
260 * @tc.desc: Unmarshalling test
261 * @tc.type: FUNC
262 * @tc.require: issueI62WJT
263 */
264 HWTEST_F(SysEventServiceOhosTest, MarshallingTAndUnmarshallingTest, testing::ext::TestSize.Level1)
265 {
266 long long defaultTimeStap = -1;
267 int queryCount = 10;
268 OHOS::HiviewDFX::QueryArgument argument(defaultTimeStap, defaultTimeStap, queryCount);
269 MessageParcel parcel1;
270 auto ret = argument.Marshalling(parcel1);
271 ASSERT_TRUE(ret);
272 QueryArgument* argsPtr = argument.Unmarshalling(parcel1);
273 ASSERT_TRUE(argsPtr != nullptr && argsPtr->maxEvents == 10 && argsPtr->beginTime == -1);
274 OHOS::HiviewDFX::SysEventRule rule("DOMAIN1", "EVENT_NAME2", "TAG3", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
275 MessageParcel parcel2;
276 ret = rule.Marshalling(parcel2);
277 ASSERT_TRUE(ret);
278 OHOS::HiviewDFX::SysEventRule* rulePtr = rule.Unmarshalling(parcel2);
279 ASSERT_TRUE(rulePtr != nullptr && rulePtr->domain == "DOMAIN1" &&
280 rulePtr->eventName == "EVENT_NAME2" && rulePtr->tag == "TAG3");
281
282 std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
283 OHOS::HiviewDFX::SysEventQueryRule eventQueryRule("DOMAIN", eventNames);
284 MessageParcel parcel3;
285 ret = eventQueryRule.Marshalling(parcel3);
286 ASSERT_TRUE(ret);
287 OHOS::HiviewDFX::SysEventQueryRule* eventQueryRulePtr = eventQueryRule.Unmarshalling(parcel3);
288 ASSERT_TRUE(eventQueryRulePtr != nullptr && eventQueryRulePtr->domain == "DOMAIN" &&
289 eventQueryRulePtr->eventList.size() == 2 && eventQueryRulePtr->eventList[0] == "EVENT_NAME1");
290 }
291
292 /**
293 * @tc.name: RunningStatusLogUtilTest
294 * @tc.desc: Test apis of RunningStatusLogUtil
295 * @tc.type: FUNC
296 * @tc.require: issueI62WJT
297 */
298 HWTEST_F(SysEventServiceOhosTest, RunningStatusLogUtilTest, testing::ext::TestSize.Level1)
299 {
300 HiviewTestContext hiviewTestContext;
301 HiviewGlobal::CreateInstance(hiviewTestContext);
302 std::vector<OHOS::HiviewDFX::SysEventQueryRule> queryRules;
303 std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
304 OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
305 RunningStatusLogUtil::LogTooManyQueryRules(queryRules);
306 ASSERT_TRUE(true);
307 queryRules.emplace_back(queryRule);
308 RunningStatusLogUtil::LogTooManyQueryRules(queryRules);
309 ASSERT_TRUE(true);
310 vector<SysEventRule> sysEventRules1;
311 RunningStatusLogUtil::LogTooManyWatchRules(sysEventRules1);
312 ASSERT_TRUE(true);
313 vector<SysEventRule> sysEventRules2 = GetTestRules(1, "", "");
314 RunningStatusLogUtil::LogTooManyWatchRules(sysEventRules2);
315 ASSERT_TRUE(true);
316 RunningStatusLogUtil::LogTooManyWatchers(30);
317 }
318
319 /**
320 * @tc.name: ConditionParserTest01
321 * @tc.desc: Test apis of ConditionParser
322 * @tc.type: FUNC
323 * @tc.require: issueI62WJT
324 */
325 HWTEST_F(SysEventServiceOhosTest, ConditionParserTest01, testing::ext::TestSize.Level1)
326 {
327 OHOS::HiviewDFX::ConditionParser parser;
328 EventStore::Cond cond;
329 std::string condStr = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
330 "value":"SysEventService"}]}})~";
331 auto ret = parser.ParseCondition(condStr, cond);
332 ASSERT_TRUE(ret);
333 std::string condStr2 = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
334 "value":"SysEventService"},{"param":"uid_","op":"=","value":1201}]}})~";
335 ret = parser.ParseCondition(condStr2, cond);
336 ASSERT_TRUE(ret);
337
338 std::string condStr3 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op":">","value":0},
339 {"param":"uid_","op":"=","value":1201}]}})~";
340 ret = parser.ParseCondition(condStr3, cond);
341 ASSERT_TRUE(ret);
342
343 std::string condStr4 = R"~({"version":"V1","condition":{"and":[{"param1":"type_","op":">","value":0},
344 {"param2":"uid_","op":"=","value":1201}]}})~";
345 ret = parser.ParseCondition(condStr4, cond);
346 ASSERT_TRUE(!ret);
347
348 std::string condSt5 = R"~({"version":"V1","condition":{"and":[{"param":"","op":">","value":0},
349 {"param":"","op":"=","value":1201}]}})~";
350 ret = parser.ParseCondition(condSt5, cond);
351 ASSERT_TRUE(!ret);
352
353 std::string condSt6 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op1":">","value":0},
354 {"param":"uid_","op2":"=","value":1201}]}})~";
355 ret = parser.ParseCondition(condSt6, cond);
356 ASSERT_TRUE(!ret);
357 }
358
359 /**
360 * @tc.name: ConditionParserTest02
361 * @tc.desc: Test apis of ConditionParser
362 * @tc.type: FUNC
363 * @tc.require: issueI62WJT
364 */
365 HWTEST_F(SysEventServiceOhosTest, ConditionParserTest02, testing::ext::TestSize.Level1)
366 {
367 OHOS::HiviewDFX::ConditionParser parser;
368 EventStore::Cond cond;
369 std::string condSt7 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op":">","value11":0},
370 {"param":"uid_","op":"=","value2":1201}]}})~";
371 auto ret = parser.ParseCondition(condSt7, cond);
372 ASSERT_TRUE(!ret);
373
374 std::string condStr8 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op":">","value":[]},
375 {"param":"uid_","op":"=","value":[]}]}})~";
376 ret = parser.ParseCondition(condStr8, cond);
377 ASSERT_TRUE(!ret);
378
379 std::string condStr9 = R"~({"version":"V1","condition1":{"and":[{"param":"type_","op":">","value":0},
380 {"param":"uid_","op":"=","value":1201}]}})~";
381 ret = parser.ParseCondition(condStr9, cond);
382 ASSERT_TRUE(!ret);
383
384 std::string condStr10 = R"~({"version":"V1","condition":1})~";
385 ret = parser.ParseCondition(condStr10, cond);
386 ASSERT_TRUE(!ret);
387
388 std::string condStr11 = R"~({"version":"V2","condition":{"and":[{"param1":"type_","op":">","value":0},
389 {"param2":"uid_","op":"=","value":1201}]}})~";
390 ret = parser.ParseCondition(condStr11, cond);
391 ASSERT_TRUE(!ret);
392 }
393
394 /**
395 * @tc.name: QueryWrapperTest01
396 * @tc.desc: BUild query wrapper with all event types
397 * @tc.type: FUNC
398 * @tc.require: issueI62WJT
399 */
400 HWTEST_F(SysEventServiceOhosTest, QueryWrapperTest01, testing::ext::TestSize.Level1)
401 {
402 HiviewTestContext hiviewTestContext;
403 HiviewGlobal::CreateInstance(hiviewTestContext);
404 QueryArgument queryArgument1(-1, -1, 10);
405 auto queryWrapperBuilder1 = std::make_shared<EventQueryWrapperBuilder>(queryArgument1);
406 QueryArgument queryArgument2(-1, -1, 10, 1, 20);
407 auto queryWrapperBuilder2 = std::make_shared<EventQueryWrapperBuilder>(queryArgument2);
408 auto queryWrapper1 = queryWrapperBuilder1->Build();
409 ASSERT_TRUE(queryWrapper1 != nullptr);
410 auto queryWrapper2 = queryWrapperBuilder2->Build();
411 ASSERT_TRUE(queryWrapper2 != nullptr);
412
413 ASSERT_FALSE(queryWrapperBuilder1->IsValid());
414 ASSERT_FALSE(queryWrapperBuilder2->IsValid());
415 queryWrapperBuilder1->Append("DOMAIN1", "EVENTNAME1", 0, "");
416 queryWrapperBuilder2->Append("DOMAIN2", "EVENTNAME2", 0, "");
417 ASSERT_TRUE(queryWrapperBuilder1->IsValid());
418 ASSERT_TRUE(queryWrapperBuilder2->IsValid());
419 }
420
421 /**
422 * @tc.name: QueryWrapperTest02
423 * @tc.desc: BUild query wrapper with domain, event name and event type.
424 * @tc.type: FUNC
425 * @tc.require: issueI62WJT
426 */
427 HWTEST_F(SysEventServiceOhosTest, QueryWrapperTest02, testing::ext::TestSize.Level1)
428 {
429 HiviewTestContext hiviewTestContext;
430 HiviewGlobal::CreateInstance(hiviewTestContext);
431 QueryArgument queryArgument1(-1, -1, 10);
432 auto queryWrapperBuilder = std::make_shared<EventQueryWrapperBuilder>(queryArgument1);
433 queryWrapperBuilder->Append("DOMAIN1", "EVENTNAME1", 1, R"~({"version":"V1","condition":
434 {"and":[{"param":"NAME","op":"=","value":"SysEventService"}]}})~");
435 queryWrapperBuilder->Append("DOMAIN2", "EVENTNAME2", 3, R"~({"version":"V1","condition":
436 {"and":[{"param":"NAME","op":"=","value":"SysEventService"}]}})~");
437 auto queryWrapper = queryWrapperBuilder->Build();
438 ASSERT_TRUE(queryWrapper != nullptr);
439 }
440 } // namespace HiviewDFX
441 } // namespace OHOS