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         FAIL();
304     }
305 }
306 
307 /**
308  * @tc.name: FreezeDetectorTest003
309  * @tc.desc: FreezeDetector send LIFECYCLE_TIMEOUT
310  * @tc.type: FUNC
311  * @tc.require: AR000H3T5D
312  */
313 HWTEST_F(FreezeDetectorTest, FreezeDetectorTest003, TestSize.Level3)
314 {
315     HiviewPlatform &platform = HiviewPlatform::GetInstance();
316     std::shared_ptr<Plugin> plugin = platform.GetPluginByName("FreezeDetectorPlugin");
317     if (plugin == nullptr) {
318         printf("Get FreezeDetectorPlugin, failed");
319         FAIL();
320     }
321 
322     std::string logPath = "/data/test/test_data/LOG003_1.log";
323     FileUtil::CreateFile(logPath);
324     if (!FileUtil::FileExists(logPath)) {
325         printf("CreateFile file, failed\n");
326         FAIL();
327     }
328 
329     auto sysEvent = makeEvent("FreezeDectorTest003", "ACE", "UI_BLOCK_6S",
330                               "FreezeDectorTest003", logPath);
331     std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
332     plugin->OnEventListeningCallback(*(event.get()));
333     sleep(10);
334     ASSERT_EQ(plugin->GetName(), "FreezeDetectorPlugin");
335 }
336 
337 /**
338  * @tc.name: FreezeDetectorTest003
339  * @tc.desc: FreezeDetector send LIFECYCLE_TIMEOUT
340  * @tc.type: FUNC
341  * @tc.require: AR000H3T5D
342  */
343 HWTEST_F(FreezeDetectorTest, FreezeDetectorTest004, TestSize.Level3)
344 {
345     HiviewPlatform &platform = HiviewPlatform::GetInstance();
346     std::shared_ptr<Plugin> plugin = platform.GetPluginByName("FreezeDetectorPlugin");
347     if (plugin == nullptr) {
348         printf("Get FreezeDetectorPlugin, failed");
349         FAIL();
350     }
351 
352     /*
353         {"domain_":"AAFWK","name_":"LIFECYCLE_TIMEOUT","type_":1,"time_":1504095513772,"tz_":"+0000",
354             "pid_":444,"tid_":1290,
355             "uid_":5523,"UID":20010031,"PID":2070,"PACKAGE_NAME":"com.ohos.example.openapi9",
356             "PROCESS_NAME":"com.ohos.example.openapi9",
357             "MSG":"ability:MainAbility background timeout","level_":"CRITICAL",
358             "tag_":"STABILITY","id_":"13720438474024340534",
359             "info_":"logPath:/data/log/eventlog/LIFECYCLE_TIMEOUT-2070-20170830121833.log,"}
360     */
361     std::string logPath = "/data/test/test_data/LOG004.log";
362     FileUtil::CreateFile(logPath);
363     if (!FileUtil::FileExists(logPath)) {
364         printf("CreateFile file, failed\n");
365         FAIL();
366     }
367 
368     auto sysEvent = makeEvent("FreezeDectorTest004", "AAFWK", "UI_BLOCK_3S",
369                               "FreezeDectorTest004", logPath);
370     std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
371     plugin->OnEventListeningCallback(*(event.get()));
372 
373     sleep(10);
374     ASSERT_EQ(plugin->GetName(), "FreezeDetectorPlugin");
375 }
376 
377 /**
378  * @tc.name: FreezeRuleTest001
379  * @tc.desc: FreezeRule
380  * @tc.type: FUNC
381  * @tc.require: AR000H3T5D
382  */
383 HWTEST_F(FreezeDetectorTest, FreezeRuleTest001, TestSize.Level3)
384 {
385     auto freezeRuleCluster = std::make_shared<FreezeRuleCluster>();
386     bool freezeRuleFlag = freezeRuleCluster->ParseRuleFile("/data/test/test_data/freeze_rules.xml");
387     ASSERT_TRUE(freezeRuleFlag);
388     std::map<std::string, std::pair<std::string, bool>> appPairs =
389         freezeRuleCluster->GetApplicationPairs();
390     if (appPairs.find("THREAD_BLOCK_6S") != appPairs.end()) {
391         auto tmp = appPairs["THREAD_BLOCK_6S"];
392         if (tmp.first != "AAFWK") {
393             printf("THREAD_BLOCK_6S tmp.first != AAFWK.");
394             FAIL();
395         }
396         if (!tmp.second) {
397             printf("THREAD_BLOCK_6S tmp.second == false.");
398             FAIL();
399         }
400     } else {
401         printf("THREAD_BLOCK_6S not find.");
402         FAIL();
403     }
404     if (appPairs.find("UI_BLOCK_3S") != appPairs.end()) {
405         auto tmp = appPairs["UI_BLOCK_3S"];
406         if (tmp.first != "ACE") {
407             printf("UI_BLOCK_3S tmp.first != AAFWK.");
408             FAIL();
409         }
410         if (tmp.second) {
411             printf("UI_BLOCK_3S tmp.second == false.");
412             FAIL();
413         }
414     } else {
415         printf("UI_BLOCK_3S not find.");
416         FAIL();
417     }
418 }
419 
420 /**
421  * @tc.name: FreezeRuleTest002
422  * @tc.desc: FreezeRule
423  * @tc.type: FUNC
424  * @tc.require: AR000H3T5D
425  */
426 HWTEST_F(FreezeDetectorTest, FreezeRuleTest002, TestSize.Level3)
427 {
428     auto freezeRuleCluster = std::make_shared<FreezeRuleCluster>();
429     bool freezeRuleFlag = freezeRuleCluster->ParseRuleFile("/data/test/test_data/freeze_rules.xml");
430     ASSERT_TRUE(freezeRuleFlag);
431     std::map<std::string, std::pair<std::string, bool>> systemPairs =
432         freezeRuleCluster->GetSystemPairs();
433     if (systemPairs.find("SCREEN_ON") != systemPairs.end()) {
434         auto tmp = systemPairs["SCREEN_ON"];
435         if (tmp.first != "KERNEL_VENDOR") {
436             printf("SCREEN_ON tmp.first != AAFWK.");
437             FAIL();
438         }
439         if (!tmp.second) {
440             printf("SCREEN_ON tmp.second == false.");
441             FAIL();
442         }
443     } else {
444         printf("SCREEN_ON not find.");
445         FAIL();
446     }
447     if (systemPairs.find("HUNGTASK") != systemPairs.end()) {
448         auto tmp = systemPairs["HUNGTASK"];
449         if (tmp.first != "KERNEL_VENDOR") {
450             printf("HUNGTASK tmp.first != AAFWK.");
451             FAIL();
452         }
453         if (tmp.second) {
454             printf("HUNGTASK tmp.second == false.");
455             FAIL();
456         }
457     } else {
458         printf("HUNGTASK not find.");
459         FAIL();
460     }
461 }
462 
463 /**
464  * @tc.name: FreezeCommonTest001
465  * @tc.desc: FreezeCommon
466  * @tc.type: FUNC
467  * @tc.require: AR000H3T5D
468  */
469 HWTEST_F(FreezeDetectorTest, FreezeCommonTest001, TestSize.Level3)
470 {
471     auto freezeCommon = std::make_shared<FreezeCommon>();
472     if (!freezeCommon->Init()) {
473         printf("failed to parse rule file.");
474         FAIL();
475     }
476 
477     if (!freezeCommon->IsFreezeEvent("AAFWK", "LIFECYCLE_TIMEOUT")) {
478         printf("IsFreezeEvent \"AAFWK\", \"LIFECYCLE_TIMEOUT\" not find.");
479         FAIL();
480     }
481 
482     if (!freezeCommon->IsFreezeEvent("KERNEL_VENDOR", "SCREEN_ON")) {
483         printf("IsFreezeEvent \"KERNEL_VENDOR\", \"SCREEN_ON\" not find.");
484         FAIL();
485     }
486 
487     if (freezeCommon->IsFreezeEvent("MULTIMODALINPUT", "NO_DRAW")) {
488         printf("IsFreezeEvent, \"NO_DRAW\" is error.");
489         FAIL();
490     }
491 
492     if (!freezeCommon->IsApplicationEvent("AAFWK", "THREAD_BLOCK_3S")) {
493         printf("\"AAFWK\", \"THREAD_BLOCK_3S\" not ApplicationEvent.");
494         FAIL();
495     }
496 
497     if (freezeCommon->IsApplicationEvent("KERNEL_VENDOR", "HUNGTASK")) {
498         printf("\"KERNEL_VENDOR\", \"HUNGTASK\" is error.");
499         FAIL();
500     }
501 
502     if (freezeCommon->IsSystemEvent("AAFWK", "THREAD_BLOCK_3S")) {
503         printf("\"AAFWK\", \"THREAD_BLOCK_3S\" is error.");
504         FAIL();
505     }
506 
507     if (!freezeCommon->IsSystemEvent("KERNEL_VENDOR", "HUNGTASK")) {
508         printf("\"KERNEL_VENDOR\", \"HUNGTASK\" not SystemEvent.");
509         FAIL();
510     }
511 }
512 }
513 }
514