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 #include <thread>
17
18 #include <gtest/gtest.h>
19
20 #ifdef GTEST_API_
21 #define private public
22 #define protected public
23 #endif
24
25 #include "net_policy_firewall.h"
26 #include "net_policy_rule.h"
27 #include "net_policy_service.h"
28 #include "net_policy_traffic.h"
29 #include "system_ability_definition.h"
30 #include "netmanager_base_test_security.h"
31
32 namespace OHOS {
33 namespace NetManagerStandard {
34 using namespace testing::ext;
35
36 class UtNetPolicyService : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 static inline std::shared_ptr<NetPolicyService> instance_ = nullptr;
43 };
44
SetUpTestCase()45 void UtNetPolicyService::SetUpTestCase() {}
46
TearDownTestCase()47 void UtNetPolicyService::TearDownTestCase() {}
48
SetUp()49 void UtNetPolicyService::SetUp()
50 {
51 instance_ = DelayedSingleton<NetPolicyService>::GetInstance();
52 instance_->netPolicyRule_ = std::make_shared<NetPolicyRule>();
53 instance_->netPolicyFirewall_ = std::make_shared<NetPolicyFirewall>();
54 instance_->netPolicyTraffic_ = std::make_shared<NetPolicyTraffic>();
55 }
56
TearDown()57 void UtNetPolicyService::TearDown() {}
58
59 /**
60 * @tc.name: SetPolicyByUid01
61 * @tc.desc: Test NetPolicyService SetPolicyByUid.
62 * @tc.type: FUNC
63 */
64 HWTEST_F(UtNetPolicyService, SetPolicyByUid01, TestSize.Level1)
65 {
66 uint32_t uid = 10000;
67 uint32_t policy = 50;
68 int32_t ret = instance_->SetPolicyByUid(uid, policy);
69 EXPECT_EQ(ret, POLICY_ERR_INVALID_POLICY);
70 }
71
72 /**
73 * @tc.name: GetPolicyByUid01
74 * @tc.desc: Test NetPolicyService GetPolicyByUid.
75 * @tc.type: FUNC
76 */
77 HWTEST_F(UtNetPolicyService, GetPolicyByUid01, TestSize.Level1)
78 {
79 uint32_t uid = 20000;
80 uint32_t policy = 50;
81 int32_t ret = instance_->GetPolicyByUid(uid, policy);
82 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
83 }
84
85 /**
86 * @tc.name: IsUidNetAllowed01
87 * @tc.desc: Test NetPolicyService IsUidNetAllowed.
88 * @tc.type: FUNC
89 */
90 HWTEST_F(UtNetPolicyService, IsUidNetAllowed01, TestSize.Level1)
91 {
92 uint32_t uid = 10000;
93 uint32_t policy = 50;
94 bool isAllowed = false;
95 int32_t ret = instance_->IsUidNetAllowed(uid, policy, isAllowed);
96 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
97 }
98
99 /**
100 * @tc.name: IsUidNetAllowed02
101 * @tc.desc: Test NetPolicyService IsUidNetAllowed.
102 * @tc.type: FUNC
103 */
104 HWTEST_F(UtNetPolicyService, IsUidNetAllowed02, TestSize.Level1)
105 {
106 uint32_t uid = 10000;
107 std::string ifaceName = "test";
108 bool isAllowed = false;
109 int32_t ret = instance_->IsUidNetAllowed(uid, ifaceName, isAllowed);
110 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
111 }
112
113 /**
114 * @tc.name: SetNetQuotaPolicies01
115 * @tc.desc: Test NetPolicyService SetNetQuotaPolicies.
116 * @tc.type: FUNC
117 */
118 HWTEST_F(UtNetPolicyService, SetNetQuotaPolicies01, TestSize.Level1)
119 {
120 std::vector<NetQuotaPolicy> quotaPolicies;
121 int32_t ret = instance_->SetNetQuotaPolicies(quotaPolicies);
122 EXPECT_EQ(ret, POLICY_ERR_INVALID_QUOTA_POLICY);
123 }
124
125 /**
126 * @tc.name: GetNetQuotaPolicies01
127 * @tc.desc: Test NetPolicyService GetNetQuotaPolicies.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(UtNetPolicyService, GetNetQuotaPolicies01, TestSize.Level1)
131 {
132 std::vector<NetQuotaPolicy> quotaPolicies;
133 int32_t ret = instance_->GetNetQuotaPolicies(quotaPolicies);
134 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
135 }
136
137 /**
138 * @tc.name: SetBackgroundPolicy01
139 * @tc.desc: Test NetPolicyService SetBackgroundPolicy.
140 * @tc.type: FUNC
141 */
142 HWTEST_F(UtNetPolicyService, SetBackgroundPolicy01, TestSize.Level1)
143 {
144 instance_->SetBackgroundPolicy(true);
145 int32_t ret = instance_->SetBackgroundPolicy(true);
146 EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
147 }
148
149 /**
150 * @tc.name: GetBackgroundPolicy01
151 * @tc.desc: Test NetPolicyService GetBackgroundPolicy.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(UtNetPolicyService, GetBackgroundPolicy01, TestSize.Level1)
155 {
156 bool backgroundPolicy = false;
157 int32_t ret = instance_->GetBackgroundPolicy(backgroundPolicy);
158 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
159 }
160
161 /**
162 * @tc.name: GetBackgroundPolicyByUid01
163 * @tc.desc: Test NetPolicyService GetBackgroundPolicyByUid.
164 * @tc.type: FUNC
165 */
166 HWTEST_F(UtNetPolicyService, GetBackgroundPolicyByUid01, TestSize.Level1)
167 {
168 uint32_t uid = 20000;
169 uint32_t backgroundPolicyOfUid = 0;
170 int32_t ret = instance_->GetBackgroundPolicyByUid(uid, backgroundPolicyOfUid);
171 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
172 }
173
174 /**
175 * @tc.name: GetDumpMessage01
176 * @tc.desc: Test NetPolicyService GetDumpMessage.
177 * @tc.type: FUNC
178 */
179 HWTEST_F(UtNetPolicyService, GetDumpMessage01, TestSize.Level1)
180 {
181 std::string message;
182 int32_t ret = instance_->GetDumpMessage(message);
183 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
184 }
185
186 /**
187 * @tc.name: NetPolicyServiceBranchTest001
188 * @tc.desc: Test NetPolicyService Branch.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(UtNetPolicyService, NetPolicyServiceBranchTest001, TestSize.Level1)
192 {
193 int32_t systemAbilityId = 0;
194 std::string deviceId = "";
195 instance_->OnAddSystemAbility(systemAbilityId, deviceId);
196 instance_->OnRemoveSystemAbility(systemAbilityId, deviceId);
197
198 systemAbilityId = COMM_NETSYS_NATIVE_SYS_ABILITY_ID;
199 instance_->OnAddSystemAbility(systemAbilityId, deviceId);
200 instance_->OnRemoveSystemAbility(systemAbilityId, deviceId);
201
202 int32_t fd = 1;
203 std::vector<std::u16string> args;
204 int32_t ret = instance_->Dump(fd, args);
205 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
206
207 uint32_t policy = 0;
208 std::vector<uint32_t> uids;
209 ret = instance_->GetUidsByPolicy(policy, uids);
210 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
211
212 int32_t netType = 0;
213 std::string simId = "";
214 uint32_t remindType = 0;
215 ret = instance_->UpdateRemindPolicy(netType, simId, remindType);
216 EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
217
218 ret = instance_->CheckPermission();
219 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
220
221 ret = instance_->GetDeviceIdleTrustlist(uids);
222 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
223
224 ret = instance_->SetDeviceIdlePolicy(false);
225 EXPECT_EQ(ret, NETMANAGER_ERR_STATUS_EXIST);
226
227 ret = instance_->SetDeviceIdlePolicy(true);
228 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
229
230 ret = instance_->SetPowerSaveTrustlist(uids, false);
231 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
232
233 ret = instance_->GetPowerSaveTrustlist(uids);
234 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
235
236 ret = instance_->SetPowerSavePolicy(false);
237 EXPECT_EQ(ret, NETMANAGER_ERR_STATUS_EXIST);
238
239 ret = instance_->SetPowerSavePolicy(true);
240 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
241 }
242
243 /**
244 * @tc.name: NetPolicyServiceBranchTest002
245 * @tc.desc: Test NetPolicyService Branch.
246 * @tc.type: FUNC
247 */
248 HWTEST_F(UtNetPolicyService, NetPolicyServiceBranchTest002, TestSize.Level1)
249 {
250 ASSERT_TRUE(instance_->netPolicyRule_ != nullptr);
251 ASSERT_TRUE(instance_->netPolicyTraffic_ != nullptr);
252 ASSERT_TRUE(instance_->netPolicyFirewall_ != nullptr);
253 if (instance_->netPolicyFirewall_ == nullptr) {
254 return;
255 }
256 instance_->netPolicyFirewall_->Init();
257
258 uint32_t policy = 0;
259 std::vector<uint32_t> uids;
260 auto ret = instance_->GetUidsByPolicy(policy, uids);
261 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
262
263 int32_t netType = 0;
264 std::string simId = "";
265 uint32_t remindType = 0;
266 ret = instance_->UpdateRemindPolicy(netType, simId, remindType);
267 EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
268
269 ret = instance_->CheckPermission();
270 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
271
272 ret = instance_->GetDeviceIdleTrustlist(uids);
273 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
274
275 ret = instance_->SetDeviceIdlePolicy(false);
276 EXPECT_EQ(ret, NETMANAGER_ERR_STATUS_EXIST);
277
278 ret = instance_->SetDeviceIdlePolicy(true);
279 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
280
281 ret = instance_->SetPowerSaveTrustlist(uids, false);
282 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
283
284 ret = instance_->GetPowerSaveTrustlist(uids);
285 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
286
287 ret = instance_->SetPowerSavePolicy(false);
288 EXPECT_EQ(ret, NETMANAGER_ERR_STATUS_EXIST);
289
290 ret = instance_->SetPowerSavePolicy(true);
291 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
292 }
293
294 /**
295 * @tc.name: NetPolicyServiceBranchTest003
296 * @tc.desc: Test NetPolicyService Branch.
297 * @tc.type: FUNC
298 */
299 HWTEST_F(UtNetPolicyService, NetPolicyServiceBranchTest003, TestSize.Level1)
300 {
301 instance_->OnStop();
302 int32_t netType = 0;
303 std::string simId = "";
304 uint32_t remindType = 0;
305 std::vector<uint32_t> uids;
306 auto ret = instance_->UpdateRemindPolicy(netType, simId, remindType);
307 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
308
309 ret = instance_->CheckPermission();
310 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
311
312 ret = instance_->GetDeviceIdleTrustlist(uids);
313 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
314
315 ret = instance_->SetDeviceIdlePolicy(false);
316 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
317
318 ret = instance_->SetPowerSaveTrustlist(uids, false);
319 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
320
321 ret = instance_->GetPowerSaveTrustlist(uids);
322 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
323
324 ret = instance_->SetPowerSavePolicy(false);
325 EXPECT_EQ(ret, NETMANAGER_ERR_LOCAL_PTR_NULL);
326 }
327
328 HWTEST_F(UtNetPolicyService, OnRemoveSystemAbility001, TestSize.Level1)
329 {
330 std::string deviceId = "dev1";
331 instance_->OnRemoveSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID, deviceId);
332 EXPECT_TRUE(instance_->hasSARemoved_);
333 }
334
335 HWTEST_F(UtNetPolicyService, OnAddSystemAbility001, TestSize.Level1)
336 {
337 std::string deviceId = "dev1";
338 instance_->OnAddSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID, deviceId);
339 EXPECT_FALSE(instance_->hasSARemoved_);
340 }
341
342 /**
343 * @tc.name: SetNetworkAccessPolicy01
344 * @tc.desc: Test NetPolicyService SetNetworkAccessPolicy.
345 * @tc.type: FUNC
346 */
347 HWTEST_F(UtNetPolicyService, SetNetworkAccessPolicy01, TestSize.Level1)
348 {
349 int32_t uid = 666;
350 NetworkAccessPolicy netAccessPolicy;
351 netAccessPolicy.wifiAllow = false;
352 netAccessPolicy.cellularAllow = false;
353 auto ret = instance_->SetNetworkAccessPolicy(uid, netAccessPolicy, true);
354 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
355 }
356
357 /**
358 * @tc.name: GetNetworkAccessPolicy01
359 * @tc.desc: Test NetPolicyService GetNetworkAccessPolicy.
360 * @tc.type: FUNC
361 */
362 HWTEST_F(UtNetPolicyService, GetNetworkAccessPolicy01, TestSize.Level1)
363 {
364 AccessPolicyParameter parameter;
365 parameter.flag = 0;
366 parameter.uid = 666;
367 AccessPolicySave resultSave;
368 auto ret = instance_->GetNetworkAccessPolicy(parameter, resultSave);
369 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
370 }
371
372 /**
373 * @tc.name: DeleteNetworkAccessPolicy01
374 * @tc.desc: Test NetPolicyService DeleteNetworkAccessPolicy.
375 * @tc.type: FUNC
376 */
377 HWTEST_F(UtNetPolicyService, DeleteNetworkAccessPolicy01, TestSize.Level1)
378 {
379 int32_t uid = 666;
380 auto ret = instance_->DeleteNetworkAccessPolicy(uid);
381 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
382 }
383
384 /**
385 * @tc.name: SetNicTrafficAllowed001
386 * @tc.desc: Test NetPolicyService SetNicTrafficAllowed.
387 * @tc.type: FUNC
388 */
389 HWTEST_F(UtNetPolicyService, SetNicTrafficAllowed001, TestSize.Level1)
390 {
391 NetManagerBaseAccessToken token;
392 std::vector<std::string> ifaceName = {"wlan0", "aaa"};
393 auto ret = instance_->SetNicTrafficAllowed(ifaceName, false);
394 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
395 }
396
397 /**
398 * @tc.name: SetNicTrafficAllowed002
399 * @tc.desc: Test NetPolicyService SetNicTrafficAllowed.
400 * @tc.type: FUNC
401 */
402 HWTEST_F(UtNetPolicyService, SetNicTrafficAllowed002, TestSize.Level1)
403 {
404 NetManagerBaseAccessToken token;
405 std::vector<std::string> ifaceName = {"wlan0", "aaa"};
406 auto ret = instance_->SetNicTrafficAllowed(ifaceName, true);
407 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
408 }
409
410 /**
411 * @tc.name: SetNicTrafficAllowed003
412 * @tc.desc: Test NetPolicyService SetNicTrafficAllowed.
413 * @tc.type: FUNC
414 */
415 HWTEST_F(UtNetPolicyService, SetNicTrafficAllowed003, TestSize.Level1)
416 {
417 NetManagerBaseAccessToken token;
418 std::vector<std::string> ifaceName = {"wlan0"};
419 auto ret = instance_->SetNicTrafficAllowed(ifaceName, false);
420 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
421 }
422
423 /**
424 * @tc.name: SetNicTrafficAllowed004
425 * @tc.desc: Test NetPolicyService SetNicTrafficAllowed.
426 * @tc.type: FUNC
427 */
428 HWTEST_F(UtNetPolicyService, SetNicTrafficAllowed004, TestSize.Level1)
429 {
430 NetManagerBaseAccessToken token;
431 std::vector<std::string> ifaceName = {"wlan0"};
432 auto ret = instance_->SetNicTrafficAllowed(ifaceName, true);
433 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
434 }
435
436 /**
437 * @tc.name: SetNicTrafficAllowed005
438 * @tc.desc: Test NetPolicyService SetNicTrafficAllowed.
439 * @tc.type: FUNC
440 */
441 HWTEST_F(UtNetPolicyService, SetNicTrafficAllowed005, TestSize.Level1)
442 {
443 NetManagerBaseAccessToken token;
444 std::vector<std::string> ifaceName = {};
445 auto ret = instance_->SetNicTrafficAllowed(ifaceName, false);
446 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
447 }
448
449 /**
450 * @tc.name: SetNicTrafficAllowed006
451 * @tc.desc: Test NetPolicyService SetNicTrafficAllowed.
452 * @tc.type: FUNC
453 */
454 HWTEST_F(UtNetPolicyService, SetNicTrafficAllowed006, TestSize.Level1)
455 {
456 NetManagerBaseAccessToken token;
457 std::vector<std::string> ifaceName = {};
458 auto ret = instance_->SetNicTrafficAllowed(ifaceName, true);
459 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
460 }
461 } // namespace NetManagerStandard
462 } // namespace OHOS