• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "query_sys_event_callback_stub.h"
37 #include "plugin.h"
38 #include "ret_code.h"
39 #include "running_status_log_util.h"
40 #include "string_ex.h"
41 #include "sys_event.h"
42 #include "sys_event_callback_default.h"
43 #include "sys_event_callback_ohos_test.h"
44 #include "sys_event_rule.h"
45 #include "sys_event_service.h"
46 #include "sys_event_service_adapter.h"
47 #include "sys_event_service_ohos.h"
48 #include "sys_event_service_proxy.h"
49 #include "system_ability.h"
50 #include "string_ex.h"
51 #include "string_util.h"
52 #include "sys_event_callback_proxy.h"
53 #include "sys_event_callback_stub.h"
54 #include "sys_event_service_proxy.h"
55 #include "sys_event_service_stub.h"
56 
57 using namespace std;
58 
59 namespace OHOS {
60 namespace HiviewDFX {
61 namespace {
62 constexpr char ASH_MEM_NAME[] = "TestSharedMemory";
63 constexpr int32_t ASH_MEM_SIZE = 1024 * 2; // 2K
64 constexpr int SYS_EVENT_SERVICE_ID = 1203;
65 constexpr char TEST_LOG_DIR[] = "/data/log/hiview/sys_event_test";
66 const std::vector<int> EVENT_TYPES = {1, 2, 3, 4}; // FAULT = 1, STATISTIC = 2 SECURITY = 3, BEHAVIOR = 4
67 
GetAshmem()68 sptr<Ashmem> GetAshmem()
69 {
70     auto ashmem = Ashmem::CreateAshmem(ASH_MEM_NAME, ASH_MEM_SIZE);
71     if (ashmem == nullptr) {
72         return nullptr;
73     }
74     if (!ashmem->MapReadAndWriteAshmem()) {
75         return ashmem;
76     }
77     return ashmem;
78 }
79 
80 class TestQuerySysEventCallbackStub : public QuerySysEventCallbackStub {
81 public:
TestQuerySysEventCallbackStub()82     TestQuerySysEventCallbackStub() {}
~TestQuerySysEventCallbackStub()83     virtual ~TestQuerySysEventCallbackStub() {}
84 
OnQuery(const std::vector<std::u16string> & sysEvent,const std::vector<int64_t> & seq)85     void OnQuery(const std::vector<std::u16string>& sysEvent, const std::vector<int64_t>& seq) {}
OnComplete(int32_t reason,int32_t total,int64_t seq)86     void OnComplete(int32_t reason, int32_t total, int64_t seq) {}
87 
88 public:
89     enum Code {
90         DEFAULT = -1,
91         ON_QUERY = 0,
92         ON_COMPLETE,
93     };
94 };
95 
96 class TestSysEventCallbackStub : public SysEventCallbackStub {
97 public:
TestSysEventCallbackStub()98     TestSysEventCallbackStub() {}
~TestSysEventCallbackStub()99     virtual ~TestSysEventCallbackStub() {}
100 
Handle(const std::u16string & domain,const std::u16string & eventName,uint32_t eventType,const std::u16string & eventDetail)101     void Handle(const std::u16string& domain, const std::u16string& eventName, uint32_t eventType,
102         const std::u16string& eventDetail) {}
103 
104 public:
105     enum Code {
106         DEFAULT = -1,
107         HANDLE = 0,
108     };
109 };
110 
111 class TestSysEventServiceStub : public SysEventServiceStub {
112 public:
TestSysEventServiceStub()113     TestSysEventServiceStub() {}
~TestSysEventServiceStub()114     virtual ~TestSysEventServiceStub() {}
115 
AddListener(const std::vector<SysEventRule> & rules,const sptr<ISysEventCallback> & callback)116     int32_t AddListener(const std::vector<SysEventRule>& rules, const sptr<ISysEventCallback>& callback)
117     {
118         return 0;
119     }
120 
RemoveListener(const sptr<ISysEventCallback> & callback)121     int32_t RemoveListener(const sptr<ISysEventCallback>& callback)
122     {
123         return 0;
124     }
125 
Query(const QueryArgument & queryArgument,const std::vector<SysEventQueryRule> & rules,const sptr<IQuerySysEventCallback> & callback)126     int32_t Query(const QueryArgument& queryArgument, const std::vector<SysEventQueryRule>& rules,
127         const sptr<IQuerySysEventCallback>& callback)
128     {
129         return 0;
130     }
131 
SetDebugMode(const sptr<ISysEventCallback> & callback,bool mode)132     int32_t SetDebugMode(const sptr<ISysEventCallback>& callback, bool mode)
133     {
134         return 0;
135     }
136 
137 public:
138     enum Code {
139         DEFAULT = -1,
140         ADD_SYS_EVENT_LISTENER = 0,
141         REMOVE_SYS_EVENT_LISTENER,
142         QUERY_SYS_EVENT,
143         SET_DEBUG_MODE
144     };
145 };
146 
147 class HiviewTestContext : public HiviewContext {
148 public:
GetHiViewDirectory(DirectoryType type __UNUSED)149     std::string GetHiViewDirectory(DirectoryType type __UNUSED)
150     {
151         return TEST_LOG_DIR;
152     }
153 };
154 }
155 
SetUpTestCase()156 void SysEventServiceOhosTest::SetUpTestCase() {}
157 
TearDownTestCase()158 void SysEventServiceOhosTest::TearDownTestCase() {}
159 
SetUp()160 void SysEventServiceOhosTest::SetUp() {}
161 
TearDown()162 void SysEventServiceOhosTest::TearDown()
163 {
164     (void)FileUtil::ForceRemoveDirectory(TEST_LOG_DIR);
165 }
166 
GetTestRule(int type,const string & domain,const string & eventName)167 static SysEventRule GetTestRule(int type, const string &domain, const string &eventName)
168 {
169     SysEventRule rule;
170     rule.ruleType = type;
171     rule.domain = domain;
172     rule.eventName = eventName;
173     return rule;
174 }
175 
GetTestRules(int type,const string & domain,const string & eventName)176 static vector<SysEventRule> GetTestRules(int type, const string &domain, const string &eventName)
177 {
178     vector<SysEventRule> rules;
179     rules.push_back(GetTestRule(type, domain, eventName));
180     return rules;
181 }
182 
183 /**
184  * @tc.name: CommonTest001
185  * @tc.desc: Check service is null condition.
186  * @tc.type: FUNC
187  * @tc.require: SR000GGSVB
188  */
189 HWTEST_F(SysEventServiceOhosTest, CommonTest001, testing::ext::TestSize.Level3)
190 {
191     sptr<ISysEventCallback> callbackDefault = new SysEventCallbackDefault();
192     vector<SysEventRule> rules = GetTestRules(1, "", "");
193     auto service = SysEventServiceOhos::GetInstance();
194     if (service == nullptr) {
195         return;
196     }
197     auto ret = service->AddListener(rules, callbackDefault);
198     printf("add listener result is %d.\n", ret);
199     ASSERT_TRUE(ret != 0);
200     ret = service->RemoveListener(callbackDefault);
201     printf("remove listener result is %d.\n", ret);
202     ASSERT_TRUE(ret != 0);
203 }
204 
205 /**
206  * @tc.name: AddListenerTest001
207  * @tc.desc: Check AddListener Function.
208  * @tc.type: FUNC
209  * @tc.require: SR000GGS49
210  */
211 HWTEST_F(SysEventServiceOhosTest, AddListenerTest001, testing::ext::TestSize.Level3)
212 {
213     sptr<ISysEventCallback> callbackDefault = new SysEventCallbackDefault();
214     sptr<ISysEventCallback> callbackTest = new SysEventCallbackOhosTest();
215     vector<SysEventRule> rules = GetTestRules(1, "", "");
216     SysEventService sysEventService;
217     SysEventServiceOhos::GetSysEventService(&sysEventService);
218     auto service = SysEventServiceOhos::GetInstance();
219     if (service == nullptr) {
220         return;
221     }
222     auto ret = service->AddListener(rules, nullptr);
223     ASSERT_TRUE(ret != 0);
224     ret = service->AddListener(rules, callbackDefault);
225     ASSERT_TRUE(ret != 0);
226     ret = service->AddListener(rules, callbackTest);
227     ASSERT_TRUE(ret != 0);
228     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
229     if (sam == nullptr) {
230         printf("SystemAbilityManager is nullptr.\n");
231         ASSERT_TRUE(false);
232     } else {
233         sptr<IRemoteObject> stub = sam->CheckSystemAbility(SYS_EVENT_SERVICE_ID);
234         if (stub != nullptr) {
235             printf("check sys event service success.\n");
236             auto proxy = new SysEventServiceProxy(stub);
237             auto ret = proxy->AddListener(rules, callbackTest);
238             printf("add listener result is %d.\n", ret);
239             ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
240             if (ret == 0) {
241                 sleep(1);
242                 proxy->AddListener(rules, callbackTest);
243             } else {
244                 printf("add listener fail.\n");
245                 ASSERT_TRUE(false);
246             }
247         } else {
248             printf("check sys event service failed.\n");
249             ASSERT_TRUE(false);
250         }
251     }
252 }
253 
254 /**
255  * @tc.name: RemoveListenerTest001
256  * @tc.desc: Check RemoveListener Function.
257  * @tc.type: FUNC
258  * @tc.require: SR000GGS49
259  */
260 HWTEST_F(SysEventServiceOhosTest, RemoveListenerTest001, testing::ext::TestSize.Level3)
261 {
262     auto service = SysEventServiceOhos::GetInstance();
263     if (service == nullptr) {
264         return;
265     }
266     service->RemoveListener(nullptr);
267     sptr<ISysEventCallback> callbackTest = new SysEventCallbackOhosTest();
268     vector<SysEventRule> rules = GetTestRules(1, "", "");
269     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
270     if (sam == nullptr) {
271         printf("SystemAbilityManager is nullptr.\n");
272         ASSERT_TRUE(false);
273     } else {
274         sptr<IRemoteObject> stub = sam->CheckSystemAbility(SYS_EVENT_SERVICE_ID);
275         if (stub != nullptr) {
276             printf("check sys event service success.\n");
277             auto proxy = new SysEventServiceProxy(stub);
278             auto ret = proxy->AddListener(rules, callbackTest);
279             if (ret == IPC_CALL_SUCCEED) {
280                 sleep(1);
281                 ret = proxy->RemoveListener(callbackTest);
282                 printf("remove listener result is %d.\n", ret);
283             } else {
284                 printf("add listener fail.\n");
285                 ASSERT_TRUE(false);
286             }
287         } else {
288             printf("check sys event service failed.\n");
289             ASSERT_TRUE(false);
290         }
291     }
292 }
293 
294 /**
295  * @tc.name: OnSysEventTest001
296  * @tc.desc: Check OnSysEvent Function.
297  * @tc.type: FUNC
298  * @tc.require: SR000GGS49
299  */
300 HWTEST_F(SysEventServiceOhosTest, OnSysEventTest001, testing::ext::TestSize.Level3)
301 {
302     sptr<ISysEventCallback> callbackTest = new SysEventCallbackOhosTest();
303     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
304     if (sam == nullptr) {
305         printf("SystemAbilityManager is nullptr.\n");
306         ASSERT_TRUE(false);
307     } else {
308         sptr<IRemoteObject> stub = sam->CheckSystemAbility(SYS_EVENT_SERVICE_ID);
309         if (stub != nullptr) {
310             printf("check sys event service success.\n");
311             auto proxy = new SysEventServiceProxy(stub);
312             vector<SysEventRule> rules;
313             SysEventRule rule0 = GetTestRule(0, "", "");
314             SysEventRule rule1 = GetTestRule(1, "Test", "Test");
315             SysEventRule rule2 = GetTestRule(2, "Test", "Test");
316             SysEventRule rule3 = GetTestRule(3, "", "[0-9]*");
317             rules.push_back(rule0);
318             rules.push_back(rule1);
319             rules.push_back(rule2);
320             rules.push_back(rule3);
321             auto ret = proxy->AddListener(rules, callbackTest);
322             sleep(5);
323             if (ret == IPC_CALL_SUCCEED) {
324                 sleep(1);
325                 proxy->RemoveListener(callbackTest);
326             } else {
327                 printf("add listener fail.\n");
328                 ASSERT_TRUE(false);
329             }
330         } else {
331             printf("check sys event service failed.\n");
332             ASSERT_TRUE(false);
333         }
334     }
335 }
336 
337 /**
338  * @tc.name: SetDebugModeTest
339  * @tc.desc: Check SetDebugMode Function.
340  * @tc.type: FUNC
341  * @tc.require: SR000GGSVA
342  */
343 HWTEST_F(SysEventServiceOhosTest, SetDebugModeTest, testing::ext::TestSize.Level3)
344 {
345     sptr<ISysEventCallback> callbackTest = new SysEventCallbackOhosTest();
346     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
347     if (sam == nullptr) {
348         printf("SystemAbilityManager is nullptr.\n");
349         ASSERT_TRUE(false);
350     } else {
351         sptr<IRemoteObject> stub = sam->CheckSystemAbility(SYS_EVENT_SERVICE_ID);
352         if (stub != nullptr) {
353             printf("check sys event service success.\n");
354             auto proxy = new SysEventServiceProxy(stub);
355             auto result = proxy->SetDebugMode(callbackTest, true);
356             printf("SetDebugMode result is %d.\n", result);
357             if (result == IPC_CALL_SUCCEED) {
358                 result = proxy->SetDebugMode(callbackTest, true);
359                 ASSERT_TRUE(result == ERR_DEBUG_MODE_SET_REPEAT);
360                 result = proxy->SetDebugMode(callbackTest, false);
361                 ASSERT_TRUE(result == IPC_CALL_SUCCEED);
362             }
363             ASSERT_TRUE(true);
364         } else {
365             printf("check sys event service failed.\n");
366             ASSERT_TRUE(false);
367         }
368     }
369 }
370 
371 /**
372  * @tc.name: SysEventServiceAdapterTest
373  * @tc.desc: test apis of SysEventServiceAdapter
374  * @tc.type: FUNC
375  * @tc.require: issueI62BDW
376  */
377 HWTEST_F(SysEventServiceOhosTest, SysEventServiceAdapterTest, testing::ext::TestSize.Level3)
378 {
379     OHOS::HiviewDFX::SysEventServiceAdapter::StartService(nullptr, nullptr);
380     std::shared_ptr<SysEvent> sysEvent = nullptr;
381     OHOS::HiviewDFX::SysEventServiceAdapter::OnSysEvent(sysEvent);
382     SysEventCreator sysEventCreator("DEMO", "EVENT_NAME", SysEventCreator::FAULT);
383     std::vector<int> values = {1, 2, 3};
384     sysEventCreator.SetKeyValue("KEY", values);
385     sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
386     OHOS::HiviewDFX::SysEventServiceAdapter::OnSysEvent(sysEvent);
387     ASSERT_TRUE(true);
388     OHOS::HiviewDFX::SysEventServiceAdapter::UpdateEventSeq(0);
389     ASSERT_TRUE(true);
390     OHOS::HiviewDFX::SysEventServiceAdapter::BindGetTagFunc(nullptr);
391     ASSERT_TRUE(true);
392     OHOS::HiviewDFX::SysEventServiceAdapter::BindGetTypeFunc(nullptr);
393     ASSERT_TRUE(true);
394 }
395 
396 /**
397  * @tc.name: TestAshMemory
398  * @tc.desc: Ashmemory test
399  * @tc.type: FUNC
400  * @tc.require: issueI62WJT
401  */
402 HWTEST_F(SysEventServiceOhosTest, TestAshMemory, testing::ext::TestSize.Level1)
403 {
404     MessageParcel msgParcel;
405     std::vector<std::u16string> from = {
406         Str8ToStr16(std::string("11")),
407         Str8ToStr16(std::string("22")),
408     };
409     auto result = AshMemUtils::WriteBulkData(msgParcel, from);
410     ASSERT_TRUE(result != nullptr);
411     std::vector<std::u16string> to;
412     auto result1 = AshMemUtils::ReadBulkData(msgParcel, to);
413     ASSERT_TRUE(result1);
414     ASSERT_TRUE(from.size() == to.size());
415     ASSERT_TRUE(Str16ToStr8(to[0]) == "11" && Str16ToStr8(to[1]) == "22");
416     AshMemUtils::CloseAshmem(nullptr);
417     ASSERT_TRUE(true);
418     AshMemUtils::CloseAshmem(GetAshmem());
419     ASSERT_TRUE(true);
420 }
421 
422 /**
423  * @tc.name: TestQuerySysEventCallback
424  * @tc.desc: QuerySysEventCallbackProxy/Stub test
425  * @tc.type: FUNC
426  * @tc.require: issueI62WJT
427  */
428 HWTEST_F(SysEventServiceOhosTest, TestQuerySysEventCallback, testing::ext::TestSize.Level1)
429 {
430     QuerySysEventCallbackStub* querySysEventCallback = new(std::nothrow) TestQuerySysEventCallbackStub();
431     MessageParcel data, reply;
432     MessageOption option;
433     querySysEventCallback->OnRemoteRequest(TestQuerySysEventCallbackStub::Code::DEFAULT, data, reply, option);
434     ASSERT_TRUE(true);
435     querySysEventCallback->OnRemoteRequest(TestQuerySysEventCallbackStub::Code::ON_QUERY, data, reply, option);
436     ASSERT_TRUE(true);
437     querySysEventCallback->OnRemoteRequest(TestQuerySysEventCallbackStub::Code::ON_COMPLETE, data, reply, option);
438     ASSERT_TRUE(true);
439     const sptr<IRemoteObject>& impl(querySysEventCallback);
440     QuerySysEventCallbackProxy sysEventCallbackProxy(impl);
441     std::vector<std::u16string> sysEvent {};
442     std::vector<int64_t> seq {};
443     sysEventCallbackProxy.OnQuery(sysEvent, seq);
444     ASSERT_TRUE(true);
445     sysEvent.emplace_back(Str8ToStr16(std::string("0")));
446     seq.emplace_back(1);
447     sysEventCallbackProxy.OnQuery(sysEvent, seq);
448     ASSERT_TRUE(true);
449     sysEventCallbackProxy.OnComplete(0, 0, 0);
450     ASSERT_TRUE(true);
451 }
452 
453 /**
454  * @tc.name: TestSysEventCallback
455  * @tc.desc: SysEventCallbackProxy/Stub test
456  * @tc.type: FUNC
457  * @tc.require: issueI62WJT
458  */
459 HWTEST_F(SysEventServiceOhosTest, TestSysEventCallback, testing::ext::TestSize.Level1)
460 {
461     SysEventCallbackStub* sysEventCallback = new(std::nothrow) TestSysEventCallbackStub();
462     MessageParcel data, reply;
463     MessageOption option;
464     sysEventCallback->OnRemoteRequest(TestSysEventCallbackStub::Code::DEFAULT, data, reply, option);
465     ASSERT_TRUE(true);
466     sysEventCallback->OnRemoteRequest(TestSysEventCallbackStub::Code::HANDLE, data, reply, option);
467     ASSERT_TRUE(true);
468     const sptr<IRemoteObject>& impl(sysEventCallback);
469     SysEventCallbackProxy sysEventCallbackProxy(impl);
470     sysEventCallbackProxy.Handle(Str8ToStr16(std::string("DOMAIN1")), Str8ToStr16(std::string("EVENT_NAME1")), 0,
471         Str8ToStr16(std::string("{}")));
472     ASSERT_TRUE(true);
473 }
474 
475 /**
476  * @tc.name: TestSysEventService
477  * @tc.desc: SysEventServiceProxy/Stub test
478  * @tc.type: FUNC
479  * @tc.require: issueI62WJT
480  */
481 HWTEST_F(SysEventServiceOhosTest, TestSysEventService, testing::ext::TestSize.Level1)
482 {
483     SysEventServiceStub* sysEventService = new(std::nothrow) TestSysEventServiceStub();
484     MessageParcel data, reply;
485     MessageOption option;
486     sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::DEFAULT, data, reply, option);
487     ASSERT_TRUE(true);
488     sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::ADD_SYS_EVENT_LISTENER, data, reply, option);
489     ASSERT_TRUE(true);
490     sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::REMOVE_SYS_EVENT_LISTENER, data, reply,
491         option);
492     ASSERT_TRUE(true);
493     sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::QUERY_SYS_EVENT, data, reply, option);
494     ASSERT_TRUE(true);
495     sysEventService->OnRemoteRequest(TestSysEventServiceStub::Code::SET_DEBUG_MODE, data, reply, option);
496     ASSERT_TRUE(true);
497     const sptr<IRemoteObject>& impl(sysEventService);
498     SysEventServiceProxy sysEventServiceProxy(impl);
499     OHOS::HiviewDFX::SysEventRule sysEventRule("DOMAIN", "EVENT_NAME", "TAG", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
500     std::vector<OHOS::HiviewDFX::SysEventRule> sysRules;
501     sysRules.emplace_back(sysEventRule);
502     const sptr<SysEventCallbackStub>& listener(new(std::nothrow) TestSysEventCallbackStub);
503     auto ret = sysEventServiceProxy.AddListener(sysRules, listener);
504     ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
505     ret = sysEventServiceProxy.SetDebugMode(listener, true);
506     ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
507     ret = sysEventServiceProxy.RemoveListener(listener);
508     ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
509     const sptr<QuerySysEventCallbackStub>& querier(new(std::nothrow) TestQuerySysEventCallbackStub);
510     long long defaultTimeStap = -1;
511     int queryCount = 10;
512     OHOS::HiviewDFX::QueryArgument argument(defaultTimeStap, defaultTimeStap, queryCount);
513     std::vector<OHOS::HiviewDFX::SysEventQueryRule> queryRules;
514     std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
515     OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
516     queryRules.emplace_back(queryRule);
517     ret = sysEventServiceProxy.Query(argument, queryRules, querier);
518     ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
519 }
520 
521 /**
522  * @tc.name: MarshallingTAndUnmarshallingTest
523  * @tc.desc: Unmarshalling test
524  * @tc.type: FUNC
525  * @tc.require: issueI62WJT
526  */
527 HWTEST_F(SysEventServiceOhosTest, MarshallingTAndUnmarshallingTest, testing::ext::TestSize.Level1)
528 {
529     long long defaultTimeStap = -1;
530     int queryCount = 10;
531     OHOS::HiviewDFX::QueryArgument argument(defaultTimeStap, defaultTimeStap, queryCount);
532     MessageParcel parcel1;
533     auto ret = argument.Marshalling(parcel1);
534     ASSERT_TRUE(ret);
535     QueryArgument* argsPtr = argument.Unmarshalling(parcel1);
536     ASSERT_TRUE(argsPtr != nullptr && argsPtr->maxEvents == 10 && argsPtr->beginTime == -1);
537     OHOS::HiviewDFX::SysEventRule rule("DOMAIN1", "EVENT_NAME2", "TAG3", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
538     MessageParcel parcel2;
539     ret = rule.Marshalling(parcel2);
540     ASSERT_TRUE(ret);
541     OHOS::HiviewDFX::SysEventRule* rulePtr = rule.Unmarshalling(parcel2);
542     ASSERT_TRUE(rulePtr != nullptr && rulePtr->domain == "DOMAIN1" &&
543         rulePtr->eventName == "EVENT_NAME2" && rulePtr->tag == "TAG3");
544 
545     std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
546     OHOS::HiviewDFX::SysEventQueryRule eventQueryRule("DOMAIN", eventNames);
547     MessageParcel parcel3;
548     ret = eventQueryRule.Marshalling(parcel3);
549     ASSERT_TRUE(ret);
550     OHOS::HiviewDFX::SysEventQueryRule* eventQueryRulePtr = eventQueryRule.Unmarshalling(parcel3);
551     ASSERT_TRUE(eventQueryRulePtr != nullptr && eventQueryRulePtr->domain == "DOMAIN" &&
552         eventQueryRulePtr->eventList.size() == 2 && eventQueryRulePtr->eventList[0] == "EVENT_NAME1");
553 }
554 
555 /**
556  * @tc.name: RunningStatusLogUtilTest
557  * @tc.desc: Test apis of RunningStatusLogUtil
558  * @tc.type: FUNC
559  * @tc.require: issueI62WJT
560  */
561 HWTEST_F(SysEventServiceOhosTest, RunningStatusLogUtilTest, testing::ext::TestSize.Level1)
562 {
563     HiviewTestContext hiviewTestContext;
564     HiviewGlobal::CreateInstance(hiviewTestContext);
565     std::vector<OHOS::HiviewDFX::SysEventQueryRule> queryRules;
566     std::vector<std::string> eventNames { "EVENT_NAME1", "EVENT_NAME2" };
567     OHOS::HiviewDFX::SysEventQueryRule queryRule("DOMAIN", eventNames);
568     RunningStatusLogUtil::LogTooManyQueryRules(queryRules);
569     ASSERT_TRUE(true);
570     queryRules.emplace_back(queryRule);
571     RunningStatusLogUtil::LogTooManyQueryRules(queryRules);
572     ASSERT_TRUE(true);
573     vector<SysEventRule> sysEventRules1;
574     RunningStatusLogUtil::LogTooManyWatchRules(sysEventRules1);
575     ASSERT_TRUE(true);
576     vector<SysEventRule> sysEventRules2 = GetTestRules(1, "", "");
577     RunningStatusLogUtil::LogTooManyWatchRules(sysEventRules2);
578     ASSERT_TRUE(true);
579     RunningStatusLogUtil::LogTooManyWatchers(30);
580 }
581 
582 /**
583  * @tc.name: SysEventServiceOhosIntanceTest
584  * @tc.desc: Test apis of SysEventServiceOhos
585  * @tc.type: FUNC
586  * @tc.require: issueI62WJT
587  */
588 HWTEST_F(SysEventServiceOhosTest, SysEventServiceOhosIntanceTest, testing::ext::TestSize.Level1)
589 {
590     auto service = SysEventServiceOhos::GetInstance();
591     if (service == nullptr) {
592         return;
593     }
594     HiviewTestContext hiviewTestContext;
595     HiviewGlobal::CreateInstance(hiviewTestContext);
596     const sptr<QuerySysEventCallbackStub>& querier(new(std::nothrow) TestQuerySysEventCallbackStub);
597     long long defaultTimeStap = -1;
598     OHOS::HiviewDFX::QueryArgument argument1(defaultTimeStap, defaultTimeStap, 10);
599     std::vector<OHOS::HiviewDFX::SysEventQueryRule> queryRules;
600     std::vector<std::string> eventNames { "" };
601     OHOS::HiviewDFX::SysEventQueryRule queryRule("", eventNames);
602     queryRules.emplace_back(queryRule);
603     auto ret = service->Query(argument1, queryRules, querier);
604     ASSERT_TRUE(ret == IPC_CALL_SUCCEED);
605     ret = service->Query(argument1, queryRules, querier);
606     ASSERT_TRUE(ret == ERR_QUERY_TOO_FREQUENTLY);
607     sptr<ISysEventCallback> callbackTest = new SysEventCallbackOhosTest();
608     vector<SysEventRule> rules;
609     SysEventRule rule = GetTestRule(0, "DOMAIN", "EVENT_NAME");
610     rules.push_back(rule);
611     ret = service->AddListener(rules, callbackTest);
612     ASSERT_TRUE(ret == ERR_ADD_DEATH_RECIPIENT);
613     ret = service->RemoveListener(callbackTest);
614     ASSERT_TRUE(ret == ERR_LISTENERS_EMPTY);
615     std::vector<std::u16string> args;
616     auto dumpRet = service->Dump(-1, args);
617     ASSERT_TRUE(dumpRet == -1);
618     dumpRet = service->Dump(0, args);
619     ASSERT_TRUE(dumpRet == 0);
620     service->OnRemoteDied(nullptr);
621 }
622 
623 /**
624  * @tc.name: ConditionParserTest01
625  * @tc.desc: Test apis of ConditionParser
626  * @tc.type: FUNC
627  * @tc.require: issueI62WJT
628  */
629 HWTEST_F(SysEventServiceOhosTest, ConditionParserTest01, testing::ext::TestSize.Level1)
630 {
631     OHOS::HiviewDFX::ConditionParser parser;
632     EventStore::Cond cond;
633     std::string condStr = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
634         "value":"SysEventService"}]}})~";
635     auto ret = parser.ParseCondition(condStr, cond);
636     ASSERT_TRUE(ret);
637     ret = parser.ParseCondition(condStr, cond);
638     ASSERT_TRUE(ret);
639     std::string condStr1 = R"~({"version":"V1","condition":{"or":[{"param":"NAME","op":"=",
640         "value":"SysEventService"},{"param":"NAME","op":"=","value":"SysEventSource"}]}})~";
641     ret = parser.ParseCondition(condStr1, cond);
642     ASSERT_TRUE(ret);
643     ret = parser.ParseCondition(condStr1, cond);
644     ASSERT_TRUE(ret);
645     std::string condStr2 = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
646         "value":"SysEventService"},{"param":"uid_","op":"=","value":1201}]}})~";
647     ret = parser.ParseCondition(condStr2, cond);
648     ASSERT_TRUE(ret);
649     ret = parser.ParseCondition(condStr2, cond);
650     ASSERT_TRUE(ret);
651     std::string condStr3 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op":">","value":0},
652         {"param":"uid_","op":"=","value":1201}],"or":[{"param":"NAME","op":"=","value":"SysEventService"},
653         {"param":"NAME","op":"=","value":"SysEventSource"}]}})~";
654     ret = parser.ParseCondition(condStr3, cond);
655     ASSERT_TRUE(ret);
656     ret = parser.ParseCondition(condStr3, cond);
657     ASSERT_TRUE(ret);
658     std::string condStr4 = R"~({"version":"V1","condition":{"and":[{"param1":"type_","op":">","value":0},
659         {"param2":"uid_","op":"=","value":1201}],"or":[{"param4":"NAME","op":"=","value":"SysEventService"},
660         {"param3":"NAME","op":"=","value":"SysEventSource"}]}})~";
661     ret = parser.ParseCondition(condStr4, cond);
662     ASSERT_TRUE(!ret);
663     ret = parser.ParseCondition(condStr4, cond);
664     ASSERT_TRUE(!ret);
665     std::string condSt5 = R"~({"version":"V1","condition":{"and":[{"param":"","op":">","value":0},
666         {"param":"","op":"=","value":1201}],"or":[{"param":"","op":"=","value":"SysEventService"},
667         {"param":"","op":"=","value":"SysEventSource"}]}})~";
668     ret = parser.ParseCondition(condSt5, cond);
669     ASSERT_TRUE(!ret);
670     ret = parser.ParseCondition(condSt5, cond);
671     ASSERT_TRUE(!ret);
672     std::string condSt6 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op1":">","value":0},
673         {"param":"uid_","op2":"=","value":1201}],"or":[{"param":"NAME","op3":"=","value":"SysEventService"},
674         {"param":"NAME","op4":"=","value":"SysEventSource"}]}})~";
675     ret = parser.ParseCondition(condSt6, cond);
676     ASSERT_TRUE(!ret);
677     ret = parser.ParseCondition(condSt6, cond);
678     ASSERT_TRUE(!ret);
679 }
680 
681 /**
682  * @tc.name: ConditionParserTest02
683  * @tc.desc: Test apis of ConditionParser
684  * @tc.type: FUNC
685  * @tc.require: issueI62WJT
686  */
687 HWTEST_F(SysEventServiceOhosTest, ConditionParserTest02, testing::ext::TestSize.Level1)
688 {
689     OHOS::HiviewDFX::ConditionParser parser;
690     EventStore::Cond cond;
691     std::string condSt7 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op":">","value11":0},
692         {"param":"uid_","op":"=","value2":1201}],"or":[{"param":"NAME","op":"=","value3":"SysEventService"},
693         {"param":"NAME","op":"=","value3":"SysEventSource"}]}})~";
694     auto ret = parser.ParseCondition(condSt7, cond);
695     ASSERT_TRUE(!ret);
696     ret = parser.ParseCondition(condSt7, cond);
697     ASSERT_TRUE(!ret);
698     std::string condStr8 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op":">","value":[]},
699         {"param":"uid_","op":"=","value":[]}],"or":[{"param":"NAME","op":"=","value":[]},
700         {"param":"NAME","op":"=","value":[]}]}})~";
701     ret = parser.ParseCondition(condStr8, cond);
702     ASSERT_TRUE(!ret);
703     ret = parser.ParseCondition(condStr8, cond);
704     ASSERT_TRUE(!ret);
705     std::string condStr9 = R"~({"version":"V1","condition1":{"and":[{"param":"type_","op":">","value":0},
706         {"param":"uid_","op":"=","value":1201}],"or":[{"param":"NAME","op":"=","value":"SysEventService"},
707         {"param":"NAME","op":"=","value":"SysEventSource"}]}})~";
708     ret = parser.ParseCondition(condStr9, cond);
709     ASSERT_TRUE(!ret);
710     ret = parser.ParseCondition(condStr9, cond);
711     ASSERT_TRUE(!ret);
712     std::string condStr10 = R"~({"version":"V1","condition":1})~";
713     ret = parser.ParseCondition(condStr10, cond);
714     ASSERT_TRUE(!ret);
715     ret = parser.ParseCondition(condStr10, cond);
716     ASSERT_TRUE(!ret);
717     std::string condStr11 = R"~({"version":"V2","condition":{"and":[{"param1":"type_","op":">","value":0},
718         {"param2":"uid_","op":"=","value":1201}],"or":[{"param4":"NAME","op":"=","value":"SysEventService"},
719         {"param3":"NAME","op":"=","value":"SysEventSource"}]}})~";
720     ret = parser.ParseCondition(condStr11, cond);
721     ASSERT_TRUE(!ret);
722     ret = parser.ParseCondition(condStr11, cond);
723     ASSERT_TRUE(!ret);
724 }
725 
726 /**
727  * @tc.name: QueryWrapperTest01
728  * @tc.desc: BUild query wrapper with all event types
729  * @tc.type: FUNC
730  * @tc.require: issueI62WJT
731  */
732 HWTEST_F(SysEventServiceOhosTest, QueryWrapperTest01, testing::ext::TestSize.Level1)
733 {
734     HiviewTestContext hiviewTestContext;
735     HiviewGlobal::CreateInstance(hiviewTestContext);
736     QueryArgument queryArgument1(-1, -1, 10);
737     auto queryWrapperBuilder1 = std::make_shared<EventQueryWrapperBuilder>(queryArgument1);
738     QueryArgument queryArgument2(-1, -1, 10, 1, 20);
739     auto queryWrapperBuilder2 = std::make_shared<EventQueryWrapperBuilder>(queryArgument2);
740     for (auto eventType : EVENT_TYPES) {
741         queryWrapperBuilder1->Append(eventType);
742         queryWrapperBuilder2->Append(eventType);
743     }
744     auto queryWrapper1 = queryWrapperBuilder1->Build();
745     ASSERT_TRUE(queryWrapper1 != nullptr);
746     auto queryWrapper2 = queryWrapperBuilder2->Build();
747     ASSERT_TRUE(queryWrapper2 != nullptr);
748 
749     const sptr<QuerySysEventCallbackStub>& querier(new(std::nothrow) TestQuerySysEventCallbackStub);
750     auto queryRetCode = IPC_CALL_SUCCEED;
751     sleep(2);
752     queryWrapper1->Query(querier, queryRetCode);
753     ASSERT_TRUE(queryRetCode == IPC_CALL_SUCCEED);
754     sleep(2);
755     queryWrapper2->Query(querier, queryRetCode);
756     ASSERT_TRUE(queryRetCode == IPC_CALL_SUCCEED);
757 }
758 
759 /**
760  * @tc.name: QueryWrapperTest02
761  * @tc.desc: BUild query wrapper with domain, event name and event type.
762  * @tc.type: FUNC
763  * @tc.require: issueI62WJT
764  */
765 HWTEST_F(SysEventServiceOhosTest, QueryWrapperTest02, testing::ext::TestSize.Level1)
766 {
767     HiviewTestContext hiviewTestContext;
768     HiviewGlobal::CreateInstance(hiviewTestContext);
769     QueryArgument queryArgument1(-1, -1, 10);
770     auto queryWrapperBuilder = std::make_shared<EventQueryWrapperBuilder>(queryArgument1);
771     queryWrapperBuilder->Append("DOMAIN1", "EVENTNAME1", 1, R"~({"version":"V1","condition":
772         {"and":[{"param":"NAME","op":"=","value":"SysEventService"}]}})~");
773     queryWrapperBuilder->Append("DOMAIN2", "EVENTNAME2", 3, R"~({"version":"V1","condition":
774         {"and":[{"param":"NAME","op":"=","value":"SysEventService"}]}})~");
775     auto queryWrapper = queryWrapperBuilder->Build();
776     ASSERT_TRUE(queryWrapper != nullptr);
777     const sptr<QuerySysEventCallbackStub>& querier(new(std::nothrow) TestQuerySysEventCallbackStub);
778     auto queryRetCode = IPC_CALL_SUCCEED;
779     sleep(2);
780     queryWrapper->Query(querier, queryRetCode);
781     ASSERT_TRUE(queryRetCode == IPC_CALL_SUCCEED);
782 }
783 } // namespace HiviewDFX
784 } // namespace OHOS