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_log_action.h"
27 #include "event_logger.h"
28 #include "hiview_platform.h"
29 using namespace testing::ext;
30 using namespace OHOS::HiviewDFX;
31
SetUp()32 void EventloggerPluginTest::SetUp()
33 {
34 printf("SetUp.\n");
35 }
36
TearDown()37 void EventloggerPluginTest::TearDown()
38 {
39 printf("TearDown.\n");
40 }
41
SetUpTestCase()42 void EventloggerPluginTest::SetUpTestCase()
43 {
44 OHOS::HiviewDFX::HiviewPlatform &platform = HiviewPlatform::GetInstance();
45 std::string defaultDir = "/data/test/test_data/hiview_platform_config";
46 if (!platform.InitEnvironment(defaultDir)) {
47 std::cout << "fail to init environment" << std::endl;
48 } else {
49 std::cout << "init environment successful" << std::endl;
50 }
51 printf("SetUpTestCase.\n");
52 }
53
TearDownTestCase()54 void EventloggerPluginTest::TearDownTestCase()
55 {
56 printf("TearDownTestCase.\n");
57 }
58
59 /**
60 * @tc.name: EventloggerPluginTest001
61 * @tc.desc: parse a correct config file and check result
62 * @tc.type: FUNC
63 * @tc.require: AR000FT62O
64 */
65 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest001, TestSize.Level3)
66 {
67 EventLogger eventLogger;
68 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
69 loop->StartLoop();
70 eventLogger.BindWorkLoop(loop);
71 std::shared_ptr<Event> event = nullptr;
72 ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
73 }
74
75 /**
76 * @tc.name: EventloggerPluginTest002
77 * @tc.desc: parse a correct config file and check result
78 */
79 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest002, TestSize.Level3)
80 {
81 EventLogger eventLogger;
82 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
83 loop->StartLoop();
84 eventLogger.BindWorkLoop(loop);
85 constexpr int eventMaxId = 1000001;
86 std::shared_ptr<Event> event = std::make_shared<Event>("Eventlogger002");
87 event->eventId_ = eventMaxId;
88 ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
89 }
90
91 /**
92 * @tc.name: EventloggerPluginTest003
93 * @tc.desc: parse a correct config file and check result
94 */
95 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest003, TestSize.Level3)
96 {
97 EventLogger eventLogger;
98 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
99 loop->StartLoop();
100 eventLogger.BindWorkLoop(loop);
101 std::shared_ptr<Event> event = std::make_shared<Event>("Eventlogger003");
102 event->eventId_ = 0;
103 event->domain_ = "FRAMEWORK";
104 event->eventName_ = "SERVICE_BLOCK2";
105 ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
106 }
107
108 /**
109 * @tc.name: EventloggerPluginTest004
110 * @tc.desc: parse a correct config file and check result
111 */
112 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest004, TestSize.Level3)
113 {
114 EventLogger eventLogger;
115 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
116 loop->StartLoop();
117 eventLogger.BindWorkLoop(loop);
118 auto jsonStr = "{\"domain_\":\"RELIABILITY\"}";
119 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("Eventlogger004", nullptr, jsonStr);
120 event->eventId_ = 0;
121 event->domain_ = "FRAMEWORK";
122 event->eventName_ = "SERVICE_BLOCK";
123 event->SetEventValue("PID", 0);
124 ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
125 }
126
127 /**
128 * @tc.name: EventloggerPluginTest005
129 * @tc.desc: parse a correct config file and check result
130 */
131 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest005, TestSize.Level3)
132 {
133 EventLogger eventLogger;
134 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
135 loop->StartLoop();
136 eventLogger.BindWorkLoop(loop);
137 auto jsonStr = "{\"domain_\":\"RELIABILITY\"}";
138 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("Eventlogger005", nullptr, jsonStr);
139 event->eventId_ = 0;
140 event->domain_ = "FRAMEWORK";
141 event->eventName_ = "SERVICE_BLOCK";
142 event->SetEventValue("PID", CommonUtils::GetPidByName("foundation"));
143 ASSERT_EQ(eventLogger.IsInterestedPipelineEvent(event), false);
144 }
145
146 /**
147 * @tc.name: EventloggerPluginTest006
148 * @tc.desc: parse a correct config file and check result
149 */
150 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest006, TestSize.Level3)
151 {
152 EventLogger eventLogger;
153 std::shared_ptr<Event> event = nullptr;
154 ASSERT_EQ(eventLogger.OnEvent(event), false);
155 }
156
157 /**
158 * @tc.name: EventloggerPluginTest007
159 * @tc.desc: parse a correct config file and check result
160 */
161 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest007, TestSize.Level3)
162 {
163 std::shared_ptr<EventLogger> eventLogger = std::make_shared<EventLogger>();
164 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
165 loop->StartLoop();
166 eventLogger->BindWorkLoop(loop);
167 eventLogger->OnLoad();
168 eventLogger->SetHiviewContext(&HiviewPlatform::GetInstance());
169 auto jsonStr = "{\"domain_\":\"RELIABILITY\"}";
170 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("Eventlogger007", nullptr, jsonStr);
171 event->eventId_ = 0;
172 event->domain_ = "FRAMEWORK";
173 event->eventName_ = "SERVICE_BLOCK";
174 event->happenTime_ = TimeUtil::GetMilliseconds();
175 event->SetEventValue("PID", CommonUtils::GetPidByName("foundation"));
176 event->SetValue("eventLog_action", "aaa");
177 event->SetValue("eventLog_interval", 0);
178 std::shared_ptr<Event> onEvent = std::static_pointer_cast<Event>(event);
179 ASSERT_EQ(eventLogger->OnEvent(onEvent), true);
180 std::this_thread::sleep_for(std::chrono::seconds(3)); // 3: 3second;
181 }
182
183 /**
184 * @tc.name: EventloggerPluginTest008
185 * @tc.desc: parse a correct config file and check result
186 */
187 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest008, TestSize.Level3)
188 {
189 std::shared_ptr<EventLogger> eventLogger = std::make_shared<EventLogger>();
190 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
191 loop->StartLoop();
192 eventLogger->BindWorkLoop(loop);
193 eventLogger->OnLoad();
194 eventLogger->SetHiviewContext(&HiviewPlatform::GetInstance());
195 auto jsonStr = "{\"domain_\":\"RELIABILITY\"}";
196 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("Eventlogger008", nullptr, jsonStr);
197 event->eventId_ = 0;
198 event->domain_ = "FRAMEWORK";
199 event->eventName_ = "SERVICE_BLOCK";
200 event->happenTime_ = TimeUtil::GetMilliseconds();
201 event->SetEventValue("PID", CommonUtils::GetPidByName("foundation"));
202 event->SetValue("eventLog_action", "s,pb:0,cmd:c,cmd:m");
203 event->SetValue("eventLog_interval", 0);
204 std::shared_ptr<Event> onEvent = std::static_pointer_cast<Event>(event);
205 ASSERT_EQ(eventLogger->OnEvent(onEvent), true);
206 std::this_thread::sleep_for(std::chrono::seconds(3)); // 3: 3second;
207 }
208
209 /**
210 * @tc.name: EventloggerPluginTest009
211 * @tc.desc: parse a correct config file and check result
212 */
213 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest009, TestSize.Level3)
214 {
215 std::shared_ptr<EventLogger> eventLogger = std::make_shared<EventLogger>();
216 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
217 loop->StartLoop();
218 eventLogger->BindWorkLoop(loop);
219 eventLogger->OnLoad();
220 eventLogger->SetHiviewContext(&HiviewPlatform::GetInstance());
221 auto jsonStr = "{\"domain_\":\"RELIABILITY\"}";
222 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("Eventlogger009", nullptr, jsonStr);
223 event->eventId_ = 0;
224 event->domain_ = "FRAMEWORK";
225 event->eventName_ = "SERVICE_BLOCK";
226 event->happenTime_ = TimeUtil::GetMilliseconds();
227 event->SetEventValue("PID", 0);
228 event->SetValue("eventLog_action", "s,pb:0,cmd:c,cmd:m");
229 event->SetValue("eventLog_interval", 0);
230 std::shared_ptr<Event> onEvent = event;
231 ASSERT_EQ(eventLogger->OnEvent(onEvent), true);
232 std::this_thread::sleep_for(std::chrono::seconds(3)); // 3: 3second;
233 }
234
235 /**
236 * @tc.name: EventloggerPluginTest010
237 * @tc.desc: parse a correct config file and check result
238 */
239 HWTEST_F(EventloggerPluginTest, EventloggerPluginTest010, TestSize.Level3)
240 {
241 std::shared_ptr<EventLogger> eventLogger = std::make_shared<EventLogger>();
242 std::shared_ptr<EventLoop> loop = std::make_shared<EventLoop>("eventLoop");
243 loop->StartLoop();
244 eventLogger->BindWorkLoop(loop);
245 eventLogger->OnLoad();
246 eventLogger->SetHiviewContext(&HiviewPlatform::GetInstance());
247 auto jsonStr = "{\"domain_\":\"RELIABILITY\"}";
248 std::shared_ptr<SysEvent> event = std::make_shared<SysEvent>("Eventlogger009", nullptr, jsonStr);
249 event->eventId_ = 0;
250 event->domain_ = "FRAMEWORK";
251 event->eventName_ = "SERVICE_BLOCK";
252 event->happenTime_ = TimeUtil::GetMilliseconds();
253
254 pid_t pid = 19990; // Non-existent pid
255 while (CommonUtils::IsSpecificCmdExist("/proc/" + std::to_string(pid) + "/status")) {
256 ++pid;
257 }
258
259 event->SetEventValue("PID", pid);
260 event->SetValue("eventLog_action", "s,pb:0,cmd:c,cmd:m");
261 event->SetValue("eventLog_interval", 0);
262 std::shared_ptr<Event> onEvent = event;
263 ASSERT_EQ(eventLogger->IsInterestedPipelineEvent(onEvent), false);
264 ASSERT_EQ(eventLogger->OnEvent(onEvent), true);
265 std::this_thread::sleep_for(std::chrono::seconds(3)); // 3: 3second;
266 }
267