• 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 #ifdef GTEST_API_
23 #define private public
24 #define protected public
25 #endif
26 
27 #include "net_policy_file.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 namespace {
32 constexpr uint32_t MAX_LIST_SIZE = 10;
33 constexpr uint32_t SLEEP_SECOND_TIME = 5;
34 } // namespace
35 std::shared_ptr<NetPolicyFile> netPolicyFile_ = nullptr;
36 
37 using namespace testing::ext;
38 class UtNetPolicyFile : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp();
43     void TearDown();
44     std::set<uint32_t> white_;
45     std::set<uint32_t> black_;
46 };
47 
SetUpTestCase()48 void UtNetPolicyFile::SetUpTestCase()
49 {
50     netPolicyFile_ = DelayedSingleton<NetPolicyFile>::GetInstance();
51     ASSERT_TRUE(DelayedSingleton<NetPolicyFile>::GetInstance());
52     netPolicyFile_->InitPolicy();
53 }
54 
TearDownTestCase()55 void UtNetPolicyFile::TearDownTestCase()
56 {
57     sleep(SLEEP_SECOND_TIME);
58     netPolicyFile_.reset();
59 }
60 
SetUp()61 void UtNetPolicyFile::SetUp()
62 {
63     netPolicyFile_->ReadFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, white_, black_);
64 }
65 
TearDown()66 void UtNetPolicyFile::TearDown()
67 {
68     netPolicyFile_->WriteFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, white_, black_);
69 }
70 
71 /**
72  * @tc.name: NetPolicyFile001
73  * @tc.desc: Test NetPolicyFile ReadFirewallRules.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(UtNetPolicyFile, NetPolicyFile001, TestSize.Level1)
77 {
78     std::set<uint32_t> allowedList;
79     std::set<uint32_t> deniedList;
80     for (uint32_t i = 0; i <= MAX_LIST_SIZE; i++) {
81         allowedList.insert(i);
82         deniedList.insert(i);
83     }
84 
85     netPolicyFile_->WriteFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, allowedList, deniedList);
86     std::set<uint32_t> allowedList1;
87     std::set<uint32_t> deniedList1;
88     netPolicyFile_->ReadFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, allowedList1, deniedList1);
89     ASSERT_TRUE(allowedList == allowedList1);
90     ASSERT_TRUE(deniedList == deniedList1);
91 }
92 
93 /**
94  * @tc.name: NetPolicyFile002
95  * @tc.desc: Test NetPolicyFile WriteFirewallRules.
96  * @tc.type: FUNC
97  */
98 HWTEST_F(UtNetPolicyFile, NetPolicyFile002, TestSize.Level1)
99 {
100     std::set<uint32_t> allowedList;
101     std::set<uint32_t> deniedList;
102     for (uint32_t i = 0; i <= MAX_LIST_SIZE; i++) {
103         allowedList.insert(i);
104         deniedList.insert(i);
105     }
106     netPolicyFile_->WriteFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, allowedList, deniedList);
107     sleep(SLEEP_SECOND_TIME);
108     netPolicyFile_->InitPolicy();
109     std::set<uint32_t> allowedList1;
110     std::set<uint32_t> deniedList1;
111     netPolicyFile_->ReadFirewallRules(FIREWALL_CHAIN_DEVICE_IDLE, allowedList1, deniedList1);
112     ASSERT_TRUE(allowedList == allowedList1);
113     ASSERT_TRUE(deniedList == deniedList1);
114 }
115 
116 /**
117  * @tc.name: Json2Obj001
118  * @tc.desc: Test Json2Obj WriteFirewallRules.
119  * @tc.type: FUNC
120  */
121 HWTEST_F(UtNetPolicyFile, Json2Obj001, TestSize.Level1)
122 {
123     std::string content = "";
124     NetPolicy netPolicy;
125     auto result = netPolicyFile_->Json2Obj(content, netPolicy);
126     EXPECT_EQ(result, false);
127 }
128 
129 /**
130  * @tc.name: AddBackgroundPolicy001
131  * @tc.desc: Test AddBackgroundPolicy WriteFirewallRules.
132  * @tc.type: FUNC
133  */
134 HWTEST_F(UtNetPolicyFile, AddBackgroundPolicy001, TestSize.Level1)
135 {
136     netPolicyFile_->netPolicy_.backgroundPolicyStatus = "";
137     netPolicyFile_->AddBackgroundPolicy(nullptr);
138     EXPECT_NE(netPolicyFile_->netPolicy_.backgroundPolicyStatus, "");
139 }
140 } // namespace NetManagerStandard
141 } // namespace OHOS
142