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