1 /*
2 * Copyright (c) 2022 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 "freeze_detector_test.h"
16
17 #include <fstream>
18 #include <iostream>
19 #include <memory>
20 #include <set>
21 #include <unistd.h>
22
23 #include "event.h"
24 #include "file_util.h"
25 #include "time_util.h"
26
27 #include "plugin_proxy.h"
28 #include "hiview_platform.h"
29 #include "sys_event.h"
30
31 #include "freeze_common.h"
32 #include "rule_cluster.h"
33
34 using namespace testing::ext;
35 namespace OHOS {
36 namespace HiviewDFX {
37 namespace {
makeEvent(const std::string & name,const std::string & domain,const std::string & eventName,const std::string & packageName,const std::string & logPath)38 std::shared_ptr<SysEvent> makeEvent(const std::string& name,
39 const std::string& domain,
40 const std::string& eventName,
41 const std::string& packageName,
42 const std::string& logPath)
43 {
44 auto time = TimeUtil::GetMilliseconds();
45 std::string str = "******************\n";
46 str += "this is test " + eventName + " at " + std::to_string(time) + "\n";
47 str += "******************\n";
48 FileUtil::SaveStringToFile(logPath, str);
49
50 auto jsonStr = "{\"domain_\":\"" + domain + "\"}";
51 auto sysEvent = std::make_shared<SysEvent>(name, nullptr, jsonStr);
52 sysEvent->SetEventValue("name_", eventName);
53 sysEvent->SetEventValue("type_", 1);
54 sysEvent->SetEventValue("time_", time);
55 sysEvent->SetEventValue("pid_", getpid());
56 sysEvent->SetEventValue("tid_", gettid());
57 sysEvent->SetEventValue("uid_", getuid());
58 sysEvent->SetEventValue("tz_", TimeUtil::GetTimeZone());
59 sysEvent->SetEventValue("PID", getpid());
60 sysEvent->SetEventValue("UID", getuid());
61 sysEvent->SetEventValue("MSG", "test " + eventName + " event");
62 sysEvent->SetEventValue("PACKAGE_NAME", packageName);
63 sysEvent->SetEventValue("PROCESS_NAME", packageName);
64
65 std::string tmpStr = R"~(logPath:)~" + logPath;
66 sysEvent->SetEventValue("info_", tmpStr);
67 return sysEvent;
68 }
69
GetFreezeDectorTestFile(const std::string & eventName,const std::string & packageName,uint64_t time)70 bool GetFreezeDectorTestFile(const std::string& eventName,
71 const std::string& packageName,
72 uint64_t time)
73 {
74 int count = 0;
75 std::string decLogPath = "";
76 while (count < 10) { // 10: 最大等待10s
77 sleep(1);
78 std::vector<std::string> files;
79 FileUtil::GetDirFiles("/data/log/faultlog/", files);
80 ++count;
81 for (auto& i : files) {
82 if (i.find(packageName) == std::string::npos) {
83 continue;
84 }
85 std::string content;
86 FileUtil::LoadStringFromFile(i, content);
87 if (content.find(std::to_string(time)) == std::string::npos) {
88 printf("time is not match.\n");
89 FileUtil::RemoveFile(i);
90 continue;
91 }
92
93 if (content.find(eventName) == std::string::npos) {
94 printf("Not %s.\n", eventName.c_str());
95 FileUtil::RemoveFile(i);
96 continue;
97 }
98
99 if (content.find(packageName) == std::string::npos) {
100 printf("Not %s.\n", packageName.c_str());
101 FileUtil::RemoveFile(i);
102 continue;
103 }
104 decLogPath = i;
105 break;
106 }
107
108 if (decLogPath != "") {
109 break;
110 }
111 }
112
113 if (decLogPath == "") {
114 printf("Not find files.\n");
115 return false;
116 }
117
118 FileUtil::RemoveFile(decLogPath);
119 return true;
120 }
121 }
122
SetUp()123 void FreezeDetectorTest::SetUp()
124 {
125 /**
126 * @tc.setup: create work directories
127 */
128 printf("SetUp.\n");
129 }
SetUpTestCase()130 void FreezeDetectorTest::SetUpTestCase()
131 {
132 /**
133 * @tc.setup: all first
134 */
135 printf("SetUpTestCase.\n");
136 HiviewPlatform &platform = HiviewPlatform::GetInstance();
137 if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
138 printf("Fail to init environment.\n");
139 }
140 }
141
TearDownTestCase()142 void FreezeDetectorTest::TearDownTestCase()
143 {
144 /**
145 * @tc.setup: all end
146 */
147 printf("TearDownTestCase.\n");
148 }
149
TearDown()150 void FreezeDetectorTest::TearDown()
151 {
152 /**
153 * @tc.teardown: destroy the event loop we have created
154 */
155 printf("TearDown.\n");
156 }
157
158 /**
159 * @tc.name: FreezeDetectorProxyTest001
160 * @tc.desc: FreezeDetector proxy
161 * @tc.type: FUNC
162 * @tc.require: AR000H3T5D
163 */
164 HWTEST_F(FreezeDetectorTest, FreezeDetectorProxyTest001, TestSize.Level3)
165 {
166 HiviewPlatform &platform = HiviewPlatform::GetInstance();
167 std::shared_ptr<Plugin> plugin = platform.GetPluginByName("FreezeDetectorPlugin");
168 if (plugin == nullptr) {
169 printf("Get FreezeDetectorPlugin, failed\n");
170 FAIL();
171 }
172
173 std::shared_ptr<PluginProxy> pluginProxy = std::static_pointer_cast<PluginProxy>(plugin);
174
175 if (pluginProxy->HoldInstance()) {
176 printf("FreezeDetectorPlugin, HoldInstance is true\n");
177 FAIL();
178 }
179
180 auto jsonStr = "{\"domain_\":\"TEST_INPUT\"}";
181 auto sysEvent = std::make_shared<SysEvent>("FreezeDetectorProxyTest001", nullptr, jsonStr);
182 sysEvent->SetEventValue("name_", "TEST_INPUT");
183 sysEvent->SetEventValue("type_", 1);
184 sysEvent->SetEventValue("time_", TimeUtil::GetMilliseconds());
185 sysEvent->SetEventValue("pid_", getpid());
186 sysEvent->SetEventValue("tid_", gettid());
187 sysEvent->SetEventValue("uid_", getuid());
188 sysEvent->SetEventValue("tz_", TimeUtil::GetTimeZone());
189 sysEvent->SetEventValue("PID", getpid());
190 sysEvent->SetEventValue("UID", getuid());
191 sysEvent->SetEventValue("MSG", "test TEST_INPUT event");
192 sysEvent->SetEventValue("PACKAGE_NAME", "FreezeDetectorProxyTest001");
193 sysEvent->SetEventValue("PROCESS_NAME", "FreezeDetectorProxyTest001");
194 sysEvent->SetEventValue("info_", "");
195
196 std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
197 plugin->OnEventListeningCallback(*(event.get()));
198
199 sleep(1);
200 if (!pluginProxy->HoldInstance()) {
201 printf("FreezeDetectorPlugin, HoldInstance is false\n");
202 FAIL();
203 }
204 }
205
206 /**
207 * @tc.name: FreezeDetectorTest001
208 * @tc.desc: FreezeDetector send APPLICATION_BLOCK_INPUT
209 * @tc.type: FUNC
210 * @tc.require: AR000H3T5D
211 */
212 HWTEST_F(FreezeDetectorTest, FreezeDetectorTest001, TestSize.Level3)
213 {
214 HiviewPlatform &platform = HiviewPlatform::GetInstance();
215 std::shared_ptr<Plugin> plugin = platform.GetPluginByName("FreezeDetectorPlugin");
216 if (plugin == nullptr) {
217 printf("Get FreezeDetectorPlugin, failed\n");
218 FAIL();
219 }
220
221 /*
222 {"domain_":"MULTIMODALINPUT","name_":"APPLICATION_BLOCK_INPUT","type_":1,"time_":1504016751820,
223 "tz_":"+0000","pid_":253,"tid_":1220,
224 "uid_":6696,"PID":1886,"UID":20010031,"PACKAGE_NAME":"com.ohos.example.openapi9",
225 "PROCESS_NAME":"com.ohos.example.openapi9",
226 "MSG":"User input does not respond","level_":"CRITICAL","id_":"53545984916101040510",
227 "info_":"isResolved,eventId:0,logPath:/data/log/eventlog/APPLICATION_BLOCK_INPUT-1886-20170829142551.log"}
228 */
229 std::string logPath = "/data/test/test_data/LOG001.log";
230 FileUtil::CreateFile(logPath);
231 if (!FileUtil::FileExists(logPath)) {
232 printf("CreateFile file, failed\n");
233 FAIL();
234 }
235
236 auto sysEvent = makeEvent("FreezeDectorTest001", "AAFWK", "APP_INPUT_BLOCK",
237 "FreezeDectorTest001", logPath);
238 if (sysEvent == nullptr) {
239 printf("GetFreezeDectorTest001File, failed\n");
240 FAIL();
241 }
242 uint64_t time = sysEvent->GetEventIntValue("time_");
243 std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
244 event->eventName_ = sysEvent->GetEventValue("name_");
245 plugin->OnEventListeningCallback(*(event.get()));
246
247 sleep(10);
248 if (!GetFreezeDectorTestFile("APP_INPUT_BLOCK",
249 "FreezeDectorTest001",
250 time)) {
251 printf("GetFreezeDectorTest001File, failed\n");
252 FAIL();
253 }
254 }
255
256 /**
257 * @tc.name: FreezeDetectorTestTest002
258 * @tc.desc: FreezeDetector send LIFECYCLE_TIMEOUT
259 * @tc.type: FUNC
260 * @tc.require: AR000H3T5D
261 */
262 HWTEST_F(FreezeDetectorTest, FreezeDetectorTest002, TestSize.Level3)
263 {
264 HiviewPlatform &platform = HiviewPlatform::GetInstance();
265 std::shared_ptr<Plugin> plugin = platform.GetPluginByName("FreezeDetectorPlugin");
266 if (plugin == nullptr) {
267 printf("Get FreezeDetectorPlugin, failed");
268 FAIL();
269 }
270
271 /*
272 {"domain_":"AAFWK","name_":"LIFECYCLE_TIMEOUT","type_":1,"time_":1504095513772,"tz_":"+0000",
273 "pid_":444,"tid_":1290,
274 "uid_":5523,"UID":20010031,"PID":2070,"PACKAGE_NAME":"com.ohos.example.openapi9",
275 "PROCESS_NAME":"com.ohos.example.openapi9",
276 "MSG":"ability:MainAbility background timeout","level_":"CRITICAL",
277 "tag_":"STABILITY","id_":"13720438474024340534",
278 "info_":"logPath:/data/log/eventlog/LIFECYCLE_TIMEOUT-2070-20170830121833.log,"}
279 */
280 std::string logPath = "/data/test/test_data/LOG002.log";
281 FileUtil::CreateFile(logPath);
282 if (!FileUtil::FileExists(logPath)) {
283 printf("CreateFile file, failed\n");
284 FAIL();
285 }
286
287 auto sysEvent = makeEvent("FreezeDectorTest002", "GRAPHIC", "NO_DRAW",
288 "FreezeDectorTest002", logPath);
289 if (sysEvent == nullptr) {
290 printf("GetFreezeDectorTest002File, failed\n");
291 FAIL();
292 }
293 uint64_t time = sysEvent->GetEventIntValue("time_");
294 std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
295 event->eventName_ = sysEvent->GetEventValue("name_");
296 plugin->OnEventListeningCallback(*(event.get()));
297
298 sleep(10);
299 if (!GetFreezeDectorTestFile("NO_DRAW",
300 "FreezeDectorTest002",
301 time)) {
302 printf("GetFreezeDectorTest002File, failed\n");
303 }
304 }
305
306 /**
307 * @tc.name: FreezeDetectorTest003
308 * @tc.desc: FreezeDetector send LIFECYCLE_TIMEOUT
309 * @tc.type: FUNC
310 * @tc.require: AR000H3T5D
311 */
312 HWTEST_F(FreezeDetectorTest, FreezeDetectorTest003, TestSize.Level3)
313 {
314 HiviewPlatform &platform = HiviewPlatform::GetInstance();
315 std::shared_ptr<Plugin> plugin = platform.GetPluginByName("FreezeDetectorPlugin");
316 if (plugin == nullptr) {
317 printf("Get FreezeDetectorPlugin, failed");
318 FAIL();
319 }
320
321 std::string logPath = "/data/test/test_data/LOG003_1.log";
322 FileUtil::CreateFile(logPath);
323 if (!FileUtil::FileExists(logPath)) {
324 printf("CreateFile file, failed\n");
325 FAIL();
326 }
327
328 auto sysEvent = makeEvent("FreezeDectorTest003", "ACE", "UI_BLOCK_6S",
329 "FreezeDectorTest003", logPath);
330 std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
331 plugin->OnEventListeningCallback(*(event.get()));
332 sleep(10);
333 ASSERT_EQ(plugin->GetName(), "FreezeDetectorPlugin");
334 }
335
336 /**
337 * @tc.name: FreezeDetectorTest003
338 * @tc.desc: FreezeDetector send LIFECYCLE_TIMEOUT
339 * @tc.type: FUNC
340 * @tc.require: AR000H3T5D
341 */
342 HWTEST_F(FreezeDetectorTest, FreezeDetectorTest004, TestSize.Level3)
343 {
344 HiviewPlatform &platform = HiviewPlatform::GetInstance();
345 std::shared_ptr<Plugin> plugin = platform.GetPluginByName("FreezeDetectorPlugin");
346 if (plugin == nullptr) {
347 printf("Get FreezeDetectorPlugin, failed");
348 FAIL();
349 }
350
351 /*
352 {"domain_":"AAFWK","name_":"LIFECYCLE_TIMEOUT","type_":1,"time_":1504095513772,"tz_":"+0000",
353 "pid_":444,"tid_":1290,
354 "uid_":5523,"UID":20010031,"PID":2070,"PACKAGE_NAME":"com.ohos.example.openapi9",
355 "PROCESS_NAME":"com.ohos.example.openapi9",
356 "MSG":"ability:MainAbility background timeout","level_":"CRITICAL",
357 "tag_":"STABILITY","id_":"13720438474024340534",
358 "info_":"logPath:/data/log/eventlog/LIFECYCLE_TIMEOUT-2070-20170830121833.log,"}
359 */
360 std::string logPath = "/data/test/test_data/LOG004.log";
361 FileUtil::CreateFile(logPath);
362 if (!FileUtil::FileExists(logPath)) {
363 printf("CreateFile file, failed\n");
364 FAIL();
365 }
366
367 auto sysEvent = makeEvent("FreezeDectorTest004", "AAFWK", "UI_BLOCK_3S",
368 "FreezeDectorTest004", logPath);
369 std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
370 plugin->OnEventListeningCallback(*(event.get()));
371
372 sleep(10);
373 ASSERT_EQ(plugin->GetName(), "FreezeDetectorPlugin");
374 }
375
376 /**
377 * @tc.name: FreezeRuleTest001
378 * @tc.desc: FreezeRule
379 * @tc.type: FUNC
380 * @tc.require: AR000H3T5D
381 */
382 HWTEST_F(FreezeDetectorTest, FreezeRuleTest001, TestSize.Level3)
383 {
384 auto freezeRuleCluster = std::make_shared<FreezeRuleCluster>();
385
386 bool freezeRuleFlag = freezeRuleCluster->ParseRuleFile("/data/test/test_data/freeze_rules.xml");
387 ASSERT_TRUE(freezeRuleFlag);
388
389 std::map<std::string, std::pair<std::string, bool>> appPairs =
390 freezeRuleCluster->GetApplicationPairs();
391
392 if (appPairs.find("THREAD_BLOCK_6S") != appPairs.end()) {
393 auto tmp = appPairs["THREAD_BLOCK_6S"];
394
395 if (tmp.first != "AAFWK") {
396 printf("THREAD_BLOCK_6S tmp.first != AAFWK.");
397 FAIL();
398 }
399
400 if (!tmp.second) {
401 printf("THREAD_BLOCK_6S tmp.second == false.");
402 FAIL();
403 }
404 } else {
405 printf("THREAD_BLOCK_6S not find.");
406 FAIL();
407 }
408
409 if (appPairs.find("UI_BLOCK_3S") != appPairs.end()) {
410 auto tmp = appPairs["UI_BLOCK_3S"];
411
412 if (tmp.first != "ACE") {
413 printf("UI_BLOCK_3S tmp.first != AAFWK.");
414 FAIL();
415 }
416
417 if (tmp.second) {
418 printf("UI_BLOCK_3S tmp.second == false.");
419 FAIL();
420 }
421 } else {
422 printf("UI_BLOCK_3S not find.");
423 FAIL();
424 }
425 }
426
427 /**
428 * @tc.name: FreezeRuleTest002
429 * @tc.desc: FreezeRule
430 * @tc.type: FUNC
431 * @tc.require: AR000H3T5D
432 */
433 HWTEST_F(FreezeDetectorTest, FreezeRuleTest002, TestSize.Level3)
434 {
435 auto freezeRuleCluster = std::make_shared<FreezeRuleCluster>();
436
437 bool freezeRuleFlag = freezeRuleCluster->ParseRuleFile("/data/test/test_data/freeze_rules.xml");
438 ASSERT_TRUE(freezeRuleFlag);
439
440 std::map<std::string, std::pair<std::string, bool>> systemPairs =
441 freezeRuleCluster->GetSystemPairs();
442
443
444 if (systemPairs.find("SCREEN_ON") != systemPairs.end()) {
445 auto tmp = systemPairs["SCREEN_ON"];
446
447 if (tmp.first != "KERNEL_VENDOR") {
448 printf("SCREEN_ON tmp.first != AAFWK.");
449 FAIL();
450 }
451
452 if (!tmp.second) {
453 printf("SCREEN_ON tmp.second == false.");
454 FAIL();
455 }
456 } else {
457 printf("SCREEN_ON not find.");
458 FAIL();
459 }
460
461 if (systemPairs.find("HUNGTASK") != systemPairs.end()) {
462 auto tmp = systemPairs["HUNGTASK"];
463
464 if (tmp.first != "KERNEL_VENDOR") {
465 printf("HUNGTASK tmp.first != AAFWK.");
466 FAIL();
467 }
468
469 if (tmp.second) {
470 printf("HUNGTASK tmp.second == false.");
471 FAIL();
472 }
473 } else {
474 printf("HUNGTASK not find.");
475 FAIL();
476 }
477 }
478
479 /**
480 * @tc.name: FreezeCommonTest001
481 * @tc.desc: FreezeCommon
482 * @tc.type: FUNC
483 * @tc.require: AR000H3T5D
484 */
485 HWTEST_F(FreezeDetectorTest, FreezeCommonTest001, TestSize.Level3)
486 {
487 auto freezeCommon = std::make_shared<FreezeCommon>();
488 if (!freezeCommon->Init()) {
489 printf("failed to parse rule file.");
490 FAIL();
491 }
492
493 if (!freezeCommon->IsFreezeEvent("AAFWK", "LIFECYCLE_TIMEOUT")) {
494 printf("IsFreezeEvent \"AAFWK\", \"LIFECYCLE_TIMEOUT\" not find.");
495 FAIL();
496 }
497
498 if (!freezeCommon->IsFreezeEvent("KERNEL_VENDOR", "SCREEN_ON")) {
499 printf("IsFreezeEvent \"KERNEL_VENDOR\", \"SCREEN_ON\" not find.");
500 FAIL();
501 }
502
503 if (freezeCommon->IsFreezeEvent("MULTIMODALINPUT", "NO_DRAW")) {
504 printf("IsFreezeEvent, \"NO_DRAW\" is error.");
505 FAIL();
506 }
507
508 if (!freezeCommon->IsApplicationEvent("AAFWK", "THREAD_BLOCK_3S")) {
509 printf("\"AAFWK\", \"THREAD_BLOCK_3S\" not ApplicationEvent.");
510 FAIL();
511 }
512
513 if (freezeCommon->IsApplicationEvent("KERNEL_VENDOR", "HUNGTASK")) {
514 printf("\"KERNEL_VENDOR\", \"HUNGTASK\" is error.");
515 FAIL();
516 }
517
518 if (freezeCommon->IsSystemEvent("AAFWK", "THREAD_BLOCK_3S")) {
519 printf("\"AAFWK\", \"THREAD_BLOCK_3S\" is error.");
520 FAIL();
521 }
522
523 if (!freezeCommon->IsSystemEvent("KERNEL_VENDOR", "HUNGTASK")) {
524 printf("\"KERNEL_VENDOR\", \"HUNGTASK\" not SystemEvent.");
525 FAIL();
526 }
527 }
528 }
529 }
530