1 /*
2  * Copyright (c) 2022-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 #include <chrono>
16 #include <cinttypes>
17 #include <cstring>
18 #include <map>
19 #include <unistd.h>
20 
21 #include <gtest/gtest.h>
22 #include "hilog/log.h"
23 #include "hisysevent_manager_c.h"
24 #include "hisysevent_record_c.h"
25 #include "ret_code.h"
26 #include "string_util.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS::HiviewDFX;
30 
31 #undef LOG_DOMAIN
32 #define LOG_DOMAIN 0xD002D08
33 
34 #undef LOG_TAG
35 #define LOG_TAG "HISYSEVENT_MANAGER_C_TEST"
36 
37 namespace {
38 constexpr int64_t MAX_NUM_OF_QUERY = 10;
39 constexpr size_t MAX_LEN_OF_DOMAIN = 16;
40 constexpr size_t MAX_LEN_OF_NAME = 32;
41 constexpr char TEST_DOMAIN[] = "HIVIEWDFX";
42 constexpr char TEST_NAME[] = "PLUGIN_LOAD";
43 constexpr uint32_t QUERY_INTERVAL_TIME = 2;
44 constexpr int ERR_NULL = -1;
45 
GetMilliseconds()46 int64_t GetMilliseconds()
47 {
48     auto now = std::chrono::system_clock::now();
49     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
50     return millisecs.count();
51 }
52 
InitQueryArg(HiSysEventQueryArg & arg)53 void InitQueryArg(HiSysEventQueryArg& arg)
54 {
55     arg.beginTime = 0;
56     arg.endTime = GetMilliseconds();
57     arg.maxEvents = MAX_NUM_OF_QUERY;
58 }
59 
InitQueryRule(HiSysEventQueryRule & rule)60 void InitQueryRule(HiSysEventQueryRule& rule)
61 {
62     (void)StringUtil::CopyCString(rule.domain, TEST_DOMAIN, MAX_LEN_OF_DOMAIN);
63     (void)StringUtil::CopyCString(rule.eventList[0], TEST_NAME, MAX_LEN_OF_NAME);
64     rule.eventListSize = 1;
65     rule.condition = nullptr;
66 }
67 
InitQueryRuleWithCondition(HiSysEventQueryRule & rule,const std::string & cond)68 void InitQueryRuleWithCondition(HiSysEventQueryRule& rule, const std::string& cond)
69 {
70     (void)StringUtil::CopyCString(rule.domain, TEST_DOMAIN, MAX_LEN_OF_DOMAIN);
71     (void)StringUtil::CopyCString(rule.eventList[0], TEST_NAME, MAX_LEN_OF_NAME);
72     rule.eventListSize = 1;
73     (void)StringUtil::CreateCString(&rule.condition, cond);
74 }
75 
RecordBaseParamPrint(const HiSysEventRecord & record)76 void RecordBaseParamPrint(const HiSysEventRecord& record)
77 {
78     HILOG_DEBUG(LOG_CORE, "event: domain=%{public}s, name=%{public}s, type=%{public}d, tz=%{public}s, "
79             "time=%{public}" PRIu64 ", pid=%{public}" PRId64 ", tid=%{public}" PRId64 ", uid=%{public}"
80             PRId64 ", traceId=%{public}" PRIu64 ", spandId=%{public}" PRIu64 ", pspanId=%{public}"
81             PRIu64 ", level=%{public}s" ", tag=%{public}s",
82             record.domain, record.eventName, record.type,
83             record.tz, record.time, record.pid, record.tid, record.uid,
84             record.traceId, record.spandId, record.pspanId,
85             record.level, record.tag == nullptr ? "null" : record.tag);
86 }
87 
OnQueryTest(HiSysEventRecord records[],size_t size)88 void OnQueryTest(HiSysEventRecord records[], size_t size)
89 {
90     HILOG_INFO(LOG_CORE, "OnQuery: size of records is %{public}zu", size);
91     for (size_t i = 0; i < size; i++) {
92         HiSysEventRecord record = records[i];
93         ASSERT_EQ(strcmp(record.domain, TEST_DOMAIN), 0);
94         ASSERT_GT(strlen(record.eventName), 0);
95         ASSERT_GT(strlen(record.tz), 0);
96         ASSERT_GT(record.type, 0);
97         ASSERT_GT(record.time, 0);
98         ASSERT_GE(record.pid, 0);
99         ASSERT_GE(record.tid, 0);
100         ASSERT_GE(record.uid, 0);
101         ASSERT_GE(record.traceId, 0);
102         ASSERT_GE(record.spandId, 0);
103         ASSERT_GE(record.pspanId, 0);
104         ASSERT_GT(strlen(record.level), 0);
105         if (record.tag != nullptr) {
106             ASSERT_GT(strlen(record.tag), 0);
107         }
108         ASSERT_GT(strlen(record.jsonStr), 0);
109         RecordBaseParamPrint(record);
110         HILOG_INFO(LOG_CORE, "OnQuery: event=%{public}s", record.jsonStr);
111     }
112 }
113 
OnCompleteTest(int32_t reason,int32_t total)114 void OnCompleteTest(int32_t reason, int32_t total)
115 {
116     HILOG_INFO(LOG_CORE, "OnCompleted, res=%{public}d, total=%{public}d", reason, total);
117 }
118 
InitCallback(HiSysEventQueryCallback & callback)119 void InitCallback(HiSysEventQueryCallback& callback)
120 {
121     callback.OnQuery = OnQueryTest;
122     callback.OnComplete = OnCompleteTest;
123 }
124 
OnEventTest(HiSysEventRecordC record)125 void OnEventTest(HiSysEventRecordC record)
126 {
127     ASSERT_GT(strlen(record.jsonStr), 0);
128     HILOG_INFO(LOG_CORE, "OnEvent: event=%{public}s", record.jsonStr);
129 }
130 
OnServiceDiedTest()131 void OnServiceDiedTest()
132 {
133     HILOG_INFO(LOG_CORE, "OnServiceDied");
134 }
135 
InitWatcher(HiSysEventWatcher & watcher)136 void InitWatcher(HiSysEventWatcher& watcher)
137 {
138     watcher.OnEvent = OnEventTest;
139     watcher.OnServiceDied = OnServiceDiedTest;
140 }
141 
QueryTestWithCondition(const std::string & cond)142 void QueryTestWithCondition(const std::string& cond)
143 {
144     sleep(QUERY_INTERVAL_TIME); // avoid triggering high frequency queries
145     HiSysEventQueryArg arg;
146     InitQueryArg(arg);
147 
148     HiSysEventQueryRule rule;
149     InitQueryRuleWithCondition(rule, cond);
150     HiSysEventQueryRule rules[] = { rule };
151 
152     HiSysEventQueryCallback callback;
153     InitCallback(callback);
154 
155     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
156     ASSERT_EQ(res, 0);
157     StringUtil::DeletePointer<char>(&rule.condition);
158 }
159 
BuildRecordString(const std::map<std::string,std::string> & recordData)160 std::string BuildRecordString(const std::map<std::string, std::string>& recordData)
161 {
162     std::string recordStr = "{";
163     for (auto& recordParam : recordData) {
164         recordStr.append(recordParam.first).append(":").append(recordParam.second).append(",");
165     }
166     if (recordData.size() > 0) {
167         recordStr.pop_back();
168     }
169     recordStr.append("}");
170     return recordStr;
171 }
172 
RecordParamNameTest(const HiSysEventRecord & record,const std::map<std::string,std::string> & recordData)173 void RecordParamNameTest(const HiSysEventRecord& record, const std::map<std::string, std::string>& recordData)
174 {
175     char** params = nullptr;
176     size_t len = 0;
177     OH_HiSysEvent_GetParamNames(&record, &params, &len);
178     ASSERT_EQ(len, recordData.size());
179     for (size_t i = 0; i < len; i++) {
180         HILOG_DEBUG(LOG_CORE, "param[%{public}zu]=%{public}s", i, params[i]);
181         ASSERT_NE(recordData.find("\"" + std::string(params[i]) + "\""), recordData.end());
182     }
183     StringUtil::DeletePointers<char>(&params, len);
184 }
185 
RecordParamIntValueTest(const HiSysEventRecord & record,const std::string & name,int64_t value)186 void RecordParamIntValueTest(const HiSysEventRecord& record, const std::string& name, int64_t value)
187 {
188     int64_t testValue = 0;
189     int res = OH_HiSysEvent_GetParamInt64Value(&record, name.c_str(), &testValue);
190     ASSERT_EQ(res, 0);
191     ASSERT_EQ(testValue, value);
192 }
193 
RecordParamUintValueTest(const HiSysEventRecord & record,const std::string & name,uint64_t value)194 void RecordParamUintValueTest(const HiSysEventRecord& record, const std::string& name, uint64_t value)
195 {
196     uint64_t testValue = 0;
197     int res = OH_HiSysEvent_GetParamUint64Value(&record, name.c_str(), &testValue);
198     ASSERT_EQ(res, 0);
199     ASSERT_EQ(testValue, value);
200 }
201 
RecordParamDouValueTest(const HiSysEventRecord & record,const std::string & name,double value)202 void RecordParamDouValueTest(const HiSysEventRecord& record, const std::string& name, double value)
203 {
204     double testValue = 0;
205     int res = OH_HiSysEvent_GetParamDoubleValue(&record, name.c_str(), &testValue);
206     ASSERT_EQ(res, 0);
207     ASSERT_EQ(testValue, value);
208 }
209 
RecordParamStrValueTest(const HiSysEventRecord & record,const std::string & name,const std::string & value)210 void RecordParamStrValueTest(const HiSysEventRecord& record, const std::string& name, const std::string& value)
211 {
212     char* testValue = nullptr;
213     int res = OH_HiSysEvent_GetParamStringValue(&record, name.c_str(), &testValue);
214     ASSERT_EQ(res, 0);
215     ASSERT_EQ(strcmp(testValue, value.c_str()), 0);
216     StringUtil::DeletePointer<char>(&testValue);
217 }
218 
RecordParamIntValuesTest(const HiSysEventRecord & record,const std::string & name,const std::vector<int64_t> & values)219 void RecordParamIntValuesTest(const HiSysEventRecord& record, const std::string& name,
220     const std::vector<int64_t>& values)
221 {
222     int64_t* testValues = nullptr;
223     size_t len = 0;
224     int res = OH_HiSysEvent_GetParamInt64Values(&record, name.c_str(), &testValues, &len);
225     ASSERT_EQ(res, 0);
226     ASSERT_EQ(values.size(), len);
227     for (size_t i = 0; i < len; i++) {
228         ASSERT_EQ(testValues[i], values[i]);
229     }
230     StringUtil::DeletePointer<int64_t>(&testValues);
231 }
232 
RecordParamUintValuesTest(const HiSysEventRecord & record,const std::string & name,const std::vector<uint64_t> & values)233 void RecordParamUintValuesTest(const HiSysEventRecord& record, const std::string& name,
234     const std::vector<uint64_t>& values)
235 {
236     uint64_t* testValues = nullptr;
237     size_t len = 0;
238     int res = OH_HiSysEvent_GetParamUint64Values(&record, name.c_str(), &testValues, &len);
239     ASSERT_EQ(res, 0);
240     ASSERT_EQ(values.size(), len);
241     for (size_t i = 0; i < len; i++) {
242         ASSERT_EQ(testValues[i], values[i]);
243     }
244     StringUtil::DeletePointer<uint64_t>(&testValues);
245 }
246 
RecordParamDouValuesTest(const HiSysEventRecord & record,const std::string & name,const std::vector<double> & values)247 void RecordParamDouValuesTest(const HiSysEventRecord& record, const std::string& name,
248     const std::vector<double>& values)
249 {
250     double* testValues = nullptr;
251     size_t len = 0;
252     int res = OH_HiSysEvent_GetParamDoubleValues(&record, name.c_str(), &testValues, &len);
253     ASSERT_EQ(res, 0);
254     ASSERT_EQ(values.size(), len);
255     for (size_t i = 0; i < len; i++) {
256         ASSERT_EQ(testValues[i], values[i]);
257     }
258     StringUtil::DeletePointer<double>(&testValues);
259 }
260 
RecordParamStrValuesTest(const HiSysEventRecord & record,const std::string & name,const std::vector<std::string> & values)261 void RecordParamStrValuesTest(const HiSysEventRecord& record, const std::string& name,
262     const std::vector<std::string>& values)
263 {
264     char** testValues = nullptr;
265     size_t len = 0;
266     int res = OH_HiSysEvent_GetParamStringValues(&record, name.c_str(), &testValues, &len);
267     ASSERT_EQ(res, 0);
268     for (size_t i = 0; i < len; i++) {
269         ASSERT_EQ(strcmp(testValues[i], values[i].c_str()), 0);
270     }
271     StringUtil::DeletePointers<char>(&testValues, len);
272 }
273 
RecordParamNameInvalidTest(const HiSysEventRecord & record)274 void RecordParamNameInvalidTest(const HiSysEventRecord& record)
275 {
276     char** params = nullptr;
277     size_t len = 0;
278     OH_HiSysEvent_GetParamNames(&record, &params, &len);
279     ASSERT_EQ(len, 0);
280 }
281 
RecordParamIntValueInvalidTest(const HiSysEventRecord & record,const std::string & name,int expRes)282 void RecordParamIntValueInvalidTest(const HiSysEventRecord& record, const std::string& name, int expRes)
283 {
284     int64_t testValue = 0;
285     int res = OH_HiSysEvent_GetParamInt64Value(&record, name.c_str(), &testValue);
286     ASSERT_EQ(res, expRes);
287 }
288 
RecordParamUintValueInvalidTest(const HiSysEventRecord & record,const std::string & name,int expRes)289 void RecordParamUintValueInvalidTest(const HiSysEventRecord& record, const std::string& name, int expRes)
290 {
291     uint64_t testValue = 0;
292     int res = OH_HiSysEvent_GetParamUint64Value(&record, name.c_str(), &testValue);
293     ASSERT_EQ(res, expRes);
294 }
295 
RecordParamDouValueInvalidTest(const HiSysEventRecord & record,const std::string & name,int expRes)296 void RecordParamDouValueInvalidTest(const HiSysEventRecord& record, const std::string& name, int expRes)
297 {
298     double testValue = 0;
299     int res = OH_HiSysEvent_GetParamDoubleValue(&record, name.c_str(), &testValue);
300     ASSERT_EQ(res, expRes);
301 }
302 
RecordParamStrValueInvalidTest(const HiSysEventRecord & record,const std::string & name,int expRes)303 void RecordParamStrValueInvalidTest(const HiSysEventRecord& record, const std::string& name, int expRes)
304 {
305     char* testValue = nullptr;
306     int res = OH_HiSysEvent_GetParamStringValue(&record, name.c_str(), &testValue);
307     ASSERT_EQ(res, expRes);
308 }
309 
RecordParamIntValuesInvalidTest(const HiSysEventRecord & record,const std::string & name,int expRes)310 void RecordParamIntValuesInvalidTest(const HiSysEventRecord& record, const std::string& name, int expRes)
311 {
312     int64_t* testValues = nullptr;
313     size_t len = 0;
314     int res = OH_HiSysEvent_GetParamInt64Values(&record, name.c_str(), &testValues, &len);
315     ASSERT_EQ(res, expRes);
316 }
317 
RecordParamUintValuesInvalidTest(const HiSysEventRecord & record,const std::string & name,int expRes)318 void RecordParamUintValuesInvalidTest(const HiSysEventRecord& record, const std::string& name, int expRes)
319 {
320     uint64_t* testValues = nullptr;
321     size_t len = 0;
322     int res = OH_HiSysEvent_GetParamUint64Values(&record, name.c_str(), &testValues, &len);
323     ASSERT_EQ(res, expRes);
324 }
325 
RecordParamDouValuesInvalidTest(const HiSysEventRecord & record,const std::string & name,int expRes)326 void RecordParamDouValuesInvalidTest(const HiSysEventRecord& record, const std::string& name, int expRes)
327 {
328     double* testValues = nullptr;
329     size_t len = 0;
330     int res = OH_HiSysEvent_GetParamDoubleValues(&record, name.c_str(), &testValues, &len);
331     ASSERT_EQ(res, expRes);
332 }
333 
RecordParamStrValuesInvalidTest(const HiSysEventRecord & record,const std::string & name,int expRes)334 void RecordParamStrValuesInvalidTest(const HiSysEventRecord& record, const std::string& name, int expRes)
335 {
336     char** testValues = nullptr;
337     size_t len = 0;
338     int res = OH_HiSysEvent_GetParamStringValues(&record, name.c_str(), &testValues, &len);
339     ASSERT_EQ(res, expRes);
340 }
341 }
342 
343 class HiSysEventManagerCTest : public testing::Test {
344 public:
345     void SetUp();
346     void TearDown();
347 };
348 
SetUp()349 void HiSysEventManagerCTest::SetUp()
350 {}
351 
TearDown()352 void HiSysEventManagerCTest::TearDown()
353 {}
354 
355 /**
356  * @tc.name: HiSysEventMgrCQueryTest001
357  * @tc.desc: Testing to query events.
358  * @tc.type: FUNC
359  * @tc.require: issueI5X08B
360  */
361 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest001, TestSize.Level3)
362 {
363     /**
364      * @tc.steps: step1. create HiSysEventQueryArg.
365      * @tc.steps: step2. create HiSysEventQueryRule.
366      * @tc.steps: step3. create HiSysEventQueryCallback.
367      * @tc.steps: step4. query event.
368      */
369     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest001 start");
370     HiSysEventQueryArg arg;
371     InitQueryArg(arg);
372 
373     HiSysEventQueryRule rule;
374     InitQueryRule(rule);
375     HiSysEventQueryRule rules[] = { rule };
376 
377     HiSysEventQueryCallback callback;
378     InitCallback(callback);
379 
380     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
381     ASSERT_EQ(res, 0);
382     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest001 end");
383 }
384 
385 /**
386  * @tc.name: HiSysEventMgrCQueryTest002
387  * @tc.desc: Testing to query events with condition.
388  * @tc.type: FUNC
389  * @tc.require: issueI5X08B
390  */
391 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest002, TestSize.Level3)
392 {
393     /**
394      * @tc.steps: step1. create HiSysEventQueryArg.
395      * @tc.steps: step2. create HiSysEventQueryRule.
396      * @tc.steps: step3. create HiSysEventQueryCallback.
397      * @tc.steps: step4. query event.
398      */
399     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest002 start");
400     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
401         "value":"SysEventStore"}]}})~";
402     QueryTestWithCondition(cond);
403     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest002 end");
404 }
405 
406 /**
407  * @tc.name: HiSysEventMgrCQueryTest003
408  * @tc.desc: Testing to query events with condition.
409  * @tc.type: FUNC
410  * @tc.require: issueI5X08B
411  */
412 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest003, TestSize.Level3)
413 {
414     /**
415      * @tc.steps: step1. create HiSysEventQueryArg.
416      * @tc.steps: step2. create HiSysEventQueryRule.
417      * @tc.steps: step3. create HiSysEventQueryCallback.
418      * @tc.steps: step4. query event.
419      */
420     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest003 start");
421     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"uid_","op":"=","value":1201}]}})~";
422     QueryTestWithCondition(cond);
423     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest003 end");
424 }
425 
426 /**
427  * @tc.name: HiSysEventMgrCQueryTest004
428  * @tc.desc: Testing to query events with condition.
429  * @tc.type: FUNC
430  * @tc.require: issueI5X08B
431  */
432 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest004, TestSize.Level3)
433 {
434     /**
435      * @tc.steps: step1. create HiSysEventQueryArg.
436      * @tc.steps: step2. create HiSysEventQueryRule.
437      * @tc.steps: step3. create HiSysEventQueryCallback.
438      * @tc.steps: step4. query event.
439      */
440     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest004 start");
441     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"pid_","op":">=","value":0}]}})~";
442     QueryTestWithCondition(cond);
443     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest004 end");
444 }
445 
446 /**
447  * @tc.name: HiSysEventMgrCQueryTest005
448  * @tc.desc: Testing to query events with condition.
449  * @tc.type: FUNC
450  * @tc.require: issueI5X08B
451  */
452 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest005, TestSize.Level3)
453 {
454     /**
455      * @tc.steps: step1. create HiSysEventQueryArg.
456      * @tc.steps: step2. create HiSysEventQueryRule.
457      * @tc.steps: step3. create HiSysEventQueryCallback.
458      * @tc.steps: step4. query event.
459      */
460     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest005 start");
461     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"type_","op":"<=","value":4}]}})~";
462     QueryTestWithCondition(cond);
463     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest005 end");
464 }
465 
466 /**
467  * @tc.name: HiSysEventMgrCQueryTest006
468  * @tc.desc: Testing to query events with condition.
469  * @tc.type: FUNC
470  * @tc.require: issueI5X08B
471  */
472 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest006, TestSize.Level3)
473 {
474     /**
475      * @tc.steps: step1. create HiSysEventQueryArg.
476      * @tc.steps: step2. create HiSysEventQueryRule.
477      * @tc.steps: step3. create HiSysEventQueryCallback.
478      * @tc.steps: step4. query event.
479      */
480     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest006 start");
481     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"pid_","op":">","value":0}]}})~";
482     QueryTestWithCondition(cond);
483     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest006 end");
484 }
485 
486 /**
487  * @tc.name: HiSysEventMgrCQueryTest007
488  * @tc.desc: Testing to query events with condition.
489  * @tc.type: FUNC
490  * @tc.require: issueI5X08B
491  */
492 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest007, TestSize.Level3)
493 {
494     /**
495      * @tc.steps: step1. create HiSysEventQueryArg.
496      * @tc.steps: step2. create HiSysEventQueryRule.
497      * @tc.steps: step3. create HiSysEventQueryCallback.
498      * @tc.steps: step4. query event.
499      */
500     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest007 start");
501     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"pid_","op":"<","value":0}]}})~";
502     QueryTestWithCondition(cond);
503     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest007 end");
504 }
505 
506 /**
507  * @tc.name: HiSysEventMgrCQueryTest008
508  * @tc.desc: Testing to query events with many rules.
509  * @tc.type: FUNC
510  * @tc.require: issueI5X08B
511  */
512 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest008, TestSize.Level3)
513 {
514     /**
515      * @tc.steps: step1. create HiSysEventQueryArg.
516      * @tc.steps: step2. create HiSysEventQueryRule.
517      * @tc.steps: step3. create HiSysEventQueryCallback.
518      * @tc.steps: step4. query event.
519      */
520     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest008 start");
521     sleep(QUERY_INTERVAL_TIME);
522     HiSysEventQueryArg arg;
523     InitQueryArg(arg);
524 
525     HiSysEventQueryRule rule1;
526     (void)StringUtil::CopyCString(rule1.domain, TEST_DOMAIN, MAX_LEN_OF_DOMAIN);
527     (void)StringUtil::CopyCString(rule1.eventList[0], TEST_NAME, MAX_LEN_OF_NAME);
528     (void)StringUtil::CopyCString(rule1.eventList[1], "PLUGIN_UNLOAD", MAX_LEN_OF_NAME);
529     rule1.eventListSize = 2;
530     HiSysEventQueryRule rule2;
531     (void)StringUtil::CopyCString(rule2.domain, TEST_DOMAIN, MAX_LEN_OF_DOMAIN);
532     (void)StringUtil::CopyCString(rule2.eventList[0], "APP_USAGE", MAX_LEN_OF_NAME);
533     (void)StringUtil::CopyCString(rule2.eventList[1], "SYS_USAGE", MAX_LEN_OF_NAME);
534     rule2.eventListSize = 2;
535     HiSysEventQueryRule rules[] = { rule1, rule2 };
536 
537     HiSysEventQueryCallback callback;
538     InitCallback(callback);
539 
540     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
541     ASSERT_EQ(res, 0);
542     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest008 end");
543 }
544 
545 /**
546  * @tc.name: HiSysEventMgrCQueryTest009
547  * @tc.desc: Testing to query events with many conditions.
548  * @tc.type: FUNC
549  * @tc.require: issueI5X08B
550  */
551 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest009, TestSize.Level3)
552 {
553     /**
554      * @tc.steps: step1. create HiSysEventQueryArg.
555      * @tc.steps: step2. create HiSysEventQueryRule.
556      * @tc.steps: step3. create HiSysEventQueryCallback.
557      * @tc.steps: step4. query event.
558      */
559     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest009 start");
560     std::string cond1 = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
561         "value":"SysEventStore"},{"param":"uid_","op":"=","value":1201}]}})~";
562     QueryTestWithCondition(cond1);
563 
564     std::string cond2 = R"~({"version":"V1","condition":{"and":[{"param":"type_","op":">","value":0},
565         {"param":"uid_","op":"=","value":1201}]}})~";
566     QueryTestWithCondition(cond2);
567     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest009 end");
568 }
569 
570 /**
571  * @tc.name: HiSysEventMgrCQueryTest010
572  * @tc.desc: Testing to query events with many rules and condtions.
573  * @tc.type: FUNC
574  * @tc.require: issueI5X08B
575  */
576 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest010, TestSize.Level3)
577 {
578     /**
579      * @tc.steps: step1. create HiSysEventQueryArg.
580      * @tc.steps: step2. create HiSysEventQueryRule.
581      * @tc.steps: step3. create HiSysEventQueryCallback.
582      * @tc.steps: step4. query event.
583      */
584     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest010 start");
585     sleep(QUERY_INTERVAL_TIME);
586     HiSysEventQueryArg arg;
587     InitQueryArg(arg);
588 
589     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"time_","op":">",
590         "value":0},{"param":"type_","op":">","value":0}]}})~";
591     HiSysEventQueryRule rule1;
592     (void)StringUtil::CopyCString(rule1.domain, TEST_DOMAIN, MAX_LEN_OF_DOMAIN);
593     (void)StringUtil::CopyCString(rule1.eventList[0], TEST_NAME, MAX_LEN_OF_NAME);
594     (void)StringUtil::CopyCString(rule1.eventList[1], "PLUGIN_UNLOAD", MAX_LEN_OF_NAME);
595     rule1.eventListSize = 2;
596     (void)StringUtil::CreateCString(&rule1.condition, cond);
597     HiSysEventQueryRule rule2;
598     (void)StringUtil::CopyCString(rule2.domain, TEST_DOMAIN, MAX_LEN_OF_DOMAIN);
599     (void)StringUtil::CopyCString(rule2.eventList[0], "APP_USAGE", MAX_LEN_OF_NAME);
600     (void)StringUtil::CopyCString(rule2.eventList[1], "SYS_USAGE", MAX_LEN_OF_NAME);
601     rule2.eventListSize = 2;
602     (void)StringUtil::CreateCString(&rule2.condition, cond);
603     HiSysEventQueryRule rules[] = { rule1, rule2 };
604 
605     HiSysEventQueryCallback callback;
606     InitCallback(callback);
607 
608     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
609     ASSERT_EQ(res, 0);
610     StringUtil::DeletePointer<char>(&rule1.condition);
611     StringUtil::DeletePointer<char>(&rule2.condition);
612     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest010 end");
613 }
614 
615 /**
616  * @tc.name: HiSysEventMgrCQueryTest011
617  * @tc.desc: Testing to query events with invalid condition.
618  * @tc.type: FUNC
619  * @tc.require: issueI5X08B
620  */
621 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest011, TestSize.Level3)
622 {
623     /**
624      * @tc.steps: step1. create HiSysEventQueryArg.
625      * @tc.steps: step2. create HiSysEventQueryRule.
626      * @tc.steps: step3. create HiSysEventQueryCallback.
627      * @tc.steps: step4. query event.
628      */
629     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest011 start");
630 
631     std::string cond1 = R"~({"version":"xx","condition":{}})~";
632     QueryTestWithCondition(cond1);
633 
634     std::string cond2 = "invalid condition";
635     QueryTestWithCondition(cond2);
636 
637     std::string cond3 = R"~({"version":"V1","condition":{"invalid":[]}})~";
638     QueryTestWithCondition(cond3);
639 
640     std::string cond4 = R"~({"version":"V1","condition":{"and":[{"invalid":"PLUGIN_NAME","op":"=",
641         "value":"SysEventStore"}]}})~";
642     QueryTestWithCondition(cond4);
643 
644     std::string cond5 = R"~({"version":"V1","condition":{"and":[{"param":"PLUGIN_NAME","invalid":"=",
645         "value":"SysEventStore"}]}})~";
646     QueryTestWithCondition(cond5);
647 
648     std::string cond6 = R"~({"version":"V1","condition":{"and":[{"param":"PLUGIN_NAME","op":"**",
649         "value":"SysEventStore"}]}})~";
650     QueryTestWithCondition(cond6);
651 
652     std::string cond7 = R"~({"version":"V1","condition":{"and":[{"param":"PLUGIN_NAME","op":"=",
653         "invalid":"SysEventStore"}]}})~";
654     QueryTestWithCondition(cond7);
655 
656     std::string cond8 = R"~({"version":"V1","condition":{"and":[{"param":"PLUGIN_NAME","op":"=",
657         "value":[]}]}})~";
658     QueryTestWithCondition(cond8);
659 
660     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest011 end");
661 }
662 
663 /**
664  * @tc.name: HiSysEventMgrCQueryTest012
665  * @tc.desc: Testing to query events only with domain.
666  * @tc.type: FUNC
667  * @tc.require: issueI5X08B
668  */
669 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest012, TestSize.Level3)
670 {
671     /**
672      * @tc.steps: step1. create HiSysEventQueryArg.
673      * @tc.steps: step2. create HiSysEventQueryRule.
674      * @tc.steps: step3. create HiSysEventQueryCallback.
675      * @tc.steps: step4. query event.
676      */
677     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest012 start");
678     sleep(QUERY_INTERVAL_TIME);
679     HiSysEventQueryArg arg;
680     InitQueryArg(arg);
681     HiSysEventQueryRule rule;
682     (void)StringUtil::CopyCString(rule.domain, TEST_DOMAIN, MAX_LEN_OF_DOMAIN);
683     rule.eventListSize = 0;
684     HiSysEventQueryRule rules[] = { rule };
685     HiSysEventQueryCallback callback;
686     InitCallback(callback);
687     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
688     ASSERT_EQ(res, ERR_QUERY_RULE_INVALID);
689     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest012 end");
690 }
691 
692 /**
693  * @tc.name: HiSysEventMgrCQueryTest013
694  * @tc.desc: Testing to query events only with name.
695  * @tc.type: FUNC
696  * @tc.require: issueI5X08B
697  */
698 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest013, TestSize.Level3)
699 {
700     /**
701      * @tc.steps: step1. create HiSysEventQueryArg.
702      * @tc.steps: step2. create HiSysEventQueryRule.
703      * @tc.steps: step3. create HiSysEventQueryCallback.
704      * @tc.steps: step4. query event.
705      */
706     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest013 start");
707     sleep(QUERY_INTERVAL_TIME);
708     HiSysEventQueryArg arg;
709     InitQueryArg(arg);
710     HiSysEventQueryRule rule;
711     (void)StringUtil::CopyCString(rule.eventList[0], TEST_NAME, MAX_LEN_OF_NAME);
712     rule.eventListSize = 1;
713     HiSysEventQueryRule rules[] = { rule };
714     HiSysEventQueryCallback callback;
715     InitCallback(callback);
716     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
717     ASSERT_EQ(res, ERR_QUERY_RULE_INVALID);
718     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest013 end");
719 }
720 
721 /**
722  * @tc.name: HiSysEventMgrCQueryTest014
723  * @tc.desc: Testing to query events only with domain and condition.
724  * @tc.type: FUNC
725  * @tc.require: issueI5X08B
726  */
727 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest014, TestSize.Level3)
728 {
729     /**
730      * @tc.steps: step1. create HiSysEventQueryArg.
731      * @tc.steps: step2. create HiSysEventQueryRule.
732      * @tc.steps: step3. create HiSysEventQueryCallback.
733      * @tc.steps: step4. query event.
734      */
735     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest014 start");
736     sleep(QUERY_INTERVAL_TIME);
737     HiSysEventQueryArg arg;
738     InitQueryArg(arg);
739     HiSysEventQueryRule rule;
740     (void)StringUtil::CopyCString(rule.domain, TEST_DOMAIN, MAX_LEN_OF_DOMAIN);
741     rule.eventListSize = 0;
742     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
743         "value":"SysEventStore"}]}})~";
744     (void)StringUtil::CreateCString(&rule.condition, cond);
745     HiSysEventQueryRule rules[] = { rule };
746     HiSysEventQueryCallback callback;
747     InitCallback(callback);
748     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
749     ASSERT_EQ(res, ERR_QUERY_RULE_INVALID);
750     StringUtil::DeletePointer<char>(&rule.condition);
751     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest014 end");
752 }
753 
754 /**
755  * @tc.name: HiSysEventMgrCQueryTest015
756  * @tc.desc: Testing to query events only with name and condition.
757  * @tc.type: FUNC
758  * @tc.require: issueI5X08B
759  */
760 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest015, TestSize.Level3)
761 {
762     /**
763      * @tc.steps: step1. create HiSysEventQueryArg.
764      * @tc.steps: step2. create HiSysEventQueryRule.
765      * @tc.steps: step3. create HiSysEventQueryCallback.
766      * @tc.steps: step4. query event.
767      */
768     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest015 start");
769     sleep(QUERY_INTERVAL_TIME);
770     HiSysEventQueryArg arg;
771     InitQueryArg(arg);
772     HiSysEventQueryRule rule;
773     (void)StringUtil::CopyCString(rule.eventList[0], TEST_NAME, MAX_LEN_OF_NAME);
774     rule.eventListSize = 1;
775     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
776         "value":"SysEventStore"}]}})~";
777     (void)StringUtil::CreateCString(&rule.condition, cond);
778     HiSysEventQueryRule rules[] = { rule };
779     HiSysEventQueryCallback callback;
780     InitCallback(callback);
781     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
782     ASSERT_EQ(res, ERR_QUERY_RULE_INVALID);
783     StringUtil::DeletePointer<char>(&rule.condition);
784     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest015 end");
785 }
786 
787 /**
788  * @tc.name: HiSysEventMgrCQueryTest016
789  * @tc.desc: Testing to query events only with condition.
790  * @tc.type: FUNC
791  * @tc.require: issueI5X08B
792  */
793 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest016, TestSize.Level3)
794 {
795     /**
796      * @tc.steps: step1. create HiSysEventQueryArg.
797      * @tc.steps: step2. create HiSysEventQueryRule.
798      * @tc.steps: step3. create HiSysEventQueryCallback.
799      * @tc.steps: step4. query event.
800      */
801     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest016 start");
802     sleep(QUERY_INTERVAL_TIME);
803     HiSysEventQueryArg arg;
804     InitQueryArg(arg);
805     HiSysEventQueryRule rule;
806     rule.eventListSize = 0;
807     std::string cond = R"~({"version":"V1","condition":{"and":[{"param":"NAME","op":"=",
808         "value":"SysEventStore"}]}})~";
809     (void)StringUtil::CreateCString(&rule.condition, cond);
810     HiSysEventQueryRule rules[] = { rule };
811     HiSysEventQueryCallback callback;
812     InitCallback(callback);
813     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
814     ASSERT_EQ(res, ERR_QUERY_RULE_INVALID);
815     StringUtil::DeletePointer<char>(&rule.condition);
816     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest016 end");
817 }
818 
819 /**
820  * @tc.name: HiSysEventMgrCQueryTest017
821  * @tc.desc: Testing to query events are too frequent.
822  * @tc.type: FUNC
823  * @tc.require: issueI5X08B
824  */
825 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest017, TestSize.Level3)
826 {
827     /**
828      * @tc.steps: step1. create HiSysEventQueryArg.
829      * @tc.steps: step2. create HiSysEventQueryRule.
830      * @tc.steps: step3. create HiSysEventQueryCallback.
831      * @tc.steps: step4. query event.
832      */
833     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest017 start");
834     sleep(QUERY_INTERVAL_TIME);
835     HiSysEventQueryArg arg;
836     InitQueryArg(arg);
837 
838     HiSysEventQueryRule rule;
839     InitQueryRule(rule);
840     HiSysEventQueryRule rules[] = { rule };
841 
842     HiSysEventQueryCallback callback;
843     InitCallback(callback);
844 
845     const int threshhold = 50;
846     const int delayDuration = 1; // 1 second
847     for (int i = 0; i < 2; i++) { // 2 cycles
848         sleep(delayDuration);
849         for (int j = 0; j <= threshhold; j++) { // more than 50 queries in 1 second is never allowed
850             auto ret = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
851             ASSERT_TRUE((ret == ERR_QUERY_TOO_FREQUENTLY) || (ret == IPC_CALL_SUCCEED));
852         }
853     }
854 
855     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest017 end");
856 }
857 
858 /**
859  * @tc.name: HiSysEventMgrCQueryTest018
860  * @tc.desc: Testing to query events with too many rules.
861  * @tc.type: FUNC
862  * @tc.require: issueI5X08B
863  */
864 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest018, TestSize.Level3)
865 {
866     /**
867      * @tc.steps: step1. create HiSysEventQueryArg.
868      * @tc.steps: step2. create HiSysEventQueryRule.
869      * @tc.steps: step3. create HiSysEventQueryCallback.
870      * @tc.steps: step4. query event.
871      */
872     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest018 start");
873     sleep(QUERY_INTERVAL_TIME);
874     HiSysEventQueryArg arg;
875     InitQueryArg(arg);
876 
877     HiSysEventQueryRule rule;
878     InitQueryRule(rule);
879     const int invalidRuleCnt = 101; // maximum count for query rule is 100.
880     HiSysEventQueryRule rules[invalidRuleCnt];
881     for (int i = 0; i < invalidRuleCnt; i++) {
882         rules[i] = rule;
883     }
884 
885     HiSysEventQueryCallback callback;
886     InitCallback(callback);
887 
888     auto res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
889     ASSERT_EQ(res, ERR_TOO_MANY_QUERY_RULES);
890 
891     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest018 end");
892 }
893 
894 /**
895  * @tc.name: HiSysEventMgrCQueryTest019
896  * @tc.desc: Testing to query events with null param.
897  * @tc.type: FUNC
898  * @tc.require: issueI7O8IM
899  */
900 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest019, TestSize.Level0)
901 {
902     /**
903      * @tc.steps: step1. create HiSysEventQueryArg.
904      * @tc.steps: step2. create HiSysEventQueryRule.
905      * @tc.steps: step3. create HiSysEventQueryCallback.
906      * @tc.steps: step4. query event.
907      */
908     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest019 start");
909     HiSysEventQueryRule rules[] = {};
910     HiSysEventQueryCallback callback;
911     InitCallback(callback);
912     auto res = OH_HiSysEvent_Query(nullptr, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
913     ASSERT_EQ(res, ERR_QUERY_ARG_NULL);
914 
915     HiSysEventQueryArg arg;
916     InitQueryArg(arg);
917     res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), nullptr);
918     ASSERT_EQ(res, ERR_QUERY_CALLBACK_NULL);
919 
920     callback.OnQuery = nullptr;
921     res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
922     ASSERT_EQ(res, ERR_QUERY_CALLBACK_NULL);
923 
924     InitCallback(callback);
925     callback.OnComplete = nullptr;
926     res = OH_HiSysEvent_Query(&arg, rules, sizeof(rules) / sizeof(HiSysEventQueryRule), &callback);
927     ASSERT_EQ(res, ERR_QUERY_CALLBACK_NULL);
928 
929     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest019 end");
930 }
931 
932 /**
933  * @tc.name: HiSysEventMgrCQueryTest020
934  * @tc.desc: Testing to query events with invalid condition.
935  * @tc.type: FUNC
936  * @tc.require: issueIAXEER
937  */
938 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCQueryTest020, TestSize.Level3)
939 {
940     /**
941      * @tc.steps: step1. create HiSysEventQueryArg.
942      * @tc.steps: step2. create HiSysEventQueryRule.
943      * @tc.steps: step3. create HiSysEventQueryCallback.
944      * @tc.steps: step4. query event.
945      */
946     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest020 start");
947     std::string rootIsArrayCond = R"~([])~";
948     QueryTestWithCondition(rootIsArrayCond);
949     std::string condIsArrayCond = R"~({"version":"V1","condition":[]})~";
950     QueryTestWithCondition(condIsArrayCond);
951     HILOG_INFO(LOG_CORE, "HiSysEventMgrCQueryTest020 end");
952 }
953 
954 /**
955  * @tc.name: HiSysEventMgrCRecordTest001
956  * @tc.desc: Testing to get the record information.
957  * @tc.type: FUNC
958  * @tc.require: issueI5X08B
959  */
960 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCRecordTest001, TestSize.Level3)
961 {
962     /**
963      * @tc.steps: step1. build record.
964      * @tc.steps: step2. check the information from record.
965      */
966     HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest001 start");
967     const std::map<std::string, std::string> recordData = {
968         {"\"domain_\"", "\"TEST_DOMAIN\""},
969         {"\"name_\"", "\"TEST_NAME\""},
970         {"\"type_\"", "4"},
971         {"\"PARAM_INT\"", "-123"},
972         {"\"PARAM_INTS\"", "[-1,-2,3]"},
973         {"\"PARAM_UINT\"", "123"},
974         {"\"PARAM_UINTS\"", "[1,2,3]"},
975         {"\"PARAM_DOU\"", "123.456"},
976         {"\"PARAM_DOUS\"", "[1.1,-2.2,3.3]"},
977         {"\"PARAM_STR\"", "\"test\""},
978         {"\"PARAM_STRS\"", "[\"test1\",\"test2\",\"test3\"]"}
979     };
980     HiSysEventRecord record;
981     auto res = StringUtil::CreateCString(&record.jsonStr, BuildRecordString(recordData));
982     if (res != 0) {
983         HILOG_WARN(LOG_CORE, "failed to create record string");
984         ASSERT_TRUE(false);
985     }
986 
987     RecordParamNameTest(record, recordData);
988     RecordParamIntValueTest(record, "PARAM_INT", -123);
989     RecordParamUintValueTest(record, "PARAM_UINT", 123);
990     RecordParamDouValueTest(record, "PARAM_DOU", 123.456);
991     RecordParamStrValueTest(record, "PARAM_STR", "test");
992     RecordParamIntValuesTest(record, "PARAM_INTS", {-1, -2, 3});
993     RecordParamUintValuesTest(record, "PARAM_UINTS", {1, 2, 3});
994     RecordParamDouValuesTest(record, "PARAM_DOUS", {1.1, -2.2, 3.3});
995     RecordParamStrValuesTest(record, "PARAM_STRS", {"test1", "test2", "test3"});
996 
997     int expRes = -3;
998     RecordParamIntValueInvalidTest(record, "PARAM_STR", expRes);
999     RecordParamUintValueInvalidTest(record, "PARAM_STR", expRes);
1000     RecordParamDouValueInvalidTest(record, "PARAM_STR", expRes);
1001     RecordParamIntValuesInvalidTest(record, "PARAM_STRS", expRes);
1002     RecordParamUintValuesInvalidTest(record, "PARAM_STRS", expRes);
1003     RecordParamDouValuesInvalidTest(record, "PARAM_STRS", expRes);
1004 
1005     // number is automatically converted to string
1006     RecordParamStrValueTest(record, "PARAM_INT", "-123");
1007     RecordParamStrValuesTest(record, "PARAM_INTS", {"-1", "-2", "3"});
1008 
1009     StringUtil::DeletePointer<char>(&record.jsonStr);
1010     HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest001 end");
1011 }
1012 
1013 /**
1014  * @tc.name: HiSysEventMgrCRecordTest002
1015  * @tc.desc: Testing to get the record information.
1016  * @tc.type: FUNC
1017  * @tc.require: issueI5X08B
1018  */
1019 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCRecordTest002, TestSize.Level3)
1020 {
1021     /**
1022      * @tc.steps: step1. build record.
1023      * @tc.steps: step2. check the information from record.
1024      */
1025     HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest002 start");
1026     HiSysEventRecord record;
1027     auto res = StringUtil::CreateCString(&record.jsonStr, "invalid record");
1028     if (res != 0) {
1029         HILOG_WARN(LOG_CORE, "failed to create record string");
1030         ASSERT_TRUE(false);
1031     }
1032 
1033     int expRes = -1;
1034     RecordParamNameInvalidTest(record);
1035     RecordParamIntValueInvalidTest(record, "PARAM_INT", expRes);
1036     RecordParamUintValueInvalidTest(record, "PARAM_UINT", expRes);
1037     RecordParamDouValueInvalidTest(record, "PARAM_DOU", expRes);
1038     RecordParamStrValueInvalidTest(record, "PARAM_STR", expRes);
1039     RecordParamIntValuesInvalidTest(record, "PARAM_INTS", expRes);
1040     RecordParamUintValuesInvalidTest(record, "PARAM_UINTS", expRes);
1041     RecordParamDouValuesInvalidTest(record, "PARAM_DOUS", expRes);
1042     RecordParamStrValuesInvalidTest(record, "PARAM_STRS", expRes);
1043 
1044     StringUtil::DeletePointer<char>(&record.jsonStr);
1045     HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest002 end");
1046 }
1047 
1048 /**
1049  * @tc.name: HiSysEventMgrCRecordTest003
1050  * @tc.desc: Testing to get the record information.
1051  * @tc.type: FUNC
1052  * @tc.require: issueI5X08B
1053  */
1054 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCRecordTest003, TestSize.Level3)
1055 {
1056     /**
1057      * @tc.steps: step1. build record.
1058      * @tc.steps: step2. check the information from record.
1059      */
1060     HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest003 start");
1061     HiSysEventRecord record;
1062     auto res = StringUtil::CreateCString(&record.jsonStr, R"~({})~");
1063     if (res != 0) {
1064         HILOG_WARN(LOG_CORE, "failed to create record string");
1065         ASSERT_TRUE(false);
1066     }
1067 
1068     int expRes = -2;
1069     RecordParamNameTest(record, {});
1070     RecordParamIntValueInvalidTest(record, "PARAM_INT", expRes);
1071     RecordParamUintValueInvalidTest(record, "PARAM_UINT", expRes);
1072     RecordParamDouValueInvalidTest(record, "PARAM_DOU", expRes);
1073     RecordParamStrValueInvalidTest(record, "PARAM_STR", expRes);
1074     RecordParamIntValuesInvalidTest(record, "PARAM_INTS", expRes);
1075     RecordParamUintValuesInvalidTest(record, "PARAM_UINTS", expRes);
1076     RecordParamDouValuesInvalidTest(record, "PARAM_DOUS", expRes);
1077     RecordParamStrValuesInvalidTest(record, "PARAM_STRS", expRes);
1078 
1079     StringUtil::DeletePointer<char>(&record.jsonStr);
1080     HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest003 end");
1081 }
1082 
1083 /**
1084  * @tc.name: HiSysEventMgrCRecordTest004
1085  * @tc.desc: Test apis of HisysventRecordC
1086  * @tc.type: FUNC
1087  * @tc.require: issueI62WJT
1088  */
1089 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCRecordTest004, TestSize.Level3)
1090 {
1091     struct HiSysEventRecord record;
1092     char*** testp = nullptr;
1093     size_t len = 0;
1094     OH_HiSysEvent_GetParamNames(&record, testp, &len);
1095     ASSERT_TRUE(true);
1096     int64_t value1;
1097     auto ret = OH_HiSysEvent_GetParamInt64Value(&record, "KEY", &value1);
1098     ASSERT_EQ(ret, ERR_NULL);
1099     ret = OH_HiSysEvent_GetParamInt64Value(&record, nullptr, &value1);
1100     ASSERT_EQ(ret, ERR_NULL);
1101     uint64_t value2;
1102     ret = OH_HiSysEvent_GetParamUint64Value(&record, "KEY", &value2);
1103     ASSERT_EQ(ret, ERR_NULL);
1104     ret = OH_HiSysEvent_GetParamUint64Value(&record, nullptr, &value2);
1105     ASSERT_EQ(ret, ERR_NULL);
1106     double value3;
1107     ret = OH_HiSysEvent_GetParamDoubleValue(&record, "KEY", &value3);
1108     ASSERT_EQ(ret, ERR_NULL);
1109     ret = OH_HiSysEvent_GetParamDoubleValue(&record, nullptr, &value3);
1110     ASSERT_EQ(ret, ERR_NULL);
1111     char value4[100];
1112     char* value4p = value4;
1113     char** value4pp = &value4p;
1114     ret = OH_HiSysEvent_GetParamStringValue(&record, "KEY", value4pp);
1115     ASSERT_EQ(ret, ERR_NULL);
1116     ret = OH_HiSysEvent_GetParamStringValue(&record, nullptr, value4pp);
1117     ASSERT_EQ(ret, ERR_NULL);
1118     size_t dataLen;
1119     int64_t value5[10];
1120     int64_t* value5p = value5;
1121     int64_t** value5pp = &value5p;
1122     ret = OH_HiSysEvent_GetParamInt64Values(&record, "KEY", value5pp, &dataLen);
1123     ASSERT_EQ(ret, ERR_NULL);
1124     ret = OH_HiSysEvent_GetParamInt64Values(&record, nullptr, value5pp, &dataLen);
1125     ASSERT_EQ(ret, ERR_NULL);
1126     uint64_t value6[10];
1127     uint64_t* value6p = value6;
1128     uint64_t** value6pp = &value6p;
1129     ret = OH_HiSysEvent_GetParamUint64Values(&record, "KEY", value6pp, &dataLen);
1130     ASSERT_EQ(ret, ERR_NULL);
1131     ret = OH_HiSysEvent_GetParamUint64Values(&record, nullptr, value6pp, &dataLen);
1132     ASSERT_EQ(ret, ERR_NULL);
1133     double value7[10];
1134     double* value7p = value7;
1135     double** value7pp = &value7p;
1136     ret = OH_HiSysEvent_GetParamDoubleValues(&record, "KEY", value7pp, &dataLen);
1137     ASSERT_EQ(ret, ERR_NULL);
1138     ret = OH_HiSysEvent_GetParamDoubleValues(&record, nullptr, value7pp, &dataLen);
1139     ASSERT_EQ(ret, ERR_NULL);
1140     char v3[10][100] {};
1141     char* dest3p = v3[0];
1142     char** dest3pp = &dest3p;
1143     char*** dest3ppp = &dest3pp;
1144     ret = OH_HiSysEvent_GetParamStringValues(&record, "KEY", dest3ppp, &dataLen);
1145     ASSERT_EQ(ret, ERR_NULL);
1146     ret = OH_HiSysEvent_GetParamStringValues(&record, nullptr, dest3ppp, &dataLen);
1147     ASSERT_EQ(ret, ERR_NULL);
1148 }
1149 
1150 /**
1151  * @tc.name: HiSysEventMgrCWatchTest001
1152  * @tc.desc: Testing to watch events with null param.
1153  * @tc.type: FUNC
1154  * @tc.require: issueI7O8IM
1155  */
1156 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCWatcherTest001, TestSize.Level0)
1157 {
1158     /**
1159      * @tc.steps: step1. create HiSysEventWatcher object.
1160      * @tc.steps: step2. create HiSysEventWatchRule objects.
1161      * @tc.steps: step3. watch event.
1162      */
1163     // watcher is null
1164     HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatcherTest001 start");
1165     auto ret = OH_HiSysEvent_Add_Watcher(nullptr, nullptr, 0);
1166     ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1167 
1168     // watcher.OnEvent is null
1169     HiSysEventWatcher nullWatcher;
1170     InitWatcher(nullWatcher);
1171     nullWatcher.OnEvent = nullptr;
1172     HiSysEventWatchRule rule = {"HIVIEWDFX", "PLUGIN_LOAD", "", 1, 0};
1173     HiSysEventWatchRule rules[] = {rule};
1174     ret = OH_HiSysEvent_Add_Watcher(&nullWatcher, rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1175     ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1176 
1177     // watcher.OnServiceDied is null
1178     InitWatcher(nullWatcher);
1179     nullWatcher.OnServiceDied = nullptr;
1180     ret = OH_HiSysEvent_Add_Watcher(&nullWatcher, rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1181     ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1182 
1183     // watcher does not exist
1184     HiSysEventWatcher watcher;
1185     InitWatcher(watcher);
1186     ret = OH_HiSysEvent_Remove_Watcher(&watcher);
1187     ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1188 
1189     // normal function test
1190     ret = OH_HiSysEvent_Add_Watcher(&watcher, rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1191     ASSERT_EQ(ret, 0);
1192     ret = OH_HiSysEvent_Write("HIVIEWDFX", "PLUGIN_LOAD", HISYSEVENT_BEHAVIOR, nullptr, 0);
1193     ASSERT_EQ(ret, 0);
1194     sleep(3);
1195     ret = OH_HiSysEvent_Remove_Watcher(&watcher);
1196     ASSERT_EQ(ret, 0);
1197 
1198     HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatcherTest001 end");
1199 }
1200 
1201 /**
1202  * @tc.name: HiSysEventMgrCWatchTest002
1203  * @tc.desc: Testing to watch events with too many rules.
1204  * @tc.type: FUNC
1205  * @tc.require: issueI7O8IM
1206  */
1207 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCWatchTest002, TestSize.Level0)
1208 {
1209     /**
1210      * @tc.steps: step1. create HiSysEventWatcher object.
1211      * @tc.steps: step2. create HiSysEventWatchRule objects.
1212      * @tc.steps: step3. watch event.
1213      */
1214     HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatchTest002 start");
1215     HiSysEventWatcher watcher;
1216     InitWatcher(watcher);
1217     HiSysEventWatchRule rule = {"HIVIEWDFX", "PLUGIN_LOAD", "", 1, 0};
1218     const size_t maxNum = 20;
1219     HiSysEventWatchRule rules[maxNum + 1];
1220     for (size_t i = 0; i <= maxNum; i++) {
1221         rules[i] = rule;
1222     }
1223     auto ret = OH_HiSysEvent_Add_Watcher(&watcher, rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1224     ASSERT_EQ(ret, ERR_TOO_MANY_WATCH_RULES);
1225     HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatchTest002 end");
1226 }
1227 
1228 /**
1229  * @tc.name: HiSysEventMgrCWatchTest003
1230  * @tc.desc: Testing to watch events with too many watchers.
1231  * @tc.type: FUNC
1232  * @tc.require: issueI7O8IM
1233  */
1234 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCWatchTest003, TestSize.Level0)
1235 {
1236     /**
1237      * @tc.steps: step1. create HiSysEventWatcher object.
1238      * @tc.steps: step2. create HiSysEventWatchRule objects.
1239      * @tc.steps: step3. watch event.
1240      */
1241     HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatchTest003 start");
1242     const size_t maxNum = 30;
1243     HiSysEventWatcher watchers[maxNum + 1];
1244     for (size_t i = 0; i <= maxNum; i++) {
1245         HiSysEventWatcher watcher;
1246         InitWatcher(watcher);
1247         watchers[i] = watcher;
1248     }
1249     HiSysEventWatchRule rule = {"HIVIEWDFX", "PLUGIN_LOAD", "", 1, 0};
1250     HiSysEventWatchRule rules[] = {rule};
1251     for (size_t i = 0; i < maxNum; i++) {
1252         (void)OH_HiSysEvent_Add_Watcher(&watchers[i], rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1253     }
1254     auto ret = OH_HiSysEvent_Add_Watcher(&watchers[maxNum], rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1255     ASSERT_EQ(ret, ERR_TOO_MANY_WATCHERS);
1256 
1257     for (size_t i = 0; i <= maxNum; i++) {
1258         (void)OH_HiSysEvent_Remove_Watcher(&watchers[i]);
1259     }
1260     HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatchTest003 end");
1261 }
1262