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