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