1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hisysevent_native_test.h"
17
18 #include <functional>
19 #include <iosfwd>
20 #include <string>
21 #include <thread>
22 #include <unistd.h>
23 #include <vector>
24
25 #include "gtest/gtest-message.h"
26 #include "gtest/gtest-test-part.h"
27 #include "gtest/hwext/gtest-ext.h"
28 #include "gtest/hwext/gtest-tag.h"
29 #include "hilog/log.h"
30
31 #include "def.h"
32 #include "hisysevent.h"
33 #include "hisysevent_base_manager.h"
34 #include "hisysevent_manager.h"
35 #include "hisysevent_record.h"
36 #include "hisysevent_query_callback.h"
37 #include "hisysevent_listener.h"
38 #include "ret_code.h"
39 #include "rule_type.h"
40
41 #ifndef SYS_EVENT_PARAMS
42 #define SYS_EVENT_PARAMS(A) "key"#A, 0 + (A), "keyA"#A, 1 + (A), "keyB"#A, 2 + (A), "keyC"#A, 3 + (A), \
43 "keyD"#A, 4 + (A), "keyE"#A, 5 + (A), "keyF"#A, 6 + (A), "keyG"#A, 7 + (A), "keyH"#A, 8 + (A), \
44 "keyI"#A, 9 + (A)
45 #endif
46 using namespace testing::ext;
47 using namespace OHOS::HiviewDFX;
48
49 #undef LOG_DOMAIN
50 #define LOG_DOMAIN 0xD002D08
51
52 #undef LOG_TAG
53 #define LOG_TAG "HISYSEVENT_NATIVE_TEST"
54
55 namespace {
56 constexpr char TEST_DOMAIN[] = "DEMO";
57 constexpr char TEST_DOMAIN2[] = "KERNEL_VENDOR";
WriteSysEventByMarcoInterface()58 int32_t WriteSysEventByMarcoInterface()
59 {
60 return HiSysEventWrite(TEST_DOMAIN, "DEMO_EVENTNAME", HiSysEvent::EventType::FAULT,
61 "PARAM_KEY", "PARAM_VAL");
62 }
63
64 class Watcher : public HiSysEventListener {
65 public:
Watcher()66 Watcher() {}
~Watcher()67 virtual ~Watcher() {}
68
OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)69 void OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent) final
70 {
71 if (sysEvent == nullptr) {
72 return;
73 }
74 HILOG_DEBUG(LOG_CORE, "domain: %{public}s, eventName: %{public}s, eventType: %{public}d, extra: %{public}s.",
75 sysEvent->GetDomain().c_str(), sysEvent->GetEventName().c_str(), sysEvent->GetEventType(),
76 sysEvent->AsJson().c_str());
77 }
78
OnServiceDied()79 void OnServiceDied() final
80 {
81 HILOG_DEBUG(LOG_CORE, "OnServiceDied");
82 }
83 };
84
85 using OnQueryCallback = std::function<bool(std::shared_ptr<std::vector<HiSysEventRecord>>)>;
86 using OnCompleteCallback = std::function<bool(int32_t, int32_t)>;
87
88 class Querier : public HiSysEventQueryCallback {
89 public:
Querier(OnQueryCallback onQueryCallback=nullptr,OnCompleteCallback onCompleteCallback=nullptr)90 explicit Querier(OnQueryCallback onQueryCallback = nullptr, OnCompleteCallback onCompleteCallback = nullptr)
91 : onQueryCallback(onQueryCallback), onCompleteCallback(onCompleteCallback) {}
~Querier()92 virtual ~Querier() {}
93
OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents)94 void OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents) final
95 {
96 if (onQueryCallback != nullptr) {
97 ASSERT_TRUE(onQueryCallback(sysEvents));
98 }
99 }
100
OnComplete(int32_t reason,int32_t total)101 void OnComplete(int32_t reason, int32_t total) final
102 {
103 if (onCompleteCallback != nullptr) {
104 ASSERT_TRUE(onCompleteCallback(reason, total));
105 }
106 }
107
108 private:
109 OnQueryCallback onQueryCallback;
110 OnCompleteCallback onCompleteCallback;
111 };
112 }
113
WrapSysEventWriteAssertion(int32_t ret,bool cond)114 static bool WrapSysEventWriteAssertion(int32_t ret, bool cond)
115 {
116 return cond || ret == OHOS::HiviewDFX::ERR_SEND_FAIL ||
117 ret == OHOS::HiviewDFX::ERR_WRITE_IN_HIGH_FREQ ||
118 ret == OHOS::HiviewDFX::ERR_DOMAIN_MASKED ||
119 ret == OHOS::HiviewDFX::ERR_TOO_MANY_CONCURRENT_QUERIES ||
120 ret == OHOS::HiviewDFX::ERR_QUERY_TOO_FREQUENTLY;
121 }
122
SetUpTestCase(void)123 void HiSysEventNativeTest::SetUpTestCase(void)
124 {
125 }
126
TearDownTestCase(void)127 void HiSysEventNativeTest::TearDownTestCase(void)
128 {
129 }
130
SetUp(void)131 void HiSysEventNativeTest::SetUp(void)
132 {
133 }
134
TearDown(void)135 void HiSysEventNativeTest::TearDown(void)
136 {
137 }
138
139 /**
140 * @tc.name: TestHiSysEventManagerQueryWithInvalidQueryRules001
141 * @tc.desc: Query with query rules which contains empty domain
142 * @tc.type: FUNC
143 * @tc.require: issueI62B10
144 */
145 HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerQueryWithInvalidQueryRules001, TestSize.Level1)
146 {
147 sleep(1);
148 auto querier = std::make_shared<Querier>();
149 long long defaultTimeStap = -1;
150 int queryCount = 10;
151 struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
152 std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
153 std::vector<std::string> eventNames {"EVENT_NAME"};
154 OHOS::HiviewDFX::QueryRule rule("", eventNames); // empty domain
155 queryRules.emplace_back(rule);
156 auto ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
157 // only process with root or shell uid return OHOS::HiviewDFX::IPC_CALL_SUCCEED
158 ASSERT_TRUE(ret == OHOS::HiviewDFX::ERR_QUERY_RULE_INVALID || ret == OHOS::HiviewDFX::IPC_CALL_SUCCEED);
159 }
160
161 /**
162 * @tc.name: TestHiSysEventManagerQueryWithInvalidQueryRules002
163 * @tc.desc: Query with query rules which contains empty event names
164 * @tc.type: FUNC
165 * @tc.require: issueI62B10
166 */
167 HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerQueryWithInvalidQueryRules002, TestSize.Level1)
168 {
169 sleep(1);
170 auto querier = std::make_shared<Querier>();
171 long long defaultTimeStap = -1;
172 int queryCount = 10;
173 struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
174 std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
175 std::vector<std::string> eventNames; // empty event name
176 OHOS::HiviewDFX::QueryRule rule("DOMAIN", eventNames);
177 queryRules.emplace_back(rule);
178 auto ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
179 // only process with root or shell uid return OHOS::HiviewDFX::IPC_CALL_SUCCEED
180 ASSERT_TRUE(ret == OHOS::HiviewDFX::ERR_QUERY_RULE_INVALID || ret == OHOS::HiviewDFX::IPC_CALL_SUCCEED);
181 }
182
183 /**
184 * @tc.name: TestSubscribeSysEventByTag
185 * @tc.desc: Subscribe sysevent by event tag
186 * @tc.type: FUNC
187 * @tc.require: issueI62B10
188 */
189 HWTEST_F(HiSysEventNativeTest, TestSubscribeSysEventByTag, TestSize.Level1)
190 {
191 auto watcher = std::make_shared<Watcher>();
192 OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "TAG", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
193 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
194 sysRules.emplace_back(listenerRule);
195 auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
196 ASSERT_EQ(ret, IPC_CALL_SUCCEED);
197 ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(watcher);
198 ASSERT_EQ(ret, IPC_CALL_SUCCEED);
199 }
200
201 /**
202 * @tc.name: TestHiSysEventNormal001
203 * @tc.desc: Test normal write.
204 * @tc.type: FUNC
205 * @tc.require: AR000G2QKU AR000FT2Q1
206 */
207 HWTEST_F(HiSysEventNativeTest, TestHiSysEventNormal001, TestSize.Level1)
208 {
209 /**
210 * @tc.steps: step1.make sure SystemAbilityManager is started.
211 */
212 static constexpr char domain[] = "DEMO";
213 std::string eventName = "NORMAL001";
214
215 bool testBoolValue = true;
216 char testCharValue = 'a';
217 short testShortValue = -100;
218 int testIntValue = -200;
219 long testLongValue = -300;
220 long long testLongLongValue = -400;
221
222 unsigned char testUnsignedCharValue = 'a';
223 unsigned short testUnsignedShortValue = 100;
224 unsigned int testUnsignedIntValue = 200;
225 unsigned long testUnsignedLongValue = 300;
226 unsigned long long testUnsignedLongLongValue = 400;
227
228 float testFloatValue = 1.1;
229 double testDoubleValue = 2.2;
230 std::string testStringValue = "abc";
231
232 std::vector<bool> testBoolValues;
233 testBoolValues.push_back(true);
234 testBoolValues.push_back(true);
235 testBoolValues.push_back(false);
236
237 std::vector<char> testCharValues;
238 testCharValues.push_back('a');
239 testCharValues.push_back('b');
240 testCharValues.push_back('c');
241
242 std::vector<unsigned char> testUnsignedCharValues;
243 testUnsignedCharValues.push_back('a');
244 testUnsignedCharValues.push_back('b');
245 testUnsignedCharValues.push_back('c');
246
247 std::vector<short> testShortValues;
248 testShortValues.push_back(-100);
249 testShortValues.push_back(-200);
250 testShortValues.push_back(-300);
251
252 std::vector<unsigned short> testUnsignedShortValues;
253 testUnsignedShortValues.push_back(100);
254 testUnsignedShortValues.push_back(200);
255 testUnsignedShortValues.push_back(300);
256
257 std::vector<int> testIntValues;
258 testIntValues.push_back(-1000);
259 testIntValues.push_back(-2000);
260 testIntValues.push_back(-3000);
261
262 std::vector<unsigned int> testUnsignedIntValues;
263 testUnsignedIntValues.push_back(1000);
264 testUnsignedIntValues.push_back(2000);
265 testUnsignedIntValues.push_back(3000);
266
267 std::vector<long> testLongValues;
268 testLongValues.push_back(-10000);
269 testLongValues.push_back(-20000);
270 testLongValues.push_back(-30000);
271
272 std::vector<unsigned long> testUnsignedLongValues;
273 testUnsignedLongValues.push_back(10000);
274 testUnsignedLongValues.push_back(20000);
275 testUnsignedLongValues.push_back(30000);
276
277 std::vector<long long> testLongLongValues;
278 testLongLongValues.push_back(-100000);
279 testLongLongValues.push_back(-200000);
280 testLongLongValues.push_back(-300000);
281
282 std::vector<unsigned long long> testUnsignedLongLongValues;
283 testUnsignedLongLongValues.push_back(100000);
284 testUnsignedLongLongValues.push_back(200000);
285 testUnsignedLongLongValues.push_back(300000);
286
287 std::vector<float> testFloatValues;
288 testFloatValues.push_back(1.1);
289 testFloatValues.push_back(2.2);
290 testFloatValues.push_back(3.3);
291
292 std::vector<double> testDoubleValues;
293 testDoubleValues.push_back(10.1);
294 testDoubleValues.push_back(20.2);
295 testDoubleValues.push_back(30.3);
296
297 std::vector<std::string> testStringValues;
298 testStringValues.push_back(std::string("a"));
299 testStringValues.push_back(std::string("b"));
300 testStringValues.push_back(std::string("c"));
301
302 HILOG_INFO(LOG_CORE, "test hisysevent normal write");
303 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT,
304 "keyBool", testBoolValue, "keyChar", testCharValue, "keyShort", testShortValue,
305 "keyInt", testIntValue, "KeyLong", testLongValue, "KeyLongLong", testLongLongValue,
306 "keyUnsignedChar", testUnsignedCharValue, "keyUnsignedShort", testUnsignedShortValue,
307 "keyUnsignedInt", testUnsignedIntValue, "keyUnsignedLong", testUnsignedLongValue,
308 "keyUnsignedLongLong", testUnsignedLongLongValue, "keyFloat", testFloatValue,
309 "keyDouble", testDoubleValue, "keyString1", testStringValue, "keyString2", "efg",
310 "keyBools", testBoolValues, "keyChars", testCharValues, "keyUnsignedChars", testUnsignedCharValues,
311 "keyShorts", testShortValues, "keyUnsignedShorts", testUnsignedShortValues,
312 "keyInts", testIntValues, "keyUnsignedInts", testUnsignedIntValues, "keyLongs", testLongValues,
313 "keyUnsignedLongs", testUnsignedLongValues, "keyLongLongs", testLongLongValues,
314 "keyUnsignedLongLongs", testUnsignedLongLongValues, "keyFloats", testFloatValues,
315 "keyDoubles", testDoubleValues, "keyStrings", testStringValues);
316 HILOG_INFO(LOG_CORE, "normal write, retCode=%{public}d", result);
317 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
318 }
319
320 /**
321 * @tc.name: TestHiSysEventDomainSpecialChar002
322 * @tc.desc: Test domain has special char.
323 * @tc.type: FUNC
324 * @tc.require: AR000G2QKU AR000FT2Q1
325 */
326 HWTEST_F(HiSysEventNativeTest, TestHiSysEventDomainSpecialChar002, TestSize.Level1)
327 {
328 /**
329 * @tc.steps: step1.make sure write sys event.
330 */
331 static constexpr char domain[] = "_demo";
332 std::string eventName = "DOMAIN_SPECIAL_CHAR";
333 HILOG_INFO(LOG_CORE, "test hisysevent domain has special char");
334 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT);
335 HILOG_INFO(LOG_CORE, "domain has special char, retCode=%{public}d", result);
336 ASSERT_LT(result, 0);
337 }
338
339 /**
340 * @tc.name: TestHiSysEventDomainEmpty003
341 * @tc.desc: Test domain is empty.
342 * @tc.type: FUNC
343 * @tc.require: AR000G2QKU AR000FT2Q1
344 */
345 HWTEST_F(HiSysEventNativeTest, TestHiSysEventDomainEmpty003, TestSize.Level1)
346 {
347 /**
348 * @tc.steps: step1.make sure write sys event.
349 */
350 static constexpr char domain[] = "";
351 std::string eventName = "DOMAIN_EMPTY";
352 HILOG_INFO(LOG_CORE, "test hisysevent domain is empty");
353 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT);
354 HILOG_INFO(LOG_CORE, "domain is empty, retCode=%{public}d", result);
355 ASSERT_LT(result, 0);
356 }
357
358 /**
359 * @tc.name: TestHiSysEventDomainTooLong004
360 * @tc.desc: Test domain is too long.
361 * @tc.type: FUNC
362 * @tc.require: AR000G2QKU AR000FT2Q1
363 */
364 HWTEST_F(HiSysEventNativeTest, TestHiSysEventDomainTooLong004, TestSize.Level1)
365 {
366 /**
367 * @tc.steps: step1.make sure write sys event.
368 */
369 static constexpr char domain16[] = "AAAAAAAAAAAAAAAA";
370 std::string eventName = "DOMAIN_TOO_LONG_16";
371 HILOG_INFO(LOG_CORE, "test hisysevent domain is too long, normal length");
372
373 int result = 0;
374 result = HiSysEventWrite(domain16, eventName, HiSysEvent::EventType::FAULT);
375 HILOG_INFO(LOG_CORE, "domain too long, equal 16 retCode=%{public}d", result);
376
377 HILOG_INFO(LOG_CORE, "test hisysevent domain is too long");
378 static constexpr char domain17[] = "AAAAAAAAAAAAAAAAL";
379 eventName = "DOMAIN_TOO_LONG_17";
380 result = HiSysEventWrite(domain17, eventName, HiSysEvent::EventType::FAULT);
381 HILOG_INFO(LOG_CORE, "domain is too long, more than 16 retCode=%{public}d", result);
382 ASSERT_LT(result, 0);
383 }
384
385 /**
386 * @tc.name: TesetHiSysEventSpecailEventName005
387 * @tc.desc: Test event name has special char.
388 * @tc.type: FUNC
389 * @tc.require: AR000G2QKU AR000FT2Q1
390 */
391 HWTEST_F(HiSysEventNativeTest, TesetHiSysEventSpecailEventName005, TestSize.Level1)
392 {
393 /**
394 * @tc.steps: step1.make sure write sys event.
395 */
396 static constexpr char domain[] = "SPEC_EVT_NAME";
397 std::string eventName = "_SPECIAL_CHAR";
398 HILOG_INFO(LOG_CORE, "test hisysevent event name has special char");
399 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT);
400 HILOG_INFO(LOG_CORE, "event name has special char, retCode=%{public}d", result);
401 ASSERT_LT(result, 0);
402 }
403
404 /**
405 * @tc.name: TestHiSysEventNameEmpty006
406 * @tc.desc: Test event name is empty.
407 * @tc.type: FUNC
408 * @tc.require: AR000G2QKU AR000FT2Q1
409 */
410 HWTEST_F(HiSysEventNativeTest, TestHiSysEventNameEmpty006, TestSize.Level1)
411 {
412 /**
413 * @tc.steps: step1.make sure write sys event.
414 */
415 static constexpr char domain[] = "EMPTY";
416 std::string eventName = "";
417 HILOG_INFO(LOG_CORE, "test hisysevent event name is empty");
418 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT);
419 HILOG_INFO(LOG_CORE, "event name is empty, retCode=%{public}d", result);
420 ASSERT_LT(result, 0);
421 }
422
423 /**
424 * @tc.name: TesetHiSysEventNameTooLong007
425 * @tc.desc: Test event name too long.
426 * @tc.type: FUNC
427 * @tc.require: AR000G2QKU AR000FT2Q1
428 */
429 HWTEST_F(HiSysEventNativeTest, TesetHiSysEventNameTooLong007, TestSize.Level1)
430 {
431 /**
432 * @tc.steps: step1.make sure write sys event.
433 */
434 static constexpr char domain32[] = "NAME_32";
435 std::string eventName = "";
436 HILOG_INFO(LOG_CORE, "test hisysevent event name is too long, normal length");
437 int normal = 32;
438 for (int index = 0; index < normal; index++) {
439 eventName.append("N");
440 }
441 int result = 0;
442 result = HiSysEventWrite(domain32, eventName, HiSysEvent::EventType::FAULT);
443 HILOG_INFO(LOG_CORE, "event name is too long, equal 32, retCode=%{public}d", result);
444 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
445
446 HILOG_INFO(LOG_CORE, "test hisysevent event name is too long");
447 static constexpr char domain33[] = "NAME_33";
448 eventName.append("L");
449 result = HiSysEventWrite(domain33, eventName, HiSysEvent::EventType::FAULT);
450 HILOG_INFO(LOG_CORE, "event name is too long, more than 32, retCode=%{public}d", result);
451 ASSERT_LT(result, 0);
452 }
453
454 /**
455 * @tc.name: TestHiSysEventKeySpecialChar008
456 * @tc.desc: Test key has specail char.
457 * @tc.type: FUNC
458 * @tc.require: AR000G2QKU AR000FT2Q1
459 */
460 HWTEST_F(HiSysEventNativeTest, TestHiSysEventKeySpecialChar008, TestSize.Level1)
461 {
462 /**
463 * @tc.steps: step1.make sure write sys event.
464 */
465 static constexpr char domain[] = "DEMO";
466 std::string eventName = "HiSysEvent006";
467 std::string key1 = "_key1";
468 std::string key2 = "key2";
469 int result = 0;
470 HILOG_INFO(LOG_CORE, "test hisysevent key has special char");
471 bool value1 = true;
472 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value1, key2, value1);
473 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
474
475 short value2 = 2;
476 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value2, key2, value2);
477 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
478
479 unsigned short value3 = 3;
480 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value3, key2, value3);
481 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
482
483 int value4 = 4;
484 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value4, key2, value4);
485 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
486
487 unsigned int value5 = 5;
488 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value5, key2, value5);
489 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
490
491 long value6 = 6;
492 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value6, key2, value6);
493 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
494
495 unsigned long value7 = 7;
496 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value7, key2, value7);
497 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
498
499 long long value8 = 8;
500 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value8, key2, value8);
501 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
502
503 unsigned long long value9 = 9;
504 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value9, key2, value9);
505 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
506
507 char value10 = 'a';
508 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value10, key2, value10);
509 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
510
511 unsigned char value11 = 'b';
512 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value11, key2, value11);
513 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
514
515 float value12 = 12.12;
516 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value12, key2, value12);
517 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
518
519 double value13 = 13.13;
520 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key1, value13, key2, value13);
521 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
522 }
523
524
525 /**
526 * @tc.name: TestHiSysEventEscape009
527 * @tc.desc: Test key's value need escape.
528 * @tc.type: FUNC
529 * @tc.require: AR000G2QKU AR000FT2Q1
530 */
531 HWTEST_F(HiSysEventNativeTest, TestHiSysEventEscape009, TestSize.Level1)
532 {
533 /**
534 * @tc.steps: step1.make sure write sys event.
535 */
536 static constexpr char domain[] = "DEMO";
537 std::string eventName = "ESCAPE";
538 HILOG_INFO(LOG_CORE, "test hisysevent escape char");
539 std::string value = "\"escapeByCpp\"";
540 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", value);
541 HILOG_INFO(LOG_CORE, "key's value has espcae char, retCode=%{public}d", result);
542 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
543 }
544
545 /**
546 * @tc.name: TestHiSysEventKeyEmpty010
547 * @tc.desc: Test key is empty.
548 * @tc.type: FUNC
549 * @tc.require: AR000G2QKU AR000FT2Q1
550 */
551 HWTEST_F(HiSysEventNativeTest, TestHiSysEventKeyEmpty010, TestSize.Level1)
552 {
553 /**
554 * @tc.steps: step1.make sure write sys event.
555 */
556 static constexpr char domain[] = "DEMO";
557 std::string eventName = "KEY_EMPTY";
558 HILOG_INFO(LOG_CORE, "test hisysevent key is empty");
559 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT,
560 "", "valueIsEmpty", "key2", "notEmpty");
561 HILOG_INFO(LOG_CORE, "key is empty, retCode=%{public}d", result);
562 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
563 }
564
565 /**
566 * @tc.name: TestHiSysEventKeySpecialChar011
567 * @tc.desc: Test key has special char.
568 * @tc.type: FUNC
569 * @tc.require: AR000G2QKU AR000FT2Q1
570 */
571 HWTEST_F(HiSysEventNativeTest, TestHiSysEventKeySpecialChar011, TestSize.Level1)
572 {
573 /**
574 * @tc.steps: step1.make sure write sys event.
575 */
576 static constexpr char domain[] = "DEMO";
577 std::string eventName = "KEY_SPECIAL_CHAR";
578 HILOG_INFO(LOG_CORE, "test hisysevent key is special");
579 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT,
580 "_key1", "special", "key2", "normal");
581 HILOG_INFO(LOG_CORE, "key has special char, retCode=%{public}d", result);
582 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
583 }
584
585 /**
586 * @tc.name: TestHiSysEventKeyTooLong012
587 * @tc.desc: Test key is too long.
588 * @tc.type: FUNC
589 * @tc.require: AR000G2QKU AR000FT2Q1
590 */
591 HWTEST_F(HiSysEventNativeTest, TestHiSysEventKeyTooLong012, TestSize.Level1)
592 {
593 /**
594 * @tc.steps: step1.make sure write sys event.
595 */
596 static constexpr char domain[] = "DEMO";
597 std::string eventName = "KEY_48";
598 HILOG_INFO(LOG_CORE, "test hisysevent key 48 char");
599 std::string key = "";
600 int normal = 48;
601 for (int index = 0; index < normal; index++) {
602 key.append("V");
603 }
604 int result = 0;
605 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key, "48length", "key2", "normal");
606 HILOG_INFO(LOG_CORE, "key equal 48 char, retCode=%{public}d", result);
607 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
608
609 HILOG_INFO(LOG_CORE, "test hisysevent key 49 char");
610 eventName = "KEY_49";
611 key.append("V");
612 result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, key, "49length", "key2", "normal");
613 HILOG_INFO(LOG_CORE, "key more than 48 char, retCode=%{public}d", result);
614 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
615 }
616
617 /**
618 * @tc.name: TestHiSysEvent128Keys013
619 * @tc.desc: Test 128 key.
620 * @tc.type: FUNC
621 * @tc.require: AR000G2QKU AR000FT2Q1
622 */
623 HWTEST_F(HiSysEventNativeTest, TestHiSysEvent128Keys013, TestSize.Level1)
624 {
625 /**
626 * @tc.steps: step1.make sure write sys event.
627 */
628 static constexpr char domain[] = "TEST";
629 std::string eventName = "KEY_EQUAL_128";
630 HILOG_INFO(LOG_CORE, "test hisysevent 128 keys");
631 std::string k = "k";
632 bool v = true;
633 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT,
634 SYS_EVENT_PARAMS(10), SYS_EVENT_PARAMS(20), SYS_EVENT_PARAMS(30), SYS_EVENT_PARAMS(40), SYS_EVENT_PARAMS(50),
635 SYS_EVENT_PARAMS(60), SYS_EVENT_PARAMS(70), SYS_EVENT_PARAMS(80), SYS_EVENT_PARAMS(90), SYS_EVENT_PARAMS(100),
636 SYS_EVENT_PARAMS(110), SYS_EVENT_PARAMS(120),
637 k, v, k, v, k, v, k, v, k, v, k, v, k, v, k, v);
638 HILOG_INFO(LOG_CORE, "has 128 key, retCode=%{public}d", result);
639 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
640 }
641
642 /**
643 * @tc.name: TestHiSysEvent129Keys014
644 * @tc.desc: Test 129 key.
645 * @tc.type: FUNC
646 * @tc.require: AR000G2QKU AR000FT2Q1
647 */
648 HWTEST_F(HiSysEventNativeTest, TestHiSysEvent129Keys014, TestSize.Level1)
649 {
650 /**
651 * @tc.steps: step1.make sure write sys event.
652 */
653 static constexpr char domain[] = "TEST";
654 std::string eventName = "KEY_EQUAL_129";
655 HILOG_INFO(LOG_CORE, "test hisysevent 129 key");
656 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT,
657 SYS_EVENT_PARAMS(10), SYS_EVENT_PARAMS(20), SYS_EVENT_PARAMS(30), SYS_EVENT_PARAMS(40), SYS_EVENT_PARAMS(50),
658 SYS_EVENT_PARAMS(60), SYS_EVENT_PARAMS(70), SYS_EVENT_PARAMS(80), SYS_EVENT_PARAMS(90), SYS_EVENT_PARAMS(100),
659 SYS_EVENT_PARAMS(110), SYS_EVENT_PARAMS(120),
660 "key1", true, "key2", true, "key3", true, "key4", true, "key5", true,
661 "key6", true, "key7", true, "key8", true, "key9", true);
662 HILOG_INFO(LOG_CORE, "has 129 key, retCode=%{public}d", result);
663 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
664 }
665
666 /**
667 * @tc.name: TestHiSysEventStringValueEqual256K015
668 * @tc.desc: Test 256K string.
669 * @tc.type: FUNC
670 * @tc.require: AR000G2QKU AR000FT2Q1
671 */
672 HWTEST_F(HiSysEventNativeTest, TestHiSysEventStringValueEqual256K015, TestSize.Level1)
673 {
674 /**
675 * @tc.steps: step1.make sure write sys event.
676 */
677 static constexpr char domain[] = "TEST";
678 std::string eventName = "EQUAL_256K";
679 HILOG_INFO(LOG_CORE, "test key's value 256K string");
680 std::string value;
681 int length = 256 * 1024;
682 for (int index = 0; index < length; index++) {
683 value.push_back('1' + index % 10);
684 }
685 sleep(1); // make sure hiview read all data before send large data
686 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", value);
687 HILOG_INFO(LOG_CORE, "string length is 256K, retCode=%{public}d", result);
688 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
689 }
690
691 /**
692 * @tc.name: TestHiSysEventStringValueMoreThan256K016
693 * @tc.desc: Test 256K + 1 string.
694 * @tc.type: FUNC
695 * @tc.require: AR000G2QKU AR000FT2Q1
696 */
697 HWTEST_F(HiSysEventNativeTest, TestHiSysEventStringValueMoreThan256K016, TestSize.Level1)
698 {
699 /**
700 * @tc.steps: step1.make sure write sys event.
701 */
702 static constexpr char domain[] = "DEMO";
703 std::string eventName = "MORETHAN256K";
704 HILOG_INFO(LOG_CORE, "test more than 256K string");
705 std::string value;
706 int length = 256 * 1024 + 1;
707 for (int index = 0; index < length; index++) {
708 value.push_back('1' + index % 10);
709 }
710 sleep(1); // make sure hiview read all data before send large data
711 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", value);
712 HILOG_INFO(LOG_CORE, "string length is more than 256K, retCode=%{public}d", result);
713 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
714 }
715
716 /**
717 * @tc.name: TestHiSysEventArray100Item017
718 * @tc.desc: Test bool array item 100.
719 * @tc.type: FUNC
720 * @tc.require: AR000G2QKU AR000FT2Q1
721 */
722 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArray100Item017, TestSize.Level1)
723 {
724 /**
725 * @tc.steps: step1.make sure write sys event.
726 */
727 static constexpr char domain[] = "DEMO";
728 std::string eventName = "BOOL_ARRAY_100";
729 HILOG_INFO(LOG_CORE, "test bool array 100 item");
730 std::vector<bool> values;
731 int maxItem = 100;
732 for (int index = 0; index < maxItem; index++) {
733 values.push_back(true);
734 }
735 sleep(1); // make sure hiview read all data before send large data
736 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
737 HILOG_INFO(LOG_CORE, "array bool list 100, retCode=%{public}d", result);
738 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
739 }
740
741 /**
742 * @tc.name: TestHiSysEventArray101Item018
743 * @tc.desc: Test bool array item 101.
744 * @tc.type: FUNC
745 * @tc.require: AR000G2QKU AR000FT2Q1
746 */
747 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArray101Item018, TestSize.Level1)
748 {
749 /**
750 * @tc.steps: step1.make sure write sys event.
751 */
752 static constexpr char domain[] = "DEMO";
753 std::string eventName = "BOOL_ARRAY_101";
754 HILOG_INFO(LOG_CORE, "test bool array 101 item");
755 std::vector<bool> values;
756 int maxItem = 101;
757 for (int index = 0; index < maxItem; index++) {
758 values.push_back(true);
759 }
760 sleep(1); // make sure hiview read all data before send large data
761 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
762 HILOG_INFO(LOG_CORE, "array bool list 101, retCode=%{public}d", result);
763 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
764 }
765
766 /**
767 * @tc.name: TestHiSysEventArray100CharItem019
768 * @tc.desc: Test char array item 100.
769 * @tc.type: FUNC
770 * @tc.require: AR000G2QKU AR000FT2Q1
771 */
772 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArray100CharItem019, TestSize.Level1)
773 {
774 /**
775 * @tc.steps: step1.make sure write sys event.
776 */
777 static constexpr char domain[] = "DEMO";
778 std::string eventName = "CHAR_ARRAY_100";
779 HILOG_INFO(LOG_CORE, "test char array 100 item");
780 std::vector<char> values;
781 int maxItem = 100;
782 for (int index = 0; index < maxItem; index++) {
783 values.push_back('a');
784 }
785 sleep(1); // make sure hiview read all data before send large data
786 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
787 HILOG_INFO(LOG_CORE, "array char list 100, retCode=%{public}d", result);
788 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
789 }
790
791 /**
792 * @tc.name: TestHiSysEventArray101CharItem020
793 * @tc.desc: Test char array item 101.
794 * @tc.type: FUNC
795 * @tc.require: AR000G2QKU AR000FT2Q1
796 */
797 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArray101CharItem020, TestSize.Level1)
798 {
799 /**
800 * @tc.steps: step1.make sure write sys event.
801 */
802 static constexpr char domain[] = "DEMO";
803 std::string eventName = "CHAR_ARRAY_101";
804 HILOG_INFO(LOG_CORE, "test char array 101 item");
805 std::vector<char> values;
806 int maxItem = 101;
807 for (int index = 0; index < maxItem; index++) {
808 values.push_back('z');
809 }
810 sleep(1); // make sure hiview read all data before send large data
811 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
812 HILOG_INFO(LOG_CORE, "array char list 101, retCode=%{public}d", result);
813 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
814 }
815
816 /**
817 * @tc.name: TestHiSysEventArray100UnsignedCharItem021
818 * @tc.desc: Test unsigned char array item 100.
819 * @tc.type: FUNC
820 * @tc.require: AR000G2QKU AR000FT2Q1
821 */
822 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArray100UnsignedCharItem021, TestSize.Level1)
823 {
824 /**
825 * @tc.steps: step1.make sure write sys event.
826 */
827 static constexpr char domain[] = "DEMO";
828 std::string eventName = "UCHAR_ARRAY_100";
829 HILOG_INFO(LOG_CORE, "test unsigned char array 100 item");
830 std::vector<unsigned char> values;
831 int maxItem = 100;
832 for (int index = 0; index < maxItem; index++) {
833 values.push_back('a');
834 }
835 sleep(1); // make sure hiview read all data before send large data
836 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
837 HILOG_INFO(LOG_CORE, "array unsigned char list 100, retCode=%{public}d", result);
838 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
839 }
840
841 /**
842 * @tc.name: TestHiSysEventArray101UnsignedCharItem022
843 * @tc.desc: Test unsigned char array item 101.
844 * @tc.type: FUNC
845 * @tc.require: AR000G2QKU AR000FT2Q1
846 */
847 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArray101UnsignedCharItem022, TestSize.Level1)
848 {
849 /**
850 * @tc.steps: step1.make sure write sys event.
851 */
852 static constexpr char domain[] = "DEMO";
853 std::string eventName = "UCHAR_ARRAY_101";
854 HILOG_INFO(LOG_CORE, "test unsigned char array 101 item");
855 std::vector<unsigned char> values;
856 int maxItem = 101;
857 for (int index = 0; index < maxItem; index++) {
858 values.push_back('z');
859 }
860 sleep(1); // make sure hiview read all data before send large data
861 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
862 HILOG_INFO(LOG_CORE, "array unsigned char list 101, retCode=%{public}d", result);
863 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > OHOS::HiviewDFX::SUCCESS));
864 }
865
866
867 /**
868 * @tc.name: TestHiSysEventArray100StringItem023
869 * @tc.desc: Test string array item 100.
870 * @tc.type: FUNC
871 * @tc.require: AR000G2QKU AR000FT2Q1
872 */
873 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArray100StringItem023, TestSize.Level1)
874 {
875 /**
876 * @tc.steps: step1.make sure write sys event.
877 */
878 static constexpr char domain[] = "DEMO";
879 std::string eventName = "STR_ARRAY_100";
880 HILOG_INFO(LOG_CORE, "test string array 100 item");
881 std::vector<std::string> values;
882 int maxItem = 100;
883 for (int index = 0; index < maxItem; index++) {
884 values.push_back("a");
885 }
886 sleep(1); // make sure hiview read all data before send large data
887 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
888 HILOG_INFO(LOG_CORE, "array string list 100, retCode=%{public}d", result);
889 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::SUCCESS));
890 }
891
892 /**
893 * @tc.name: TestHiSysEventArray101StringItem024
894 * @tc.desc: Test string array item 101.
895 * @tc.type: FUNC
896 * @tc.require: AR000G2QKU AR000FT2Q1
897 */
898 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArray101StringItem024, TestSize.Level1)
899 {
900 /**
901 * @tc.steps: step1.make sure write sys event.
902 */
903 static constexpr char domain[] = "DEMO";
904 std::string eventName = "STR_ARRAY_101";
905 HILOG_INFO(LOG_CORE, "test string array 101 item");
906 std::vector<std::string> values;
907 int maxItem = 101;
908 for (int index = 0; index < maxItem; index++) {
909 values.push_back("z");
910 }
911 sleep(1); // make sure hiview read all data before send large data
912 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
913 HILOG_INFO(LOG_CORE, "array string list 101, retCode=%{public}d", result);
914 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > 0));
915 }
916
917 /**
918 * @tc.name: TestHiSysEventArrayStringValueEqual256K025
919 * @tc.desc: Test array item 256K string.
920 * @tc.type: FUNC
921 * @tc.require: AR000G2QKU AR000FT2Q1
922 */
923 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArrayStringValueEqual256K025, TestSize.Level1)
924 {
925 /**
926 * @tc.steps: step1.make sure write sys event.
927 */
928 static constexpr char domain[] = "TEST";
929 std::string eventName = "EQUAL_256K";
930 HILOG_INFO(LOG_CORE, "test array item value 256K string");
931 std::string value;
932 int length = 256 * 1024;
933 for (int index = 0; index < length; index++) {
934 value.push_back('1' + index % 10);
935 }
936 sleep(1); // make sure hiview read all data before send large data
937 std::vector<std::string> values;
938 values.push_back("c");
939 values.push_back(value);
940 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
941 HILOG_INFO(LOG_CORE, "array item value length is 256K, retCode=%{public}d", result);
942 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == 0));
943 }
944
945 /**
946 * @tc.name: TestHiSysEventArrayStringValueMoreThan256K016
947 * @tc.desc: Test 256K + 1 string.
948 * @tc.type: FUNC
949 * @tc.require: AR000G2QKU AR000FT2Q1
950 */
951 HWTEST_F(HiSysEventNativeTest, TestHiSysEventArrayStringValueMoreThan256K026, TestSize.Level1)
952 {
953 /**
954 * @tc.steps: step1.make sure write sys event.
955 */
956 static constexpr char domain[] = "DEMO";
957 std::string eventName = "MORETHAN256K";
958 HILOG_INFO(LOG_CORE, "test array item value more than 256K string");
959 std::string value;
960 int length = 256 * 1024 + 1;
961 for (int index = 0; index < length; index++) {
962 value.push_back('1' + index % 10);
963 }
964 sleep(1); // make sure hiview read all data before send large data
965 std::vector<std::string> values;
966 values.push_back("c");
967 values.push_back(value);
968 int result = HiSysEventWrite(domain, eventName, HiSysEvent::EventType::FAULT, "key1", values);
969 HILOG_INFO(LOG_CORE, "array item value length is more than 256K, retCode=%{public}d", result);
970 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result > 0));
971 }
972
973 /**
974 * @tc.name: TestDefensingHiSysEventStorm
975 * @tc.desc: Write event more than 100 times in 5 seconds
976 * @tc.type: FUNC
977 * @tc.require: issueI5FNPQ
978 */
979 HWTEST_F(HiSysEventNativeTest, TestDefensingHiSysEventStorm, TestSize.Level1)
980 {
981 int writeCount = 102;
982 for (int i = 0; i < writeCount; i++) {
983 auto result = WriteSysEventByMarcoInterface();
984 if (i < HISYSEVENT_THRESHOLD) {
985 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == 0));
986 } else {
987 ASSERT_TRUE(WrapSysEventWriteAssertion(result, result == OHOS::HiviewDFX::ERR_WRITE_IN_HIGH_FREQ));
988 }
989 }
990 }
991
992 /**
993 * @tc.name: TestAddAndRemoveListener
994 * @tc.desc: Add listener and then remove it
995 * @tc.type: FUNC
996 * @tc.require: issueI5KDIG
997 */
998 HWTEST_F(HiSysEventNativeTest, TestAddAndRemoveListener, TestSize.Level1)
999 {
1000 auto watcher = std::make_shared<Watcher>();
1001 OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1002 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1003 sysRules.emplace_back(listenerRule);
1004 auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(nullptr, sysRules);
1005 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1006 ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
1007 ASSERT_EQ(ret, 0);
1008 ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(nullptr);
1009 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1010 auto newWatcher = std::make_shared<Watcher>();
1011 ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(newWatcher);
1012 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1013 ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(watcher);
1014 ASSERT_EQ(ret, 0);
1015 }
1016
1017 /**
1018 * @tc.name: TestEnableAndDisableDebugMode
1019 * @tc.desc: Enable debug mode and then disable it on listener
1020 * @tc.type: FUNC
1021 * @tc.require: issueI5KDIG
1022 */
1023 HWTEST_F(HiSysEventNativeTest, TestEnableAndDisableDebugMode, TestSize.Level1)
1024 {
1025 auto watcher = std::make_shared<Watcher>();
1026 OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1027 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1028 sysRules.emplace_back(listenerRule);
1029 auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
1030 ASSERT_EQ(ret, 0);
1031 }
1032
1033 /**
1034 * @tc.name: TestHiSysEventBaseManagerAddAndRemoveListener
1035 * @tc.desc: Add a base listener and then remove it
1036 * @tc.type: FUNC
1037 * @tc.require: issueI5OA3F
1038 */
1039 HWTEST_F(HiSysEventNativeTest, TestHiSysEventBaseManagerAddAndRemoveListener, TestSize.Level1)
1040 {
1041 auto watcher = std::make_shared<Watcher>();
1042 auto baseWatcher = std::make_shared<HiSysEventBaseListener>(watcher);
1043 OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1044 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1045 sysRules.emplace_back(listenerRule);
1046 auto ret = OHOS::HiviewDFX::HiSysEventBaseManager::AddListener(nullptr, sysRules);
1047 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1048 ret = OHOS::HiviewDFX::HiSysEventBaseManager::AddListener(baseWatcher, sysRules);
1049 ASSERT_EQ(ret, 0);
1050 ret = OHOS::HiviewDFX::HiSysEventBaseManager::RemoveListener(nullptr);
1051 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1052 auto newBaseWatcher = std::make_shared<HiSysEventBaseListener>(watcher);
1053 ret = OHOS::HiviewDFX::HiSysEventBaseManager::RemoveListener(newBaseWatcher);
1054 ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1055 ret = OHOS::HiviewDFX::HiSysEventBaseManager::RemoveListener(baseWatcher);
1056 ASSERT_EQ(ret, 0);
1057 }
1058
1059 /**
1060 * @tc.name: TestHiSysEventBaseManagerQueryEvent
1061 * @tc.desc: Query sys events by base manager
1062 * @tc.type: FUNC
1063 * @tc.require: issueI5KDIG
1064 */
1065 HWTEST_F(HiSysEventNativeTest, TestHiSysEventBaseManagerQueryEvent, TestSize.Level1)
1066 {
1067 auto querier = std::make_shared<Querier>();
1068 long long defaultTimeStap = -1;
1069 int queryCount = 10;
1070 struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1071 std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1072 auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>(querier);
1073 auto ret = OHOS::HiviewDFX::HiSysEventBaseManager::Query(args, queryRules, baseQuerier);
1074 ASSERT_EQ(ret, 0);
1075 }
1076
1077 /**
1078 * @tc.name: TestHiSysEventManagerAddListenerWithTooManyRules
1079 * @tc.desc: Add listener with more than 20 rules
1080 * @tc.type: FUNC
1081 * @tc.require: issueI5KDIG
1082 */
1083 HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerAddListenerWithTooManyRules, TestSize.Level1)
1084 {
1085 auto watcher = std::make_shared<Watcher>();
1086 OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1087 int ruleCount = 20;
1088 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1089 while (ruleCount-- > 0) {
1090 sysRules.emplace_back(listenerRule);
1091 }
1092 auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
1093 ASSERT_EQ(ret, 0);
1094 sysRules.emplace_back(listenerRule);
1095 ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
1096 ASSERT_EQ(ret, OHOS::HiviewDFX::ERR_TOO_MANY_WATCH_RULES);
1097 }
1098
1099 /**
1100 * @tc.name: TestHiSysEventManagerAddTooManyEventListener
1101 * @tc.desc: Adding more than 30 event listener
1102 * @tc.type: FUNC
1103 * @tc.require: issueI5KDIG
1104 */
1105 HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerAddTooManyEventListener, TestSize.Level1)
1106 {
1107 OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1108 std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1109 sysRules.emplace_back(listenerRule);
1110 int cnt = 30;
1111 int32_t ret = 0;
1112 while (cnt-- > 0) {
1113 ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(std::make_shared<Watcher>(), sysRules);
1114 }
1115 ASSERT_EQ(ret, OHOS::HiviewDFX::ERR_TOO_MANY_WATCHERS);
1116 }
1117
1118 /**
1119 * @tc.name: TestHiSysEventManagerQueryWithTooManyRules
1120 * @tc.desc: Query with 11 query rules
1121 * @tc.type: FUNC
1122 * @tc.require: issueI5L2RV
1123 */
1124 HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerQueryWithTooManyRules, TestSize.Level1)
1125 {
1126 auto querier = std::make_shared<Querier>();
1127 long long defaultTimeStap = -1;
1128 int queryCount = 10;
1129 struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1130 std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1131 int rulesCount = 101; // limit to 100
1132 while (rulesCount-- > 0) {
1133 std::vector<std::string> eventNames {"EVENT_NAME"};
1134 OHOS::HiviewDFX::QueryRule rule("DOMAIN", eventNames);
1135 queryRules.emplace_back(rule);
1136 }
1137 auto ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
1138 ASSERT_EQ(ret, OHOS::HiviewDFX::ERR_TOO_MANY_QUERY_RULES);
1139 }
1140
1141 /**
1142 * @tc.name: TestHiSysEventManagerTooManyConcurrentQueries
1143 * @tc.desc: Query more than 4 times at same time
1144 * @tc.type: FUNC
1145 * @tc.require: issueI5L2RV
1146 */
1147 HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerTooManyConcurrentQueries, TestSize.Level1)
1148 {
1149 auto querier = std::make_shared<Querier>();
1150 long long defaultTimeStap = -1;
1151 int queryCount = 10;
1152 struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1153 std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1154 int threadCount = 5;
1155 auto ret = OHOS::HiviewDFX::IPC_CALL_SUCCEED;
1156 for (int i = 0; i < threadCount; i++) {
__anon87309d150202() 1157 std::thread t([&ret, &args, &queryRules, &querier] () {
1158 ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
1159 });
1160 t.join();
1161 }
1162 ASSERT_TRUE((ret == OHOS::HiviewDFX::ERR_TOO_MANY_CONCURRENT_QUERIES) ||
1163 (ret == OHOS::HiviewDFX::ERR_QUERY_TOO_FREQUENTLY) ||
1164 (ret == OHOS::HiviewDFX::IPC_CALL_SUCCEED));
1165 }
1166
1167 /**
1168 * @tc.name: TestHiSysEventManagerQueryTooFrequently
1169 * @tc.desc: Query twice in 1 seconds
1170 * @tc.type: FUNC
1171 * @tc.require: issueI5L2RV
1172 */
1173 HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerQueryTooFrequently, TestSize.Level1)
1174 {
1175 auto querier = std::make_shared<Querier>();
1176 long long defaultTimeStap = -1;
1177 int queryCount = 10;
1178 struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1179 std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1180 const int threshhold = 50;
1181 const int delayDuration = 1; // 1 second
1182 for (int i = 0; i < 2; i++) { // 2 cycles
1183 sleep(delayDuration);
1184 for (int j = 0; j <= threshhold; j++) { // more than 50 queries in 1 second is never allowed
1185 auto ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
1186 ASSERT_TRUE((ret == OHOS::HiviewDFX::ERR_QUERY_TOO_FREQUENTLY) ||
1187 (ret == OHOS::HiviewDFX::IPC_CALL_SUCCEED));
1188 }
1189 }
1190 }
1191
1192 /**
1193 * @tc.name: TestInitHiSysEventRecordWithIncorrectStr
1194 * @tc.desc: Init a hisysevent record with an incorrect string
1195 * @tc.type: FUNC
1196 * @tc.require: issueI5OA3F
1197 */
1198 HWTEST_F(HiSysEventNativeTest, TestInitHiSysEventRecordWithIncorrectStr, TestSize.Level1)
1199 {
1200 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1201 \"PARAM_A\":\"param a\",";
1202 HiSysEventRecord record(JSON_STR);
1203 int64_t val = 0;
1204 int ret = record.GetParamValue("type_", val);
1205 ASSERT_EQ(ret, ERR_INIT_FAILED);
1206 }
1207
1208 /**
1209 * @tc.name: TestParseValueByInvalidKeyFromHiSysEventRecord
1210 * @tc.desc: Parse value by a invalid key from a hisysevent record
1211 * @tc.type: FUNC
1212 * @tc.require: issueI5OA3F
1213 */
1214 HWTEST_F(HiSysEventNativeTest, TestParseValueByInvalidKeyFromHiSysEventRecord, TestSize.Level1)
1215 {
1216 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1217 \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
1218 HiSysEventRecord record(JSON_STR);
1219 int64_t val = 0;
1220 int ret = record.GetParamValue("XXX", val);
1221 ASSERT_EQ(ret, ERR_KEY_NOT_EXIST);
1222 }
1223
1224 /**
1225 * @tc.name: TestParseValueByInvalidTypeFromHiSysEventRecord
1226 * @tc.desc: Parse value by a invalid type from a hisysevent record
1227 * @tc.type: FUNC
1228 * @tc.require: issueI5OA3F
1229 */
1230 HWTEST_F(HiSysEventNativeTest, TestParseValueByInvalidTypeFromHiSysEventRecord, TestSize.Level1)
1231 {
1232 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1233 \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
1234 HiSysEventRecord record(JSON_STR);
1235 int64_t val = 0;
1236 int ret = record.GetParamValue("PARAM_B", val);
1237 ASSERT_EQ(ret, ERR_TYPE_NOT_MATCH);
1238 }
1239
1240 /**
1241 * @tc.name: TestParseEventDomainNameTypeFromHiSysEventRecord
1242 * @tc.desc: Parse event domain, name and type from a hisysevent record
1243 * @tc.type: FUNC
1244 * @tc.require: issueI5OA3F
1245 */
1246 HWTEST_F(HiSysEventNativeTest, TestParseEventDomainNameTypeFromHiSysEventRecord, TestSize.Level1)
1247 {
1248 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1249 \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
1250 HiSysEventRecord record(JSON_STR);
1251 ASSERT_EQ(record.GetDomain(), "DEMO");
1252 ASSERT_EQ(record.GetEventName(), "EVENT_NAME_A");
1253 ASSERT_EQ(record.GetEventType(), 4);
1254 }
1255
1256 /**
1257 * @tc.name: TestParseInt64ValueFromHiSysEventRecord
1258 * @tc.desc: Parse int64 value from a hisysevent record
1259 * @tc.type: FUNC
1260 * @tc.require: issueI5OA3F
1261 */
1262 HWTEST_F(HiSysEventNativeTest, TestParseInt64ValueFromHiSysEventRecord, TestSize.Level1)
1263 {
1264 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1265 \"PARAM_A\":-1,\"PARAM_B\":\"param b\"}";
1266 HiSysEventRecord record(JSON_STR);
1267 int64_t val = 0;
1268 int ret = record.GetParamValue("PARAM_A", val);
1269 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1270 ASSERT_EQ(val, -1);
1271 }
1272
1273 /**
1274 * @tc.name: TestParseUInt64ValueFromHiSysEventRecord
1275 * @tc.desc: Parse uint64 value from a hisysevent record
1276 * @tc.type: FUNC
1277 * @tc.require: issueI5OA3F
1278 */
1279 HWTEST_F(HiSysEventNativeTest, TestParseUInt64ValueFromHiSysEventRecord, TestSize.Level1)
1280 {
1281 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1282 \"PARAM_A\":3,\"PARAM_B\":\"param b\"}";
1283 HiSysEventRecord record(JSON_STR);
1284 uint64_t val = 0;
1285 int ret = record.GetParamValue("PARAM_A", val);
1286 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1287 ASSERT_EQ(val, 3);
1288 }
1289
1290 /**
1291 * @tc.name: TestParseDoubleValueFromHiSysEventRecord
1292 * @tc.desc: Parse double value from a hisysevent record
1293 * @tc.type: FUNC
1294 * @tc.require: issueI5OA3F
1295 */
1296 HWTEST_F(HiSysEventNativeTest, TestParseDoubleValueFromHiSysEventRecord, TestSize.Level1)
1297 {
1298 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1299 \"PARAM_A\":3.4,\"PARAM_B\":\"param b\"}";
1300 HiSysEventRecord record(JSON_STR);
1301 double val = 0;
1302 int ret = record.GetParamValue("PARAM_A", val);
1303 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1304 ASSERT_LT(abs(val - 3.4), 1e-8);
1305 }
1306
1307 /**
1308 * @tc.name: TestParseStringValueFromHiSysEventRecord
1309 * @tc.desc: Parse string value from a hisysevent record
1310 * @tc.type: FUNC
1311 * @tc.require: issueI5OA3F
1312 */
1313 HWTEST_F(HiSysEventNativeTest, TestParseStringValueFromHiSysEventRecord, TestSize.Level1)
1314 {
1315 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1316 \"PARAM_A\":3.4,\"PARAM_B\":\"param b\"}";
1317 HiSysEventRecord record(JSON_STR);
1318 std::string val;
1319 int ret = record.GetParamValue("PARAM_B", val);
1320 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1321 ASSERT_EQ(val, "param b");
1322 }
1323
1324 /**
1325 * @tc.name: TestParseInt64ArrayFromHiSysEventRecord
1326 * @tc.desc: Parse int64 array from a hisysevent record
1327 * @tc.type: FUNC
1328 * @tc.require: issueI5OA3F
1329 */
1330 HWTEST_F(HiSysEventNativeTest, TestParseInt64ArrayFromHiSysEventRecord, TestSize.Level1)
1331 {
1332 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1333 \"PARAM_A\":3.4,\"PARAM_B\":[-1, 0, 1]}";
1334 HiSysEventRecord record(JSON_STR);
1335 std::vector<int64_t> val;
1336 int ret = record.GetParamValue("PARAM_B", val);
1337 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1338 ASSERT_EQ(val.size(), 3);
1339 ASSERT_EQ(val[0], -1);
1340 }
1341
1342 /**
1343 * @tc.name: TestParseUInt64ArrayFromHiSysEventRecord
1344 * @tc.desc: Parse uint64 array from a hisysevent record
1345 * @tc.type: FUNC
1346 * @tc.require: issueI5OA3F
1347 */
1348 HWTEST_F(HiSysEventNativeTest, TestParseUInt64ArrayFromHiSysEventRecord, TestSize.Level1)
1349 {
1350 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1351 \"PARAM_A\":3.4,\"PARAM_B\":[1, 2, 3]}";
1352 HiSysEventRecord record(JSON_STR);
1353 std::vector<uint64_t> val;
1354 int ret = record.GetParamValue("PARAM_B", val);
1355 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1356 ASSERT_EQ(val.size(), 3);
1357 ASSERT_EQ(val[0], 1);
1358 }
1359
1360 /**
1361 * @tc.name: TestParseDoubleArrayFromHiSysEventRecord
1362 * @tc.desc: Parse double array from a hisysevent record
1363 * @tc.type: FUNC
1364 * @tc.require: issueI5OA3F
1365 */
1366 HWTEST_F(HiSysEventNativeTest, TestParseDoubleArrayFromHiSysEventRecord, TestSize.Level1)
1367 {
1368 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1369 \"PARAM_A\":3.4,\"PARAM_B\":[2.1, 0.0, 3.3]}";
1370 HiSysEventRecord record(JSON_STR);
1371 std::vector<double> val;
1372 int ret = record.GetParamValue("PARAM_B", val);
1373 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1374 ASSERT_EQ(val.size(), 3);
1375 ASSERT_LT(abs(val[0] - 2.1), 1e-8);
1376 }
1377
1378 /**
1379 * @tc.name: TestParseStringArrayFromHiSysEventRecord
1380 * @tc.desc: Parse string array from a hisysevent record
1381 * @tc.type: FUNC
1382 * @tc.require: issueI5OA3F
1383 */
1384 HWTEST_F(HiSysEventNativeTest, TestParseStringArrayFromHiSysEventRecord, TestSize.Level1)
1385 {
1386 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1387 \"PARAM_A\":3.4,\"PARAM_B\":[\"123\", \"456\", \"789\"]}";
1388 HiSysEventRecord record(JSON_STR);
1389 std::vector<std::string> val;
1390 int ret = record.GetParamValue("PARAM_B", val);
1391 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1392 ASSERT_EQ(val.size(), 3);
1393 ASSERT_EQ(val[0], "123");
1394 }
1395
1396 /**
1397 * @tc.name: TestParseParamsFromHiSysEventRecord
1398 * @tc.desc: Parse some inlined parameters from a hisysevent record
1399 * @tc.type: FUNC
1400 * @tc.require: issueI5OA3F
1401 */
1402 HWTEST_F(HiSysEventNativeTest, TestParseParamsFromHiSysEventRecord, TestSize.Level1)
1403 {
1404 constexpr char JSON_STR[] = "{\"domain_\":\"SAMGR\",\"name_\":\"SAMGR_ADD_SYSTEMABILITY_FAIL\",\"type_\":1,\
1405 \"time_\":1502114170549,\"tz_\":\"+0000\",\"pid_\":398,\"tid_\":398,\"uid_\":1099,\"SAID\":1155,\
1406 \"FILE_NAME\":\"libexternal_vpn_service.z.so\",\"level_\":\"CRITICAL\",\"tag_\":\"fault\",\
1407 \"id_\":\"14947264126694503475\",\"traceid_\":243156040590758234, \"spanid_\":11870123,\
1408 \"pspanid_\":28408891,\"trace_flag_\":1,\"info_\":\"\"}";
1409 HiSysEventRecord record(JSON_STR);
1410 auto time = record.GetTime();
1411 ASSERT_GT(time, 0);
1412 auto timeZone = record.GetTimeZone();
1413 ASSERT_EQ(timeZone.size(), 5);
1414 auto pid = record.GetPid();
1415 ASSERT_GT(pid, 0);
1416 auto tid = record.GetTid();
1417 ASSERT_GT(tid, 0);
1418 auto uid = record.GetUid();
1419 ASSERT_GT(uid, 0);
1420 auto traceId = record.GetTraceId();
1421 ASSERT_GE(traceId, 0);
1422 auto spanId = record.GetSpanId();
1423 ASSERT_GE(spanId, 0);
1424 auto pspanId = record.GetPspanId();
1425 ASSERT_GE(pspanId, 0);
1426 auto traceFlag = record.GetTraceFlag();
1427 ASSERT_GE(traceFlag, 0);
1428 auto level = record.GetLevel();
1429 ASSERT_EQ(level, "CRITICAL");
1430 auto tag = record.GetTag();
1431 ASSERT_GE(timeZone.size(), 0);
1432 std::vector<std::string> paramNames;
1433 record.GetParamNames(paramNames);
__anon87309d150302(auto& name) 1434 ASSERT_TRUE(std::any_of(paramNames.begin(), paramNames.end(), [] (auto& name) {
1435 return name == "domain_" || name == "name_" || name == "type_";
1436 }));
1437 }
1438
1439 /**
1440 * @tc.name: TestParseParamsFromUninitializedHiSysEventRecord
1441 * @tc.desc: Parse parameters from a uninitialized hisysevent record
1442 * @tc.type: FUNC
1443 * @tc.require: issueI5OA3F
1444 */
1445 HWTEST_F(HiSysEventNativeTest, TestParseParamsFromUninitializedHiSysEventRecord, TestSize.Level1)
1446 {
1447 constexpr char JSON_STR[] = "";
1448 HiSysEventRecord record(JSON_STR);
1449 auto time = record.GetTime();
1450 ASSERT_EQ(time, 0);
1451 auto timeZone = record.GetTimeZone();
1452 ASSERT_EQ(timeZone.size(), 0);
1453 auto traceId = record.GetTraceId();
1454 ASSERT_EQ(traceId, 0);
1455 }
1456
1457 /**
1458 * @tc.name: TestParseWrongTypeParamsFromUninitializedHiSysEventRecord
1459 * @tc.desc: Parse parameters with unmatched type from a hisysevent record
1460 * @tc.type: FUNC
1461 * @tc.require: issueI5OA3F
1462 */
1463 HWTEST_F(HiSysEventNativeTest, TestParseWrongTypeParamsFromUninitializedHiSysEventRecord, TestSize.Level1)
1464 {
1465 constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1466 \"PARAM_A\":3.4,\"UINT64_T\":18446744073709551610,\"DOUBLE_T\":3.3,\"INT64_T\":9223372036854775800,\
1467 \"PARAM_B\":[\"123\", \"456\", \"789\"],\"PARAM_C\":[]}";
1468 HiSysEventRecord record(JSON_STR);
1469 double num = 0;
1470 auto ret = record.GetParamValue("domain_", num);
1471 ASSERT_EQ(ret, ERR_TYPE_NOT_MATCH);
1472 std::vector<std::string> paramC;
1473 ret = record.GetParamValue("name_", paramC);
1474 ASSERT_EQ(ret, ERR_TYPE_NOT_MATCH);
1475 ret = record.GetParamValue("PARAM_C", paramC);
1476 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1477 uint64_t uint64TypeParam = 0;
1478 ret = record.GetParamValue("UINT64_T", uint64TypeParam);
1479 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1480 int64_t int64TypeParam = 0;
1481 ret = record.GetParamValue("INT64_T", int64TypeParam);
1482 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1483 double doubleTypeParam = 0;
1484 ret = record.GetParamValue("DOUBLE_T", doubleTypeParam);
1485 ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1486 double doubleTypeParam2 = 0;
1487 ret = record.GetParamValue("DOUBLE_T_NOT_EXIST", doubleTypeParam2);
1488 ASSERT_EQ(ret, ERR_KEY_NOT_EXIST);
1489 }
1490
1491 /**
1492 * @tc.name: TestHiSysEventManagerQueryWithDefaultQueryArgument
1493 * @tc.desc: Query with default arugumen
1494 * @tc.type: FUNC
1495 * @tc.require: issueI5L2RV
1496 */
1497 HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerQueryWithDefaultQueryArgument, TestSize.Level1)
1498 {
1499 int eventWroiteCnt = 3;
1500 for (int index = 0; index < eventWroiteCnt; index++) {
1501 HiSysEventWrite(TEST_DOMAIN2, "POWER_KEY", HiSysEvent::EventType::FAULT, "DESC", "in test case");
1502 }
1503 sleep(2);
__anon87309d150402(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents) 1504 auto querier = std::make_shared<Querier>([] (std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents) {
1505 return true;
1506 }, [] (int32_t reason, int32_t total) {
1507 return total > 0;
1508 });
1509 long long defaultTimeStap = -1; // default value
1510 int queryCount = -1; // default value
1511 struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1512 std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1513 std::vector<std::string> eventNames {"POWER_KEY"};
1514 OHOS::HiviewDFX::QueryRule rule("KERNEL_VENDOR", eventNames); // empty domain
1515 queryRules.emplace_back(rule);
1516 auto ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
1517 ASSERT_EQ(ret, OHOS::HiviewDFX::IPC_CALL_SUCCEED);
1518 }
1519
1520