1 /*
2 * Copyright (c) 2022 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_mgr_log_wrapper.h"
26 #include "net_policy_callback_test.h"
27 #include "net_policy_client.h"
28 #include "net_policy_constants.h"
29 #include "net_policy_firewall.h"
30 #include "net_policy_inner_define.h"
31
32 namespace OHOS {
33 namespace NetManagerStandard {
34 const std::string TEST_STRING_PERIODDURATION = "M1";
35 const std::string ICCID_1 = "sim_abcdefg_1";
36 const std::string ICCID_2 = "sim_abcdefg_2";
37
38 static std::shared_ptr<NetPolicyFirewall> netPolicyFirewall_ = nullptr;
39
40 using namespace testing::ext;
41 class UtNetPolicyFirewall : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp();
46 void TearDown();
47 sptr<NetPolicyCallbackTest> GetINetPolicyCallbackSample() const;
48 };
49
SetUpTestCase()50 void UtNetPolicyFirewall::SetUpTestCase()
51 {
52 netPolicyFirewall_ = std::make_shared<NetPolicyFirewall>();
53 netPolicyFirewall_->Init();
54 }
55
TearDownTestCase()56 void UtNetPolicyFirewall::TearDownTestCase()
57 {
58 netPolicyFirewall_.reset();
59 }
60
SetUp()61 void UtNetPolicyFirewall::SetUp() {}
62
TearDown()63 void UtNetPolicyFirewall::TearDown() {}
64
65 /**
66 * @tc.name: NetPolicyFirewall001
67 * @tc.desc: Test NetPolicyFirewall SetDeviceIdleTrustlist.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall001, TestSize.Level1)
71 {
72 const uint32_t uid = 123;
73 netPolicyFirewall_->SetDeviceIdleTrustlist({uid}, false);
74 std::vector<uint32_t> allowedList;
75 netPolicyFirewall_->GetDeviceIdleTrustlist(allowedList);
76 ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) == allowedList.end());
77 }
78
79 /**
80 * @tc.name: NetPolicyFirewall002
81 * @tc.desc: Test NetPolicyFirewall GetDeviceIdleTrustlist.
82 * @tc.type: FUNC
83 */
84 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall002, TestSize.Level1)
85 {
86 const uint32_t uid = 456;
87 netPolicyFirewall_->SetDeviceIdleTrustlist({uid}, true);
88 std::vector<uint32_t> allowedList;
89 netPolicyFirewall_->GetDeviceIdleTrustlist(allowedList);
90 ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) != allowedList.end());
91 }
92
93 /**
94 * @tc.name: NetPolicyFirewall003
95 * @tc.desc: Test NetPolicyFirewall UpdateDeviceIdlePolicy
96 * @tc.type: FUNC
97 */
98 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall003, TestSize.Level1)
99 {
100 const uint32_t uid = 789;
101 netPolicyFirewall_->SetDeviceIdleTrustlist({uid}, true);
102 netPolicyFirewall_->UpdateDeviceIdlePolicy(true);
103 std::vector<uint32_t> allowedList;
104 netPolicyFirewall_->GetDeviceIdleTrustlist(allowedList);
105 ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) != allowedList.end());
106 }
107
108 /**
109 * @tc.name: NetPolicyFirewall004
110 * @tc.desc: Test NetPolicyFirewall ResetPolicies
111 * @tc.type: FUNC
112 */
113 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall004, TestSize.Level1)
114 {
115 netPolicyFirewall_->ResetPolicies();
116 std::vector<uint32_t> allowedList;
117 netPolicyFirewall_->GetDeviceIdleTrustlist(allowedList);
118 ASSERT_TRUE(allowedList.size() == 0);
119 }
120
121 /**
122 * @tc.name: NetPolicyFirewall005
123 * @tc.desc: Test NetPolicyFirewall HandleEvent.
124 * @tc.type: FUNC
125 */
126 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall005, TestSize.Level1)
127 {
128 const uint32_t uid = 101;
129 netPolicyFirewall_->SetDeviceIdleTrustlist({uid}, true);
130 std::vector<uint32_t> allowedList;
131 netPolicyFirewall_->GetDeviceIdleTrustlist(allowedList);
132 ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) != allowedList.end());
133 auto policyEvent = std::make_shared<PolicyEvent>();
134 policyEvent->deletedUid = uid;
135 netPolicyFirewall_->HandleEvent(NetPolicyEventHandler::MSG_UID_REMOVED, policyEvent);
136 netPolicyFirewall_->GetDeviceIdleTrustlist(allowedList);
137 ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) == allowedList.end());
138 }
139
140 /**
141 * @tc.name: NetPolicyFirewall006
142 * @tc.desc: Test NetPolicyFirewall UpdateDeviceIdlePolicy.
143 * @tc.type: FUNC
144 */
145 HWTEST_F(UtNetPolicyFirewall, NetPolicyFirewall006, TestSize.Level1)
146 {
147 netPolicyFirewall_->UpdateDeviceIdlePolicy(false);
148 netPolicyFirewall_->UpdateDeviceIdlePolicy(true);
149 int32_t ret = netPolicyFirewall_->UpdateDeviceIdlePolicy(true);
150 EXPECT_EQ(ret, NETMANAGER_ERR_STATUS_EXIST);
151 }
152
153 /**
154 * @tc.name: SetDeviceIdleTrustlist001
155 * @tc.desc: Test SetDeviceIdleTrustlist Func.
156 * @tc.type: FUNC
157 */
158 HWTEST_F(UtNetPolicyFirewall, SetDeviceIdleTrustlist001, TestSize.Level1)
159 {
160 const uint32_t uid = 101;
161 std::vector<uint32_t> initialUids(1001);
162 for (size_t i = 0; i < initialUids.size(); ++i) {
163 initialUids[i] = i;
164 }
165 for (uint32_t testuid : initialUids) {
166 netPolicyFirewall_->powerSaveAllowedList_.insert(testuid);
167 }
168 auto result = netPolicyFirewall_->SetDeviceIdleTrustlist({uid}, true);
169 EXPECT_EQ(result, NETMANAGER_ERR_PARAMETER_ERROR);
170 }
171
172 /**
173 * @tc.name: UpdateFirewallPolicyList001
174 * @tc.desc: Test UpdateFirewallPolicyList Func.
175 * @tc.type: FUNC
176 */
177 HWTEST_F(UtNetPolicyFirewall, UpdateFirewallPolicyList001, TestSize.Level1)
178 {
179 uint32_t chainType = 17;
180 std::vector<uint32_t> uids = {1, 2, 3};
181 bool isAllowed = true;
182 netPolicyFirewall_->powerSaveAllowedList_.clear();
183 netPolicyFirewall_->UpdateFirewallPolicyList(chainType, uids, isAllowed);
184 EXPECT_EQ(netPolicyFirewall_->powerSaveAllowedList_.size(), 3);
185 }
186
187 /**
188 * @tc.name: UpdatePowerSavePolicy001
189 * @tc.desc: Test UpdatePowerSavePolicy Func.
190 * @tc.type: FUNC
191 */
192 HWTEST_F(UtNetPolicyFirewall, UpdatePowerSavePolicy001, TestSize.Level1)
193 {
194 bool enable = false;
195 netPolicyFirewall_->powerSaveMode_ = true;
196 auto result = netPolicyFirewall_->UpdatePowerSavePolicy(enable);
197 EXPECT_NE(result, NETMANAGER_ERR_STATUS_EXIST);
198 EXPECT_NE(result, NETMANAGER_ERR_LOCAL_PTR_NULL);
199 }
200
201 /**
202 * @tc.name: ResetPolicies001
203 * @tc.desc: Test ResetPolicies ResetPolicies Func.
204 * @tc.type: FUNC
205 */
206 HWTEST_F(UtNetPolicyFirewall, ResetPolicies001, TestSize.Level1)
207 {
208 EXPECT_NE(netPolicyFirewall_->powerSaveFirewallRule_, nullptr);
209 netPolicyFirewall_->powerSaveFirewallRule_ = nullptr;
210 netPolicyFirewall_->ResetPolicies();
211 }
212
213 /**
214 * @tc.name: HandleEvent001
215 * @tc.desc: Test NetPolicyFirewall HandleEvent Func.
216 * @tc.type: FUNC
217 */
218 HWTEST_F(UtNetPolicyFirewall, HandleEvent001, TestSize.Level1)
219 {
220 const uint32_t uid = 101;
221 netPolicyFirewall_->SetDeviceIdleTrustlist({uid}, true);
222 std::vector<uint32_t> allowedList;
223 netPolicyFirewall_->GetDeviceIdleTrustlist(allowedList);
224 ASSERT_TRUE(std::find(allowedList.begin(), allowedList.end(), uid) != allowedList.end());
225 auto policyEvent = std::make_shared<PolicyEvent>();
226 netPolicyFirewall_->HandleEvent(NetPolicyEventHandler::MSG_POWER_SAVE_MODE_CHANGED, policyEvent);
227 netPolicyFirewall_->HandleEvent(NetPolicyEventHandler::MSG_DEVICE_IDLE_MODE_CHANGED, policyEvent);
228 netPolicyFirewall_->HandleEvent(NetPolicyEventHandler::MSG_DEVICE_IDLE_LIST_UPDATED, policyEvent);
229 ASSERT_FALSE(std::find(allowedList.begin(), allowedList.end(), uid) == allowedList.end());
230 }
231
232 HWTEST_F(UtNetPolicyFirewall, SetPowerSaveTrustlist001, TestSize.Level1)
233 {
234 std::vector<uint32_t> uids(1001);
235 bool isAllowed = true;
236 for (uint32_t i = 0; i < 1001; ++i) {
237 netPolicyFirewall_->powerSaveAllowedList_.insert(i);
238 }
239 int32_t ret = netPolicyFirewall_->SetPowerSaveTrustlist(uids, isAllowed);
240 EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
241 netPolicyFirewall_->powerSaveAllowedList_.clear();
242 }
243 } // namespace NetManagerStandard
244 } // namespace OHOS
245