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