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 "dns_base_service.h"
19 #include "net_conn_base_service.h"
20 #include "net_conn_service_iface.h"
21 #include "net_conn_types.h"
22 #include "net_ethernet_base_service.h"
23 #include "net_manager_center.h"
24 #include "net_manager_constants.h"
25 #include "net_policy_base_service.h"
26 #include "net_stats_base_service.h"
27
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace {
31 using namespace testing::ext;
32 constexpr const char *TEST_IDENT = "testIdent";
33 constexpr std::initializer_list<NetBearType> BEAR_TYPE_LIST = {
34 NetBearType::BEARER_CELLULAR, NetBearType::BEARER_WIFI, NetBearType::BEARER_BLUETOOTH,
35 NetBearType::BEARER_ETHERNET, NetBearType::BEARER_VPN, NetBearType::BEARER_WIFI_AWARE,
36 };
37
38 class TestDnsService : public DnsBaseService {
GetAddressesByName(const std::string & hostName,int32_t netId,std::vector<INetAddr> & addrInfo)39 inline int32_t GetAddressesByName(const std::string &hostName, int32_t netId,
40 std::vector<INetAddr> &addrInfo) override
41 {
42 return NETMANAGER_SUCCESS;
43 }
44 };
45
46 class TestConnService : public NetConnBaseService {
47 public:
GetIfaceNames(NetBearType bearerType,std::list<std::string> & ifaceNames)48 inline int32_t GetIfaceNames(NetBearType bearerType, std::list<std::string> &ifaceNames) override
49 {
50 return NETMANAGER_SUCCESS;
51 }
GetIfaceNameByType(NetBearType bearerType,const std::string & ident,std::string & ifaceName)52 inline int32_t GetIfaceNameByType(NetBearType bearerType, const std::string &ident, std::string &ifaceName) override
53 {
54 return NETMANAGER_SUCCESS;
55 }
RegisterNetSupplier(NetBearType bearerType,const std::string & ident,const std::set<NetCap> & netCaps,uint32_t & supplierId)56 inline int32_t RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
57 const std::set<NetCap> &netCaps, uint32_t &supplierId) override
58 {
59 return NETMANAGER_SUCCESS;
60 }
UnregisterNetSupplier(uint32_t supplierId)61 inline int32_t UnregisterNetSupplier(uint32_t supplierId) override
62 {
63 return NETMANAGER_SUCCESS;
64 }
UpdateNetLinkInfo(uint32_t supplierId,const sptr<NetLinkInfo> & netLinkInfo)65 inline int32_t UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo) override
66 {
67 return NETMANAGER_SUCCESS;
68 }
UpdateNetSupplierInfo(uint32_t supplierId,const sptr<NetSupplierInfo> & netSupplierInfo)69 inline int32_t UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo) override
70 {
71 return NETMANAGER_SUCCESS;
72 }
RestrictBackgroundChanged(bool isRestrictBackground)73 inline int32_t RestrictBackgroundChanged(bool isRestrictBackground) override
74 {
75 return NETMANAGER_SUCCESS;
76 }
RegisterNetConnCallback(const sptr<INetConnCallback> & callback)77 inline int32_t RegisterNetConnCallback(const sptr<INetConnCallback> &callback) override
78 {
79 return NETMANAGER_SUCCESS;
80 }
81 };
82
83 class TestNetEthernetService : public NetEthernetBaseService {
84 public:
ResetEthernetFactory()85 inline int32_t ResetEthernetFactory() override
86 {
87 return NETMANAGER_SUCCESS;
88 }
89 };
90
91 class TestNetPolicyService : public NetPolicyBaseService {
92 public:
ResetPolicies()93 inline int32_t ResetPolicies() override
94 {
95 return NETMANAGER_SUCCESS;
96 }
IsUidNetAllowed(uint32_t uid,bool metered)97 inline bool IsUidNetAllowed(uint32_t uid, bool metered) override
98 {
99 return NETMANAGER_SUCCESS;
100 }
101 };
102
103 class TestNetStatsService : public NetStatsBaseService {
104 public:
GetIfaceStatsDetail(const std::string & iface,uint64_t start,uint64_t end,NetStatsInfo & info)105 inline int32_t GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
106 NetStatsInfo &info) override
107 {
108 return NETMANAGER_SUCCESS;
109 }
ResetStatsFactory()110 inline int32_t ResetStatsFactory() override
111 {
112 return NETMANAGER_SUCCESS;
113 }
114 };
115 } // namespace
116
117 class NetManagerCenterTest : public testing::Test {
118 public:
119 static void SetUpTestCase();
120 static void TearDownTestCase();
121 void SetUp();
122 void TearDown();
123 static inline NetManagerCenter &instance_ = NetManagerCenter::GetInstance();
124 static inline uint32_t supplierId_ = 0;
125 };
126
SetUpTestCase()127 void NetManagerCenterTest::SetUpTestCase() {}
128
TearDownTestCase()129 void NetManagerCenterTest::TearDownTestCase() {}
130
SetUp()131 void NetManagerCenterTest::SetUp()
132 {
133 instance_.RegisterConnService(nullptr);
134 instance_.RegisterStatsService(nullptr);
135 instance_.RegisterPolicyService(nullptr);
136 instance_.RegisterEthernetService(nullptr);
137 instance_.RegisterDnsService(nullptr);
138 }
139
TearDown()140 void NetManagerCenterTest::TearDown() {}
141
142 HWTEST_F(NetManagerCenterTest, GetIfaceNamesTest001, TestSize.Level1)
143 {
144 std::list<std::string> list;
__anona88b5ee90202(const auto &type) 145 std::for_each(BEAR_TYPE_LIST.begin(), BEAR_TYPE_LIST.end(), [this, &list](const auto &type) {
146 int32_t ret = instance_.GetIfaceNames(type, list);
147 std::cout << "TYPE:" << type << "LIST_SIZE:" << list.size() << std::endl;
148 EXPECT_EQ(ret, NETMANAGER_ERROR);
149 EXPECT_TRUE(list.empty());
150 list.clear();
151 });
152 }
153
154 HWTEST_F(NetManagerCenterTest, GetIfaceNamesTest002, TestSize.Level1)
155 {
156 std::list<std::string> list;
157 int32_t ret = instance_.GetIfaceNames(NetBearType::BEARER_DEFAULT, list);
158 EXPECT_EQ(ret, NETMANAGER_ERROR);
159 EXPECT_TRUE(list.empty());
160 }
161
162 HWTEST_F(NetManagerCenterTest, GetIfaceNamesTest003, TestSize.Level1)
163 {
164 sptr<NetConnBaseService> service = new (std::nothrow) TestConnService();
165 instance_.RegisterConnService(service);
166 std::list<std::string> list;
__anona88b5ee90302(const auto &type) 167 std::for_each(BEAR_TYPE_LIST.begin(), BEAR_TYPE_LIST.end(), [this, &list](const auto &type) {
168 int32_t ret = instance_.GetIfaceNames(type, list);
169 std::cout << "TYPE:" << type << "LIST_SIZE:" << list.size() << std::endl;
170 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
171 EXPECT_TRUE(list.empty());
172 list.clear();
173 });
174 }
175
176 HWTEST_F(NetManagerCenterTest, GetIfaceNamesTest004, TestSize.Level1)
177 {
178 sptr<NetConnBaseService> service = new (std::nothrow) TestConnService();
179 instance_.RegisterConnService(service);
180 std::list<std::string> list;
181 int32_t ret = instance_.GetIfaceNames(NetBearType::BEARER_DEFAULT, list);
182 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
183 EXPECT_TRUE(list.empty());
184 }
185
186 HWTEST_F(NetManagerCenterTest, GetIfaceNameByTypeTest001, TestSize.Level1)
187 {
188 std::string ifaceName;
__anona88b5ee90402(const auto &type) 189 std::for_each(BEAR_TYPE_LIST.begin(), BEAR_TYPE_LIST.end(), [this, &ifaceName](const auto &type) {
190 int32_t ret = instance_.GetIfaceNameByType(type, TEST_IDENT, ifaceName);
191 std::cout << "TYPE:" << type << "LIST_SIZE:" << ifaceName.size() << std::endl;
192 EXPECT_EQ(ret, NETMANAGER_ERROR);
193 EXPECT_TRUE(ifaceName.empty());
194 ifaceName.clear();
195 });
196 }
197
198 HWTEST_F(NetManagerCenterTest, GetIfaceNameByTypeTest002, TestSize.Level1)
199 {
200 std::string ifaceName;
201 int32_t ret = instance_.GetIfaceNameByType(NetBearType::BEARER_DEFAULT, TEST_IDENT, ifaceName);
202 EXPECT_EQ(ret, NETMANAGER_ERROR);
203 EXPECT_TRUE(ifaceName.empty());
204 }
205
206 HWTEST_F(NetManagerCenterTest, GetIfaceNameByTypeTest003, TestSize.Level1)
207 {
208 sptr<NetConnBaseService> service = new (std::nothrow) TestConnService();
209 instance_.RegisterConnService(service);
210 std::string ifaceName;
__anona88b5ee90502(const auto &type) 211 std::for_each(BEAR_TYPE_LIST.begin(), BEAR_TYPE_LIST.end(), [this, &ifaceName](const auto &type) {
212 int32_t ret = instance_.GetIfaceNameByType(type, TEST_IDENT, ifaceName);
213 std::cout << "TYPE:" << type << "LIST_SIZE:" << ifaceName.size() << std::endl;
214 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
215 EXPECT_TRUE(ifaceName.empty());
216 ifaceName.clear();
217 });
218 }
219
220 HWTEST_F(NetManagerCenterTest, GetIfaceNameByTypeTest004, TestSize.Level1)
221 {
222 sptr<NetConnBaseService> service = new (std::nothrow) TestConnService();
223 instance_.RegisterConnService(service);
224 std::string ifaceName;
225 int32_t ret = instance_.GetIfaceNameByType(NetBearType::BEARER_DEFAULT, TEST_IDENT, ifaceName);
226 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
227 EXPECT_TRUE(ifaceName.empty());
228 }
229
230 HWTEST_F(NetManagerCenterTest, RegisterNetSupplierTest001, TestSize.Level1)
231 {
232 NetBearType bearerType = BEARER_CELLULAR;
233 std::set<NetCap> netCaps{NET_CAPABILITY_INTERNET};
234 std::string ident = "ident";
235 int32_t result = instance_.RegisterNetSupplier(bearerType, ident, netCaps, supplierId_);
236 ASSERT_EQ(result, NETMANAGER_ERROR);
237 }
238
239 HWTEST_F(NetManagerCenterTest, RegisterNetSupplierTest002, TestSize.Level1)
240 {
241 sptr<NetConnBaseService> service = new (std::nothrow) TestConnService();
242 instance_.RegisterConnService(service);
243 NetBearType bearerType = BEARER_CELLULAR;
244 std::set<NetCap> netCaps{NET_CAPABILITY_INTERNET};
245 std::string ident = "ident";
246 int32_t result = instance_.RegisterNetSupplier(bearerType, ident, netCaps, supplierId_);
247 EXPECT_EQ(result, NETMANAGER_SUCCESS);
248 }
249
250 HWTEST_F(NetManagerCenterTest, UnegisterNetSupplierTest001, TestSize.Level1)
251 {
252 int32_t result = instance_.UnregisterNetSupplier(supplierId_);
253 ASSERT_EQ(result, NETMANAGER_ERROR);
254 }
255
256 HWTEST_F(NetManagerCenterTest, UnegisterNetSupplierTest002, TestSize.Level1)
257 {
258 sptr<NetConnBaseService> service = new (std::nothrow) TestConnService();
259 instance_.RegisterConnService(service);
260 int32_t result = instance_.UnregisterNetSupplier(supplierId_);
261 ASSERT_EQ(result, NETMANAGER_SUCCESS);
262 }
263
264 HWTEST_F(NetManagerCenterTest, UpdateNetLinkInfoTest001, TestSize.Level1)
265 {
266 NetBearType bearerType = BEARER_CELLULAR;
267 std::set<NetCap> netCaps = {NET_CAPABILITY_INTERNET, NET_CAPABILITY_MMS};
268
269 std::string ident = "ident04";
270 uint32_t supplierId = 0;
271 int32_t result = instance_.RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
272 ASSERT_EQ(result, NETMANAGER_ERROR);
273
274 sptr<NetLinkInfo> netLinkInfo = new (std::nothrow) NetLinkInfo();
275 result = instance_.UpdateNetLinkInfo(supplierId, netLinkInfo);
276 ASSERT_EQ(result, NETMANAGER_ERROR);
277 }
278
279 HWTEST_F(NetManagerCenterTest, UpdateNetLinkInfoTest002, TestSize.Level1)
280 {
281 sptr<NetConnBaseService> service = new (std::nothrow) TestConnService();
282 instance_.RegisterConnService(service);
283 NetBearType bearerType = BEARER_CELLULAR;
284 std::set<NetCap> netCaps = {NET_CAPABILITY_INTERNET, NET_CAPABILITY_MMS};
285
286 std::string ident = "ident04";
287 uint32_t supplierId = 0;
288 int32_t result = instance_.RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
289 ASSERT_EQ(result, NETMANAGER_SUCCESS);
290
291 sptr<NetLinkInfo> netLinkInfo = new (std::nothrow) NetLinkInfo();
292 result = instance_.UpdateNetLinkInfo(supplierId, netLinkInfo);
293 ASSERT_EQ(result, NETMANAGER_SUCCESS);
294 }
295
296 HWTEST_F(NetManagerCenterTest, UpdateNetSupplierInfoTest001, TestSize.Level1)
297 {
298 NetBearType bearerType = BEARER_CELLULAR;
299 std::set<NetCap> netCaps{NET_CAPABILITY_INTERNET, NET_CAPABILITY_MMS};
300 std::string ident = "ident03";
301 uint32_t supplierId = 0;
302 int32_t result = instance_.RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
303 ASSERT_EQ(result, NETMANAGER_ERROR);
304
305 sptr<NetSupplierInfo> netSupplierInfo = new NetSupplierInfo();
306 netSupplierInfo->isAvailable_ = true;
307 netSupplierInfo->isRoaming_ = true;
308 netSupplierInfo->strength_ = 0x64;
309 netSupplierInfo->frequency_ = 0x10;
310 result = instance_.UpdateNetSupplierInfo(supplierId, netSupplierInfo);
311 ASSERT_EQ(result, NETMANAGER_ERROR);
312 }
313
314 HWTEST_F(NetManagerCenterTest, UpdateNetSupplierInfoTest002, TestSize.Level1)
315 {
316 sptr<NetConnBaseService> service = new (std::nothrow) TestConnService();
317 instance_.RegisterConnService(service);
318 NetBearType bearerType = BEARER_CELLULAR;
319 std::set<NetCap> netCaps{NET_CAPABILITY_INTERNET, NET_CAPABILITY_MMS};
320 std::string ident = "ident03";
321 uint32_t supplierId = 0;
322 int32_t result = instance_.RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
323 ASSERT_EQ(result, NETMANAGER_SUCCESS);
324
325 sptr<NetSupplierInfo> netSupplierInfo = new NetSupplierInfo();
326 netSupplierInfo->isAvailable_ = true;
327 netSupplierInfo->isRoaming_ = true;
328 netSupplierInfo->strength_ = 0x64;
329 netSupplierInfo->frequency_ = 0x10;
330 result = instance_.UpdateNetSupplierInfo(supplierId, netSupplierInfo);
331 ASSERT_EQ(result, NETMANAGER_SUCCESS);
332 }
333
334 HWTEST_F(NetManagerCenterTest, GetIfaceStatsDetailTest001, TestSize.Level1)
335 {
336 std::string iface = "test_iface";
337 uint32_t startTime = 0;
338 uint32_t endTime = 9999999;
339 NetStatsInfo info;
340 int32_t ret = instance_.GetIfaceStatsDetail(iface, startTime, endTime, info);
341 EXPECT_EQ(ret, NETMANAGER_ERROR);
342 }
343
344 HWTEST_F(NetManagerCenterTest, GetIfaceStatsDetailTest002, TestSize.Level1)
345 {
346 sptr<NetStatsBaseService> service = new (std::nothrow) TestNetStatsService();
347 instance_.RegisterStatsService(service);
348 std::string iface = "test_iface";
349 uint32_t startTime = 0;
350 uint32_t endTime = 9999999;
351 NetStatsInfo info;
352 int32_t ret = instance_.GetIfaceStatsDetail(iface, startTime, endTime, info);
353 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
354 }
355
356 HWTEST_F(NetManagerCenterTest, ResetStatsFactoryTest001, TestSize.Level1)
357 {
358 int32_t ret = instance_.ResetStatsFactory();
359 EXPECT_EQ(ret, NETMANAGER_ERROR);
360 }
361
362 HWTEST_F(NetManagerCenterTest, ResetStatsFactoryTest002, TestSize.Level1)
363 {
364 sptr<NetStatsBaseService> service = new (std::nothrow) TestNetStatsService();
365 instance_.RegisterStatsService(service);
366 int32_t ret = instance_.ResetStatsFactory();
367 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
368 }
369
370 HWTEST_F(NetManagerCenterTest, ResetPolicyFactoryTest001, TestSize.Level1)
371 {
372 int32_t ret = instance_.ResetPolicyFactory();
373 EXPECT_EQ(ret, NETMANAGER_ERROR);
374 }
375
376 HWTEST_F(NetManagerCenterTest, ResetPolicyFactoryTest002, TestSize.Level1)
377 {
378 sptr<NetPolicyBaseService> service = new (std::nothrow) TestNetPolicyService();
379 instance_.RegisterPolicyService(service);
380 int32_t ret = instance_.ResetPolicyFactory();
381 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
382 }
383
384 HWTEST_F(NetManagerCenterTest, ResetPoliciesTest001, TestSize.Level1)
385 {
386 int32_t ret = instance_.ResetPolicies();
387 EXPECT_EQ(ret, NETMANAGER_ERROR);
388 }
389
390 HWTEST_F(NetManagerCenterTest, ResetPoliciesTest002, TestSize.Level1)
391 {
392 sptr<NetPolicyBaseService> service = new (std::nothrow) TestNetPolicyService();
393 instance_.RegisterPolicyService(service);
394 int32_t ret = instance_.ResetPolicies();
395 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
396 }
397
398 HWTEST_F(NetManagerCenterTest, ResetEthernetFactoryTest001, TestSize.Level1)
399 {
400 int32_t ret = instance_.ResetEthernetFactory();
401 EXPECT_EQ(ret, NETMANAGER_ERROR);
402 }
403
404 HWTEST_F(NetManagerCenterTest, ResetEthernetFactoryTest002, TestSize.Level1)
405 {
406 sptr<NetEthernetBaseService> service = new (std::nothrow) TestNetEthernetService();
407 instance_.RegisterEthernetService(service);
408 int32_t ret = instance_.ResetEthernetFactory();
409 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
410 }
411
412 HWTEST_F(NetManagerCenterTest, GetAddressesByNameTest001, TestSize.Level1)
413 {
414 const std::string testHostName = "test_hostname";
415 int32_t testNetId = 111;
416 std::vector<INetAddr> addrInfo;
417 int32_t ret = instance_.GetAddressesByName(testHostName, testNetId, addrInfo);
418 EXPECT_EQ(ret, NETMANAGER_ERROR);
419 }
420
421 HWTEST_F(NetManagerCenterTest, GetAddressesByNameTest002, TestSize.Level1)
422 {
423 sptr<DnsBaseService> service = new (std::nothrow) TestDnsService();
424 instance_.RegisterDnsService(service);
425 const std::string testHostName = "test_hostname";
426 int32_t testNetId = 111;
427 std::vector<INetAddr> addrInfo;
428 int32_t ret = instance_.GetAddressesByName(testHostName, testNetId, addrInfo);
429 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
430 }
431
432 HWTEST_F(NetManagerCenterTest, RestrictBackgroundChangedTest001, TestSize.Level1)
433 {
434 int32_t ret = instance_.RestrictBackgroundChanged(true);
435 EXPECT_EQ(ret, NETMANAGER_ERROR);
436 }
437
438 HWTEST_F(NetManagerCenterTest, RestrictBackgroundChangedTest002, TestSize.Level1)
439 {
440 int32_t ret = instance_.RestrictBackgroundChanged(false);
441 EXPECT_EQ(ret, NETMANAGER_ERROR);
442 }
443
444 HWTEST_F(NetManagerCenterTest, IsUidNetAccessTest001, TestSize.Level1)
445 {
446 bool ret = instance_.IsUidNetAccess(0, false);
447 EXPECT_TRUE(ret);
448 }
449
450 HWTEST_F(NetManagerCenterTest, IsUidNetAccessTest002, TestSize.Level1)
451 {
452 bool ret = instance_.IsUidNetAccess(0, true);
453 EXPECT_TRUE(ret);
454 }
455
456 HWTEST_F(NetManagerCenterTest, IsUidNetAllowedTest001, TestSize.Level1)
457 {
458 bool ret = instance_.IsUidNetAllowed(0, true);
459 EXPECT_TRUE(ret);
460 }
461
462 HWTEST_F(NetManagerCenterTest, IsUidNetAllowedTest002, TestSize.Level1)
463 {
464 bool ret = instance_.IsUidNetAllowed(0, false);
465 EXPECT_TRUE(ret);
466 }
467 } // namespace NetManagerStandard
468 } // namespace OHOS