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 <gtest/gtest.h>
17
18 #include "accesstoken_kit.h"
19 #include "message_parcel.h"
20 #ifdef GTEST_API_
21 #define private public
22 #endif
23 #include "net_conn_client.h"
24 #include "net_conn_constants.h"
25 #include "net_conn_types.h"
26 #include "net_interface_callback_stub.h"
27 #include "net_interface_config.h"
28 #include "net_manager_constants.h"
29 #include "net_mgr_log_wrapper.h"
30 #include "network.h"
31 #include "token_setproc.h"
32
33 #include "i_net_conn_callback.h"
34 #include "iremote_stub.h"
35
36 namespace OHOS {
37 namespace NetManagerStandard {
38 namespace {
39 using namespace testing::ext;
40 using namespace Security::AccessToken;
41 using Security::AccessToken::AccessTokenID;
42
43 constexpr const char *TEST_IPV4_ADDR = "127.0.0.1";
44 constexpr const char *TEST_IPV6_ADDR = "240C:1:1:1::1";
45 constexpr const char *TEST_DOMAIN1 = ".com";
46 constexpr const char *TEST_DOMAIN2 = "test.com";
47 constexpr const char *TEST_DOMAIN3 = "testcom";
48 constexpr const char *TEST_DOMAIN4 = "com.test";
49 constexpr const char *TEST_DOMAIN5 = "test.co.uk";
50 constexpr const char *TEST_DOMAIN6 = "test.com.com";
51 constexpr const char *TEST_DOMAIN7 = "test1.test2.test3.test4.test5.com";
52 constexpr const char *TEST_DOMAIN8 = "http://www.example.com";
53 constexpr const char *TEST_DOMAIN9 = "https://www.example.com";
54 constexpr const char *TEST_DOMAIN10 = "httpd://www.example.com";
55 constexpr const char *TEST_LONG_HOST =
56 "0123456789qwertyuiopasdfghjklzxcvbnm[]:;<>?!@#$%^&*()qwdqwrtfasfj4897qwe465791qwr87tq4fq7t8qt4654qwr";
57 constexpr const char *TEST_LONG_EXCLUSION_LIST =
58 "www.test0.com,www.test1.com,www.test2.com,www.test3.com,www.test4.com,www.test5.com,www.test6.com,www.test7.com,"
59 "www.test8.com,www.test9.com,www.test10.com,www.test11.com,www.test12.com,www.test12.com,www.test12.com,www.test13."
60 "com,www.test14.com,www.test15.com,www.test16.com,www.test17.com,www.test18.com,www.test19.com,www.test20.com";
61 constexpr const char *TEST_IFACE = "eth0";
62
63 HapInfoParams testInfoParms = {.bundleName = "net_conn_manager_test",
64 .userID = 1,
65 .instIndex = 0,
66 .appIDDesc = "test",
67 .isSystemApp = true};
68
69 PermissionDef testPermDef = {
70 .permissionName = "ohos.permission.GET_NETWORK_INFO",
71 .bundleName = "net_conn_manager_test",
72 .grantMode = 1,
73 .label = "label",
74 .labelId = 1,
75 .description = "Test net connect maneger",
76 .descriptionId = 1,
77 .availableLevel = APL_SYSTEM_BASIC,
78 };
79
80 PermissionDef testInternalPermDef = {
81 .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
82 .bundleName = "net_conn_manager_test",
83 .grantMode = 1,
84 .availableLevel = APL_SYSTEM_BASIC,
85 .label = "label",
86 .labelId = 1,
87 .description = "Test net connect manager internal",
88 .descriptionId = 1,
89 };
90
91 PermissionDef testInternetPermDef = {
92 .permissionName = "ohos.permission.INTERNET",
93 .bundleName = "net_conn_manager_test",
94 .grantMode = 1,
95 .availableLevel = APL_SYSTEM_BASIC,
96 .label = "label",
97 .labelId = 1,
98 .description = "Test net connect manager internet",
99 .descriptionId = 1,
100 };
101
102 PermissionStateFull testState = {
103 .grantFlags = {2},
104 .grantStatus = {PermissionState::PERMISSION_GRANTED},
105 .isGeneral = true,
106 .permissionName = "ohos.permission.GET_NETWORK_INFO",
107 .resDeviceID = {"local"},
108 };
109
110 PermissionStateFull testInternalState = {
111 .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
112 .isGeneral = true,
113 .resDeviceID = {"local"},
114 .grantStatus = {PermissionState::PERMISSION_GRANTED},
115 .grantFlags = {2},
116 };
117
118 PermissionStateFull testInternetState = {
119 .permissionName = "ohos.permission.INTERNET",
120 .isGeneral = true,
121 .resDeviceID = {"local"},
122 .grantStatus = {PermissionState::PERMISSION_GRANTED},
123 .grantFlags = {2},
124 };
125
126 HapPolicyParams testPolicyPrams = {
127 .apl = APL_SYSTEM_BASIC,
128 .domain = "test.domain",
129 .permList = {testPermDef, testInternalPermDef, testInternetPermDef},
130 .permStateList = {testState, testInternalState, testInternetState},
131 };
132 } // namespace
133
134 class NetSupplierCallbackBaseTest : public NetSupplierCallbackBase {
135 public:
136 virtual ~NetSupplierCallbackBaseTest() = default;
137
RequestNetwork(const std::string & ident,const std::set<NetCap> & netCaps)138 int32_t RequestNetwork(const std::string &ident, const std::set<NetCap> &netCaps) override
139 {
140 return NETMANAGER_SUCCESS;
141 };
142
ReleaseNetwork(const std::string & ident,const std::set<NetCap> & netCaps)143 int32_t ReleaseNetwork(const std::string &ident, const std::set<NetCap> &netCaps) override
144 {
145 return NETMANAGER_SUCCESS;
146 };
147 };
148 class AccessToken {
149 public:
AccessToken()150 AccessToken() : currentID_(GetSelfTokenID())
151 {
152 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
153 accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
154 SetSelfTokenID(tokenIdEx.tokenIDEx);
155 }
~AccessToken()156 ~AccessToken()
157 {
158 AccessTokenKit::DeleteToken(accessID_);
159 SetSelfTokenID(currentID_);
160 }
161
162 private:
163 AccessTokenID currentID_;
164 AccessTokenID accessID_ = 0;
165 };
166
167 class NetConnClientTest : public testing::Test {
168 public:
169 static void SetUpTestCase();
170 static void TearDownTestCase();
171 void SetUp();
172 void TearDown();
173 };
174
SetUpTestCase()175 void NetConnClientTest::SetUpTestCase() {}
176
TearDownTestCase()177 void NetConnClientTest::TearDownTestCase() {}
178
SetUp()179 void NetConnClientTest::SetUp() {}
180
TearDown()181 void NetConnClientTest::TearDown() {}
182
183 class INetConnCallbackTest : public IRemoteStub<INetConnCallback> {
184 public:
NetAvailable(sptr<NetHandle> & netHandle)185 int32_t NetAvailable(sptr<NetHandle> &netHandle)
186 {
187 return 0;
188 }
189
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)190 int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
191 {
192 return 0;
193 }
194
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)195 int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
196 {
197 return 0;
198 }
199
NetLost(sptr<NetHandle> & netHandle)200 int32_t NetLost(sptr<NetHandle> &netHandle)
201 {
202 return 0;
203 }
204
NetUnavailable()205 int32_t NetUnavailable()
206 {
207 return 0;
208 }
209
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)210 int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
211 {
212 return 0;
213 }
214 };
215
216 /**
217 * @tc.name: GetDefaultNetTest001
218 * @tc.desc: Test NetConnClient::GetDefaultNet, not applying for
219 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
220 * @tc.type: FUNC
221 */
222 HWTEST_F(NetConnClientTest, GetDefaultNetTest001, TestSize.Level1)
223 {
224 std::cout << "GetDefaultNetTest001 In" << std::endl;
225 NetHandle handle;
226 auto ret = NetConnClient::GetInstance().GetDefaultNet(handle);
227 ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
228 }
229
230 /**
231 * @tc.name: GetDefaultNetTest002
232 * @tc.desc: Test NetConnClient::GetDefaultNet, not applying for
233 * permission,return NETMANAGER_SUCCESS
234 * @tc.type: FUNC
235 */
236 HWTEST_F(NetConnClientTest, GetDefaultNetTest002, TestSize.Level1)
237 {
238 std::cout << "GetDefaultNetTest002 In" << std::endl;
239 AccessToken token;
240 NetHandle handle;
241 int32_t netId = 0;
242 auto ret = NetConnClient::GetInstance().GetDefaultNet(handle);
243 netId = handle.GetNetId();
244 if (netId == 0) {
245 std::cout << "No network" << std::endl;
246 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
247 } else if (netId >= 100 && netId <= MAX_NET_ID) {
248 std::cout << "Get default network id:" << netId << std::endl;
249 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
250 } else {
251 ASSERT_FALSE(ret == NETMANAGER_SUCCESS);
252 }
253 }
254
255 /**
256 * @tc.name: HasDefaultNetTest001
257 * @tc.desc: Test NetConnClient::HasDefaultNet,not applying for
258 * permission, return NETMANAGER_ERR_PERMISSION_DENIED
259 * @tc.type: FUNC
260 */
261 HWTEST_F(NetConnClientTest, HasDefaultNetTest001, TestSize.Level1)
262 {
263 bool bFlag = false;
264 auto ret = NetConnClient::GetInstance().HasDefaultNet(bFlag);
265 ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
266 }
267
268 /**
269 * @tc.name: HasDefaultNetTest002
270 * @tc.desc: Test NetConnClient::HasDefaultNet, applying for
271 * permission, return NETMANAGER_SUCCESS
272 * @tc.type: FUNC
273 */
274 HWTEST_F(NetConnClientTest, HasDefaultNetTest002, TestSize.Level1)
275 {
276 AccessToken token;
277 bool bFlag = false;
278 auto ret = NetConnClient::GetInstance().HasDefaultNet(bFlag);
279 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
280 }
281
282 /**
283 * @tc.name: GetNetCapabilitiesTest001
284 * @tc.desc: Test NetConnClient::GetNetCapabilities, In the absence of
285 * permission, GetDefaultNet return NETMANAGER_ERR_PERMISSION_DENIED and
286 * GetNetCapabilities return NETMANAGER_ERR_PERMISSION_DENIED
287 * @tc.type: FUNC
288 */
289 HWTEST_F(NetConnClientTest, GetNetCapabilitiesTest001, TestSize.Level1)
290 {
291 NetHandle handle;
292 int32_t ret = NetConnClient::GetInstance().GetDefaultNet(handle);
293 ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
294
295 NetAllCapabilities netAllCap;
296 ret = NetConnClient::GetInstance().GetNetCapabilities(handle, netAllCap);
297 ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
298 }
299
300 /**
301 * @tc.name: GetNetCapabilitiesTest002
302 * @tc.desc: Test NetConnClient::GetNetCapabilities:In the absence of
303 * permission, GetDefaultNet return NETMANAGER_ERR_PERMISSION_DENIED, and
304 * after add permission GetNetCapabilities return NET_CONN_ERR_INVALID_NETWORK
305 * @tc.type: FUNC
306 */
307 HWTEST_F(NetConnClientTest, GetNetCapabilitiesTest002, TestSize.Level1)
308 {
309 NetHandle handle;
310 int32_t ret = NetConnClient::GetInstance().GetDefaultNet(handle);
311 ASSERT_TRUE(ret == NETMANAGER_ERR_PERMISSION_DENIED);
312
313 AccessToken token;
314 NetAllCapabilities netAllCap;
315 ret = NetConnClient::GetInstance().GetNetCapabilities(handle, netAllCap);
316 ASSERT_TRUE(ret == NET_CONN_ERR_INVALID_NETWORK);
317 }
318
319 /**
320 * @tc.name: GetNetCapabilitiesTest003
321 * @tc.desc: Test NetConnClient::GetNetCapabilities:Apply for permission at
322 * first, when net is connected,return NET_CONN_SUCCESS, or net is not connected,return
323 * NET_CONN_ERR_INVALID_NETWORK
324 * @tc.type: FUNC
325 */
326 HWTEST_F(NetConnClientTest, GetNetCapabilitiesTest003, TestSize.Level1)
327 {
328 AccessToken token;
329 NetHandle handle;
330 int32_t ret = NetConnClient::GetInstance().GetDefaultNet(handle);
331 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
332
333 NetAllCapabilities netAllCap;
334 ret = NetConnClient::GetInstance().GetNetCapabilities(handle, netAllCap);
335 ASSERT_TRUE(ret == NETMANAGER_SUCCESS || ret == NET_CONN_ERR_INVALID_NETWORK);
336 }
337
338 /**
339 * @tc.name: SetAirplaneModeTest001
340 * @tc.desc: Test NetConnClient::SetAirplaneMode
341 * @tc.type: FUNC
342 */
343 HWTEST_F(NetConnClientTest, SetAirplaneModeTest001, TestSize.Level1)
344 {
345 AccessToken token;
346 auto ret = NetConnClient::GetInstance().SetAirplaneMode(true);
347 ASSERT_EQ(ret, NETMANAGER_SUCCESS);
348 }
349
350 /**
351 * @tc.name: SetAirplaneModeTest002
352 * @tc.desc: Test NetConnClient::SetAirplaneMode
353 * @tc.type: FUNC
354 */
355 HWTEST_F(NetConnClientTest, SetAirplaneModeTest002, TestSize.Level1)
356 {
357 AccessToken token;
358 auto ret = NetConnClient::GetInstance().SetAirplaneMode(false);
359 ASSERT_EQ(ret, NETMANAGER_SUCCESS);
360 }
361
362 /**
363 * @tc.name: IsDefaultNetMeteredTest001
364 * @tc.desc: if no permission,NetConnClient::IsDefaultNetMetered return NETMANAGER_ERR_PERMISSION_DENIED
365 * @tc.type: FUNC
366 */
367 HWTEST_F(NetConnClientTest, IsDefaultNetMeteredTest001, TestSize.Level1)
368 {
369 bool bRes = false;
370 auto ret = NetConnClient::GetInstance().IsDefaultNetMetered(bRes);
371 ASSERT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
372 ASSERT_TRUE(bRes == false);
373 }
374
375 /**
376 * @tc.name: IsDefaultNetMeteredTest002
377 * @tc.desc: Test NetConnClient::IsDefaultNetMetered
378 * @tc.type: FUNC
379 */
380 HWTEST_F(NetConnClientTest, IsDefaultNetMeteredTest002, TestSize.Level1)
381 {
382 AccessToken token;
383 bool bRes = false;
384 auto ret = NetConnClient::GetInstance().IsDefaultNetMetered(bRes);
385 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
386 ASSERT_TRUE(bRes == true);
387 }
388
389 /**
390 * @tc.name: SetGlobalHttpProxyTest001
391 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
392 * @tc.type: FUNC
393 */
394 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest001, TestSize.Level1)
395 {
396 AccessToken token;
397 HttpProxy httpProxy = {"testHttpProxy", 0, {}};
398 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
399 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
400 }
401
402 /**
403 * @tc.name: SetGlobalHttpProxyTest002
404 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
405 * @tc.type: FUNC
406 */
407 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest002, TestSize.Level1)
408 {
409 AccessToken token;
410 HttpProxy httpProxy = {TEST_DOMAIN1, 8080, {}};
411 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
412 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
413 }
414
415 /**
416 * @tc.name: SetGlobalHttpProxyTest003
417 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
418 * @tc.type: FUNC
419 */
420 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest003, TestSize.Level1)
421 {
422 AccessToken token;
423 HttpProxy httpProxy = {TEST_DOMAIN2, 8080, {}};
424 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
425 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
426 }
427
428 /**
429 * @tc.name: SetGlobalHttpProxyTest004
430 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
431 * @tc.type: FUNC
432 */
433 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest004, TestSize.Level1)
434 {
435 AccessToken token;
436 HttpProxy httpProxy = {TEST_DOMAIN3, 8080, {}};
437 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
438 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
439 }
440
441 /**
442 * @tc.name: SetGlobalHttpProxyTest005
443 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
444 * @tc.type: FUNC
445 */
446 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest005, TestSize.Level1)
447 {
448 AccessToken token;
449 HttpProxy httpProxy = {TEST_DOMAIN4, 8080, {}};
450 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
451 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
452 }
453
454 /**
455 * @tc.name: SetGlobalHttpProxyTest006
456 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
457 * @tc.type: FUNC
458 */
459 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest006, TestSize.Level1)
460 {
461 AccessToken token;
462 HttpProxy httpProxy = {TEST_DOMAIN5, 8080, {}};
463 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
464 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
465 }
466
467 /**
468 * @tc.name: SetGlobalHttpProxyTest007
469 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
470 * @tc.type: FUNC
471 */
472 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest007, TestSize.Level1)
473 {
474 AccessToken token;
475 HttpProxy httpProxy = {TEST_DOMAIN6, 8080, {}};
476 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
477 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
478 }
479
480 /**
481 * @tc.name: SetGlobalHttpProxyTest008
482 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
483 * @tc.type: FUNC
484 */
485 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest008, TestSize.Level1)
486 {
487 AccessToken token;
488 HttpProxy httpProxy = {TEST_DOMAIN7, 8080, {}};
489 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
490 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
491 }
492
493 /**
494 * @tc.name: SetGlobalHttpProxyTest09
495 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
496 * @tc.type: FUNC
497 */
498 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest09, TestSize.Level1)
499 {
500 AccessToken token;
501 HttpProxy httpProxy = {TEST_DOMAIN8, 8080, {}};
502 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
503 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
504 }
505
506 /**
507 * @tc.name: SetGlobalHttpProxyTest10
508 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
509 * @tc.type: FUNC
510 */
511 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest10, TestSize.Level1)
512 {
513 AccessToken token;
514 HttpProxy httpProxy = {TEST_DOMAIN9, 8080, {}};
515 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
516 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
517 }
518
519 /**
520 * @tc.name: SetGlobalHttpProxyTest11
521 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
522 * @tc.type: FUNC
523 */
524 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest11, TestSize.Level1)
525 {
526 AccessToken token;
527 HttpProxy httpProxy = {TEST_DOMAIN10, 8080, {}};
528 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
529 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
530 }
531
532 /**
533 * @tc.name: SetGlobalHttpProxyTest012
534 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy.
535 * @tc.type: FUNC
536 */
537 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest012, TestSize.Level1)
538 {
539 AccessToken token;
540 HttpProxy httpProxy = {TEST_IPV4_ADDR, 8080, {}};
541 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
542 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
543 }
544
545 /**
546 * @tc.name: SetGlobalHttpProxyTest013
547 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy.
548 * @tc.type: FUNC
549 */
550 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest013, TestSize.Level1)
551 {
552 AccessToken token;
553 HttpProxy httpProxy = {TEST_IPV6_ADDR, 8080, {}};
554 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
555 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
556 }
557
558 /**
559 * @tc.name: SetGlobalHttpProxyTest14
560 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy
561 * @tc.type: FUNC
562 */
563 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest14, TestSize.Level1)
564 {
565 AccessToken token;
566 HttpProxy httpProxy = {TEST_LONG_HOST, 8080, {TEST_LONG_EXCLUSION_LIST}};
567 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
568 ASSERT_TRUE(ret == NETMANAGER_SUCCESS);
569 }
570
571 /**
572 * @tc.name: SetGlobalHttpProxyTest015
573 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy.
574 * @tc.type: FUNC
575 */
576 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest015, TestSize.Level1)
577 {
578 AccessToken token;
579 HttpProxy httpProxy;
580 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
581 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
582 }
583
584 /**
585 * @tc.name: SetGlobalHttpProxyTest016
586 * @tc.desc: Test NetConnClient::SetGlobalHttpProxy.not applying for permission,return NETMANAGER_ERR_PERMISSION_DENIED
587 * @tc.type: FUNC
588 */
589 HWTEST_F(NetConnClientTest, SetGlobalHttpProxyTest016, TestSize.Level1)
590 {
591 HttpProxy httpProxy = {TEST_IPV4_ADDR, 8080, {}};
592 auto ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
593 ASSERT_TRUE(ret == NETMANAGER_ERR_PERMISSION_DENIED);
594 }
595
596 /**
597 * @tc.name: GetGlobalHttpProxyTest001
598 * @tc.desc: Test NetConnClient::GetGlobalHttpProxy
599 * @tc.type: FUNC
600 */
601 HWTEST_F(NetConnClientTest, GetGlobalHttpProxyTest001, TestSize.Level1)
602 {
603 AccessToken token;
604 HttpProxy httpProxy = {TEST_IPV4_ADDR, 8080, {}};
605 int32_t ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
606 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
607
608 HttpProxy getGlobalHttpProxy;
609 ret = NetConnClient::GetInstance().GetGlobalHttpProxy(getGlobalHttpProxy);
610 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
611 ASSERT_TRUE(getGlobalHttpProxy.GetHost() == TEST_IPV4_ADDR);
612 }
613
614 /**
615 * @tc.name: GetGlobalHttpProxyTest002
616 * @tc.desc: Test NetConnClient::GetGlobalHttpProxy
617 * @tc.type: FUNC
618 */
619 HWTEST_F(NetConnClientTest, GetGlobalHttpProxyTest002, TestSize.Level1)
620 {
621 AccessToken token;
622 HttpProxy httpProxy = {TEST_IPV6_ADDR, 8080, {}};
623 int32_t ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
624 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
625
626 HttpProxy getGlobalHttpProxy;
627 ret = NetConnClient::GetInstance().GetGlobalHttpProxy(getGlobalHttpProxy);
628 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
629 ASSERT_TRUE(getGlobalHttpProxy.GetHost() == TEST_IPV6_ADDR);
630 }
631
632 /**
633 * @tc.name: GetGlobalHttpProxyTest003
634 * @tc.desc: Test NetConnClient::GetGlobalHttpProxy
635 * @tc.type: FUNC
636 */
637 HWTEST_F(NetConnClientTest, GetGlobalHttpProxyTest003, TestSize.Level1)
638 {
639 AccessToken token;
640 HttpProxy httpProxy = {TEST_DOMAIN2, 8080, {}};
641 int32_t ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
642 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
643
644 HttpProxy getGlobalHttpProxy;
645 ret = NetConnClient::GetInstance().GetGlobalHttpProxy(getGlobalHttpProxy);
646 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
647 ASSERT_TRUE(getGlobalHttpProxy.GetHost() == TEST_DOMAIN2);
648 }
649
650 /**
651 * @tc.name: GetGlobalHttpProxyTest004
652 * @tc.desc: Test NetConnClient::GetGlobalHttpProxy
653 * @tc.type: FUNC
654 */
655 HWTEST_F(NetConnClientTest, GetGlobalHttpProxyTest004, TestSize.Level1)
656 {
657 AccessToken token;
658 HttpProxy validHttpProxy = {TEST_IPV4_ADDR, 8080, {}};
659 int32_t ret = NetConnClient::GetInstance().SetGlobalHttpProxy(validHttpProxy);
660 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
661
662 HttpProxy getGlobalHttpProxy;
663 ret = NetConnClient::GetInstance().GetGlobalHttpProxy(getGlobalHttpProxy);
664 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
665 ASSERT_TRUE(getGlobalHttpProxy.GetHost() == TEST_IPV4_ADDR);
666 }
667
668 /**
669 * @tc.name: GetGlobalHttpProxyTest005
670 * @tc.desc: Test NetConnClient::GetGlobalHttpProxy
671 * @tc.type: FUNC
672 */
673 HWTEST_F(NetConnClientTest, GetGlobalHttpProxyTest005, TestSize.Level1)
674 {
675 AccessToken token;
676 HttpProxy httpProxy;
677 int32_t ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
678 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
679
680 HttpProxy getGlobalHttpProxy;
681 ret = NetConnClient::GetInstance().GetGlobalHttpProxy(getGlobalHttpProxy);
682 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
683 ASSERT_TRUE(getGlobalHttpProxy.GetHost().empty());
684 }
685
686 /**
687 * @tc.name: GetDefaultHttpProxyTest001
688 * @tc.desc: Test NetConnClient::GetDefaultHttpProxy
689 * @tc.type: FUNC
690 */
691 HWTEST_F(NetConnClientTest, GetDefaultHttpProxyTest001, TestSize.Level1)
692 {
693 AccessToken token;
694 HttpProxy validHttpProxy = {TEST_IPV4_ADDR, 8080, {}};
695 int32_t ret = NetConnClient::GetInstance().SetGlobalHttpProxy(validHttpProxy);
696 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
697
698 HttpProxy defaultHttpProxy;
699 ret = NetConnClient::GetInstance().GetDefaultHttpProxy(defaultHttpProxy);
700 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
701 ASSERT_TRUE(defaultHttpProxy.GetHost() == TEST_IPV4_ADDR);
702 }
703
704 /**
705 * @tc.name: GetDefaultHttpProxyTest002
706 * @tc.desc: Test NetConnClient::GetDefaultHttpProxy
707 * @tc.type: FUNC
708 */
709 HWTEST_F(NetConnClientTest, GetDefaultHttpProxyTest002, TestSize.Level1)
710 {
711 AccessToken token;
712 HttpProxy globalHttpProxy;
713 int32_t ret = NetConnClient::GetInstance().SetGlobalHttpProxy(globalHttpProxy);
714 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
715
716 HttpProxy defaultHttpProxy;
717 ret = NetConnClient::GetInstance().GetDefaultHttpProxy(defaultHttpProxy);
718 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
719 }
720
721 /**
722 * @tc.name: GetDefaultHttpProxyTest003
723 * @tc.desc: Test NetConnClient::SetAppNet and NetConnClient::GetDefaultHttpProxy
724 * @tc.type: FUNC
725 */
726 HWTEST_F(NetConnClientTest, GetDefaultHttpProxyTest003, TestSize.Level1)
727 {
728 AccessToken token;
729 int32_t netId = 102;
730 int32_t ret = NetConnClient::GetInstance().SetAppNet(netId);
731 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
732 HttpProxy defaultHttpProxy;
733 ret = NetConnClient::GetInstance().GetDefaultHttpProxy(defaultHttpProxy);
734 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
735
736 int32_t cancelNetId = 0;
737 ret = NetConnClient::GetInstance().SetAppNet(cancelNetId);
738 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
739 ret = NetConnClient::GetInstance().GetDefaultHttpProxy(defaultHttpProxy);
740 ASSERT_TRUE(ret == NET_CONN_SUCCESS);
741 }
742
743 /**
744 * @tc.name: RegisterNetSupplier001
745 * @tc.desc: Test NetConnClient::RegisterNetSupplier
746 * @tc.type: FUNC
747 */
748 HWTEST_F(NetConnClientTest, RegisterNetSupplier001, TestSize.Level1)
749 {
750 uint32_t supplierId = 100;
751 NetBearType netBearType = BEARER_WIFI;
752 const std::string ident = "";
753 std::set<NetCap> netCaps = {NET_CAPABILITY_INTERNET};
754 auto ret = NetConnClient::GetInstance().RegisterNetSupplier(netBearType, ident, netCaps, supplierId);
755 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
756 }
757
758 /**
759 * @tc.name: RegisterNetSupplier002
760 * @tc.desc: Test NetConnClient::RegisterNetSupplier
761 * @tc.type: FUNC
762 */
763 HWTEST_F(NetConnClientTest, RegisterNetSupplier002, TestSize.Level1)
764 {
765 AccessToken token;
766 uint32_t supplierId = 100;
767 NetBearType netBearType = BEARER_WIFI;
768 const std::string ident = "";
769 std::set<NetCap> netCaps = {NET_CAPABILITY_INTERNET};
770 auto ret = NetConnClient::GetInstance().RegisterNetSupplier(netBearType, ident, netCaps, supplierId);
771 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
772 }
773
774 /**
775 * @tc.name: UnregisterNetSupplier001
776 * @tc.desc: Test NetConnClient::UnregisterNetSupplier
777 * @tc.type: FUNC
778 */
779 HWTEST_F(NetConnClientTest, UnregisterNetSupplier001, TestSize.Level1)
780 {
781 uint32_t supplierId = 100;
782 auto ret = NetConnClient::GetInstance().UnregisterNetSupplier(supplierId);
783 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
784 }
785
786 /**
787 * @tc.name: UnregisterNetSupplier002
788 * @tc.desc: Test NetConnClient::UnregisterNetSupplier
789 * @tc.type: FUNC
790 */
791 HWTEST_F(NetConnClientTest, UnregisterNetSupplier002, TestSize.Level1)
792 {
793 AccessToken token;
794 uint32_t supplierId = 100;
795 auto ret = NetConnClient::GetInstance().UnregisterNetSupplier(supplierId);
796 EXPECT_EQ(ret, NET_CONN_ERR_NO_SUPPLIER);
797 }
798
799 /**
800 * @tc.name: RegisterNetSupplierCallbackTest001
801 * @tc.desc: Test NetConnClient::RegisterNetSupplierCallback
802 * @tc.type: FUNC
803 */
804 HWTEST_F(NetConnClientTest, RegisterNetSupplierCallbackTest001, TestSize.Level1)
805 {
806 uint32_t supplierId = 100;
807 sptr<NetSupplierCallbackBase> callback = new (std::nothrow) NetSupplierCallbackBase();
808 ASSERT_NE(callback, nullptr);
809 auto ret = NetConnClient::GetInstance().RegisterNetSupplierCallback(supplierId, callback);
810 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
811 }
812
813 /**
814 * @tc.name: RegisterNetSupplierCallbackTest002
815 * @tc.desc: Test NetConnClient::RegisterNetSupplierCallback
816 * @tc.type: FUNC
817 */
818 HWTEST_F(NetConnClientTest, RegisterNetSupplierCallbackTest002, TestSize.Level1)
819 {
820 AccessToken token;
821 uint32_t supplierId = 100;
822 sptr<NetSupplierCallbackBase> callback = new (std::nothrow) NetSupplierCallbackBase();
823 ASSERT_NE(callback, nullptr);
824 auto ret = NetConnClient::GetInstance().RegisterNetSupplierCallback(supplierId, callback);
825 EXPECT_EQ(ret, NET_CONN_ERR_NO_SUPPLIER);
826 }
827
828 /**
829 * @tc.name: RegisterNetSupplierCallbackTest003
830 * @tc.desc: Test NetConnClient::RegisterNetSupplierCallback
831 * @tc.type: FUNC
832 */
833 HWTEST_F(NetConnClientTest, RegisterNetSupplierCallbackTest003, TestSize.Level1)
834 {
835 AccessToken token;
836 NetBearType bearerType = BEARER_CELLULAR;
837 std::set<NetCap> netCaps{NET_CAPABILITY_INTERNET};
838 std::string ident = "ident";
839 uint32_t supplierId = 0;
840 int32_t result = NetConnClient::GetInstance().RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
841 ASSERT_TRUE(result == NETMANAGER_SUCCESS);
842 sptr<NetSupplierCallbackBase> callback = new (std::nothrow) NetSupplierCallbackBase();
843 ASSERT_NE(callback, nullptr);
844 auto ret = NetConnClient::GetInstance().RegisterNetSupplierCallback(supplierId, callback);
845 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
846 }
847
848 /**
849 * @tc.name: RegisterNetSupplierCallbackTest004
850 * @tc.desc: Test NetConnClient::RegisterNetSupplierCallback
851 * @tc.type: FUNC
852 */
853 HWTEST_F(NetConnClientTest, RegisterNetSupplierCallbackTest004, TestSize.Level1)
854 {
855 AccessToken token;
856 uint32_t supplierId = 0;
857 sptr<NetSupplierCallbackBase> callback;
858 auto ret = NetConnClient::GetInstance().RegisterNetSupplierCallback(supplierId, callback);
859 EXPECT_EQ(ret, NET_CONN_ERR_NO_SUPPLIER);
860 }
861
862 /**
863 * @tc.name: SetAppNetTest001
864 * @tc.desc: Test NetConnClient::SetAppNet, if param is invalid, SetAppNet return NET_CONN_ERR_INVALID_NETWORK
865 * @tc.type: FUNC
866 */
867 HWTEST_F(NetConnClientTest, SetAppNetTest001, TestSize.Level1)
868 {
869 AccessToken token;
870 int32_t netId = 99;
871 auto ret = NetConnClient::GetInstance().SetAppNet(netId);
872 EXPECT_EQ(ret, NET_CONN_ERR_INVALID_NETWORK);
873 }
874
875 /**
876 * @tc.name: SetAppNetTest002
877 * @tc.desc: Test NetConnClient::SetAppNet, if param is valid, SetAppNet return NETMANAGER_SUCCESS
878 * @tc.type: FUNC
879 */
880 HWTEST_F(NetConnClientTest, SetAppNetTest002, TestSize.Level1)
881 {
882 AccessToken token;
883 int32_t netId = 102;
884 auto ret = NetConnClient::GetInstance().SetAppNet(netId);
885 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
886
887 int32_t cancelNetId = 0;
888 ret = NetConnClient::GetInstance().SetAppNet(cancelNetId);
889 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
890 }
891
892 /**
893 * @tc.name: GetAppNetTest001
894 * @tc.desc: Test NetConnClient::GetAppNet, return NetId set by SetAppNet
895 * @tc.type: FUNC
896 */
897 HWTEST_F(NetConnClientTest, GetAppNetTest001, TestSize.Level1)
898 {
899 AccessToken token;
900 int32_t netId = 102;
901 auto ret = NetConnClient::GetInstance().SetAppNet(netId);
902 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
903
904 int32_t getNetId = 0;
905 NetConnClient::GetInstance().GetAppNet(getNetId);
906 EXPECT_EQ(getNetId, netId);
907
908 int32_t cancelNetId = 0;
909 ret = NetConnClient::GetInstance().SetAppNet(cancelNetId);
910 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
911 }
912
913 /**
914 * @tc.name: RegisterNetConnCallback001
915 * @tc.desc: Test NetConnClient::RegisterNetConnCallback, not applying for
916 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
917 * @tc.type: FUNC
918 */
919 HWTEST_F(NetConnClientTest, RegisterNetConnCallback001, TestSize.Level1)
920 {
921 AccessToken token;
922 sptr<INetConnCallbackTest> callback = new (std::nothrow) INetConnCallbackTest();
923 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
924 ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
925 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
926 }
927
928 /**
929 * @tc.name: RegisterNetConnCallback002
930 * @tc.desc: Test NetConnClient::RegisterNetConnCallback, not applying for
931 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
932 * @tc.type: FUNC
933 */
934 HWTEST_F(NetConnClientTest, RegisterNetConnCallback002, TestSize.Level1)
935 {
936 sptr<NetSpecifier> netSpecifier = nullptr;
937 sptr<INetConnCallbackTest> callback = new (std::nothrow) INetConnCallbackTest();
938 uint32_t timesOut = 1;
939 auto ret = NetConnClient::GetInstance().RegisterNetConnCallback(netSpecifier, callback, timesOut);
940 EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
941 }
942
943 /**
944 * @tc.name: RegisterNetConnCallback002
945 * @tc.desc: Test NetConnClient::RegisterNetConnCallback, not applying for
946 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
947 * @tc.type: FUNC
948 */
949 HWTEST_F(NetConnClientTest, RegisterNetConnCallback003, TestSize.Level1)
950 {
951 sptr<NetSpecifier> netSpecifier = new (std::nothrow) NetSpecifier();
952 sptr<INetConnCallbackTest> callback = new (std::nothrow) INetConnCallbackTest();
953 uint32_t timesOut = 1;
954 auto ret = NetConnClient::GetInstance().RegisterNetConnCallback(netSpecifier, callback, timesOut);
955 EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
956 }
957
958 /**
959 * @tc.name: RegisterNetConnCallback001
960 * @tc.desc: Test NetConnClient::RegisterNetConnCallback, not applying for
961 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
962 * @tc.type: FUNC
963 */
964 HWTEST_F(NetConnClientTest, UnRegisterNetConnCallback001, TestSize.Level1)
965 {
966 sptr<INetConnCallbackTest> callback = new (std::nothrow) INetConnCallbackTest();
967 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callback);
968 ret = NetConnClient::GetInstance().UnregisterNetConnCallback(callback);
969 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
970 }
971
972 /**
973 * @tc.name: UpdateNetSupplierInfo001
974 * @tc.desc: Test NetConnClient::UpdateNetSupplierInfo, not applying for
975 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
976 * @tc.type: FUNC
977 */
978 HWTEST_F(NetConnClientTest, UpdateNetSupplierInfo001, TestSize.Level1)
979 {
980 auto &client = NetConnClient::GetInstance();
981 uint32_t supplierId = 1;
982 sptr<NetSupplierInfo> netSupplierInfo = new (std::nothrow) NetSupplierInfo;
983 int32_t ret = client.UpdateNetSupplierInfo(supplierId, netSupplierInfo);
984 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
985 }
986
987 /**
988 * @tc.name: UpdateNetSupplierInfo002
989 * @tc.desc: Test NetConnClient::UpdateNetSupplierInfo, not applying for
990 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
991 * @tc.type: FUNC
992 */
993 HWTEST_F(NetConnClientTest, UpdateNetSupplierInfo002, TestSize.Level1)
994 {
995 AccessToken token;
996 auto &client = NetConnClient::GetInstance();
997 uint32_t supplierId = 1;
998 sptr<NetSupplierInfo> netSupplierInfo = new NetSupplierInfo;
999 netSupplierInfo->isAvailable_ = true;
1000 netSupplierInfo->isRoaming_ = true;
1001 netSupplierInfo->strength_ = 0x64;
1002 netSupplierInfo->frequency_ = 0x10;
1003 int32_t ret = client.UpdateNetSupplierInfo(supplierId, netSupplierInfo);
1004 EXPECT_EQ(ret, NET_CONN_ERR_NO_SUPPLIER);
1005 }
1006
1007 /**
1008 * @tc.name: GetNetInterfaceConfigurationTest001
1009 * @tc.desc: Test NetConnClient::GetNetInterfaceConfiguration
1010 * @tc.type: FUNC
1011 */
1012 HWTEST_F(NetConnClientTest, GetNetInterfaceConfigurationTest001, TestSize.Level1)
1013 {
1014 AccessToken token;
1015 NetInterfaceConfiguration config;
1016 auto ret = NetConnClient::GetInstance().GetNetInterfaceConfiguration(TEST_IFACE, config);
1017 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
1018 }
1019
1020 /**
1021 * @tc.name: GetNetInterfaceConfigurationTest001
1022 * @tc.desc: Test NetConnClient::GetNetInterfaceConfiguration
1023 * @tc.type: FUNC
1024 */
1025 HWTEST_F(NetConnClientTest, GetNetInterfaceConfigurationTest002, TestSize.Level1)
1026 {
1027 NetInterfaceConfiguration config;
1028 auto ret = NetConnClient::GetInstance().GetNetInterfaceConfiguration(TEST_IFACE, config);
1029 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
1030 }
1031
1032 /**
1033 * @tc.name: RegisterNetInterfaceCallbackTest001
1034 * @tc.desc: Test NetConnClient::RegisterNetInterfaceCallback
1035 * @tc.type: FUNC
1036 */
1037 HWTEST_F(NetConnClientTest, RegisterNetInterfaceCallbackTest001, TestSize.Level1)
1038 {
1039 sptr<INetInterfaceStateCallback> callback = new (std::nothrow) NetInterfaceStateCallbackStub();
1040 int32_t ret = NetConnClient::GetInstance().RegisterNetInterfaceCallback(callback);
1041 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
1042 }
1043
1044 /**
1045 * @tc.name: RegisterNetInterfaceCallbackTest002
1046 * @tc.desc: Test NetConnClient::RegisterNetInterfaceCallback
1047 * @tc.type: FUNC
1048 */
1049 HWTEST_F(NetConnClientTest, RegisterNetInterfaceCallbackTest002, TestSize.Level1)
1050 {
1051 AccessToken token;
1052 sptr<INetInterfaceStateCallback> callback = new (std::nothrow) NetInterfaceStateCallbackStub();
1053 int32_t ret = NetConnClient::GetInstance().RegisterNetInterfaceCallback(callback);
1054 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
1055 }
1056
1057 /**
1058 * @tc.name: SystemReadyTest002
1059 * @tc.desc: Test NetConnClient::SystemReady
1060 * @tc.type: FUNC
1061 */
1062 HWTEST_F(NetConnClientTest, SystemReadyTest002, TestSize.Level1)
1063 {
1064 AccessToken token;
1065 int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->SystemReady();
1066 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
1067 }
1068
1069 /**
1070 * @tc.name: UpdateNetLinkInfoTest002
1071 * @tc.desc: Test NetConnClient::UpdateNetLinkInfo
1072 * @tc.type: FUNC
1073 */
1074 HWTEST_F(NetConnClientTest, UpdateNetLinkInfoTest002, TestSize.Level1)
1075 {
1076 AccessToken token;
1077 uint32_t supplierId = 1;
1078 sptr<NetLinkInfo> netLinkInfo = std::make_unique<NetLinkInfo>().release();
1079 int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->UpdateNetLinkInfo(supplierId, netLinkInfo);
1080 EXPECT_EQ(ret, NET_CONN_ERR_NO_SUPPLIER);
1081 }
1082
1083 /**
1084 * @tc.name: GetAllNetsTest002
1085 * @tc.desc: Test NetConnClient::GetAllNets
1086 * @tc.type: FUNC
1087 */
1088 HWTEST_F(NetConnClientTest, GetAllNetsTest002, TestSize.Level1)
1089 {
1090 AccessToken token;
1091 std::list<sptr<NetHandle>> netList;
1092 int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->GetAllNets(netList);
1093 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
1094 }
1095
1096 /**
1097 * @tc.name: GetConnectionPropertiesTest002
1098 * @tc.desc: Test NetConnClient::GetConnectionProperties
1099 * @tc.type: FUNC
1100 */
1101 HWTEST_F(NetConnClientTest, GetConnectionPropertiesTest002, TestSize.Level1)
1102 {
1103 AccessToken token;
1104 NetHandle netHandle;
1105 NetLinkInfo info;
1106 int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->GetConnectionProperties(netHandle, info);
1107 EXPECT_EQ(ret, NET_CONN_ERR_INVALID_NETWORK);
1108 }
1109
1110 /**
1111 * @tc.name: GetAddressesByNameTest002
1112 * @tc.desc: Test NetConnClient::GetAddressesByName
1113 * @tc.type: FUNC
1114 */
1115 HWTEST_F(NetConnClientTest, GetAddressesByNameTest002, TestSize.Level1)
1116 {
1117 AccessToken token;
1118 const std::string host = "ipaddr";
1119 int32_t netId = 1;
1120 std::vector<INetAddr> addrList = {};
1121 int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->GetAddressesByName(host, netId, addrList);
1122 EXPECT_EQ(ret, -1);
1123 }
1124
1125 /**
1126 * @tc.name: GetAddressByNameTest002
1127 * @tc.desc: Test NetConnClient::GetAddressByName
1128 * @tc.type: FUNC
1129 */
1130 HWTEST_F(NetConnClientTest, GetAddressByNameTest002, TestSize.Level1)
1131 {
1132 AccessToken token;
1133 std::string host = "ipaddr";
1134 int32_t netId = 1;
1135 INetAddr addr;
1136 int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->GetAddressByName(host, netId, addr);
1137 EXPECT_EQ(ret, -1);
1138 }
1139
1140 /**
1141 * @tc.name: BindSocketTest002
1142 * @tc.desc: Test NetConnClient::BindSocket
1143 * @tc.type: FUNC
1144 */
1145 HWTEST_F(NetConnClientTest, BindSocketTest002, TestSize.Level1)
1146 {
1147 AccessToken token;
1148 NetConnClient::NetConnDeathRecipient deathRecipient(*DelayedSingleton<NetConnClient>::GetInstance());
1149 sptr<IRemoteObject> remote = nullptr;
1150 deathRecipient.OnRemoteDied(remote);
1151 int32_t socket_fd = 0;
1152 int32_t netId = 99;
1153 int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->BindSocket(socket_fd, netId);
1154 EXPECT_EQ(ret, NET_CONN_ERR_INVALID_NETWORK);
1155 netId = 101;
1156 ret = DelayedSingleton<NetConnClient>::GetInstance()->BindSocket(socket_fd, netId);
1157 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
1158 }
1159
1160 /**
1161 * @tc.name: NetDetectionTest002
1162 * @tc.desc: Test NetConnClient::NetDetection
1163 * @tc.type: FUNC
1164 */
1165 HWTEST_F(NetConnClientTest, NetDetectionTest002, TestSize.Level1)
1166 {
1167 AccessToken token;
1168 NetHandle netHandle;
1169 int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->NetDetection(netHandle);
1170 EXPECT_EQ(ret, NET_CONN_ERR_NETID_NOT_FOUND);
1171 }
1172 } // namespace NetManagerStandard
1173 } // namespace OHOS
1174