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
46using namespace testing::ext;
47using 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
55namespace {
56constexpr char TEST_DOMAIN[] = "DEMO";
57constexpr char TEST_DOMAIN2[] = "KERNEL_VENDOR";
58int32_t WriteSysEventByMarcoInterface()
59{
60    return HiSysEventWrite(TEST_DOMAIN, "DEMO_EVENTNAME", HiSysEvent::EventType::FAULT,
61        "PARAM_KEY", "PARAM_VAL");
62}
63
64class Watcher : public HiSysEventListener {
65public:
66    Watcher() {}
67    virtual ~Watcher() {}
68
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
79    void OnServiceDied() final
80    {
81        HILOG_DEBUG(LOG_CORE, "OnServiceDied");
82    }
83};
84
85using OnQueryCallback = std::function<bool(std::shared_ptr<std::vector<HiSysEventRecord>>)>;
86using OnCompleteCallback = std::function<bool(int32_t, int32_t)>;
87
88class Querier : public HiSysEventQueryCallback {
89public:
90    explicit Querier(OnQueryCallback onQueryCallback = nullptr, OnCompleteCallback onCompleteCallback = nullptr)
91        : onQueryCallback(onQueryCallback), onCompleteCallback(onCompleteCallback) {}
92    virtual ~Querier() {}
93
94    void OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents) final
95    {
96        if (onQueryCallback != nullptr) {
97            ASSERT_TRUE(onQueryCallback(sysEvents));
98        }
99    }
100
101    void OnComplete(int32_t reason, int32_t total) final
102    {
103        if (onCompleteCallback != nullptr) {
104            ASSERT_TRUE(onCompleteCallback(reason, total));
105        }
106    }
107
108private:
109    OnQueryCallback onQueryCallback;
110    OnCompleteCallback onCompleteCallback;
111};
112}
113
114static 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
123void HiSysEventNativeTest::SetUpTestCase(void)
124{
125}
126
127void HiSysEventNativeTest::TearDownTestCase(void)
128{
129}
130
131void HiSysEventNativeTest::SetUp(void)
132{
133}
134
135void 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 */
145HWTEST_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 */
167HWTEST_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 */
189HWTEST_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 */
207HWTEST_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 */
326HWTEST_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 */
345HWTEST_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 */
364HWTEST_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 */
391HWTEST_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 */
410HWTEST_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 */
429HWTEST_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 */
460HWTEST_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 */
531HWTEST_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 */
551HWTEST_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 */
571HWTEST_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 */
591HWTEST_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 */
623HWTEST_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 */
648HWTEST_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 */
672HWTEST_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 */
697HWTEST_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 */
722HWTEST_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 */
747HWTEST_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 */
772HWTEST_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 */
797HWTEST_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 */
822HWTEST_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 */
847HWTEST_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 */
873HWTEST_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 */
898HWTEST_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 */
923HWTEST_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 */
951HWTEST_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 */
979HWTEST_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 */
998HWTEST_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 */
1023HWTEST_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    auto firstDebugRet = OHOS::HiviewDFX::HiSysEventManager::SetDebugMode(watcher, true);
1032    ret = OHOS::HiviewDFX::HiSysEventManager::SetDebugMode(nullptr, true);
1033    ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1034    if (firstDebugRet == 0 || ret == ERR_DEBUG_MODE_SET_REPEAT) {
1035        ret = OHOS::HiviewDFX::HiSysEventManager::SetDebugMode(watcher, true);
1036        ASSERT_EQ(ret, ERR_DEBUG_MODE_SET_REPEAT);
1037        ret = OHOS::HiviewDFX::HiSysEventManager::SetDebugMode(watcher, false);
1038        ASSERT_EQ(ret, 0);
1039    }
1040    auto newWatcher = std::make_shared<Watcher>();
1041    ret = OHOS::HiviewDFX::HiSysEventManager::SetDebugMode(newWatcher, true);
1042    ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1043}
1044
1045/**
1046 * @tc.name: TestHiSysEventBaseManagerAddAndRemoveListener
1047 * @tc.desc: Add a base listener and then remove it
1048 * @tc.type: FUNC
1049 * @tc.require: issueI5OA3F
1050 */
1051HWTEST_F(HiSysEventNativeTest, TestHiSysEventBaseManagerAddAndRemoveListener, TestSize.Level1)
1052{
1053    auto watcher = std::make_shared<Watcher>();
1054    auto baseWatcher = std::make_shared<HiSysEventBaseListener>(watcher);
1055    OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1056    std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1057    sysRules.emplace_back(listenerRule);
1058    auto ret = OHOS::HiviewDFX::HiSysEventBaseManager::AddListener(nullptr, sysRules);
1059    ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1060    ret = OHOS::HiviewDFX::HiSysEventBaseManager::AddListener(baseWatcher, sysRules);
1061    ASSERT_EQ(ret, 0);
1062    ret = OHOS::HiviewDFX::HiSysEventBaseManager::RemoveListener(nullptr);
1063    ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1064    auto newBaseWatcher = std::make_shared<HiSysEventBaseListener>(watcher);
1065    ret = OHOS::HiviewDFX::HiSysEventBaseManager::RemoveListener(newBaseWatcher);
1066    ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1067    ret = OHOS::HiviewDFX::HiSysEventBaseManager::RemoveListener(baseWatcher);
1068    ASSERT_EQ(ret, 0);
1069}
1070
1071/**
1072 * @tc.name: TestHiSysEventBaseManagerSetDebugMode
1073 * @tc.desc: Enable debug mode and then disable it on base listener
1074 * @tc.type: FUNC
1075 * @tc.require: issueI5KDIG
1076 */
1077HWTEST_F(HiSysEventNativeTest, TestHiSysEventBaseManagerSetDebugMode, TestSize.Level1)
1078{
1079    auto watcher = std::make_shared<Watcher>();
1080    auto baseWatcher = std::make_shared<HiSysEventBaseListener>(watcher);
1081    OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1082    std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1083    sysRules.emplace_back(listenerRule);
1084    auto ret = OHOS::HiviewDFX::HiSysEventBaseManager::AddListener(baseWatcher, sysRules);
1085    ASSERT_EQ(ret, 0);
1086    auto firstDebugRet = OHOS::HiviewDFX::HiSysEventBaseManager::SetDebugMode(baseWatcher, true);
1087    ret = OHOS::HiviewDFX::HiSysEventBaseManager::SetDebugMode(nullptr, true);
1088    ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1089    if (firstDebugRet == 0 || ret == ERR_DEBUG_MODE_SET_REPEAT) {
1090        ret = OHOS::HiviewDFX::HiSysEventBaseManager::SetDebugMode(baseWatcher, true);
1091        ASSERT_EQ(ret, ERR_DEBUG_MODE_SET_REPEAT);
1092        ret = OHOS::HiviewDFX::HiSysEventBaseManager::SetDebugMode(baseWatcher, false);
1093        ASSERT_EQ(ret, 0);
1094    }
1095    auto newBaseWatcher = std::make_shared<HiSysEventBaseListener>(watcher);
1096    ret = OHOS::HiviewDFX::HiSysEventBaseManager::SetDebugMode(newBaseWatcher, true);
1097    ASSERT_EQ(ret, ERR_LISTENER_NOT_EXIST);
1098}
1099
1100/**
1101 * @tc.name: TestHiSysEventBaseManagerQueryEvent
1102 * @tc.desc: Query sys events by base manager
1103 * @tc.type: FUNC
1104 * @tc.require: issueI5KDIG
1105 */
1106HWTEST_F(HiSysEventNativeTest, TestHiSysEventBaseManagerQueryEvent, TestSize.Level1)
1107{
1108    auto querier = std::make_shared<Querier>();
1109    long long defaultTimeStap = -1;
1110    int queryCount = 10;
1111    struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1112    std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1113    auto baseQuerier = std::make_shared<HiSysEventBaseQueryCallback>(querier);
1114    auto ret = OHOS::HiviewDFX::HiSysEventBaseManager::Query(args, queryRules, baseQuerier);
1115    ASSERT_EQ(ret, 0);
1116}
1117
1118/**
1119 * @tc.name: TestHiSysEventManagerAddListenerWithTooManyRules
1120 * @tc.desc: Add listener with more than 20 rules
1121 * @tc.type: FUNC
1122 * @tc.require: issueI5KDIG
1123 */
1124HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerAddListenerWithTooManyRules, TestSize.Level1)
1125{
1126    auto watcher = std::make_shared<Watcher>();
1127    OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1128    int ruleCount = 20;
1129    std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1130    while (ruleCount-- > 0) {
1131        sysRules.emplace_back(listenerRule);
1132    }
1133    auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
1134    ASSERT_EQ(ret, 0);
1135    sysRules.emplace_back(listenerRule);
1136    ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
1137    ASSERT_EQ(ret, OHOS::HiviewDFX::ERR_TOO_MANY_WATCH_RULES);
1138}
1139
1140/**
1141 * @tc.name: TestHiSysEventManagerAddTooManyEventListener
1142 * @tc.desc: Adding more than 30 event listener
1143 * @tc.type: FUNC
1144 * @tc.require: issueI5KDIG
1145 */
1146HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerAddTooManyEventListener, TestSize.Level1)
1147{
1148    OHOS::HiviewDFX::ListenerRule listenerRule("DOMAIN", "EVENT_NAME", "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
1149    std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
1150    sysRules.emplace_back(listenerRule);
1151    int cnt = 30;
1152    int32_t ret = 0;
1153    while (cnt-- > 0) {
1154        ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(std::make_shared<Watcher>(), sysRules);
1155    }
1156    ASSERT_EQ(ret, OHOS::HiviewDFX::ERR_TOO_MANY_WATCHERS);
1157}
1158
1159/**
1160 * @tc.name: TestHiSysEventManagerQueryWithTooManyRules
1161 * @tc.desc: Query with 11 query rules
1162 * @tc.type: FUNC
1163 * @tc.require: issueI5L2RV
1164 */
1165HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerQueryWithTooManyRules, TestSize.Level1)
1166{
1167    auto querier = std::make_shared<Querier>();
1168    long long defaultTimeStap = -1;
1169    int queryCount = 10;
1170    struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1171    std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1172    int rulesCount = 101; // limit to 100
1173    while (rulesCount-- > 0) {
1174        std::vector<std::string> eventNames {"EVENT_NAME"};
1175        OHOS::HiviewDFX::QueryRule rule("DOMAIN", eventNames);
1176        queryRules.emplace_back(rule);
1177    }
1178    auto ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
1179    ASSERT_EQ(ret, OHOS::HiviewDFX::ERR_TOO_MANY_QUERY_RULES);
1180}
1181
1182/**
1183 * @tc.name: TestHiSysEventManagerTooManyConcurrentQueries
1184 * @tc.desc: Query more than 4 times at same time
1185 * @tc.type: FUNC
1186 * @tc.require: issueI5L2RV
1187 */
1188HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerTooManyConcurrentQueries, TestSize.Level1)
1189{
1190    auto querier = std::make_shared<Querier>();
1191    long long defaultTimeStap = -1;
1192    int queryCount = 10;
1193    struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1194    std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1195    int threadCount = 5;
1196    auto ret = OHOS::HiviewDFX::IPC_CALL_SUCCEED;
1197    for (int i = 0; i < threadCount; i++) {
1198        std::thread t([&ret, &args, &queryRules, &querier] () {
1199            ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
1200        });
1201        t.join();
1202    }
1203    ASSERT_TRUE((ret == OHOS::HiviewDFX::ERR_TOO_MANY_CONCURRENT_QUERIES) ||
1204        (ret == OHOS::HiviewDFX::ERR_QUERY_TOO_FREQUENTLY) ||
1205        (ret == OHOS::HiviewDFX::IPC_CALL_SUCCEED));
1206}
1207
1208/**
1209 * @tc.name: TestHiSysEventManagerQueryTooFrequently
1210 * @tc.desc: Query twice in 1 seconds
1211 * @tc.type: FUNC
1212 * @tc.require: issueI5L2RV
1213 */
1214HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerQueryTooFrequently, TestSize.Level1)
1215{
1216    auto querier = std::make_shared<Querier>();
1217    long long defaultTimeStap = -1;
1218    int queryCount = 10;
1219    struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1220    std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1221    const int threshhold = 50;
1222    const int delayDuration = 1; // 1 second
1223    for (int i = 0; i < 2; i++) { // 2 cycles
1224        sleep(delayDuration);
1225        for (int j = 0; j <= threshhold; j++) { // more than 50 queries in 1 second is never allowed
1226            auto ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
1227            ASSERT_TRUE((ret == OHOS::HiviewDFX::ERR_QUERY_TOO_FREQUENTLY) ||
1228                (ret == OHOS::HiviewDFX::IPC_CALL_SUCCEED));
1229        }
1230    }
1231}
1232
1233/**
1234 * @tc.name: TestInitHiSysEventRecordWithIncorrectStr
1235 * @tc.desc: Init a hisysevent record with an incorrect string
1236 * @tc.type: FUNC
1237 * @tc.require: issueI5OA3F
1238 */
1239HWTEST_F(HiSysEventNativeTest, TestInitHiSysEventRecordWithIncorrectStr, TestSize.Level1)
1240{
1241    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1242        \"PARAM_A\":\"param a\",";
1243    HiSysEventRecord record(JSON_STR);
1244    int64_t val = 0;
1245    int ret = record.GetParamValue("type_", val);
1246    ASSERT_EQ(ret, ERR_INIT_FAILED);
1247}
1248
1249/**
1250 * @tc.name: TestParseValueByInvalidKeyFromHiSysEventRecord
1251 * @tc.desc: Parse value by a invalid key from a hisysevent record
1252 * @tc.type: FUNC
1253 * @tc.require: issueI5OA3F
1254 */
1255HWTEST_F(HiSysEventNativeTest, TestParseValueByInvalidKeyFromHiSysEventRecord, TestSize.Level1)
1256{
1257    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1258        \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
1259    HiSysEventRecord record(JSON_STR);
1260    int64_t val = 0;
1261    int ret = record.GetParamValue("XXX", val);
1262    ASSERT_EQ(ret, ERR_KEY_NOT_EXIST);
1263}
1264
1265/**
1266 * @tc.name: TestParseValueByInvalidTypeFromHiSysEventRecord
1267 * @tc.desc: Parse value by a invalid type from a hisysevent record
1268 * @tc.type: FUNC
1269 * @tc.require: issueI5OA3F
1270 */
1271HWTEST_F(HiSysEventNativeTest, TestParseValueByInvalidTypeFromHiSysEventRecord, TestSize.Level1)
1272{
1273    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1274        \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
1275    HiSysEventRecord record(JSON_STR);
1276    int64_t val = 0;
1277    int ret = record.GetParamValue("PARAM_B", val);
1278    ASSERT_EQ(ret, ERR_TYPE_NOT_MATCH);
1279}
1280
1281/**
1282 * @tc.name: TestParseEventDomainNameTypeFromHiSysEventRecord
1283 * @tc.desc: Parse event domain, name and type from a hisysevent record
1284 * @tc.type: FUNC
1285 * @tc.require: issueI5OA3F
1286 */
1287HWTEST_F(HiSysEventNativeTest, TestParseEventDomainNameTypeFromHiSysEventRecord, TestSize.Level1)
1288{
1289    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1290        \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
1291    HiSysEventRecord record(JSON_STR);
1292    ASSERT_EQ(record.GetDomain(), "DEMO");
1293    ASSERT_EQ(record.GetEventName(), "EVENT_NAME_A");
1294    ASSERT_EQ(record.GetEventType(), 4);
1295}
1296
1297/**
1298 * @tc.name: TestParseInt64ValueFromHiSysEventRecord
1299 * @tc.desc: Parse int64 value from a hisysevent record
1300 * @tc.type: FUNC
1301 * @tc.require: issueI5OA3F
1302 */
1303HWTEST_F(HiSysEventNativeTest, TestParseInt64ValueFromHiSysEventRecord, TestSize.Level1)
1304{
1305    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1306        \"PARAM_A\":-1,\"PARAM_B\":\"param b\"}";
1307    HiSysEventRecord record(JSON_STR);
1308    int64_t val = 0;
1309    int ret = record.GetParamValue("PARAM_A", val);
1310    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1311    ASSERT_EQ(val, -1);
1312}
1313
1314/**
1315 * @tc.name: TestParseUInt64ValueFromHiSysEventRecord
1316 * @tc.desc: Parse uint64 value from a hisysevent record
1317 * @tc.type: FUNC
1318 * @tc.require: issueI5OA3F
1319 */
1320HWTEST_F(HiSysEventNativeTest, TestParseUInt64ValueFromHiSysEventRecord, TestSize.Level1)
1321{
1322    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1323        \"PARAM_A\":3,\"PARAM_B\":\"param b\"}";
1324    HiSysEventRecord record(JSON_STR);
1325    uint64_t val = 0;
1326    int ret = record.GetParamValue("PARAM_A", val);
1327    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1328    ASSERT_EQ(val, 3);
1329}
1330
1331/**
1332 * @tc.name: TestParseDoubleValueFromHiSysEventRecord
1333 * @tc.desc: Parse double value from a hisysevent record
1334 * @tc.type: FUNC
1335 * @tc.require: issueI5OA3F
1336 */
1337HWTEST_F(HiSysEventNativeTest, TestParseDoubleValueFromHiSysEventRecord, TestSize.Level1)
1338{
1339    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1340        \"PARAM_A\":3.4,\"PARAM_B\":\"param b\"}";
1341    HiSysEventRecord record(JSON_STR);
1342    double val = 0;
1343    int ret = record.GetParamValue("PARAM_A", val);
1344    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1345    ASSERT_LT(abs(val - 3.4), 1e-8);
1346}
1347
1348/**
1349 * @tc.name: TestParseStringValueFromHiSysEventRecord
1350 * @tc.desc: Parse string value from a hisysevent record
1351 * @tc.type: FUNC
1352 * @tc.require: issueI5OA3F
1353 */
1354HWTEST_F(HiSysEventNativeTest, TestParseStringValueFromHiSysEventRecord, TestSize.Level1)
1355{
1356    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1357        \"PARAM_A\":3.4,\"PARAM_B\":\"param b\"}";
1358    HiSysEventRecord record(JSON_STR);
1359    std::string val;
1360    int ret = record.GetParamValue("PARAM_B", val);
1361    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1362    ASSERT_EQ(val, "param b");
1363}
1364
1365/**
1366 * @tc.name: TestParseInt64ArrayFromHiSysEventRecord
1367 * @tc.desc: Parse int64 array from a hisysevent record
1368 * @tc.type: FUNC
1369 * @tc.require: issueI5OA3F
1370 */
1371HWTEST_F(HiSysEventNativeTest, TestParseInt64ArrayFromHiSysEventRecord, TestSize.Level1)
1372{
1373    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1374        \"PARAM_A\":3.4,\"PARAM_B\":[-1, 0, 1]}";
1375    HiSysEventRecord record(JSON_STR);
1376    std::vector<int64_t> val;
1377    int ret = record.GetParamValue("PARAM_B", val);
1378    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1379    ASSERT_EQ(val.size(), 3);
1380    ASSERT_EQ(val[0], -1);
1381}
1382
1383/**
1384 * @tc.name: TestParseUInt64ArrayFromHiSysEventRecord
1385 * @tc.desc: Parse uint64 array from a hisysevent record
1386 * @tc.type: FUNC
1387 * @tc.require: issueI5OA3F
1388 */
1389HWTEST_F(HiSysEventNativeTest, TestParseUInt64ArrayFromHiSysEventRecord, TestSize.Level1)
1390{
1391    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1392        \"PARAM_A\":3.4,\"PARAM_B\":[1, 2, 3]}";
1393    HiSysEventRecord record(JSON_STR);
1394    std::vector<uint64_t> val;
1395    int ret = record.GetParamValue("PARAM_B", val);
1396    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1397    ASSERT_EQ(val.size(), 3);
1398    ASSERT_EQ(val[0], 1);
1399}
1400
1401/**
1402 * @tc.name: TestParseDoubleArrayFromHiSysEventRecord
1403 * @tc.desc: Parse double array from a hisysevent record
1404 * @tc.type: FUNC
1405 * @tc.require: issueI5OA3F
1406 */
1407HWTEST_F(HiSysEventNativeTest, TestParseDoubleArrayFromHiSysEventRecord, TestSize.Level1)
1408{
1409    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1410        \"PARAM_A\":3.4,\"PARAM_B\":[2.1, 0.0, 3.3]}";
1411    HiSysEventRecord record(JSON_STR);
1412    std::vector<double> val;
1413    int ret = record.GetParamValue("PARAM_B", val);
1414    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1415    ASSERT_EQ(val.size(), 3);
1416    ASSERT_LT(abs(val[0] - 2.1), 1e-8);
1417}
1418
1419/**
1420 * @tc.name: TestParseStringArrayFromHiSysEventRecord
1421 * @tc.desc: Parse string array from a hisysevent record
1422 * @tc.type: FUNC
1423 * @tc.require: issueI5OA3F
1424 */
1425HWTEST_F(HiSysEventNativeTest, TestParseStringArrayFromHiSysEventRecord, TestSize.Level1)
1426{
1427    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1428        \"PARAM_A\":3.4,\"PARAM_B\":[\"123\", \"456\", \"789\"]}";
1429    HiSysEventRecord record(JSON_STR);
1430    std::vector<std::string> val;
1431    int ret = record.GetParamValue("PARAM_B", val);
1432    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1433    ASSERT_EQ(val.size(), 3);
1434    ASSERT_EQ(val[0], "123");
1435}
1436
1437/**
1438 * @tc.name: TestParseParamsFromHiSysEventRecord
1439 * @tc.desc: Parse some inlined parameters from a hisysevent record
1440 * @tc.type: FUNC
1441 * @tc.require: issueI5OA3F
1442 */
1443HWTEST_F(HiSysEventNativeTest, TestParseParamsFromHiSysEventRecord, TestSize.Level1)
1444{
1445    constexpr char JSON_STR[] = "{\"domain_\":\"SAMGR\",\"name_\":\"SAMGR_ADD_SYSTEMABILITY_FAIL\",\"type_\":1,\
1446        \"time_\":1502114170549,\"tz_\":\"+0000\",\"pid_\":398,\"tid_\":398,\"uid_\":1099,\"SAID\":1155,\
1447        \"FILE_NAME\":\"libexternal_vpn_service.z.so\",\"level_\":\"CRITICAL\",\"tag_\":\"fault\",\
1448        \"id_\":\"14947264126694503475\",\"traceid_\":243156040590758234, \"spanid_\":11870123,\
1449        \"pspanid_\":28408891,\"trace_flag_\":1,\"info_\":\"\"}";
1450    HiSysEventRecord record(JSON_STR);
1451    auto time = record.GetTime();
1452    ASSERT_GT(time, 0);
1453    auto timeZone = record.GetTimeZone();
1454    ASSERT_EQ(timeZone.size(), 5);
1455    auto pid = record.GetPid();
1456    ASSERT_GT(pid, 0);
1457    auto tid = record.GetTid();
1458    ASSERT_GT(tid, 0);
1459    auto uid = record.GetUid();
1460    ASSERT_GT(uid, 0);
1461    auto traceId = record.GetTraceId();
1462    ASSERT_GE(traceId, 0);
1463    auto spanId = record.GetSpanId();
1464    ASSERT_GE(spanId, 0);
1465    auto pspanId = record.GetPspanId();
1466    ASSERT_GE(pspanId, 0);
1467    auto traceFlag = record.GetTraceFlag();
1468    ASSERT_GE(traceFlag, 0);
1469    auto level = record.GetLevel();
1470    ASSERT_EQ(level, "CRITICAL");
1471    auto tag = record.GetTag();
1472    ASSERT_GE(timeZone.size(), 0);
1473    std::vector<std::string> paramNames;
1474    record.GetParamNames(paramNames);
1475    ASSERT_TRUE(std::any_of(paramNames.begin(), paramNames.end(), [] (auto& name) {
1476        return name == "domain_" || name == "name_" || name == "type_";
1477    }));
1478}
1479
1480/**
1481 * @tc.name: TestParseParamsFromUninitializedHiSysEventRecord
1482 * @tc.desc: Parse parameters from a uninitialized hisysevent record
1483 * @tc.type: FUNC
1484 * @tc.require: issueI5OA3F
1485 */
1486HWTEST_F(HiSysEventNativeTest, TestParseParamsFromUninitializedHiSysEventRecord, TestSize.Level1)
1487{
1488    constexpr char JSON_STR[] = "";
1489    HiSysEventRecord record(JSON_STR);
1490    auto time = record.GetTime();
1491    ASSERT_EQ(time, 0);
1492    auto timeZone = record.GetTimeZone();
1493    ASSERT_EQ(timeZone.size(), 0);
1494    auto traceId = record.GetTraceId();
1495    ASSERT_EQ(traceId, 0);
1496}
1497
1498/**
1499 * @tc.name: TestParseWrongTypeParamsFromUninitializedHiSysEventRecord
1500 * @tc.desc: Parse parameters with unmatched type from a hisysevent record
1501 * @tc.type: FUNC
1502 * @tc.require: issueI5OA3F
1503 */
1504HWTEST_F(HiSysEventNativeTest, TestParseWrongTypeParamsFromUninitializedHiSysEventRecord, TestSize.Level1)
1505{
1506    constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
1507        \"PARAM_A\":3.4,\"UINT64_T\":18446744073709551610,\"DOUBLE_T\":3.3,\"INT64_T\":9223372036854775800,\
1508        \"PARAM_B\":[\"123\", \"456\", \"789\"],\"PARAM_C\":[]}";
1509    HiSysEventRecord record(JSON_STR);
1510    double num = 0;
1511    auto ret = record.GetParamValue("domain_", num);
1512    ASSERT_EQ(ret, ERR_TYPE_NOT_MATCH);
1513    std::vector<std::string> paramC;
1514    ret = record.GetParamValue("name_", paramC);
1515    ASSERT_EQ(ret, ERR_TYPE_NOT_MATCH);
1516    ret = record.GetParamValue("PARAM_C", paramC);
1517    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1518    uint64_t uint64TypeParam = 0;
1519    ret = record.GetParamValue("UINT64_T", uint64TypeParam);
1520    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1521    int64_t int64TypeParam = 0;
1522    ret = record.GetParamValue("INT64_T", int64TypeParam);
1523    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1524    double doubleTypeParam = 0;
1525    ret = record.GetParamValue("DOUBLE_T", doubleTypeParam);
1526    ASSERT_EQ(ret, VALUE_PARSED_SUCCEED);
1527    double doubleTypeParam2 = 0;
1528    ret = record.GetParamValue("DOUBLE_T_NOT_EXIST", doubleTypeParam2);
1529    ASSERT_EQ(ret, ERR_KEY_NOT_EXIST);
1530}
1531
1532/**
1533 * @tc.name: TestHiSysEventManagerQueryWithDefaultQueryArgument
1534 * @tc.desc: Query with default arugumen
1535 * @tc.type: FUNC
1536 * @tc.require: issueI5L2RV
1537 */
1538HWTEST_F(HiSysEventNativeTest, TestHiSysEventManagerQueryWithDefaultQueryArgument, TestSize.Level1)
1539{
1540    int eventWroiteCnt = 3;
1541    for (int index = 0; index < eventWroiteCnt; index++) {
1542        HiSysEventWrite(TEST_DOMAIN2, "POWER_KEY", HiSysEvent::EventType::FAULT, "DESC", "in test case");
1543    }
1544    sleep(2);
1545    auto querier = std::make_shared<Querier>([] (std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents) {
1546        return true;
1547    }, [] (int32_t reason, int32_t total) {
1548        return total > 0;
1549    });
1550    long long defaultTimeStap = -1; // default value
1551    int queryCount = -1; // default value
1552    struct OHOS::HiviewDFX::QueryArg args(defaultTimeStap, defaultTimeStap, queryCount);
1553    std::vector<OHOS::HiviewDFX::QueryRule> queryRules;
1554    std::vector<std::string> eventNames {"POWER_KEY"};
1555    OHOS::HiviewDFX::QueryRule rule("KERNEL_VENDOR", eventNames); // empty domain
1556    queryRules.emplace_back(rule);
1557    auto ret = OHOS::HiviewDFX::HiSysEventManager::Query(args, queryRules, querier);
1558    ASSERT_EQ(ret, OHOS::HiviewDFX::IPC_CALL_SUCCEED);
1559}
1560
1561