1 /*
2 * Copyright (c) 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 #include "event_json_parser_test.h"
16
17 #include <iostream>
18
19 #include "event_json_parser.h"
20 #include "flat_json_parser.h"
21 #include "hiview_platform.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
SetUpTestCase()25 void EventJsonParserTest::SetUpTestCase() {}
26
TearDownTestCase()27 void EventJsonParserTest::TearDownTestCase() {}
28
SetUp()29 void EventJsonParserTest::SetUp()
30 {
31 HiviewPlatform &platform = HiviewPlatform::GetInstance();
32 if (!platform.InitEnvironment()) {
33 std::cout << "fail to init environment" << std::endl;
34 } else {
35 std::cout << "init environment successful" << std::endl;
36 }
37 }
38
TearDown()39 void EventJsonParserTest::TearDown() {}
40
41 /**
42 * @tc.name: EventJsonParserTest001
43 * @tc.desc: parse a event and check Json info
44 * @tc.type: FUNC
45 * @tc.require: issueI62WJT
46 */
47 HWTEST_F(EventJsonParserTest, EventJsonParserTest001, testing::ext::TestSize.Level0)
48 {
49 printf("start EventJsonParserTest001\n");
50 std::string defFile = "/system/etc/hiview/hisysevent.def";
51 std::vector<std::string> defFiles;
52 defFiles.emplace_back(defFile);
53 auto sysEventParser = std::make_unique<EventJsonParser>(defFiles);
54
55 std::shared_ptr<SysEvent> sysEvent = nullptr;
56 ASSERT_FALSE(sysEventParser->HandleEventJson(sysEvent));
57 constexpr char invalidJsonStr[] = "{\"domain_\":\"HIVIEWDFX\",\"type_\":4,\
58 \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
59 sysEvent = std::make_shared<SysEvent>("SysEventService", nullptr, invalidJsonStr);
60 ASSERT_FALSE(sysEventParser->HandleEventJson(sysEvent));
61 constexpr char jsonStr[] = "{\"domain_\":\"HIVIEWDFX\",\"name_\":\"PLUGIN_LOAD\",\"type_\":4,\
62 \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
63 sysEvent = std::make_shared<SysEvent>("SysEventService", nullptr, jsonStr);
64
65 ASSERT_TRUE(sysEventParser->HandleEventJson(sysEvent));
66 ASSERT_TRUE(sysEventParser->GetTagByDomainAndName("abc", "abc") == "");
67 ASSERT_TRUE(sysEventParser->GetTagByDomainAndName("DEMO", "abc") == "");
68 ASSERT_TRUE(sysEventParser->GetTypeByDomainAndName("DEMO", "abc") == 0);
69
70 DuplicateIdFilter filter;
71 ASSERT_FALSE(filter.IsDuplicateEvent(0));
72 ASSERT_FALSE(filter.IsDuplicateEvent(1));
73 ASSERT_TRUE(filter.IsDuplicateEvent(1));
74
75 FlatJsonParser parser("{\"level\":[abc]}");
76 std::cout << "FlatJsonParser:" << parser.Print() << std::endl;
77 ASSERT_TRUE(parser.Print() == "{\"level\":[abc]}");
78 }
79 } // namespace HiviewDFX
80 } // namespace OHOS
81