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