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 
16 #include "freeze_detector_plugin_module_test.h"
17 
18 #include <fcntl.h>
19 #include <fstream>
20 #include <mutex>
21 
22 #include "file_util.h"
23 #include "freeze_detector_resolver.h"
24 #include "freeze_detector_utils.h"
25 #include "log_manager.h"
26 #include "plugin_platform.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 using namespace testing::ext;
31 
SetUp()32 void FreezeDetectorPluginModuleTest::SetUp() {}
33 
TearDown()34 void FreezeDetectorPluginModuleTest::TearDown()
35 {
36 }
37 
SetUpTestCase()38 void FreezeDetectorPluginModuleTest::SetUpTestCase()
39 {
40     OHOS::HiviewDFX::PluginPlatform &platform = PluginPlatform::GetInstance();
41     if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
42         printf("Fail to init environment.\n");
43     }
44 }
45 
TearDownTestCase()46 void FreezeDetectorPluginModuleTest::TearDownTestCase()
47 {
48     std::shared_ptr<Plugin> plugin = PluginPlatform::GetInstance().GetPluginMap().find("FreezeDetectorPlugin")->second;
49     if (plugin != nullptr) {
50         plugin->OnUnload();
51     }
52 }
53 
CreateFreezeEvent(const std::string & extraInfo,bool fromKernel,long happenTime,const std::string & domain,const std::string & stringid) const54 std::shared_ptr<SysEvent> FreezeDetectorPluginModuleTest::CreateFreezeEvent(const std::string& extraInfo,
55     bool fromKernel, long happenTime, const std::string& domain, const std::string& stringid) const
56 {
57     auto event = std::make_shared<SysEvent>("SysEvent", nullptr, extraInfo);
58     if (fromKernel) {
59         event->normalExtraInfo_ = extraInfo;
60     }
61     event->SetEventValue("domain_", domain);
62     event->domain_ = domain;
63     event->SetEventValue("name_", stringid);
64     event->eventName_ = stringid;
65     event->SetEventValue("time_", happenTime);
66     event->happenTime_ = happenTime;
67     return event;
68 }
69 
GetFreezeDetectorPlugin()70 std::shared_ptr<FreezeDetectorPlugin> FreezeDetectorPluginModuleTest::GetFreezeDetectorPlugin()
71 {
72     auto plugin = std::static_pointer_cast<FreezeDetectorPlugin>(PluginPlatform::GetInstance()
73         .GetPluginMap().find("FreezeDetectorPlugin")->second);
74     return plugin;
75 }
76 
77 /**
78  * @tc.name: OnEventTest001
79  * @tc.desc: check resolver
80  * @tc.type: FUNC
81  * @tc.require: AR000CPN26
82  */
83 HWTEST_F(FreezeDetectorPluginModuleTest, OnEventTest001, TestSize.Level3)
84 {
85     /**
86      * @tc.steps: step1. create UI_BLOCK_6S watchpoint
87      * @tc.steps: step2. create FWK_BLOCK watchpoint
88      * @tc.steps: step3. check resolver, FWK_BLOCK trigger UI_BLOCK_6S resolver
89      */
90     auto sysEventPlugin = PluginPlatform::GetInstance().GetPluginMap().find("SysEventSource")->second;
91     ASSERT_NE(sysEventPlugin, nullptr);
92 
93     long startTime = time(nullptr);
94     std::string json = "{\"MSG\":\"UI_BLOCK_6S\",\"PID\":1669,\"UID\":2208,"
95         "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
96     auto event = CreateFreezeEvent(json, false, startTime, "APPEXECFWK", "UI_BLOCK_6S");
97     PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", event, true);
98     ASSERT_EQ("SysEventPipeline", event->GetPipelineInfo());
99     event->OnContinue();
100     json = "{\"MSG\":\"FWK_BLOCK\",\"PID\":1669,\"UID\":2208,"
101         "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
102     auto eventTrigger = CreateFreezeEvent(json, false, startTime + 6, "FRAMEWORK", "FWK_BLOCK");
103     PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", eventTrigger, true);
104     ASSERT_EQ("SysEventPipeline", eventTrigger->GetPipelineInfo());
105     eventTrigger->OnContinue();
106 }
107 
108 /**
109  * @tc.name: FreezeResolverNotiyActionTest001
110  * @tc.desc: check resolver and action
111  * @tc.type: FUNC
112  * @tc.require: AR000CPN2D SR000CPN29
113  */
114 HWTEST_F(FreezeDetectorPluginModuleTest, FreezeResolverNotiyActionTest001, TestSize.Level3)
115 {
116     /**
117      * @tc.steps: step1. create UI_BLOCK_3S watchpoint
118      * @tc.steps: step2. create APP_NOT_RESPONSE watchpoint
119      * @tc.steps: step5. check resolver, APP_NOT_RESPONSE trigger UI_BLOCK_3S resolver
120      */
121     auto sysEventPlugin = PluginPlatform::GetInstance().GetPluginMap().find("SysEventSource")->second;
122     ASSERT_NE(sysEventPlugin, nullptr);
123 
124     long startTime = time(nullptr);
125     std::string json = "{\"MSG\":\"UI_BLOCK_3S\",\"PID\":1669,\"UID\":2208,"
126         "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
127     auto event = CreateFreezeEvent(json, false, startTime, "APPEXECFWK", "UI_BLOCK_3S");
128     PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", event, true);
129     ASSERT_EQ("SysEventPipeline", event->GetPipelineInfo());
130     event->OnContinue();
131 
132     json = "{\"MSG\":\"APP_NOT_RESPONSE\",\"PID\":1669,\"UID\":2208,"
133         "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
134     auto eventTrigger = CreateFreezeEvent(json, false, startTime + 6, "FRAMEWORK", "APP_NOT_RESPONSE");
135     PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", eventTrigger, true);
136     ASSERT_EQ("SysEventPipeline", eventTrigger->GetPipelineInfo());
137     eventTrigger->OnContinue();
138 }
139 
140 
141 /**
142  * @tc.name: FreezeResolverCheckIsFaultTest001
143  * @tc.desc: check resolver and action
144  * @tc.type: FUNC
145  * @tc.require: AR000CPN24
146  */
147 HWTEST_F(FreezeDetectorPluginModuleTest, FreezeResolverCheckIsFaultTest001, TestSize.Level3)
148 {
149     /**
150      * @tc.steps: step1. create TRANSPARENT_WINDOWS watchpoint
151      * @tc.steps: step5. check resolver, TRANSPARENT_WINDOWS resolver
152      */
153     auto sysEventPlugin = PluginPlatform::GetInstance().GetPluginMap().find("SysEventSource")->second;
154     ASSERT_NE(sysEventPlugin, nullptr);
155 
156     long startTime = time(nullptr);
157     std::string json = "{\"MSG\":\"TRANSPARENT_WINDOWS, isFault = true\",\"PID\":1669,\"UID\":2208,"
158         "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
159     auto event = CreateFreezeEvent(json, false, startTime, "FRAMEWORK", "TRANSPARENT_WINDOWS");
160     PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", event, true);
161     ASSERT_EQ("SysEventPipeline", event->GetPipelineInfo());
162     event->OnContinue();
163 }
164 
165 /**
166  * @tc.name: FreezeResolverBackKeyTest001
167  * @tc.desc: check resolver and action
168  * @tc.type: FUNC
169  * @tc.require: AR000CPN2E
170  */
171 HWTEST_F(FreezeDetectorPluginModuleTest, FreezeResolverBackKeyTest001, TestSize.Level3)
172 {
173     /**
174      * @tc.steps: step1. create UI_BLOCK_6S watchpoint
175      * @tc.steps: step5. check resolver, BACK_KEY resolver
176      */
177     auto sysEventPlugin = PluginPlatform::GetInstance().GetPluginMap().find("SysEventSource")->second;
178     ASSERT_NE(sysEventPlugin, nullptr);
179 
180     long startTime = time(nullptr);
181     std::string json = "{\"MSG\":\"UI_BLOCK_6S\",\"PID\":1669,\"UID\":2208,"
182         "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
183     auto event = CreateFreezeEvent(json, false, startTime, "APPEXECFWK", "UI_BLOCK_6S");
184     PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", event, true);
185     ASSERT_EQ("SysEventPipeline", event->GetPipelineInfo());
186     event->OnContinue();
187 
188     auto triggerEvent = CreateFreezeEvent("", false, startTime + 1, "KEY_PRESS", "BACK_KEY");
189     PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", triggerEvent, true);
190     ASSERT_EQ("SysEventPipeline", triggerEvent->GetPipelineInfo());
191     riggerEvent->OnContinue();
192 }
193 }
194 }
195