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