1 /*
2  * Copyright (c) 2021 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_logger_plugin_test.h"
16 
17 #include <ctime>
18 #include <fstream>
19 #include <iostream>
20 #include <memory>
21 
22 #include "common_utils.h"
23 #include "file_util.h"
24 #include "time_util.h"
25 
26 #include "event_logger.h"
27 #include "hiview_platform.h"
28 #include "sysevent_source.h"
29 using namespace testing::ext;
30 using namespace OHOS::HiviewDFX;
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 SysEventSource source;
SetUp()35 void EventloggerPluginTest::SetUp()
36 {
37     printf("SetUp.\n");
38 }
39 
TearDown()40 void EventloggerPluginTest::TearDown()
41 {
42     printf("TearDown.\n");
43 }
44 
SetUpTestCase()45 void EventloggerPluginTest::SetUpTestCase()
46 {
47     HiviewPlatform platform;
48     source.SetHiviewContext(&platform);
49     source.OnLoad();
50 }
51 
TearDownTestCase()52 void EventloggerPluginTest::TearDownTestCase()
53 {
54     source.OnUnload();
55     printf("TearDownTestCase.\n");
56 }
57 
58 /**
59  * @tc.name: EventloggerPluginTest001
60  * @tc.desc: parse a correct config file and check result
61  * @tc.type: FUNC
62  * @tc.require: AR000FT62O
63  */
64 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest001, TestSize.Level3)
65 {
66     EventLogger eventLogger;
67     std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
68     loop->StartLoop();
69     eventLogger.BindWorkLoop(loop);
70     std::shared_ptr<Event> event = nullptr;
71     ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
72 }
73 
74 /**
75  * @tc.name: EventloggerPluginTest002
76  * @tc.desc: parse a correct config file and check result
77  */
78 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest002, TestSize.Level3)
79 {
80     EventLogger eventLogger;
81     std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
82     loop->StartLoop();
83     eventLogger.BindWorkLoop(loop);
84     constexpr int eventMaxId = 1000001;
85     std::shared_ptr<Event> event = std::make_shared<Event>("Eventlogger002");
86     event->eventId_ = eventMaxId;
87     ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
88 }
89 
90 /**
91  * @tc.name: EventloggerPluginTest003
92  * @tc.desc: parse a correct config file and check result
93  */
94 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest003, TestSize.Level3)
95 {
96     EventLogger eventLogger;
97     std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
98     loop->StartLoop();
99     eventLogger.BindWorkLoop(loop);
100     std::shared_ptr<Event> event = std::make_shared<Event>("Eventlogger003");
101     event->eventId_ = 0;
102     event->domain_ = "FRAMEWORK";
103     event->eventName_ = "SERVICE_BLOCK2";
104     ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
105 }
106 
107 /**
108  * @tc.name: EventloggerPluginTest004
109  * @tc.desc: parse a correct config file and check result
110  */
111 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest004, TestSize.Level3)
112 {
113     EventLogger eventLogger;
114     std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
115     loop->StartLoop();
116     eventLogger.BindWorkLoop(loop);
117     auto jsonStr = "{\"domain_\":\"RELIABILITY\"}";
118     std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("Eventlogger004", nullptr, jsonStr);
119     event->eventId_ = 0;
120     event->domain_ = "FRAMEWORK";
121     event->eventName_ = "SERVICE_BLOCK";
122     event->SetEventValue("PID", 0);
123     ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
124 }
125 
126 /**
127  * @tc.name: EventloggerPluginTest005
128  * @tc.desc: parse a correct config file and check result
129  */
130 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest005, TestSize.Level3)
131 {
132     EventLogger eventLogger;
133     std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
134     loop->StartLoop();
135     eventLogger.BindWorkLoop(loop);
136     auto jsonStr = "{\"domain_\":\"RELIABILITY\"}";
137     std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("Eventlogger005", nullptr, jsonStr);
138     event->eventId_ = 0;
139     event->domain_ = "FRAMEWORK";
140     event->eventName_ = "SERVICE_BLOCK";
141     event->SetEventValue("PID", CommonUtils::GetPidByName("foundation"));
142     ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
143 }
144 
145 /**
146  * @tc.name: EventloggerPluginTest006
147  * @tc.desc: parse a correct config file and check result
148  */
149 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest006, TestSize.Level3)
150 {
151     EventLogger eventLogger;
152     std::shared_ptr<Event> event = nullptr;
153     ASSERT_EQ(eventLogger.OnEvent(event), false);
154 }
155 } // namespace HiviewDFX
156 } // namespace OHOS
157