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 <thread>
17
18 #include <gtest/gtest.h>
19
20 #include "accesstoken_kit.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23
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
29 namespace OHOS {
30 namespace NetManagerStandard {
31 namespace {
32 using namespace testing::ext;
33 using namespace Security::AccessToken;
34 using Security::AccessToken::AccessTokenID;
35 constexpr int32_t WAIT_TIME_SECOND_LONG = 10;
36 constexpr int32_t WAIT_TIME_THIRTY_SECOND_LONG = 30;
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 HapInfoParams testInfoParms = {.userID = 1,
42 .bundleName = "net_policy_manager_test",
43 .instIndex = 0,
44 .appIDDesc = "test",
45 .isSystemApp = true};
46
47 PermissionDef testPermDef = {.permissionName = "ohos.permission.MANAGE_NET_STRATEGY",
48 .bundleName = "net_policy_manager_test",
49 .grantMode = 1,
50 .availableLevel = APL_SYSTEM_BASIC,
51 .label = "label",
52 .labelId = 1,
53 .description = "Test net policy connectivity internal",
54 .descriptionId = 1};
55
56 PermissionStateFull testState = {.permissionName = "ohos.permission.MANAGE_NET_STRATEGY",
57 .isGeneral = true,
58 .resDeviceID = {"local"},
59 .grantStatus = {PermissionState::PERMISSION_GRANTED},
60 .grantFlags = {2}};
61
62 HapPolicyParams testPolicyPrams = {.apl = APL_SYSTEM_BASIC,
63 .domain = "test.domain",
64 .permList = {testPermDef},
65 .permStateList = {testState}};
66
67 HapInfoParams testInfoParms2 = {.userID = 1,
68 .bundleName = "net_policy_manager_test",
69 .instIndex = 0,
70 .appIDDesc = "test",
71 .isSystemApp = true};
72
73 PermissionDef testPermDef2 = {.permissionName = "ohos.permission.MANAGE_NET_STRATEGY",
74 .bundleName = "net_policy_manager_test",
75 .grantMode = 1,
76 .availableLevel = APL_SYSTEM_BASIC,
77 .label = "label",
78 .labelId = 1,
79 .description = "Test net policy connectivity internal",
80 .descriptionId = 1};
81
82 PermissionStateFull testState2 = {.permissionName = "ohos.permission.MANAGE_NET_STRATEGY",
83 .isGeneral = true,
84 .resDeviceID = {"local"},
85 .grantStatus = {PermissionState::PERMISSION_GRANTED},
86 .grantFlags = {2}};
87
88 HapPolicyParams testPolicyPrams2 = {.apl = APL_SYSTEM_BASIC,
89 .domain = "test.domain",
90 .permList = {testPermDef2},
91 .permStateList = {testState2}};
92 } // namespace
93
94 class AccessToken {
95 public:
AccessToken(HapInfoParams & testInfoParms,HapPolicyParams & testPolicyPrams)96 AccessToken(HapInfoParams &testInfoParms, HapPolicyParams &testPolicyPrams) : currentID_(GetSelfTokenID())
97 {
98 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
99 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
100 SetSelfTokenID(tokenIdEx.tokenIDEx);
101 }
~AccessToken()102 ~AccessToken()
103 {
104 AccessTokenKit::DeleteToken(accessID_);
105 SetSelfTokenID(currentID_);
106 }
107
108 private:
109 AccessTokenID currentID_;
110 AccessTokenID accessID_ = 0;
111 };
112
113 class UtNetPolicyRule : public testing::Test {
114 public:
115 static void SetUpTestCase();
116 static void TearDownTestCase();
117 void SetUp();
118 void TearDown();
119 sptr<NetPolicyCallbackTest> GetINetPolicyCallbackSample() const;
120 };
121
SetUpTestCase()122 void UtNetPolicyRule::SetUpTestCase()
123 {
124 g_netPolicyRule = std::make_shared<NetPolicyRule>();
125 g_netPolicyFirewallR = std::make_shared<NetPolicyFirewall>();
126 g_netPolicyRule->Init();
127 g_netPolicyRule->ResetPolicies();
128 }
129
TearDownTestCase()130 void UtNetPolicyRule::TearDownTestCase()
131 {
132 g_netPolicyRule->TransPolicyToRule(TEST_UID2, NetUidPolicy::NET_POLICY_NONE);
133 g_netPolicyRule->TransPolicyToRule(TEST_UID1, NetUidPolicy::NET_POLICY_NONE);
134 g_netPolicyRule.reset();
135 }
136
SetUp()137 void UtNetPolicyRule::SetUp() {}
138
TearDown()139 void UtNetPolicyRule::TearDown() {}
140
GetINetPolicyCallbackSample() const141 sptr<NetPolicyCallbackTest> UtNetPolicyRule::GetINetPolicyCallbackSample() const
142 {
143 sptr<NetPolicyCallbackTest> callbackR = new (std::nothrow) NetPolicyCallbackTest();
144 return callbackR;
145 }
146
147 /**
148 * @tc.name: NetPolicyRule001
149 * @tc.desc: Test NetPolicyRule TransPolicyToRule.
150 * @tc.type: FUNC
151 */
152 HWTEST_F(UtNetPolicyRule, NetPolicyRule001, TestSize.Level1)
153 {
154 int32_t result = g_netPolicyRule->TransPolicyToRule(10000, 1);
155 ASSERT_EQ(result, NETMANAGER_SUCCESS);
156 }
157
158 /**
159 * @tc.name: NetPolicyRule002
160 * @tc.desc: Test NetPolicyRule IsUidNetAllowed.
161 * @tc.type: FUNC
162 */
163 HWTEST_F(UtNetPolicyRule, NetPolicyRule002, TestSize.Level1)
164 {
165 int32_t result = g_netPolicyRule->TransPolicyToRule(15000, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
166 ASSERT_EQ(result, NETMANAGER_SUCCESS);
167 bool isAllowed = false;
168 g_netPolicyRule->IsUidNetAllowed(15000, false, isAllowed);
169 ASSERT_TRUE(isAllowed);
170 }
171
172 /**
173 * @tc.name: NetPolicyRule003
174 * @tc.desc: Test NetPolicyRule GetPolicyByUid.
175 * @tc.type: FUNC
176 */
177 HWTEST_F(UtNetPolicyRule, NetPolicyRule003, TestSize.Level1)
178 {
179 int32_t result = g_netPolicyRule->TransPolicyToRule(16000, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
180 ASSERT_EQ(result, NETMANAGER_SUCCESS);
181 uint32_t policy = 0;
182 g_netPolicyRule->GetPolicyByUid(16000, policy);
183 ASSERT_EQ(policy, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
184 }
185
186 /**
187 * @tc.name: NetPolicyRule004
188 * @tc.desc: Test NetPolicyRule GetUidsByPolicy.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(UtNetPolicyRule, NetPolicyRule004, TestSize.Level1)
192 {
193 int32_t result = g_netPolicyRule->TransPolicyToRule(16000, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
194 ASSERT_EQ(result, NETMANAGER_SUCCESS);
195 int32_t result2 = g_netPolicyRule->TransPolicyToRule(17000, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
196 ASSERT_EQ(result2, NETMANAGER_SUCCESS);
197 int32_t result3 = g_netPolicyRule->TransPolicyToRule(18000, NetUidPolicy::NET_POLICY_REJECT_METERED_BACKGROUND);
198 ASSERT_EQ(result3, NETMANAGER_SUCCESS);
199 int32_t result4 = g_netPolicyRule->TransPolicyToRule(19000, NetUidPolicy::NET_POLICY_REJECT_METERED_BACKGROUND);
200 ASSERT_EQ(result4, NETMANAGER_SUCCESS);
201
202 std::vector<uint32_t> uids;
203 g_netPolicyRule->GetUidsByPolicy(NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND, uids);
204
205 bool result5 = false;
206 bool result6 = false;
207 for (const auto &i : uids) {
208 if (i == 16000) {
209 result5 = true;
210 }
211 }
212
213 for (const auto &i : uids) {
214 if (i == 17000) {
215 result6 = true;
216 }
217 }
218 ASSERT_TRUE(result5 && result6);
219 result5 = false;
220 result6 = false;
221 g_netPolicyRule->GetUidsByPolicy(NetUidPolicy::NET_POLICY_REJECT_METERED_BACKGROUND, uids);
222 for (const auto &i : uids) {
223 if (i == 18000) {
224 result5 = true;
225 }
226 }
227
228 for (const auto &i : uids) {
229 if (i == 19000) {
230 result6 = true;
231 }
232 }
233 ASSERT_TRUE(result5 && result6);
234 }
235
236 /**
237 * @tc.name: NetPolicyRule005
238 * @tc.desc: Test NetPolicyRule ResetPolicies.
239 * @tc.type: FUNC
240 */
241 HWTEST_F(UtNetPolicyRule, NetPolicyRule005, TestSize.Level1)
242 {
243 int32_t result = g_netPolicyRule->TransPolicyToRule(TEST_UID2, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
244 ASSERT_EQ(result, NETMANAGER_SUCCESS);
245
246 int32_t result2 = g_netPolicyRule->SetBackgroundPolicy(false);
247 ASSERT_EQ(result2, NETMANAGER_SUCCESS);
248
249 int32_t result3 = g_netPolicyRule->ResetPolicies();
250 ASSERT_EQ(result3, NETMANAGER_SUCCESS);
251
252 uint32_t policy = 0;
253 g_netPolicyRule->GetPolicyByUid(TEST_UID2, policy);
254 ASSERT_EQ(policy, NET_POLICY_NONE);
255 bool backgroundPolicy = false;
256 g_netPolicyRule->GetBackgroundPolicy(backgroundPolicy);
257 ASSERT_TRUE(backgroundPolicy);
258 }
259
260 /**
261 * @tc.name: NetPolicyRule006
262 * @tc.desc: Test NetPolicyRule SetBackgroundPolicy.
263 * @tc.type: FUNC
264 */
265 HWTEST_F(UtNetPolicyRule, NetPolicyRule006, TestSize.Level1)
266 {
267 int32_t result = g_netPolicyRule->TransPolicyToRule(TEST_UID2, NetUidPolicy::NET_POLICY_REJECT_METERED_BACKGROUND);
268 ASSERT_EQ(result, NETMANAGER_SUCCESS);
269 uint32_t backgroundPolicyOfUid = 0;
270 g_netPolicyRule->GetBackgroundPolicyByUid(TEST_UID2, backgroundPolicyOfUid);
271 ASSERT_EQ(backgroundPolicyOfUid, NetBackgroundPolicy::NET_BACKGROUND_POLICY_DISABLE);
272 }
273
274 /**
275 * @tc.name: NetPolicyRule007
276 * @tc.desc: Test NetPolicyRule GetBackgroundPolicy.
277 * @tc.type: FUNC
278 */
279 HWTEST_F(UtNetPolicyRule, NetPolicyRule007, TestSize.Level1)
280 {
281 g_netPolicyRule->SetBackgroundPolicy(false);
282 int32_t result = g_netPolicyRule->SetBackgroundPolicy(true);
283 ASSERT_EQ(result, NETMANAGER_SUCCESS);
284 bool backgroundPolicy;
285 g_netPolicyRule->GetBackgroundPolicy(backgroundPolicy);
286 ASSERT_TRUE(backgroundPolicy);
287 }
288
SetPolicyUid()289 void SetPolicyUid()
290 {
291 AccessToken token2(testInfoParms2, testPolicyPrams2);
292 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetPolicyByUid(
293 TEST_UID1, NetUidPolicy::NET_POLICY_ALLOW_METERED_BACKGROUND);
294 ASSERT_EQ(result, NETMANAGER_SUCCESS);
295 }
296
SendMessage()297 void SendMessage()
298 {
299 AccessToken token(testInfoParms, testPolicyPrams);
300 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdlePolicy(true);
301 ASSERT_EQ(result, NETMANAGER_SUCCESS);
302 int32_t result2 = DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdleTrustlist({TEST_UID1}, true);
303 ASSERT_EQ(result2, NETMANAGER_SUCCESS);
304 }
305
306 /**
307 * @tc.name: NetPolicyRule008
308 * @tc.desc: Test NetPolicyRule HandleEvent.
309 * @tc.type: FUNC
310 */
311 HWTEST_F(UtNetPolicyRule, NetPolicyRule008, TestSize.Level1)
312 {
313 AccessToken token(testInfoParms, testPolicyPrams);
314 DelayedSingleton<NetPolicyClient>::GetInstance()->SetDeviceIdlePolicy(false);
315 sptr<NetPolicyCallbackTest> callback = GetINetPolicyCallbackSample();
316 int32_t result = DelayedSingleton<NetPolicyClient>::GetInstance()->RegisterNetPolicyCallback(callback);
317 uint32_t rule = 0;
318 uint32_t rule2 = 0;
319 if (result == NETMANAGER_SUCCESS) {
320 std::thread setPolicy(SetPolicyUid);
321 callback->WaitFor(WAIT_TIME_SECOND_LONG);
322 setPolicy.join();
323 rule = callback->GetRule();
324 std::cout << "rule:" << rule << std::endl;
325 } else {
326 std::cout << "RegisterNetPolicyCallback failed!" << std::endl;
327 }
328 AccessToken token2(testInfoParms, testPolicyPrams);
329 int32_t result2 = DelayedSingleton<NetPolicyClient>::GetInstance()->UnregisterNetPolicyCallback(callback);
330 ASSERT_EQ(result2, NETMANAGER_SUCCESS);
331
332 sptr<NetPolicyCallbackTest> callbackR = GetINetPolicyCallbackSample();
333 AccessToken token3(testInfoParms, testPolicyPrams);
334 int32_t result3 = DelayedSingleton<NetPolicyClient>::GetInstance()->RegisterNetPolicyCallback(callbackR);
335 if (result3 == NETMANAGER_SUCCESS) {
336 std::thread sendMessage(SendMessage);
337 callbackR->WaitFor(WAIT_TIME_THIRTY_SECOND_LONG);
338 sendMessage.join();
339 rule2 = callbackR->GetRule();
340 std::cout << "rule2:" << rule2 << std::endl;
341 ASSERT_TRUE(rule2 != rule);
342 } else {
343 std::cout << "RegisterNetPolicyCallbackR failed!" << std::endl;
344 }
345 AccessToken token4(testInfoParms, testPolicyPrams);
346 int32_t result4 = DelayedSingleton<NetPolicyClient>::GetInstance()->UnregisterNetPolicyCallback(callbackR);
347 ASSERT_EQ(result4, NETMANAGER_SUCCESS);
348 }
349 } // namespace NetManagerStandard
350 } // namespace OHOS
351