1 /*
2 * Copyright (c) 2022-2024 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 #include "net_policy_callback_test.h"
25 #include "net_policy_client.h"
26 #include "net_policy_firewall.h"
27 #include "net_policy_rule.h"
28 #include "netmanager_base_test_security.h"
29
30 namespace OHOS {
31 namespace NetManagerStandard {
32 namespace {
33 using namespace testing::ext;
34 constexpr int32_t WAIT_TIME_SECOND_LONG = 10;
35 constexpr int32_t WAIT_TIME_THIRTY_SECOND_LONG = 30;
36 constexpr int32_t INVALID_VALUE = 100;
37 constexpr uint32_t TEST_UID1 = 200;
38 constexpr uint32_t TEST_UID2 = 13000;
39 std::shared_ptr<NetPolicyRule> g_netPolicyRule = nullptr;
40 std::shared_ptr<NetPolicyFirewall> g_netPolicyFirewallR = nullptr;
41 } // namespace
42
43 class UtNetPolicyRule : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49 sptr<NetPolicyCallbackTest> GetINetPolicyCallbackSample() const;
50 };
51
SetUpTestCase()52 void UtNetPolicyRule::SetUpTestCase()
53 {
54 g_netPolicyRule = std::make_shared<NetPolicyRule>();
55 g_netPolicyFirewallR = std::make_shared<NetPolicyFirewall>();
56 g_netPolicyRule->Init();
57 g_netPolicyRule->ResetPolicies();
58 }
59
TearDownTestCase()60 void UtNetPolicyRule::TearDownTestCase()
61 {
62 g_netPolicyRule->TransPolicyToRule(TEST_UID2, NetUidPolicy::NET_POLICY_NONE);
63 g_netPolicyRule->TransPolicyToRule(TEST_UID1, NetUidPolicy::NET_POLICY_NONE);
64 g_netPolicyRule.reset();
65 }
66
SetUp()67 void UtNetPolicyRule::SetUp() {}
68
TearDown()69 void UtNetPolicyRule::TearDown() {}
70
GetINetPolicyCallbackSample() const71 sptr<NetPolicyCallbackTest> UtNetPolicyRule::GetINetPolicyCallbackSample() const
72 {
73 sptr<NetPolicyCallbackTest> callbackR = new (std::nothrow) NetPolicyCallbackTest();
74 return callbackR;
75 }
76
77 /**
78 * @tc.name: NetPolicyRule001
79 * @tc.desc: Test NetPolicyRule TransPolicyToRule.
80 * @tc.type: FUNC
81 */
82 HWTEST_F(UtNetPolicyRule, NetPolicyRule001, TestSize.Level1)
83 {
84 int32_t result = g_netPolicyRule->TransPolicyToRule(10000, 1);
85 ASSERT_EQ(result, NETMANAGER_SUCCESS);
86 }
87
88 /**
89 * @tc.name: NetPolicyRule002
90 * @tc.desc: Test NetPolicyRule IsUidNetAllowed.
91 * @tc.type: FUNC
92 */
93 HWTEST_F(UtNetPolicyRule, NetPolicyRule002, TestSize.Level1)
94 {
95 int32_t result = g_netPolicyRule->TransPolicyToRule(15000, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
96 ASSERT_EQ(result, NETMANAGER_SUCCESS);
97 bool isAllowed = false;
98 g_netPolicyRule->IsUidNetAllowed(15000, false, isAllowed);
99 ASSERT_TRUE(isAllowed);
100 }
101
102 /**
103 * @tc.name: NetPolicyRule003
104 * @tc.desc: Test NetPolicyRule GetPolicyByUid.
105 * @tc.type: FUNC
106 */
107 HWTEST_F(UtNetPolicyRule, NetPolicyRule003, TestSize.Level1)
108 {
109 int32_t result = g_netPolicyRule->TransPolicyToRule(16000, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
110 ASSERT_EQ(result, NETMANAGER_SUCCESS);
111 uint32_t policy = 0;
112 g_netPolicyRule->GetPolicyByUid(16000, policy);
113 ASSERT_EQ(policy, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
114 }
115
116 /**
117 * @tc.name: NetPolicyRule004
118 * @tc.desc: Test NetPolicyRule GetUidsByPolicy.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(UtNetPolicyRule, NetPolicyRule004, TestSize.Level1)
122 {
123 int32_t result = g_netPolicyRule->TransPolicyToRule(16000, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
124 ASSERT_EQ(result, NETMANAGER_SUCCESS);
125 int32_t result2 = g_netPolicyRule->TransPolicyToRule(17000, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
126 ASSERT_EQ(result2, NETMANAGER_SUCCESS);
127 int32_t result3 = g_netPolicyRule->TransPolicyToRule(18000, NetUidPolicy::NET_POLICY_REJECT_METERED_BACKGROUND);
128 ASSERT_EQ(result3, NETMANAGER_SUCCESS);
129 int32_t result4 = g_netPolicyRule->TransPolicyToRule(19000, NetUidPolicy::NET_POLICY_REJECT_METERED_BACKGROUND);
130 ASSERT_EQ(result4, NETMANAGER_SUCCESS);
131
132 std::vector<uint32_t> uids;
133 g_netPolicyRule->GetUidsByPolicy(NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND, uids);
134
135 bool result5 = false;
136 bool result6 = false;
137 for (const auto &i : uids) {
138 if (i == 16000) {
139 result5 = true;
140 }
141 }
142
143 for (const auto &i : uids) {
144 if (i == 17000) {
145 result6 = true;
146 }
147 }
148 ASSERT_TRUE(result5 && result6);
149 result5 = false;
150 result6 = false;
151 g_netPolicyRule->GetUidsByPolicy(NetUidPolicy::NET_POLICY_REJECT_METERED_BACKGROUND, uids);
152 for (const auto &i : uids) {
153 if (i == 18000) {
154 result5 = true;
155 }
156 }
157
158 for (const auto &i : uids) {
159 if (i == 19000) {
160 result6 = true;
161 }
162 }
163 ASSERT_TRUE(result5 && result6);
164 }
165
166 /**
167 * @tc.name: NetPolicyRule005
168 * @tc.desc: Test NetPolicyRule ResetPolicies.
169 * @tc.type: FUNC
170 */
171 HWTEST_F(UtNetPolicyRule, NetPolicyRule005, TestSize.Level1)
172 {
173 int32_t result = g_netPolicyRule->TransPolicyToRule(TEST_UID2, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
174 ASSERT_EQ(result, NETMANAGER_SUCCESS);
175
176 int32_t result2 = g_netPolicyRule->SetBackgroundPolicy(false);
177 ASSERT_EQ(result2, NETMANAGER_SUCCESS);
178
179 int32_t result3 = g_netPolicyRule->ResetPolicies();
180 ASSERT_EQ(result3, NETMANAGER_SUCCESS);
181
182 uint32_t policy = 0;
183 g_netPolicyRule->GetPolicyByUid(TEST_UID2, policy);
184 ASSERT_EQ(policy, NET_POLICY_NONE);
185 bool backgroundPolicy = false;
186 g_netPolicyRule->GetBackgroundPolicy(backgroundPolicy);
187 ASSERT_TRUE(backgroundPolicy);
188 }
189
190 /**
191 * @tc.name: NetPolicyRule006
192 * @tc.desc: Test NetPolicyRule SetBackgroundPolicy.
193 * @tc.type: FUNC
194 */
195 HWTEST_F(UtNetPolicyRule, NetPolicyRule006, TestSize.Level1)
196 {
197 int32_t result = g_netPolicyRule->TransPolicyToRule(TEST_UID2, NetUidPolicy::NET_POLICY_REJECT_METERED_BACKGROUND);
198 ASSERT_EQ(result, NETMANAGER_SUCCESS);
199 uint32_t backgroundPolicyOfUid = 0;
200 g_netPolicyRule->GetBackgroundPolicyByUid(TEST_UID2, backgroundPolicyOfUid);
201 ASSERT_EQ(backgroundPolicyOfUid, NetBackgroundPolicy::NET_BACKGROUND_POLICY_DISABLE);
202 }
203
204 /**
205 * @tc.name: NetPolicyRule007
206 * @tc.desc: Test NetPolicyRule GetBackgroundPolicy.
207 * @tc.type: FUNC
208 */
209 HWTEST_F(UtNetPolicyRule, NetPolicyRule007, TestSize.Level1)
210 {
211 g_netPolicyRule->SetBackgroundPolicy(false);
212 int32_t result = g_netPolicyRule->SetBackgroundPolicy(true);
213 ASSERT_EQ(result, NETMANAGER_SUCCESS);
214 bool backgroundPolicy;
215 g_netPolicyRule->GetBackgroundPolicy(backgroundPolicy);
216 ASSERT_TRUE(backgroundPolicy);
217 }
218
SetPolicyUid()219 void SetPolicyUid()
220 {
221 NetManagerBaseAccessToken token;
222 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetPolicyByUid(
223 TEST_UID1, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
224 ASSERT_EQ(result, NETMANAGER_SUCCESS);
225 }
226
SendMessage()227 void SendMessage()
228 {
229 NetManagerBaseAccessToken token;
230 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdlePolicy(true);
231 ASSERT_EQ(result, NETMANAGER_SUCCESS);
232 int32_t result2 = DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdleTrustlist({TEST_UID1}, true);
233 ASSERT_EQ(result2, NETMANAGER_SUCCESS);
234 }
235
236 /**
237 * @tc.name: NetPolicyRule008
238 * @tc.desc: Test NetPolicyRule HandleEvent.
239 * @tc.type: FUNC
240 */
241 HWTEST_F(UtNetPolicyRule, NetPolicyRule008, TestSize.Level1)
242 {
243 NetManagerBaseAccessToken token;
244 DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdlePolicy(false);
245 sptr<NetPolicyCallbackTest> callback = GetINetPolicyCallbackSample();
246 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->RegisterNetPolicyCallback(callback);
247 uint32_t rule = 0;
248 uint32_t rule2 = 0;
249 if (result == NETMANAGER_SUCCESS) {
250 std::thread setPolicy(SetPolicyUid);
251 callback->WaitFor(WAIT_TIME_SECOND_LONG);
252 setPolicy.join();
253 rule = callback->GetRule();
254 std::cout << "rule:" << rule << std::endl;
255 } else {
256 std::cout << "RegisterNetPolicyCallback failed!" << std::endl;
257 }
258 NetManagerBaseAccessToken token1;
259 int32_t result2 = DelayedSingleton<NetPolicyClient>::GetInstance()->UnregisterNetPolicyCallback(callback);
260 ASSERT_EQ(result2, NETMANAGER_SUCCESS);
261
262 NetManagerBaseAccessToken token2;
263 sptr<NetPolicyCallbackTest> callbackR = GetINetPolicyCallbackSample();
264 int32_t result3 = DelayedSingleton<NetPolicyClient>::GetInstance()->RegisterNetPolicyCallback(callbackR);
265 if (result3 == NETMANAGER_SUCCESS) {
266 std::thread sendMessage(SendMessage);
267 callbackR->WaitFor(WAIT_TIME_THIRTY_SECOND_LONG);
268 sendMessage.join();
269 rule2 = callbackR->GetRule();
270 std::cout << "rule2:" << rule2 << std::endl;
271 ASSERT_FALSE(rule2 != rule);
272 } else {
273 std::cout << "RegisterNetPolicyCallbackR failed!" << std::endl;
274 }
275 NetManagerBaseAccessToken token3;
276 int32_t result4 = DelayedSingleton<NetPolicyClient>::GetInstance()->UnregisterNetPolicyCallback(callbackR);
277 ASSERT_EQ(result4, NETMANAGER_SUCCESS);
278 }
279
280 /**
281 * @tc.name: NetPolicyRuleBranchTest001
282 * @tc.desc: Test NetPolicyRule Branch.
283 * @tc.type: FUNC
284 */
285 HWTEST_F(UtNetPolicyRule, NetPolicyRuleBranchTest001, TestSize.Level1)
286 {
287 g_netPolicyRule->DeleteUid(TEST_UID2);
288
289 auto policyEvent = std::make_shared<PolicyEvent>();
290 int32_t eventId = static_cast<int32_t>(NetPolicyEventHandler::MSG_UID_REMOVED);
291 g_netPolicyRule->HandleEvent(eventId, policyEvent);
292
293 eventId = static_cast<int32_t>(NetPolicyEventHandler::MSG_UID_STATE_FOREGROUND);
294 g_netPolicyRule->HandleEvent(eventId, policyEvent);
295
296 eventId = static_cast<int32_t>(NetPolicyEventHandler::MSG_UID_STATE_BACKGROUND);
297 g_netPolicyRule->HandleEvent(eventId, policyEvent);
298
299 eventId = INVALID_VALUE;
300 g_netPolicyRule->HandleEvent(eventId, policyEvent);
301
302 g_netPolicyRule->UpdateForegroundUidList(TEST_UID2, false);
303 g_netPolicyRule->UpdateForegroundUidList(TEST_UID2, true);
304
305 std::string message = "";
306 g_netPolicyRule->GetDumpMessage(message);
307
308 bool ret = g_netPolicyRule->IsValidNetPolicy(INVALID_VALUE);
309 ASSERT_FALSE(ret);
310 }
311 } // namespace NetManagerStandard
312 } // namespace OHOS
313