1 /*
2 * Copyright (c) 2023 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 #define private public
17 #define protected public
18
19 #include "gtest/gtest.h"
20 #include "gtest/hwext/gtest-multithread.h"
21 #include "system_ability_definition.h"
22
23 #include "running_lock_strategy.h"
24 #ifdef STANDBY_COMMUNICATION_NETMANAGER_BASE_ENABLE
25 #include "network_strategy.h"
26 #include "base_network_strategy.h"
27 #endif
28 #include "standby_messsage.h"
29 #include "common_constant.h"
30 #include "want.h"
31 #include "workscheduler_srv_client.h"
32
33 using namespace testing::ext;
34 using namespace testing::mt;
35
36 namespace OHOS {
37
38 bool g_getAllRunningWorks = true;
39
GetAllRunningWorks(std::list<std::shared_ptr<WorkScheduler::WorkInfo>>& workInfos)40 ErrCode WorkScheduler::WorkSchedulerSrvClient::GetAllRunningWorks
41 (std::list<std::shared_ptr<WorkScheduler::WorkInfo>>& workInfos)
42 {
43 std::shared_ptr<WorkInfo> workInfo1 = std::make_shared<WorkScheduler::WorkInfo>();
44 std::shared_ptr<WorkInfo> workInfo2 = std::make_shared<WorkScheduler::WorkInfo>();
45 workInfos.emplace_back(workInfo1);
46 workInfos.emplace_back(workInfo2);
47 if (g_getAllRunningWorks) {
48 return ERR_OK;
49 } else {
50 return DevStandbyMgr::ERR_STRATEGY_DEPENDS_SA_NOT_AVAILABLE;
51 }
52 }
53
54 namespace DevStandbyMgr {
55 class StandbyPluginStrategyTest : public testing::Test {
56 public:
57 static void SetUpTestCase();
58 static void TearDownTestCase();
59 void SetUp() override {}
60 void TearDown() override {}
61 };
62
TearDownTestCase()63 void StandbyPluginStrategyTest::TearDownTestCase()
64 {
65 }
66
SetUpTestCase()67 void StandbyPluginStrategyTest::SetUpTestCase()
68 {
69 }
70
71 /**
72 * @tc.name: StandbyPluginStrategyTest_001
73 * @tc.desc: test GetAndCreateAppInfo.
74 * @tc.type: FUNC
75 * @tc.require:
76 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_001, TestSize.Level1)77 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_001, TestSize.Level1)
78 {
79 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
80 int32_t uid = 1;
81 int32_t pid = 1;
82 std::string bundleName = "defaultBundleName";
83 std::string mapKey = std::to_string(uid) + "_" + bundleName;
84 struct ProxiedProcInfo procInfo = {
85 bundleName,
86 uid,
87 {pid}
88 };
89 runningLockStrategy->proxiedAppInfo_.emplace(mapKey, procInfo);
90 runningLockStrategy->GetAndCreateAppInfo(uid, pid, bundleName);
91
92 uid = 2;
93 runningLockStrategy->GetAndCreateAppInfo(uid, pid, bundleName);
94 EXPECT_NE(runningLockStrategy, nullptr);
95 }
96
97 /**
98 * @tc.name: StandbyPluginStrategyTest_002
99 * @tc.desc: test GetExemptionConfigForApp.
100 * @tc.type: FUNC
101 * @tc.require:
102 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_002, TestSize.Level1)103 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_002, TestSize.Level1)
104 {
105 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
106 uint32_t uid = 1;
107 uint32_t pid = 1;
108 std::string bundleName = "defaultBundleName";
109 struct ProxiedProcInfo procInfo = {
110 bundleName,
111 uid,
112 {pid}
113 };
114 runningLockStrategy->GetExemptionConfigForApp(procInfo, bundleName);
115 EXPECT_NE(runningLockStrategy, nullptr);
116 }
117
118 /**
119 * @tc.name: StandbyPluginStrategyTest_003
120 * @tc.desc: test ProxyRunningLockList.
121 * @tc.type: FUNC
122 * @tc.require:
123 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_003, TestSize.Level1)124 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_003, TestSize.Level1)
125 {
126 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
127 bool isProxied = true;
128 std::vector<std::pair<int32_t, int32_t>> proxiedAppList;
129 int32_t a = 1;
130 int32_t b = 1;
131 proxiedAppList.emplace_back(std::make_pair(a, b));
132 runningLockStrategy->isIdleMaintence_ = false;
133 runningLockStrategy->ProxyRunningLockList(isProxied, proxiedAppList);
134
135 runningLockStrategy->isIdleMaintence_ = true;
136 runningLockStrategy->ProxyRunningLockList(isProxied, proxiedAppList);
137 EXPECT_NE(runningLockStrategy, nullptr);
138 }
139
140 /**
141 * @tc.name: StandbyPluginStrategyTest_004
142 * @tc.desc: test HandleProcessStatusChanged.
143 * @tc.type: FUNC
144 * @tc.require:
145 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_004, TestSize.Level1)146 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_004, TestSize.Level1)
147 {
148 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
149 int32_t uid = 1;
150 int32_t pid = 1;
151 std::string bundleName = "defaultBundleName";
152 StandbyMessage standbyMessage {StandbyMessageType::PROCESS_STATE_CHANGED};
153 AAFwk::Want want = AAFwk::Want();
154 standbyMessage.want_ = want;
155 standbyMessage.want_->SetParam("uid", uid);
156 standbyMessage.want_->SetParam("pid", pid);
157 standbyMessage.want_->SetParam("name", bundleName);
158 standbyMessage.want_->SetParam("isCreated", true);
159
160 runningLockStrategy->isProxied_ = true;
161 std::string mapKey = std::to_string(uid) + "_" + bundleName;
162 struct ProxiedProcInfo procInfo = {
163 bundleName,
164 uid,
165 {pid}
166 };
167 runningLockStrategy->proxiedAppInfo_.emplace(mapKey, procInfo);
168 runningLockStrategy->HandleProcessStatusChanged(standbyMessage);
169
170 uid = 2;
171 standbyMessage.want_->SetParam("uid", uid);
172 standbyMessage.want_->SetParam("isCreated", false);
173 runningLockStrategy->HandleProcessStatusChanged(standbyMessage);
174 EXPECT_NE(runningLockStrategy, nullptr);
175 }
176
177 /**
178 * @tc.name: StandbyPluginStrategyTest_005
179 * @tc.desc: test GetBackgroundTaskApp.
180 * @tc.type: FUNC
181 * @tc.require:
182 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_005, TestSize.Level1)183 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_005, TestSize.Level1)
184 {
185 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
186 EXPECT_EQ(runningLockStrategy->GetBackgroundTaskApp(), ERR_OK);
187 }
188
189 /**
190 * @tc.name: StandbyPluginStrategyTest_006
191 * @tc.desc: test GetBackgroundTaskApp.
192 * @tc.type: FUNC
193 * @tc.require:
194 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_006, TestSize.Level1)195 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_006, TestSize.Level1)
196 {
197 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
198 EXPECT_EQ(runningLockStrategy->GetForegroundApplications(), ERR_OK);
199 }
200
201 /**
202 * @tc.name: StandbyPluginStrategyTest_007
203 * @tc.desc: test GetWorkSchedulerTask.
204 * @tc.type: FUNC
205 * @tc.require:
206 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_007, TestSize.Level1)207 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_007, TestSize.Level1)
208 {
209 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
210 EXPECT_EQ(runningLockStrategy->GetWorkSchedulerTask(), ERR_OK);
211 }
212
213 /**
214 * @tc.name: StandbyPluginStrategyTest_008
215 * @tc.desc: test GetAllRunningAppInfo.
216 * @tc.type: FUNC
217 * @tc.require:
218 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_008, TestSize.Level1)219 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_008, TestSize.Level1)
220 {
221 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
222 EXPECT_EQ(runningLockStrategy->GetAllRunningAppInfo(), ERR_OK);
223 }
224
225 /**
226 * @tc.name: StandbyPluginStrategyTest_009
227 * @tc.desc: test OnDestroy.
228 * @tc.type: FUNC
229 * @tc.require:
230 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_009, TestSize.Level1)231 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_009, TestSize.Level1)
232 {
233 auto runningLockStrategy = std::make_shared<RunningLockStrategy>();
234 runningLockStrategy->isProxied_ = true;
235 runningLockStrategy->isIdleMaintence_ = true;
236 EXPECT_EQ(runningLockStrategy->OnDestroy(), ERR_OK);
237
238 runningLockStrategy->isIdleMaintence_ = false;
239 EXPECT_EQ(runningLockStrategy->OnDestroy(), ERR_OK);
240 }
241
242 #ifdef STANDBY_COMMUNICATION_NETMANAGER_BASE_ENABLE
243 /**
244 * @tc.name: StandbyPluginStrategyTest_010
245 * @tc.desc: test ResetFirewallStatus.
246 * @tc.type: FUNC
247 * @tc.require:
248 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_010, TestSize.Level1)249 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_010, TestSize.Level1)
250 {
251 auto baseNetworkStrategy = std::make_shared<NetworkStrategy>();
252 StandbyMessage standbyMessage {StandbyMessageType::SYS_ABILITY_STATUS_CHANGED};
253
254 baseNetworkStrategy->isFirewallEnabled_ = true;
255 baseNetworkStrategy->isIdleMaintence_ = true;
256 baseNetworkStrategy->ResetFirewallStatus(standbyMessage);
257
258 baseNetworkStrategy->isFirewallEnabled_ = true;
259 baseNetworkStrategy->isIdleMaintence_ = false;
260 AAFwk::Want want = AAFwk::Want();
261 standbyMessage.want_ = want;
262 standbyMessage.want_->SetParam(SA_STATUS, false);
263 standbyMessage.want_->SetParam(SA_ID, WORK_SCHEDULE_SERVICE_ID);
264 baseNetworkStrategy->ResetFirewallStatus(standbyMessage);
265
266 standbyMessage.want_->SetParam(SA_ID, BACKGROUND_TASK_MANAGER_SERVICE_ID);
267 baseNetworkStrategy->ResetFirewallStatus(standbyMessage);
268
269 standbyMessage.want_->SetParam(SA_ID, DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID);
270 baseNetworkStrategy->ResetFirewallStatus(standbyMessage);
271 EXPECT_NE(baseNetworkStrategy, nullptr);
272 }
273
274 /**
275 * @tc.name: StandbyPluginStrategyTest_011
276 * @tc.desc: test GetExemptionConfigForApp.
277 * @tc.type: FUNC
278 * @tc.require:
279 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_011, TestSize.Level1)280 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_011, TestSize.Level1)
281 {
282 auto baseNetworkStrategy = std::make_shared<NetworkStrategy>();
283 std::string bundleName = "defaultBundleName";
284 NetLimtedAppInfo appInfo {bundleName};
285
286 baseNetworkStrategy->GetExemptionConfigForApp(appInfo, bundleName);
287 EXPECT_NE(baseNetworkStrategy, nullptr);
288 }
289
290 /**
291 * @tc.name: StandbyPluginStrategyTest_012
292 * @tc.desc: test GetExemptionConfig.
293 * @tc.type: FUNC
294 * @tc.require:
295 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_012, TestSize.Level1)296 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_012, TestSize.Level1)
297 {
298 auto baseNetworkStrategy = std::make_shared<NetworkStrategy>();
299 baseNetworkStrategy->GetExemptionConfig();
300 EXPECT_NE(baseNetworkStrategy, nullptr);
301 }
302
303 /**
304 * @tc.name: StandbyPluginStrategyTest_013
305 * @tc.desc: test GetExemptionConfig.
306 * @tc.type: FUNC
307 * @tc.require:
308 */
HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_013, TestSize.Level1)309 HWTEST_F(StandbyPluginStrategyTest, StandbyPluginStrategyTest_013, TestSize.Level1)
310 {
311 auto baseNetworkStrategy = std::make_shared<NetworkStrategy>();
312 int32_t uid = 1;
313 uint8_t flag = ExemptionTypeFlag::UNRESTRICTED;
314 NetLimtedAppInfo appInfo {"defaulBundleName"};
315 baseNetworkStrategy->netLimitedAppInfo_.emplace(uid, appInfo);
316 baseNetworkStrategy->AddExemptionFlagByUid(uid, flag);
317
318 uid = 2;
319 baseNetworkStrategy->AddExemptionFlagByUid(uid, flag);
320 EXPECT_NE(baseNetworkStrategy, nullptr);
321 }
322 #endif // STANDBY_COMMUNICATION_NETMANAGER_BASE_ENABLE
323 } // namespace DevStandbyMgr
324 } // namespace OHOS
325