1 /*
2  * Copyright (c) 2024 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 "hisysevent_c_wrapper_test.h"
17 
18 #include "def.h"
19 #include "hisysevent_c_wrapper.h"
20 #include "hisysevent_rust_listener.h"
21 #include "hisysevent_rust_manager.h"
22 #include "hisysevent_rust_querier.h"
23 #include "ret_code.h"
24 #include "securec.h"
25 
26 using namespace std;
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 constexpr int TEST_WROTE_EVENT_SIZE = 5;
32 constexpr int TEST_RULE_CNT = 1;
33 constexpr int RECORDS_CNT = 1;
34 constexpr int EXT_BYTE_CNT = 1;
35 
TestCallback()36 void TestCallback()
37 {
38     // do nothing
39 }
40 
TestOnEventWrapperCb(OnRustCb callback,HiSysEventRecordC record)41 void TestOnEventWrapperCb(OnRustCb callback, HiSysEventRecordC record)
42 {
43     // do nothing
44 }
45 
TestOnServiceDiedWrapperCb(OnRustCb callback)46 void TestOnServiceDiedWrapperCb(OnRustCb callback)
47 {
48     // do nothing
49 }
50 
TestOnQueryWrapperCb(OnRustCb callback,HiSysEventRecordC * events,unsigned int size)51 void TestOnQueryWrapperCb(OnRustCb callback, HiSysEventRecordC* events, unsigned int size)
52 {
53     // do nothing
54 }
55 
TestOnCompleteWrapperCb(OnRustCb callback,int reason,int total)56 void TestOnCompleteWrapperCb(OnRustCb callback, int reason, int total)
57 {
58     // do nothing
59 }
60 
CreateWatchRule(const std::string & domain,const std::string & name,const std::string & tag)61 HiSysEventWatchRule CreateWatchRule(const std::string& domain, const std::string& name, const std::string& tag)
62 {
63     HiSysEventWatchRule rule;
64     if (memcpy_s(rule.domain, MAX_LENGTH_OF_EVENT_DOMAIN, domain.c_str(), domain.length() + EXT_BYTE_CNT) != EOK ||
65         memcpy_s(rule.name, MAX_LENGTH_OF_EVENT_NAME, name.c_str(), name.length() + EXT_BYTE_CNT) != EOK ||
66         memcpy_s(rule.tag, MAX_LENGTH_OF_EVENT_TAG, tag.c_str(), tag.length() + EXT_BYTE_CNT) != EOK) {
67         return rule;
68     }
69     rule.ruleType = 1; // 1 means whole_word
70     rule.eventType = 1; // 1 means event type is fault
71     return rule;
72 }
73 
CreateQueryRule(const std::string & domain,const std::string & name,const std::string & condition)74 HiSysEventQueryRuleWrapper CreateQueryRule(const std::string& domain, const std::string& name,
75     const std::string& condition)
76 {
77     HiSysEventQueryRuleWrapper rule;
78     if (memcpy_s(rule.domain, MAX_LENGTH_OF_EVENT_DOMAIN, domain.c_str(), domain.length() + EXT_BYTE_CNT) != EOK ||
79         memcpy_s(rule.eventList, MAX_EVENT_LIST_LEN, name.c_str(), name.length() + EXT_BYTE_CNT) != EOK) {
80         return rule;
81     }
82     rule.eventListSize = name.length();
83     rule.condition = const_cast<char*>(condition.c_str());
84     return rule;
85 }
86 
CreateSysEventRecord(const std::string & domain,const std::string & name,const std::string & timeZone)87 HiSysEventRecordC CreateSysEventRecord(const std::string& domain, const std::string& name,
88     const std::string& timeZone)
89 {
90     HiSysEventRecordC record;
91     if (memcpy_s(record.domain, MAX_LENGTH_OF_EVENT_DOMAIN, domain.c_str(), domain.length() + EXT_BYTE_CNT) != EOK ||
92         memcpy_s(record.eventName, MAX_LENGTH_OF_EVENT_NAME, name.c_str(), name.length() + EXT_BYTE_CNT) != EOK ||
93         memcpy_s(record.tz, MAX_LENGTH_OF_TIME_ZONE, timeZone.c_str(), timeZone.length() + EXT_BYTE_CNT) != EOK) {
94         return record;
95     }
96     record.type = 1; // 1 means event type is fault
97     record.time = 0; // 0 is a test timestamp
98     return record;
99 }
100 }
101 
SetUpTestCase()102 void HiSysEventCWrapperUnitTest::SetUpTestCase() {}
103 
TearDownTestCase()104 void HiSysEventCWrapperUnitTest::TearDownTestCase() {}
105 
SetUp()106 void HiSysEventCWrapperUnitTest::SetUp() {}
107 
TearDown()108 void HiSysEventCWrapperUnitTest::TearDown() {}
109 
110 /**
111  * @tc.name: HiSysEventCWrapperUnitTest001
112  * @tc.desc: Test APIs of defined in hisysevent_c_wrapper.h head file
113  * @tc.type: FUNC
114  * @tc.require: issueI8YWH1
115  */
116 HWTEST_F(HiSysEventCWrapperUnitTest, HiSysEventCWrapperUnitTest001, testing::ext::TestSize.Level3)
117 {
118     const std::string testKey = "TEST_KEY";
119     HiSysEventParamWrapper wroteEvents[TEST_WROTE_EVENT_SIZE];
120     for (int i = 0; i < TEST_WROTE_EVENT_SIZE; ++i) {
121         HiSysEventParamWrapper wrapper;
122         auto ret = memcpy_s(wrapper.paramName, MAX_LENGTH_OF_PARAM_NAME, testKey.c_str(),
123             testKey.length() + 1); // copy length + 1 bytes
124         if (ret != EOK) {
125             ASSERT_TRUE(false);
126         }
127         if (i == 0) {
128             wrapper.paramType = HISYSEVENT_STRING;
129         } else {
130             wrapper.paramType = HISYSEVENT_UINT64;
131         }
132         HiSysEventParamValue value;
133         if (i == 0) {
134             value.s = new char[5]; // 5 is a random length
135         } else {
136             value.ui64 = 18;
137         }
138         wrapper.paramValue = value;
139         wrapper.arraySize = 0; // 0 means value is not an array
140         wroteEvents[i] = wrapper;
141     }
142     auto ret = HiSysEventWriteWrapper("TestFuncName", 1000, "KERNEL_VENDOR", "POWER_KEY", 1,
143         wroteEvents, TEST_WROTE_EVENT_SIZE); // test code line number
144     ASSERT_EQ(ret, SUCCESS);
145 }
146 
147 /**
148  * @tc.name: HiSysEventCWrapperUnitTest002
149  * @tc.desc: Test APIs of HiSysEventRustListener
150  * @tc.type: FUNC
151  * @tc.require: issueI8YWH1
152  */
153 HWTEST_F(HiSysEventCWrapperUnitTest, HiSysEventCWrapperUnitTest002, testing::ext::TestSize.Level3)
154 {
155     HiSysEventRustWatcherC* watcher = CreateRustEventWatcher(reinterpret_cast<const void*>(TestCallback),
156         TestOnEventWrapperCb, reinterpret_cast<const void*>(TestCallback), TestOnServiceDiedWrapperCb);
157     const HiSysEventWatchRule rules[TEST_RULE_CNT] = {
158         CreateWatchRule("KERNEL_VENDOR", "POWER_KEY", "")
159     };
160     auto watchRet = HiSysEventAddWatcherWrapper(watcher, rules, TEST_RULE_CNT);
161     ASSERT_EQ(watchRet, SUCCESS);
162     watchRet = HiSysEventRemoveWatcherWrapper(watcher);
163     ASSERT_EQ(watchRet, SUCCESS);
164     watchRet = HiSysEventRemoveWatcherWrapper(watcher);
165     ASSERT_EQ(watchRet, ERR_LISTENER_NOT_EXIST);
166     RecycleRustEventWatcher(watcher);
167 }
168 
169 /**
170  * @tc.name: HiSysEventCWrapperUnitTest003
171  * @tc.desc: Test GetHiSysEventRecordByIndexWrapper
172  * @tc.type: FUNC
173  * @tc.require: issueI8YWH1
174  */
175 HWTEST_F(HiSysEventCWrapperUnitTest, HiSysEventCWrapperUnitTest003, testing::ext::TestSize.Level3)
176 {
177     const std::string testDomain = "KERNEL_VENDOR";
178     const std::string testEventName = "POWER_KEY";
179     const HiSysEventRecordC records[RECORDS_CNT] = {
180         CreateSysEventRecord(testDomain, testEventName, "+0800")
181     };
182     HiSysEventRecordC recordRet = GetHiSysEventRecordByIndexWrapper(records, RECORDS_CNT, 0);
183     ASSERT_EQ(recordRet.domain, testDomain);
184     ASSERT_EQ(recordRet.eventName, testEventName);
185     recordRet = GetHiSysEventRecordByIndexWrapper(records, RECORDS_CNT, RECORDS_CNT);
186     ASSERT_TRUE(recordRet.domain != testDomain);
187     ASSERT_TRUE(recordRet.eventName != testEventName);
188 }
189 
190 /**
191  * @tc.name: HiSysEventCWrapperUnitTest004
192  * @tc.desc: Test APIs of HiSysEventRustQuerier
193  * @tc.type: FUNC
194  * @tc.require: issueI8YWH1
195  */
196 HWTEST_F(HiSysEventCWrapperUnitTest, HiSysEventCWrapperUnitTest004, testing::ext::TestSize.Level3)
197 {
198     HiSysEventQueryArg quertArg {
199         .beginTime = -1, // -1 means querying without beginning time limit
200         .endTime = -1,   // -1 means querying without ending time limit
201         .maxEvents = 10, // query 10 events
202     };
203     const HiSysEventQueryRuleWrapper rules[TEST_RULE_CNT] = {
204         CreateQueryRule("KERNEL_VENDOR", "POWER_KEY", "")
205     };
206     HiSysEventRustQuerierC* querier = CreateRustEventQuerier(reinterpret_cast<const void*>(TestCallback),
207         TestOnQueryWrapperCb, reinterpret_cast<const void*>(TestCallback), TestOnCompleteWrapperCb);
208     auto queryRet = HiSysEventQueryWrapper(&quertArg, rules, TEST_RULE_CNT, querier);
209     sleep(3);
210     ASSERT_EQ(queryRet, SUCCESS);
211     ASSERT_EQ(querier->status, STATUS_NORMAL);
212     RecycleRustEventQuerier(nullptr);
213     RecycleRustEventQuerier(querier);
214     ASSERT_EQ(querier->status, STATUS_MEM_NEED_RECYCLE);
215 }
216 
217 /**
218  * @tc.name: HiSysEventCWrapperUnitTest005
219  * @tc.desc: Test APIs of HiSysEventRustQuerier under unnormal conditon
220  * @tc.type: FUNC
221  * @tc.require: issueI8YWH1
222  */
223 HWTEST_F(HiSysEventCWrapperUnitTest, HiSysEventCWrapperUnitTest005, testing::ext::TestSize.Level3)
224 {
225     HiSysEventRustQuerierC* querier = nullptr;
226     HiSysEventRustQuerier eventQuerier1(querier);
227     eventQuerier1.OnQuery(nullptr);
228     eventQuerier1.OnComplete(0, 0); // 0 is a test value
229     ASSERT_TRUE(querier == nullptr);
230     auto events = std::make_shared<std::vector<OHOS::HiviewDFX::HiSysEventRecord>>();
231     ASSERT_TRUE(events != nullptr);
232     std::string eventStrListeral = "{\"domain_\": \"DEMO\", \"name_\": \"EVENT_NAME_A\", \"type_\": 4,\
233         \"PARAM_A\": 3.4, \"UINT64_T\": 18446744073709551610, \"DOUBLE_T\": 3.3, \"INT64_T\": 9223372036854775800,\
234         \"PARAM_B\": [\"123\", \"456\", \"789\"], \"PARAM_C\": []}";
235     OHOS::HiviewDFX::HiSysEventRecord event(eventStrListeral);
236     events->emplace_back(event);
237     events->emplace_back(event);
238     querier = CreateRustEventQuerier(reinterpret_cast<const void*>(TestCallback),
239         TestOnQueryWrapperCb, reinterpret_cast<const void*>(TestCallback), TestOnCompleteWrapperCb);
240     HiSysEventRustQuerier eventQuerier2(querier);
241     eventQuerier2.OnQuery(events);
242     eventQuerier2.OnComplete(0, events->size()); // 0 is a test value
243     ASSERT_TRUE(querier != nullptr);
244 }
245 
246 /**
247  * @tc.name: HiSysEventCWrapperUnitTest006
248  * @tc.desc: Test APIs of HiSysEventRustListener under unnormal conditon
249  * @tc.type: FUNC
250  * @tc.require: issueI8YWH1
251  */
252 HWTEST_F(HiSysEventCWrapperUnitTest, HiSysEventCWrapperUnitTest006, testing::ext::TestSize.Level3)
253 {
254     HiSysEventRustWatcherC* watcher = nullptr;
255     HiSysEventRustListener listerner1(watcher);
256     listerner1.OnEvent(nullptr);
257     listerner1.OnServiceDied();
258     ASSERT_TRUE(watcher == nullptr);
259     watcher = CreateRustEventWatcher(reinterpret_cast<const void*>(TestCallback),
260         TestOnEventWrapperCb, reinterpret_cast<const void*>(TestCallback), TestOnServiceDiedWrapperCb);
261     HiSysEventRustListener listerner2(watcher);
262     listerner2.OnEvent(nullptr);
263     ASSERT_TRUE(watcher != nullptr);
264     std::string eventStrListeral = "{\"domain_\": \"DEMO\", \"name_\": \"EVENT_NAME_A\", \"type_\": 4,\
265         \"PARAM_A\": 3.4, \"UINT64_T\": 18446744073709551611, \"DOUBLE_T\": 3.3, \"INT64_T\": 9223372036854775801,\
266         \"PARAM_B\": [\"123\", \"456\", \"789\"], \"PARAM_C\": []}";
267     auto event = std::make_shared<OHOS::HiviewDFX::HiSysEventRecord>(eventStrListeral);
268     listerner2.OnEvent(event);
269     listerner2.OnServiceDied();
270     ASSERT_TRUE(event != nullptr);
271 }
272 
273 /**
274  * @tc.name: HiSysEventCWrapperUnitTest007
275  * @tc.desc: Test APIs of HiSysEventRustQuerier with invalid querier
276  * @tc.type: FUNC
277  * @tc.require: issueI8ZXDD
278  */
279 HWTEST_F(HiSysEventCWrapperUnitTest, HiSysEventCWrapperUnitTest007, testing::ext::TestSize.Level3)
280 {
281     HiSysEventQueryArg quertArg {
282         .beginTime = -1, // -1 means querying without beginning time limit
283         .endTime = -1,   // -1 means querying without ending time limit
284         .maxEvents = 10, // query 10 events
285     };
286     const HiSysEventQueryRuleWrapper rules[TEST_RULE_CNT] = {
287         CreateQueryRule("", "", "")
288     };
289     HiSysEventRustQuerierC* querier = CreateRustEventQuerier(reinterpret_cast<const void*>(TestCallback),
290         TestOnQueryWrapperCb, reinterpret_cast<const void*>(TestCallback), TestOnCompleteWrapperCb);
291     auto queryRet = HiSysEventQueryWrapper(&quertArg, rules, TEST_RULE_CNT, querier);
292     sleep(3);
293     ASSERT_EQ(queryRet, ERR_QUERY_RULE_INVALID);
294     HiSysEventRustQuerierC* invalidQuerier = CreateRustEventQuerier(nullptr, nullptr, nullptr, nullptr);
295     ASSERT_TRUE(invalidQuerier == nullptr);
296     queryRet = HiSysEventQueryWrapper(&quertArg, rules, TEST_RULE_CNT, invalidQuerier);
297     ASSERT_EQ(queryRet, ERR_LISTENER_NOT_EXIST);
298 }
299 
300 /**
301  * @tc.name: HiSysEventCWrapperUnitTest008
302  * @tc.desc: Test APIs of HiSysEventRustListener with invlaid watcher
303  * @tc.type: FUNC
304  * @tc.require: issueI8ZXDD
305  */
306 HWTEST_F(HiSysEventCWrapperUnitTest, HiSysEventCWrapperUnitTest008, testing::ext::TestSize.Level3)
307 {
308     HiSysEventRustWatcherC* watcher = CreateRustEventWatcher(nullptr, nullptr, nullptr, nullptr);
309     ASSERT_TRUE(watcher == nullptr);
310     const HiSysEventWatchRule rules[TEST_RULE_CNT] = {
311         CreateWatchRule("", "", "")
312     };
313     auto watchRet = HiSysEventAddWatcherWrapper(watcher, rules, TEST_RULE_CNT);
314     ASSERT_EQ(watchRet, ERR_LISTENER_NOT_EXIST);
315     watchRet = HiSysEventRemoveWatcherWrapper(watcher);
316     ASSERT_EQ(watchRet, ERR_LISTENER_NOT_EXIST);
317     watchRet = HiSysEventRemoveWatcherWrapper(nullptr);
318     ASSERT_EQ(watchRet, ERR_LISTENER_NOT_EXIST);
319     RecycleRustEventWatcher(watcher);
320     watchRet = HiSysEventRemoveWatcherWrapper(watcher);
321     ASSERT_EQ(watchRet, ERR_LISTENER_NOT_EXIST);
322 }
323 } // HiviewDFX
324 } // OHOS