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