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