• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 <random>
17 #include <thread>
18 #include <unistd.h>
19 
20 #include <gtest/gtest.h>
21 
22 #include "net_policy_file.h"
23 
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 constexpr uint32_t MAX_LIST_SIZE = 10;
28 constexpr uint32_t SLEEP_SECOND_TIME = 5;
29 } // namespace
30 std::shared_ptr<NetPolicyFile> netPolicyFile_ = nullptr;
31 
32 using namespace testing::ext;
33 class UtNetPolicyFile : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39     std::set<uint32_t> white_;
40     std::set<uint32_t> black_;
41 };
42 
SetUpTestCase()43 void UtNetPolicyFile::SetUpTestCase()
44 {
45     netPolicyFile_ = DelayedSingleton<NetPolicyFile>::GetInstance();
46     ASSERT_TRUE(DelayedSingleton<NetPolicyFile>::GetInstance());
47     netPolicyFile_->InitPolicy();
48 }
49 
TearDownTestCase()50 void UtNetPolicyFile::TearDownTestCase()
51 {
52     sleep(SLEEP_SECOND_TIME);
53     netPolicyFile_.reset();
54 }
55 
SetUp()56 void UtNetPolicyFile::SetUp()
57 {
58     netPolicyFile_->ReadFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, white_, black_);
59 }
60 
TearDown()61 void UtNetPolicyFile::TearDown()
62 {
63     netPolicyFile_->WriteFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, white_, black_);
64 }
65 
66 /**
67  * @tc.name: NetPolicyFile001
68  * @tc.desc: Test NetPolicyFile ReadFirewallRules.
69  * @tc.type: FUNC
70  */
71 HWTEST_F(UtNetPolicyFile, NetPolicyFile001, TestSize.Level1)
72 {
73     std::set<uint32_t> allowedList;
74     std::set<uint32_t> deniedList;
75     for (uint32_t i = 0; i <= MAX_LIST_SIZE; i++) {
76         allowedList.insert(i);
77         deniedList.insert(i);
78     }
79 
80     netPolicyFile_->WriteFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, allowedList, deniedList);
81     std::set<uint32_t> allowedList1;
82     std::set<uint32_t> deniedList1;
83     netPolicyFile_->ReadFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, allowedList1, deniedList1);
84     ASSERT_TRUE(allowedList == allowedList1);
85     ASSERT_TRUE(deniedList == deniedList1);
86 }
87 
88 /**
89  * @tc.name: NetPolicyFile002
90  * @tc.desc: Test NetPolicyFile WriteFirewallRules.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(UtNetPolicyFile, NetPolicyFile002, TestSize.Level1)
94 {
95     std::set<uint32_t> allowedList;
96     std::set<uint32_t> deniedList;
97     for (uint32_t i = 0; i <= MAX_LIST_SIZE; i++) {
98         allowedList.insert(i);
99         deniedList.insert(i);
100     }
101     netPolicyFile_->WriteFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, allowedList, deniedList);
102     sleep(SLEEP_SECOND_TIME);
103     netPolicyFile_->InitPolicy();
104     std::set<uint32_t> allowedList1;
105     std::set<uint32_t> deniedList1;
106     netPolicyFile_->ReadFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, allowedList1, deniedList1);
107     ASSERT_TRUE(allowedList == allowedList1);
108     ASSERT_TRUE(deniedList == deniedList1);
109 }
110 
111 } // namespace NetManagerStandard
112 } // namespace OHOS
113