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);
58 if (fromKernel) {
59 event->normalExtraInfo_ = extraInfo;
60 } else {
61 event->jsonExtraInfo_ = extraInfo;
62 }
63 event->domain_ = domain;
64 event->eventName_ = stringid;
65 event->happenTime_ = happenTime;
66 return event;
67 }
68
GetFreezeDetectorPlugin()69 std::shared_ptr<FreezeDetectorPlugin> FreezeDetectorPluginModuleTest::GetFreezeDetectorPlugin()
70 {
71 auto plugin = std::static_pointer_cast<FreezeDetectorPlugin>(PluginPlatform::GetInstance()
72 .GetPluginMap().find("FreezeDetectorPlugin")->second);
73 return plugin;
74 }
75
76 /**
77 * @tc.name: OnEventTest001
78 * @tc.desc: check resolver
79 * @tc.type: FUNC
80 * @tc.require: AR000CPN26
81 */
82 HWTEST_F(FreezeDetectorPluginModuleTest, OnEventTest001, TestSize.Level3)
83 {
84 /**
85 * @tc.steps: step1. create UI_BLOCK_6S watchpoint
86 * @tc.steps: step2. create FWK_BLOCK watchpoint
87 * @tc.steps: step3. check resolver, FWK_BLOCK trigger UI_BLOCK_6S resolver
88 */
89 auto sysEventPlugin = PluginPlatform::GetInstance().GetPluginMap().find("SysEventSource")->second;
90 ASSERT_NE(sysEventPlugin, nullptr);
91
92 long startTime = time(nullptr);
93 std::string json = "{\"MSG\":\"UI_BLOCK_6S\",\"PID\":1669,\"UID\":2208,"
94 "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
95 auto event = CreateFreezeEvent(json, false, startTime, "APPEXECFWK", "UI_BLOCK_6S");
96 PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", event, true);
97 ASSERT_EQ("SysEventPipeline", event->GetPipelineInfo());
98 event->OnContinue();
99 json = "{\"MSG\":\"FWK_BLOCK\",\"PID\":1669,\"UID\":2208,"
100 "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
101 auto eventTrigger = CreateFreezeEvent(json, false, startTime + 6, "FRAMEWORK", "FWK_BLOCK");
102 PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", eventTrigger, true);
103 ASSERT_EQ("SysEventPipeline", eventTrigger->GetPipelineInfo());
104 eventTrigger->OnContinue();
105 }
106
107 /**
108 * @tc.name: FreezeResolverNotiyActionTest001
109 * @tc.desc: check resolver and action
110 * @tc.type: FUNC
111 * @tc.require: AR000CPN2D SR000CPN29
112 */
113 HWTEST_F(FreezeDetectorPluginModuleTest, FreezeResolverNotiyActionTest001, TestSize.Level3)
114 {
115 /**
116 * @tc.steps: step1. create UI_BLOCK_3S watchpoint
117 * @tc.steps: step2. create APP_NOT_RESPONSE watchpoint
118 * @tc.steps: step5. check resolver, APP_NOT_RESPONSE trigger UI_BLOCK_3S resolver
119 */
120 auto sysEventPlugin = PluginPlatform::GetInstance().GetPluginMap().find("SysEventSource")->second;
121 ASSERT_NE(sysEventPlugin, nullptr);
122
123 long startTime = time(nullptr);
124 std::string json = "{\"MSG\":\"UI_BLOCK_3S\",\"PID\":1669,\"UID\":2208,"
125 "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
126 auto event = CreateFreezeEvent(json, false, startTime, "APPEXECFWK", "UI_BLOCK_3S");
127 PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", event, true);
128 ASSERT_EQ("SysEventPipeline", event->GetPipelineInfo());
129 event->OnContinue();
130
131 json = "{\"MSG\":\"APP_NOT_RESPONSE\",\"PID\":1669,\"UID\":2208,"
132 "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
133 auto eventTrigger = CreateFreezeEvent(json, false, startTime + 6, "FRAMEWORK", "APP_NOT_RESPONSE");
134 PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", eventTrigger, true);
135 ASSERT_EQ("SysEventPipeline", eventTrigger->GetPipelineInfo());
136 eventTrigger->OnContinue();
137 }
138
139
140 /**
141 * @tc.name: FreezeResolverCheckIsFaultTest001
142 * @tc.desc: check resolver and action
143 * @tc.type: FUNC
144 * @tc.require: AR000CPN24
145 */
146 HWTEST_F(FreezeDetectorPluginModuleTest, FreezeResolverCheckIsFaultTest001, TestSize.Level3)
147 {
148 /**
149 * @tc.steps: step1. create TRANSPARENT_WINDOWS watchpoint
150 * @tc.steps: step5. check resolver, TRANSPARENT_WINDOWS resolver
151 */
152 auto sysEventPlugin = PluginPlatform::GetInstance().GetPluginMap().find("SysEventSource")->second;
153 ASSERT_NE(sysEventPlugin, nullptr);
154
155 long startTime = time(nullptr);
156 std::string json = "{\"MSG\":\"TRANSPARENT_WINDOWS, isFault = true\",\"PID\":1669,\"UID\":2208,"
157 "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
158 auto event = CreateFreezeEvent(json, false, startTime, "FRAMEWORK", "TRANSPARENT_WINDOWS");
159 PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", event, true);
160 ASSERT_EQ("SysEventPipeline", event->GetPipelineInfo());
161 event->OnContinue();
162 }
163
164 /**
165 * @tc.name: FreezeResolverBackKeyTest001
166 * @tc.desc: check resolver and action
167 * @tc.type: FUNC
168 * @tc.require: AR000CPN2E
169 */
170 HWTEST_F(FreezeDetectorPluginModuleTest, FreezeResolverBackKeyTest001, TestSize.Level3)
171 {
172 /**
173 * @tc.steps: step1. create UI_BLOCK_6S watchpoint
174 * @tc.steps: step5. check resolver, BACK_KEY resolver
175 */
176 auto sysEventPlugin = PluginPlatform::GetInstance().GetPluginMap().find("SysEventSource")->second;
177 ASSERT_NE(sysEventPlugin, nullptr);
178
179 long startTime = time(nullptr);
180 std::string json = "{\"MSG\":\"UI_BLOCK_6S\",\"PID\":1669,\"UID\":2208,"
181 "\"PROCESS_NAME\":\"test.app\",\"PACKAGE_NAME\":\"test.app\"}";
182 auto event = CreateFreezeEvent(json, false, startTime, "APPEXECFWK", "UI_BLOCK_6S");
183 PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", event, true);
184 ASSERT_EQ("SysEventPipeline", event->GetPipelineInfo());
185 event->OnContinue();
186
187 auto triggerEvent = CreateFreezeEvent("", false, startTime + 1, "KEY_PRESS", "BACK_KEY");
188 PipelineEvent::FillPipelineInfo(sysEventPlugin, "SysEventPipeline", triggerEvent, true);
189 ASSERT_EQ("SysEventPipeline", triggerEvent->GetPipelineInfo());
190 riggerEvent->OnContinue();
191 }
192 }
193 }
194