• 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 <gtest/gtest.h>
17 
18 #include "accesstoken_kit.h"
19 #include "message_parcel.h"
20 #include "network.h"
21 #include "net_conn_client.h"
22 #include "net_conn_constants.h"
23 #include "net_conn_types.h"
24 #include "net_manager_constants.h"
25 #include "net_mgr_log_wrapper.h"
26 #include "token_setproc.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace {
31 using namespace testing::ext;
32 using namespace Security::AccessToken;
33 using Security::AccessToken::AccessTokenID;
34 
35 HapInfoParams testInfoParms = {.bundleName = "net_conn_manager_test",
36                                .userID = 1,
37                                .instIndex = 0,
38                                .appIDDesc = "test"};
39 
40 PermissionDef testPermDef = {
41     .permissionName = "ohos.permission.GET_NETWORK_INFO",
42     .bundleName = "net_conn_manager_test",
43     .grantMode = 1,
44     .label = "label",
45     .labelId = 1,
46     .description = "Test net connect maneger",
47     .descriptionId = 1,
48     .availableLevel = APL_SYSTEM_BASIC,
49 };
50 
51 PermissionDef testInternalPermDef = {
52     .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
53     .bundleName = "net_conn_manager_test",
54     .grantMode = 1,
55     .availableLevel = APL_SYSTEM_BASIC,
56     .label = "label",
57     .labelId = 1,
58     .description = "Test net connect manager internet",
59     .descriptionId = 1,
60 };
61 
62 PermissionStateFull testState = {
63     .grantFlags = {2},
64     .grantStatus = {PermissionState::PERMISSION_GRANTED},
65     .isGeneral = true,
66     .permissionName = "ohos.permission.GET_NETWORK_INFO",
67     .resDeviceID = {"local"},
68 };
69 
70 PermissionStateFull testInternalState = {
71     .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
72     .isGeneral = true,
73     .resDeviceID = {"local"},
74     .grantStatus = {PermissionState::PERMISSION_GRANTED},
75     .grantFlags = {2},
76 };
77 
78 HapPolicyParams testPolicyPrams = {
79     .apl = APL_SYSTEM_BASIC,
80     .domain = "test.domain",
81     .permList = {testPermDef, testInternalPermDef},
82     .permStateList = {testState, testInternalState},
83 };
84 } // namespace
85 
86 class NetSupplierCallbackBaseTest : public NetSupplierCallbackBase {
87 public:
88     virtual ~NetSupplierCallbackBaseTest() = default;
89 
RequestNetwork(const std::string & ident,const std::set<NetCap> & netCaps)90     int32_t RequestNetwork(const std::string &ident, const std::set<NetCap> &netCaps) override
91     {
92         return NETMANAGER_SUCCESS;
93     };
94 
ReleaseNetwork(const std::string & ident,const std::set<NetCap> & netCaps)95     int32_t ReleaseNetwork(const std::string &ident, const std::set<NetCap> &netCaps) override
96     {
97         return NETMANAGER_SUCCESS;
98     };
99 };
100 class AccessToken {
101 public:
AccessToken()102     AccessToken() : currentID_(GetSelfTokenID())
103     {
104         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
105         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
106         SetSelfTokenID(accessID_);
107     }
~AccessToken()108     ~AccessToken()
109     {
110         AccessTokenKit::DeleteToken(accessID_);
111         SetSelfTokenID(currentID_);
112     }
113 
114 private:
115     AccessTokenID currentID_;
116     AccessTokenID accessID_ = 0;
117 };
118 
119 class NetConnClientTest : public testing::Test {
120 public:
121     static void SetUpTestCase();
122     static void TearDownTestCase();
123     void SetUp();
124     void TearDown();
125 };
126 
SetUpTestCase()127 void NetConnClientTest::SetUpTestCase() {}
128 
TearDownTestCase()129 void NetConnClientTest::TearDownTestCase() {}
130 
SetUp()131 void NetConnClientTest::SetUp() {}
132 
TearDown()133 void NetConnClientTest::TearDown() {}
134 
135 /**
136  * @tc.name: GetDefaultNetTest001
137  * @tc.desc: Test NetConnClient::GetDefaultNet, not applying for
138  * permission,return NETMANAGER_ERR_PERMISSION_DENIED
139  * @tc.type: FUNC
140  */
141 HWTEST_F(NetConnClientTest, GetDefaultNetTest001, TestSize.Level1)
142 {
143     std::cout << "GetDefaultNetTest001 In" << std::endl;
144     NetHandle handle;
145     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->GetDefaultNet(handle);
146     ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
147 }
148 
149 /**
150  * @tc.name: GetDefaultNetTest002
151  * @tc.desc: Test NetConnClient::GetDefaultNet, not applying for
152  * permission,return NETMANAGER_SUCCESS
153  * @tc.type: FUNC
154  */
155 HWTEST_F(NetConnClientTest, GetDefaultNetTest002, TestSize.Level1)
156 {
157     std::cout << "GetDefaultNetTest002 In" << std::endl;
158     AccessToken token;
159     NetHandle handle;
160     int32_t netId = 0;
161     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->GetDefaultNet(handle);
162     netId = handle.GetNetId();
163     if (netId == 0) {
164         std::cout << "No network" << std::endl;
165         ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
166     } else if (netId >= 100 && netId <= MAX_NET_ID) {
167         std::cout << "Get default network id:" << netId << std::endl;
168         ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
169     } else {
170         ASSERT_FALSE(ret == NETMANAGER_SUCCESS);
171     }
172 }
173 
174 /**
175  * @tc.name: HasDefaultNetTest001
176  * @tc.desc: Test NetConnClient::HasDefaultNet,not applying for
177  * permission, return NETMANAGER_ERR_PERMISSION_DENIED
178  * @tc.type: FUNC
179  */
180 HWTEST_F(NetConnClientTest, HasDefaultNetTest001, TestSize.Level1)
181 {
182     bool bFlag = false;
183     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->HasDefaultNet(bFlag);
184     ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
185 }
186 
187 /**
188  * @tc.name: HasDefaultNetTest002
189  * @tc.desc: Test NetConnClient::HasDefaultNet, applying for
190  * permission, return NETMANAGER_SUCCESS
191  * @tc.type: FUNC
192  */
193 HWTEST_F(NetConnClientTest, HasDefaultNetTest002, TestSize.Level1)
194 {
195     AccessToken token;
196     bool bFlag = false;
197     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->HasDefaultNet(bFlag);
198     ASSERT_EQ(ret, NETMANAGER_SUCCESS);
199 }
200 
201 /**
202  * @tc.name: GetNetCapabilitiesTest001
203  * @tc.desc: Test NetConnClient::GetNetCapabilities, In the absence of
204  * permission, GetDefaultNet return NETMANAGER_ERR_PERMISSION_DENIED and
205  * GetNetCapabilities return NETMANAGER_ERR_PERMISSION_DENIED
206  * @tc.type: FUNC
207  */
208 HWTEST_F(NetConnClientTest, GetNetCapabilitiesTest001, TestSize.Level1)
209 {
210     NetHandle handle;
211     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->GetDefaultNet(handle);
212     ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
213 
214     NetAllCapabilities netAllCap;
215     ret = DelayedSingleton<NetConnClient>::GetInstance()->GetNetCapabilities(handle, netAllCap);
216     ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
217 }
218 
219 /**
220  * @tc.name: GetNetCapabilitiesTest002
221  * @tc.desc: Test NetConnClient::GetNetCapabilities:In the absence of
222  * permission, GetDefaultNet return NETMANAGER_ERR_PERMISSION_DENIED, and
223  * after add permission GetNetCapabilities return NET_CONN_ERR_INVALID_NETWORK
224  * @tc.type: FUNC
225  */
226 HWTEST_F(NetConnClientTest, GetNetCapabilitiesTest002, TestSize.Level1)
227 {
228     NetHandle handle;
229     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->GetDefaultNet(handle);
230     ASSERT_TRUE(ret == NETMANAGER_ERR_PERMISSION_DENIED);
231 
232     AccessToken token;
233     NetAllCapabilities netAllCap;
234     ret = DelayedSingleton<NetConnClient>::GetInstance()->GetNetCapabilities(handle, netAllCap);
235     ASSERT_TRUE(ret == NET_CONN_ERR_INVALID_NETWORK);
236 }
237 
238 /**
239  * @tc.name: GetNetCapabilitiesTest003
240  * @tc.desc: Test NetConnClient::GetNetCapabilities:Apply for permission at
241  * first, when net is connected,return NET_CONN_SUCCESS, or net is not connected,return
242  * NET_CONN_ERR_INVALID_NETWORK
243  * @tc.type: FUNC
244  */
245 HWTEST_F(NetConnClientTest, GetNetCapabilitiesTest003, TestSize.Level1)
246 {
247     AccessToken token;
248     NetHandle handle;
249     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->GetDefaultNet(handle);
250     ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
251 
252     NetAllCapabilities netAllCap;
253     ret = DelayedSingleton<NetConnClient>::GetInstance()->GetNetCapabilities(handle, netAllCap);
254     ASSERT_TRUE(ret == NETMANAGER_SUCCESS || ret == NET_CONN_ERR_INVALID_NETWORK);
255 }
256 
257 /**
258  * @tc.name: SetAirplaneModeTest
259  * @tc.desc: Test NetConnClient::SetAirplaneMode
260  * @tc.type: FUNC
261  */
262 HWTEST_F(NetConnClientTest, SetAirplaneModeTest, TestSize.Level1)
263 {
264     AccessToken token;
265     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->SetAirplaneMode(true);
266     ASSERT_EQ(ret, NETMANAGER_SUCCESS);
267 }
268 
269 /**
270  * @tc.name: IsDefaultNetMeteredTest001
271  * @tc.desc: if no permission,NetConnClient::IsDefaultNetMetered return NETMANAGER_ERR_PERMISSION_DENIED
272  * @tc.type: FUNC
273  */
274 HWTEST_F(NetConnClientTest, IsDefaultNetMeteredTest001, TestSize.Level1)
275 {
276     bool bRes = false;
277     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->IsDefaultNetMetered(bRes);
278     ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
279     ASSERT_TRUE(bRes == false);
280 }
281 
282 /**
283  * @tc.name: IsDefaultNetMeteredTest002
284  * @tc.desc: Test NetConnClient::IsDefaultNetMetered
285  * @tc.type: FUNC
286  */
287 HWTEST_F(NetConnClientTest, IsDefaultNetMeteredTest002, TestSize.Level1)
288 {
289     AccessToken token;
290     bool bRes = false;
291     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->IsDefaultNetMetered(bRes);
292     ASSERT_EQ(ret, NETMANAGER_SUCCESS);
293     ASSERT_EQ(bRes, true);
294 }
295 
296 /**
297  * @tc.name: SetGlobalHttpProxyTest001
298  * @tc.desc: Test NetConnClient::SetGlobalHttpProxy,if param is not null,SetGlobalHttpProxy return NET_CONN_SUCCESS
299  * @tc.type: FUNC
300  */
301 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest001, TestSize.Level1)
302 {
303     AccessToken token;
304     HttpProxy httpProxy = {"testHttpProxy", 0, {}};
305     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->SetGlobalHttpProxy(httpProxy);
306     ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
307 }
308 
309 /**
310  * @tc.name: SetGlobalHttpProxyTest002
311  * @tc.desc: Test NetConnClient::SetGlobalHttpProxy.if param is null, return NET_CONN_ERR_INTERNAL_ERROR
312  * @tc.type: FUNC
313  */
314 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest002, TestSize.Level1)
315 {
316     AccessToken token;
317     HttpProxy httpProxy;
318     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->SetGlobalHttpProxy(httpProxy);
319     ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
320 }
321 
322 /**
323  * @tc.name: GetGlobalHttpProxyTest001
324  * @tc.desc: Test NetConnClient::GetHttpProxy
325  * @tc.type: FUNC
326  */
327 HWTEST_F(NetConnClientTest, GetGlobalHttpProxyTest001, TestSize.Level1)
328 {
329     AccessToken token;
330     HttpProxy httpProxy = {"testHttpProxy", 0, {}};
331     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->SetGlobalHttpProxy(httpProxy);
332     ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
333 
334     HttpProxy getGlobalHttpProxy;
335     ret = DelayedSingleton<NetConnClient>::GetInstance()->GetGlobalHttpProxy(getGlobalHttpProxy);
336     ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
337     ASSERT_TRUE(getGlobalHttpProxy.GetHost() == "testHttpProxy");
338 }
339 
340 /**
341  * @tc.name: RegisterNetSupplierCallbackTest001
342  * @tc.desc: Test NetConnClient::RegisterNetSupplierCallback
343  * @tc.type: FUNC
344  */
345 HWTEST_F(NetConnClientTest, RegisterNetSupplierCallbackTest001, TestSize.Level1)
346 {
347     uint32_t supplierId = 100;
348     sptr<NetSupplierCallbackBase> callback = new (std::nothrow) NetSupplierCallbackBaseTest();
349     ASSERT_NE(callback, nullptr);
350     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->RegisterNetSupplierCallback(supplierId, callback);
351     EXPECT_EQ(ret, NET_CONN_ERR_NO_SUPPLIER);
352 }
353 
354 /**
355  * @tc.name: RegisterNetSupplierCallbackTest002
356  * @tc.desc: Test NetConnClient::RegisterNetSupplierCallback
357  * @tc.type: FUNC
358  */
359 HWTEST_F(NetConnClientTest, RegisterNetSupplierCallbackTest002, TestSize.Level1)
360 {
361     NetBearType bearerType = BEARER_CELLULAR;
362     std::set<NetCap> netCaps{NET_CAPABILITY_INTERNET};
363     std::string ident = "ident";
364     uint32_t supplierId = 0;
365     int32_t result =
366         DelayedSingleton<NetConnClient>::GetInstance()->RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
367     ASSERT_TRUE(result == NETMANAGER_SUCCESS);
368     sptr<NetSupplierCallbackBase> callback = new (std::nothrow) NetSupplierCallbackBaseTest();
369     ASSERT_NE(callback, nullptr);
370     auto ret = DelayedSingleton<NetConnClient>::GetInstance()->RegisterNetSupplierCallback(supplierId, callback);
371     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
372 }
373 } // namespace NetManagerStandard
374 } // namespace OHOS
375