1 /*
2 * Copyright (c) 2022 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_TRUE(strlen(record.eventName) > 0);
95 ASSERT_TRUE(strlen(record.tz) > 0);
96 ASSERT_TRUE(record.type > 0);
97 ASSERT_TRUE(record.time > 0);
98 ASSERT_TRUE(record.pid >= 0);
99 ASSERT_TRUE(record.tid >= 0);
100 ASSERT_TRUE(record.uid >= 0);
101 ASSERT_TRUE(record.traceId >= 0);
102 ASSERT_TRUE(record.spandId >= 0);
103 ASSERT_TRUE(record.pspanId >= 0);
104 ASSERT_TRUE(strlen(record.level) > 0);
105 if (record.tag != nullptr) {
106 ASSERT_TRUE(strlen(record.tag) > 0);
107 }
108 ASSERT_TRUE(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_TRUE(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, ¶ms, &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_TRUE(recordData.find("\"" + std::string(params[i]) + "\"") != recordData.end());
182 }
183 StringUtil::DeletePointers<char>(¶ms, 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, ¶ms, &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: HiSysEventMgrCRecordTest001
934 * @tc.desc: Testing to get the record information.
935 * @tc.type: FUNC
936 * @tc.require: issueI5X08B
937 */
938 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCRecordTest001, TestSize.Level3)
939 {
940 /**
941 * @tc.steps: step1. build record.
942 * @tc.steps: step2. check the information from record.
943 */
944 HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest001 start");
945 const std::map<std::string, std::string> recordData = {
946 {"\"domain_\"", "\"TEST_DOMAIN\""},
947 {"\"name_\"", "\"TEST_NAME\""},
948 {"\"type_\"", "4"},
949 {"\"PARAM_INT\"", "-123"},
950 {"\"PARAM_INTS\"", "[-1,-2,3]"},
951 {"\"PARAM_UINT\"", "123"},
952 {"\"PARAM_UINTS\"", "[1,2,3]"},
953 {"\"PARAM_DOU\"", "123.456"},
954 {"\"PARAM_DOUS\"", "[1.1,-2.2,3.3]"},
955 {"\"PARAM_STR\"", "\"test\""},
956 {"\"PARAM_STRS\"", "[\"test1\",\"test2\",\"test3\"]"}
957 };
958 HiSysEventRecord record;
959 auto res = StringUtil::CreateCString(&record.jsonStr, BuildRecordString(recordData));
960 if (res != 0) {
961 HILOG_WARN(LOG_CORE, "failed to create record string");
962 return;
963 }
964
965 RecordParamNameTest(record, recordData);
966 RecordParamIntValueTest(record, "PARAM_INT", -123);
967 RecordParamUintValueTest(record, "PARAM_UINT", 123);
968 RecordParamDouValueTest(record, "PARAM_DOU", 123.456);
969 RecordParamStrValueTest(record, "PARAM_STR", "test");
970 RecordParamIntValuesTest(record, "PARAM_INTS", {-1, -2, 3});
971 RecordParamUintValuesTest(record, "PARAM_UINTS", {1, 2, 3});
972 RecordParamDouValuesTest(record, "PARAM_DOUS", {1.1, -2.2, 3.3});
973 RecordParamStrValuesTest(record, "PARAM_STRS", {"test1", "test2", "test3"});
974
975 int expRes = -3;
976 RecordParamIntValueInvalidTest(record, "PARAM_STR", expRes);
977 RecordParamUintValueInvalidTest(record, "PARAM_STR", expRes);
978 RecordParamDouValueInvalidTest(record, "PARAM_STR", expRes);
979 RecordParamIntValuesInvalidTest(record, "PARAM_STRS", expRes);
980 RecordParamUintValuesInvalidTest(record, "PARAM_STRS", expRes);
981 RecordParamDouValuesInvalidTest(record, "PARAM_STRS", expRes);
982
983 // number is automatically converted to string
984 RecordParamStrValueTest(record, "PARAM_INT", "-123");
985 RecordParamStrValuesTest(record, "PARAM_INTS", {"-1", "-2", "3"});
986
987 StringUtil::DeletePointer<char>(&record.jsonStr);
988 HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest001 end");
989 }
990
991 /**
992 * @tc.name: HiSysEventMgrCRecordTest002
993 * @tc.desc: Testing to get the record information.
994 * @tc.type: FUNC
995 * @tc.require: issueI5X08B
996 */
997 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCRecordTest002, TestSize.Level3)
998 {
999 /**
1000 * @tc.steps: step1. build record.
1001 * @tc.steps: step2. check the information from record.
1002 */
1003 HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest002 start");
1004 HiSysEventRecord record;
1005 auto res = StringUtil::CreateCString(&record.jsonStr, "invalid record");
1006 if (res != 0) {
1007 HILOG_WARN(LOG_CORE, "failed to create record string");
1008 return;
1009 }
1010
1011 int expRes = -1;
1012 RecordParamNameInvalidTest(record);
1013 RecordParamIntValueInvalidTest(record, "PARAM_INT", expRes);
1014 RecordParamUintValueInvalidTest(record, "PARAM_UINT", expRes);
1015 RecordParamDouValueInvalidTest(record, "PARAM_DOU", expRes);
1016 RecordParamStrValueInvalidTest(record, "PARAM_STR", expRes);
1017 RecordParamIntValuesInvalidTest(record, "PARAM_INTS", expRes);
1018 RecordParamUintValuesInvalidTest(record, "PARAM_UINTS", expRes);
1019 RecordParamDouValuesInvalidTest(record, "PARAM_DOUS", expRes);
1020 RecordParamStrValuesInvalidTest(record, "PARAM_STRS", expRes);
1021
1022 StringUtil::DeletePointer<char>(&record.jsonStr);
1023 HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest002 end");
1024 }
1025
1026 /**
1027 * @tc.name: HiSysEventMgrCRecordTest003
1028 * @tc.desc: Testing to get the record information.
1029 * @tc.type: FUNC
1030 * @tc.require: issueI5X08B
1031 */
1032 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCRecordTest003, TestSize.Level3)
1033 {
1034 /**
1035 * @tc.steps: step1. build record.
1036 * @tc.steps: step2. check the information from record.
1037 */
1038 HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest003 start");
1039 HiSysEventRecord record;
1040 auto res = StringUtil::CreateCString(&record.jsonStr, R"~({})~");
1041 if (res != 0) {
1042 HILOG_WARN(LOG_CORE, "failed to create record string");
1043 return;
1044 }
1045
1046 int expRes = -2;
1047 RecordParamNameTest(record, {});
1048 RecordParamIntValueInvalidTest(record, "PARAM_INT", expRes);
1049 RecordParamUintValueInvalidTest(record, "PARAM_UINT", expRes);
1050 RecordParamDouValueInvalidTest(record, "PARAM_DOU", expRes);
1051 RecordParamStrValueInvalidTest(record, "PARAM_STR", expRes);
1052 RecordParamIntValuesInvalidTest(record, "PARAM_INTS", expRes);
1053 RecordParamUintValuesInvalidTest(record, "PARAM_UINTS", expRes);
1054 RecordParamDouValuesInvalidTest(record, "PARAM_DOUS", expRes);
1055 RecordParamStrValuesInvalidTest(record, "PARAM_STRS", expRes);
1056
1057 StringUtil::DeletePointer<char>(&record.jsonStr);
1058 HILOG_INFO(LOG_CORE, "HiSysEventMgrCRecordTest003 end");
1059 }
1060
1061 /**
1062 * @tc.name: HiSysEventMgrCRecordTest004
1063 * @tc.desc: Test apis of HisysventRecordC
1064 * @tc.type: FUNC
1065 * @tc.require: issueI62WJT
1066 */
1067 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCRecordTest004, TestSize.Level3)
1068 {
1069 struct HiSysEventRecord record;
1070 char*** testp = nullptr;
1071 size_t len = 0;
1072 OH_HiSysEvent_GetParamNames(&record, testp, &len);
1073 ASSERT_TRUE(true);
1074 int64_t value1;
1075 auto ret = OH_HiSysEvent_GetParamInt64Value(&record, "KEY", &value1);
1076 ASSERT_TRUE(ret == ERR_NULL);
1077 ret = OH_HiSysEvent_GetParamInt64Value(&record, nullptr, &value1);
1078 ASSERT_TRUE(ret == ERR_NULL);
1079 uint64_t value2;
1080 ret = OH_HiSysEvent_GetParamUint64Value(&record, "KEY", &value2);
1081 ASSERT_TRUE(ret == ERR_NULL);
1082 ret = OH_HiSysEvent_GetParamUint64Value(&record, nullptr, &value2);
1083 ASSERT_TRUE(ret == ERR_NULL);
1084 double value3;
1085 ret = OH_HiSysEvent_GetParamDoubleValue(&record, "KEY", &value3);
1086 ASSERT_TRUE(ret == ERR_NULL);
1087 ret = OH_HiSysEvent_GetParamDoubleValue(&record, nullptr, &value3);
1088 ASSERT_TRUE(ret == ERR_NULL);
1089 char value4[100];
1090 char* value4p = value4;
1091 char** value4pp = &value4p;
1092 ret = OH_HiSysEvent_GetParamStringValue(&record, "KEY", value4pp);
1093 ASSERT_TRUE(ret == ERR_NULL);
1094 ret = OH_HiSysEvent_GetParamStringValue(&record, nullptr, value4pp);
1095 ASSERT_TRUE(ret == ERR_NULL);
1096 size_t dataLen;
1097 int64_t value5[10];
1098 int64_t* value5p = value5;
1099 int64_t** value5pp = &value5p;
1100 ret = OH_HiSysEvent_GetParamInt64Values(&record, "KEY", value5pp, &dataLen);
1101 ASSERT_TRUE(ret == ERR_NULL);
1102 ret = OH_HiSysEvent_GetParamInt64Values(&record, nullptr, value5pp, &dataLen);
1103 ASSERT_TRUE(ret == ERR_NULL);
1104 uint64_t value6[10];
1105 uint64_t* value6p = value6;
1106 uint64_t** value6pp = &value6p;
1107 ret = OH_HiSysEvent_GetParamUint64Values(&record, "KEY", value6pp, &dataLen);
1108 ASSERT_TRUE(ret == ERR_NULL);
1109 ret = OH_HiSysEvent_GetParamUint64Values(&record, nullptr, value6pp, &dataLen);
1110 ASSERT_TRUE(ret == ERR_NULL);
1111 double value7[10];
1112 double* value7p = value7;
1113 double** value7pp = &value7p;
1114 ret = OH_HiSysEvent_GetParamDoubleValues(&record, "KEY", value7pp, &dataLen);
1115 ASSERT_TRUE(ret == ERR_NULL);
1116 ret = OH_HiSysEvent_GetParamDoubleValues(&record, nullptr, value7pp, &dataLen);
1117 ASSERT_TRUE(ret == ERR_NULL);
1118 char v3[10][100] {};
1119 char* dest3p = v3[0];
1120 char** dest3pp = &dest3p;
1121 char*** dest3ppp = &dest3pp;
1122 ret = OH_HiSysEvent_GetParamStringValues(&record, "KEY", dest3ppp, &dataLen);
1123 ASSERT_TRUE(ret == ERR_NULL);
1124 ret = OH_HiSysEvent_GetParamStringValues(&record, nullptr, dest3ppp, &dataLen);
1125 ASSERT_TRUE(ret == ERR_NULL);
1126 }
1127
1128 /**
1129 * @tc.name: HiSysEventMgrCWatchTest001
1130 * @tc.desc: Testing to watch events with null param.
1131 * @tc.type: FUNC
1132 * @tc.require: issueI7O8IM
1133 */
1134 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCWatcherTest001, TestSize.Level0)
1135 {
1136 /**
1137 * @tc.steps: step1. create HiSysEventWatcher object.
1138 * @tc.steps: step2. create HiSysEventWatchRule objects.
1139 * @tc.steps: step3. watch event.
1140 */
1141 // watcher is null
1142 HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatcherTest001 start");
1143 auto ret = OH_HiSysEvent_Add_Watcher(nullptr, nullptr, 0);
1144 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1145
1146 // watcher.OnEvent is null
1147 HiSysEventWatcher nullWatcher;
1148 InitWatcher(nullWatcher);
1149 nullWatcher.OnEvent = nullptr;
1150 HiSysEventWatchRule rule = {"HIVIEWDFX", "PLUGIN_LOAD", "", 1, 0};
1151 HiSysEventWatchRule rules[] = {rule};
1152 ret = OH_HiSysEvent_Add_Watcher(&nullWatcher, rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1153 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1154
1155 // watcher.OnServiceDied is null
1156 InitWatcher(nullWatcher);
1157 nullWatcher.OnServiceDied = nullptr;
1158 ret = OH_HiSysEvent_Add_Watcher(&nullWatcher, rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1159 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1160
1161 // watcher does not exist
1162 HiSysEventWatcher watcher;
1163 InitWatcher(watcher);
1164 ret = OH_HiSysEvent_Remove_Watcher(&watcher);
1165 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1166
1167 // normal function test
1168 ret = OH_HiSysEvent_Add_Watcher(&watcher, rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1169 ASSERT_EQ(ret, 0);
1170 ret = OH_HiSysEvent_Write("HIVIEWDFX", "PLUGIN_LOAD", HISYSEVENT_BEHAVIOR, nullptr, 0);
1171 ASSERT_EQ(ret, 0);
1172 sleep(3);
1173 ret = OH_HiSysEvent_Remove_Watcher(&watcher);
1174 ASSERT_EQ(ret, 0);
1175
1176 HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatcherTest001 end");
1177 }
1178
1179 /**
1180 * @tc.name: HiSysEventMgrCWatchTest002
1181 * @tc.desc: Testing to watch events with too many rules.
1182 * @tc.type: FUNC
1183 * @tc.require: issueI7O8IM
1184 */
1185 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCWatchTest002, TestSize.Level0)
1186 {
1187 /**
1188 * @tc.steps: step1. create HiSysEventWatcher object.
1189 * @tc.steps: step2. create HiSysEventWatchRule objects.
1190 * @tc.steps: step3. watch event.
1191 */
1192 HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatchTest002 start");
1193 HiSysEventWatcher watcher;
1194 InitWatcher(watcher);
1195 HiSysEventWatchRule rule = {"HIVIEWDFX", "PLUGIN_LOAD", "", 1, 0};
1196 const size_t maxNum = 20;
1197 HiSysEventWatchRule rules[maxNum + 1];
1198 for (size_t i = 0; i <= maxNum; i++) {
1199 rules[i] = rule;
1200 }
1201 auto ret = OH_HiSysEvent_Add_Watcher(&watcher, rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1202 ASSERT_EQ(ret, ERR_TOO_MANY_WATCH_RULES);
1203 HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatchTest002 end");
1204 }
1205
1206 /**
1207 * @tc.name: HiSysEventMgrCWatchTest003
1208 * @tc.desc: Testing to watch events with too many watchers.
1209 * @tc.type: FUNC
1210 * @tc.require: issueI7O8IM
1211 */
1212 HWTEST_F(HiSysEventManagerCTest, HiSysEventMgrCWatchTest003, TestSize.Level0)
1213 {
1214 /**
1215 * @tc.steps: step1. create HiSysEventWatcher object.
1216 * @tc.steps: step2. create HiSysEventWatchRule objects.
1217 * @tc.steps: step3. watch event.
1218 */
1219 HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatchTest003 start");
1220 const size_t maxNum = 30;
1221 HiSysEventWatcher watchers[maxNum + 1];
1222 for (size_t i = 0; i <= maxNum; i++) {
1223 HiSysEventWatcher watcher;
1224 InitWatcher(watcher);
1225 watchers[i] = watcher;
1226 }
1227 HiSysEventWatchRule rule = {"HIVIEWDFX", "PLUGIN_LOAD", "", 1, 0};
1228 HiSysEventWatchRule rules[] = {rule};
1229 for (size_t i = 0; i < maxNum; i++) {
1230 (void)OH_HiSysEvent_Add_Watcher(&watchers[i], rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1231 }
1232 auto ret = OH_HiSysEvent_Add_Watcher(&watchers[maxNum], rules, sizeof(rules) / sizeof(HiSysEventWatchRule));
1233 ASSERT_EQ(ret, ERR_TOO_MANY_WATCHERS);
1234
1235 for (size_t i = 0; i <= maxNum; i++) {
1236 (void)OH_HiSysEvent_Remove_Watcher(&watchers[i]);
1237 }
1238 HILOG_INFO(LOG_CORE, "HiSysEventMgrCWatchTest003 end");
1239 }
1240