• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "netmanager_base_common_utils.h"
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
22 namespace {
23 using namespace testing::ext;
24 constexpr const char *TEST_TEXT = "adfsjfjkfk^#$ajf!@!#$#kjd nck?fgnf<kdjnf>kjask?.fcnvdkjfn kjdkj.,.vd";
25 constexpr const char *SPLIT = "?";
26 constexpr const char *TEST_IP = "155.153.144.154";
27 constexpr const char *TEST_IPV4 = "534/6::45/144.15:4::44";
28 constexpr const char *TEST_DOMAIN1 = "123445";
29 constexpr const char *TEST_DOMAIN2 = ".com";
30 constexpr const char *TEST_DOMAIN3 = "test.com";
31 constexpr const char *TEST_DOMAIN4 = "testcom";
32 constexpr const char *TEST_DOMAIN5 = "com.test";
33 constexpr const char *TEST_DOMAIN6 = "test.co.uk";
34 constexpr const char *TEST_DOMAIN7 = "test.com.com";
35 constexpr const char *TEST_DOMAIN8 = "test1.test2.test3.test4.test5.com";
36 constexpr uint32_t ADDREDD_LEN = 16;
37 } // namespace
38 class UtNetmanagerBaseCommon : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp();
43     void TearDown();
44 };
SetUpTestCase()45 void UtNetmanagerBaseCommon::SetUpTestCase() {}
46 
TearDownTestCase()47 void UtNetmanagerBaseCommon::TearDownTestCase() {}
48 
SetUp()49 void UtNetmanagerBaseCommon::SetUp() {}
50 
TearDown()51 void UtNetmanagerBaseCommon::TearDown() {}
52 
53 /**
54  * @tc.name: UtNetmanagerBaseCommon001
55  * @tc.desc: Test UtNetmanagerBaseCommon ForkExec.
56  * @tc.type: FUNC
57  */
58 HWTEST_F(UtNetmanagerBaseCommon, UtNetmanagerBaseCommon001, TestSize.Level1)
59 {
60     std::string out;
61     CommonUtils::ForkExec("/system/bin/ls -a", &out);
62     ASSERT_FALSE(out.empty());
63     std::cout << "out: " << out << std::endl;
64 }
65 
66 /**
67  * @tc.name: UtNetmanagerBaseCommon002
68  * @tc.desc: Test UtNetmanagerBaseCommon ForkExec.
69  * @tc.type: FUNC
70  */
71 HWTEST_F(UtNetmanagerBaseCommon, UtNetmanagerBaseCommon002, TestSize.Level1)
72 {
73     std::string out;
74     CommonUtils::ForkExec("/system/bin/ls -l", &out);
75     ASSERT_FALSE(out.empty());
76     std::cout << "out: " << out << std::endl;
77 }
78 
79 /**
80  * @tc.name: UtNetmanagerBaseCommon003
81  * @tc.desc: Test UtNetmanagerBaseCommon ForkExec.
82  * @tc.type: FUNC
83  */
84 HWTEST_F(UtNetmanagerBaseCommon, UtNetmanagerBaseCommon003, TestSize.Level1)
85 {
86     CommonUtils::ForkExec("/system/bin/mount -o rw,remount /");
87     CommonUtils::ForkExec("/system/bin/mkdir uttest");
88     std::string out;
89     CommonUtils::ForkExec("/system/bin/ls -a", &out);
90     ASSERT_TRUE(out.find("uttest") != std::string::npos);
91     CommonUtils::ForkExec("/system/bin/rm -rf uttest");
92 }
93 
94 /**
95  * @tc.name: SplitTest001
96  * @tc.desc: Test UtNetmanagerBaseCommon Split.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(UtNetmanagerBaseCommon, SplitTest001, TestSize.Level1)
100 {
101     std::vector<std::string> result = CommonUtils::Split(TEST_TEXT, SPLIT);
102     ASSERT_FALSE(result.empty());
103 }
104 
105 /**
106  * @tc.name: SplitTest002
107  * @tc.desc: Test UtNetmanagerBaseCommon Split.
108  * @tc.type: FUNC
109  */
110 HWTEST_F(UtNetmanagerBaseCommon, SplitTest002, TestSize.Level1)
111 {
112     std::vector<std::string> result = CommonUtils::Split({}, SPLIT);
113     ASSERT_TRUE(result.empty());
114 }
115 
116 /**
117  * @tc.name: StripTest001
118  * @tc.desc: Test UtNetmanagerBaseCommon Strip.
119  * @tc.type: FUNC
120  */
121 HWTEST_F(UtNetmanagerBaseCommon, StripTest001, TestSize.Level1)
122 {
123     auto result = CommonUtils::Strip(TEST_TEXT, '?');
124     ASSERT_FALSE(result.empty());
125 }
126 
127 /**
128  * @tc.name: IsValidIPV4Test001
129  * @tc.desc: Test UtNetmanagerBaseCommon IsValidIPV4.
130  * @tc.type: FUNC
131  */
132 HWTEST_F(UtNetmanagerBaseCommon, IsValidIPV4Test001, TestSize.Level1)
133 {
134     auto result = CommonUtils::IsValidIPV4(TEST_TEXT);
135     ASSERT_FALSE(result);
136 }
137 
138 /**
139  * @tc.name: IsValidIPV4Test002
140  * @tc.desc: Test UtNetmanagerBaseCommon IsValidIPV4.
141  * @tc.type: FUNC
142  */
143 HWTEST_F(UtNetmanagerBaseCommon, IsValidIPV4Test002, TestSize.Level1)
144 {
145     auto result = CommonUtils::IsValidIPV4({});
146     ASSERT_FALSE(result);
147 }
148 
149 /**
150  * @tc.name: IsValidIPV6Test001
151  * @tc.desc: Test UtNetmanagerBaseCommon IsValidIPV6.
152  * @tc.type: FUNC
153  */
154 HWTEST_F(UtNetmanagerBaseCommon, IsValidIPV6Test001, TestSize.Level1)
155 {
156     auto result = CommonUtils::IsValidIPV6(TEST_TEXT);
157     ASSERT_FALSE(result);
158 }
159 
160 /**
161  * @tc.name: IsValidIPV6Test002
162  * @tc.desc: Test UtNetmanagerBaseCommon IsValidIPV6.
163  * @tc.type: FUNC
164  */
165 HWTEST_F(UtNetmanagerBaseCommon, IsValidIPV6Test002, TestSize.Level1)
166 {
167     auto result = CommonUtils::IsValidIPV6({});
168     ASSERT_FALSE(result);
169 }
170 
171 /**
172  * @tc.name: GetAddrFamilyTest001
173  * @tc.desc: Test UtNetmanagerBaseCommon GetAddrFamily.
174  * @tc.type: FUNC
175  */
176 HWTEST_F(UtNetmanagerBaseCommon, GetAddrFamilyTest001, TestSize.Level1)
177 {
178     auto result = CommonUtils::GetAddrFamily(TEST_IP);
179     ASSERT_NE(result, 0);
180 }
181 
182 /**
183  * @tc.name: GetMaskLengthTest001
184  * @tc.desc: Test UtNetmanagerBaseCommon GetMaskLength.
185  * @tc.type: FUNC
186  */
187 HWTEST_F(UtNetmanagerBaseCommon, GetMaskLengthTest001, TestSize.Level1)
188 {
189     auto result = CommonUtils::GetMaskLength(TEST_TEXT);
190     ASSERT_NE(result, 0);
191 }
192 
193 /**
194  * @tc.name: ConvertIpv4AddressTest001
195  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
196  * @tc.type: FUNC
197  */
198 HWTEST_F(UtNetmanagerBaseCommon, ConvertIpv4AddressTest001, TestSize.Level1)
199 {
200     auto result = CommonUtils::ConvertIpv4Address(0);
201     ASSERT_TRUE(result.empty());
202 }
203 
204 /**
205  * @tc.name: ConvertIpv4AddressTest002
206  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
207  * @tc.type: FUNC
208  */
209 HWTEST_F(UtNetmanagerBaseCommon, ConvertIpv4AddressTest002, TestSize.Level1)
210 {
211     auto result = CommonUtils::ConvertIpv4Address(ADDREDD_LEN);
212     ASSERT_FALSE(result.empty());
213 }
214 
215 /**
216  * @tc.name: ConvertIpv4AddressTest003
217  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
218  * @tc.type: FUNC
219  */
220 HWTEST_F(UtNetmanagerBaseCommon, ConvertIpv4AddressTest003, TestSize.Level1)
221 {
222     auto result = CommonUtils::ConvertIpv4Address(TEST_IP);
223     ASSERT_NE(result, static_cast<uint32_t>(0));
224 }
225 
226 /**
227  * @tc.name: ConvertIpv4AddressTest004
228  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
229  * @tc.type: FUNC
230  */
231 HWTEST_F(UtNetmanagerBaseCommon, ConvertIpv4AddressTest004, TestSize.Level1)
232 {
233     std::string addr;
234     auto result = CommonUtils::ConvertIpv4Address(addr);
235     ASSERT_EQ(result, static_cast<uint32_t>(0));
236 }
237 
238 /**
239  * @tc.name: Ipv4PrefixLenTest001
240  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
241  * @tc.type: FUNC
242  */
243 HWTEST_F(UtNetmanagerBaseCommon, Ipv4PrefixLenTest001, TestSize.Level1)
244 {
245     std::string addr;
246     auto result = CommonUtils::ConvertIpv4Address(addr);
247     ASSERT_EQ(result, static_cast<uint32_t>(0));
248 }
249 
250 /**
251  * @tc.name: Ipv4PrefixLenTest002
252  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
253  * @tc.type: FUNC
254  */
255 HWTEST_F(UtNetmanagerBaseCommon, Ipv4PrefixLenTest002, TestSize.Level1)
256 {
257     auto result = CommonUtils::ConvertIpv4Address(TEST_IP);
258     ASSERT_NE(result, static_cast<uint32_t>(0));
259 }
260 
261 /**
262  * @tc.name: ParseIntTest001
263  * @tc.desc: Test UtNetmanagerBaseCommon ParseInt.
264  * @tc.type: FUNC
265  */
266 HWTEST_F(UtNetmanagerBaseCommon, ParseIntTest001, TestSize.Level1)
267 {
268     std::string testStr = "123";
269     int32_t value = 0;
270     auto result = CommonUtils::ParseInt(testStr, &value);
271     ASSERT_NE(value, 0);
272     ASSERT_TRUE(result);
273 }
274 
275 /**
276  * @tc.name: ParseIntTest002
277  * @tc.desc: Test UtNetmanagerBaseCommon ParseInt.
278  * @tc.type: FUNC
279  */
280 HWTEST_F(UtNetmanagerBaseCommon, ParseIntTest002, TestSize.Level1)
281 {
282     std::string testStr = "abcdfagdshrfsth";
283     int32_t value = 0;
284     auto result = CommonUtils::ParseInt(testStr, &value);
285     ASSERT_EQ(value, 0);
286     ASSERT_FALSE(result);
287 }
288 
289 /**
290  * @tc.name: ParseIntTest003
291  * @tc.desc: Test UtNetmanagerBaseCommon ParseInt.
292  * @tc.type: FUNC
293  */
294 HWTEST_F(UtNetmanagerBaseCommon, ParseIntTest003, TestSize.Level1)
295 {
296     std::string testStr = "44514564121561456745456891564564894";
297     int32_t value = 0;
298     auto result = CommonUtils::ParseInt(testStr, &value);
299     ASSERT_EQ(value, 0);
300     ASSERT_FALSE(result);
301 }
302 
303 /**
304  * @tc.name: ParseIntTest004
305  * @tc.desc: Test UtNetmanagerBaseCommon ParseInt.
306  * @tc.type: FUNC
307  */
308 HWTEST_F(UtNetmanagerBaseCommon, ParseIntTest004, TestSize.Level1)
309 {
310     std::string testStr = "-156423456123512423456146";
311     int32_t value = 0;
312     auto result = CommonUtils::ParseInt(testStr, &value);
313     ASSERT_EQ(value, 0);
314     ASSERT_FALSE(result);
315 }
316 
317 /**
318  * @tc.name: ConvertToInt64Test001
319  * @tc.desc: Test UtNetmanagerBaseCommon ConvertToInt64.
320  * @tc.type: FUNC
321  */
322 HWTEST_F(UtNetmanagerBaseCommon, ConvertToInt64Test001, TestSize.Level1)
323 {
324     std::string testStr = "145689";
325     auto result = CommonUtils::ConvertToInt64(testStr);
326     ASSERT_NE(result, 0);
327 }
328 
329 /**
330  * @tc.name: ToAnonymousIpTest001
331  * @tc.desc: Test UtNetmanagerBaseCommon ToAnonymousIp.
332  * @tc.type: FUNC
333  */
334 HWTEST_F(UtNetmanagerBaseCommon, ToAnonymousIpTest001, TestSize.Level1)
335 {
336     auto result = CommonUtils::ToAnonymousIp(TEST_IPV4);
337     ASSERT_FALSE(result.empty());
338 }
339 
340 /**
341  * @tc.name: ToAnonymousIpTest002
342  * @tc.desc: Test UtNetmanagerBaseCommon ToAnonymousIp.
343  * @tc.type: FUNC
344  */
345 HWTEST_F(UtNetmanagerBaseCommon, ToAnonymousIpTest002, TestSize.Level1)
346 {
347     auto result = CommonUtils::ToAnonymousIp(TEST_IP);
348     ASSERT_FALSE(result.empty());
349 }
350 
351 /*
352  * @tc.name: ToAnonymousIpTest003
353  * @tc.desc: Test UtNetmanagerBaseCommon ToAnonymousIp.
354  * @tc.type: FUNC
355  */
356 HWTEST_F(UtNetmanagerBaseCommon, ToAnonymousIpTest003, TestSize.Level1)
357 {
358     auto result = CommonUtils::ToAnonymousIp({});
359     ASSERT_TRUE(result.empty());
360 }
361 
362 /**
363  * @tc.name: StrToIntTest001
364  * @tc.desc: Test UtNetmanagerBaseCommon StrToInt.
365  * @tc.type: FUNC
366  */
367 HWTEST_F(UtNetmanagerBaseCommon, StrToIntTest001, TestSize.Level1)
368 {
369     std::string testStr = "145689";
370     auto result = CommonUtils::StrToInt(testStr);
371     ASSERT_NE(result, 0);
372 }
373 
374 /**
375  * @tc.name: StrToUintTest001
376  * @tc.desc: Test UtNetmanagerBaseCommon StrToUint.
377  * @tc.type: FUNC
378  */
379 HWTEST_F(UtNetmanagerBaseCommon, StrToUintTest001, TestSize.Level1)
380 {
381     std::string testStr = "145689";
382     auto result = CommonUtils::StrToUint(testStr);
383     ASSERT_NE(result, static_cast<uint32_t>(0));
384 }
385 
386 /**
387  * @tc.name: StrToBoolTest001
388  * @tc.desc: Test UtNetmanagerBaseCommon StrToBool.
389  * @tc.type: FUNC
390  */
391 HWTEST_F(UtNetmanagerBaseCommon, StrToBoolTest001, TestSize.Level1)
392 {
393     std::string testStr = "145689";
394     auto result = CommonUtils::StrToBool(testStr);
395     ASSERT_TRUE(result);
396 }
397 
398 /**
399  * @tc.name: StrToLongTest001
400  * @tc.desc: Test UtNetmanagerBaseCommon StrToUint.
401  * @tc.type: FUNC
402  */
403 HWTEST_F(UtNetmanagerBaseCommon, StrToLongTest001, TestSize.Level1)
404 {
405     std::string testStr = "145689";
406     auto result = CommonUtils::StrToLong(testStr);
407     ASSERT_NE(result, 0);
408 }
409 
410 /**
411  * @tc.name: IsValidDomainTest001
412  * @tc.desc: Test UtNetmanagerBaseCommon IsValidDomain.
413  * @tc.type: FUNC
414  */
415 HWTEST_F(UtNetmanagerBaseCommon, IsValidDomainTest001, TestSize.Level1)
416 {
417     auto result = CommonUtils::IsValidDomain(TEST_DOMAIN1);
418     ASSERT_FALSE(result);
419 }
420 
421 /**
422  * @tc.name: IsValidDomainTest002
423  * @tc.desc: Test UtNetmanagerBaseCommon IsValidDomain.
424  * @tc.type: FUNC
425  */
426 HWTEST_F(UtNetmanagerBaseCommon, IsValidDomainTest002, TestSize.Level1)
427 {
428     auto result = CommonUtils::IsValidDomain(TEST_DOMAIN2);
429     ASSERT_FALSE(result);
430 }
431 
432 /**
433  * @tc.name: IsValidDomainTest003
434  * @tc.desc: Test UtNetmanagerBaseCommon IsValidDomain.
435  * @tc.type: FUNC
436  */
437 HWTEST_F(UtNetmanagerBaseCommon, IsValidDomainTest003, TestSize.Level1)
438 {
439     auto result = CommonUtils::IsValidDomain(TEST_DOMAIN3);
440     ASSERT_TRUE(result);
441 }
442 
443 /**
444  * @tc.name: IsValidDomainTest004
445  * @tc.desc: Test UtNetmanagerBaseCommon IsValidDomain.
446  * @tc.type: FUNC
447  */
448 HWTEST_F(UtNetmanagerBaseCommon, IsValidDomainTest004, TestSize.Level1)
449 {
450     auto result = CommonUtils::IsValidDomain(TEST_DOMAIN4);
451     ASSERT_FALSE(result);
452 }
453 
454 /**
455  * @tc.name: IsValidDomainTest005
456  * @tc.desc: Test UtNetmanagerBaseCommon IsValidDomain.
457  * @tc.type: FUNC
458  */
459 HWTEST_F(UtNetmanagerBaseCommon, IsValidDomainTest005, TestSize.Level1)
460 {
461     auto result = CommonUtils::IsValidDomain(TEST_DOMAIN5);
462     ASSERT_FALSE(result);
463 }
464 
465 /**
466  * @tc.name: IsValidDomainTest006
467  * @tc.desc: Test UtNetmanagerBaseCommon IsValidDomain.
468  * @tc.type: FUNC
469  */
470 HWTEST_F(UtNetmanagerBaseCommon, IsValidDomainTest006, TestSize.Level1)
471 {
472     auto result = CommonUtils::IsValidDomain(TEST_DOMAIN6);
473     ASSERT_TRUE(result);
474 }
475 
476 /**
477  * @tc.name: IsValidDomainTest007
478  * @tc.desc: Test UtNetmanagerBaseCommon IsValidDomain.
479  * @tc.type: FUNC
480  */
481 HWTEST_F(UtNetmanagerBaseCommon, IsValidDomainTest007, TestSize.Level1)
482 {
483     auto result = CommonUtils::IsValidDomain(TEST_DOMAIN7);
484     ASSERT_FALSE(result);
485 }
486 
487 /**
488  * @tc.name: IsValidDomainTest008
489  * @tc.desc: Test UtNetmanagerBaseCommon IsValidDomain.
490  * @tc.type: FUNC
491  */
492 HWTEST_F(UtNetmanagerBaseCommon, IsValidDomainTest008, TestSize.Level1)
493 {
494     auto result = CommonUtils::IsValidDomain(TEST_DOMAIN8);
495     ASSERT_FALSE(result);
496 }
497 
498 /**
499  * @tc.name: ToLowerTest008
500  * @tc.desc: Test UtNetmanagerBaseCommon ToLower.
501  * @tc.type: FUNC
502  */
503 HWTEST_F(UtNetmanagerBaseCommon, ToLowerTest008, TestSize.Level1)
504 {
505     std::string res = "HeLLo worLd";
506     auto result = CommonUtils::ToLower(res);
507     EXPECT_EQ(result, "hello world");
508 }
509 
510 /**
511  * @tc.name: GetMaskByLengthTest008
512  * @tc.desc: Test UtNetmanagerBaseCommon GetMaskByLength.
513  * @tc.type: FUNC
514  */
515 HWTEST_F(UtNetmanagerBaseCommon, GetMaskByLengthTest008, TestSize.Level1)
516 {
517     uint32_t length = 8;
518     auto result = CommonUtils::GetMaskByLength(length);
519     EXPECT_NE(result, "255.255.255.0");
520 }
521 
522 /**
523  * @tc.name: Ipv4PrefixLenTest008
524  * @tc.desc: Test UtNetmanagerBaseCommon Ipv4PrefixLen.
525  * @tc.type: FUNC
526  */
527 HWTEST_F(UtNetmanagerBaseCommon, Ipv4PrefixLenTest008, TestSize.Level1)
528 {
529     constexpr int32_t BIT32 = 32;
530     constexpr int32_t BIT24 = 24;
531     constexpr int32_t BIT16 = 16;
532     constexpr int32_t BIT8 = 8;
533     std::string ip = {};
534     auto result = CommonUtils::Ipv4PrefixLen(ip);
535     EXPECT_EQ(result, 0);
536     ip = "192.168.0";
537     result = CommonUtils::Ipv4PrefixLen(ip);
538     EXPECT_EQ(result, 0);
539     ip = "255.255.255.255";
540     result = CommonUtils::Ipv4PrefixLen(ip);
541     EXPECT_EQ(result, BIT32);
542     ip = "255.255.255.0";
543     result = CommonUtils::Ipv4PrefixLen(ip);
544     EXPECT_EQ(result, BIT24);
545     ip = "255.255.0.0";
546     result = CommonUtils::Ipv4PrefixLen(ip);
547     EXPECT_EQ(result, BIT16);
548     ip = "255.0.0.0";
549     result = CommonUtils::Ipv4PrefixLen(ip);
550     EXPECT_EQ(result, BIT8);
551     ip = "255.192.0.0";
552     result = CommonUtils::Ipv4PrefixLen(ip);
553     EXPECT_EQ(result, 10);
554 }
555 
556 /**
557  * @tc.name: StrToUint64Test008
558  * @tc.desc: Test UtNetmanagerBaseCommon StrToUint64.
559  * @tc.type: FUNC
560  */
561 HWTEST_F(UtNetmanagerBaseCommon, StrToUint64Test008, TestSize.Level1)
562 {
563     std::string value = {};
564     uint64_t defaultErr = 0;
565     auto result = CommonUtils::StrToUint64(value, defaultErr);
566     EXPECT_EQ(result, defaultErr);
567     value = "100";
568     uint64_t value2 = 100;
569     result = CommonUtils::StrToUint64(value, defaultErr);
570     EXPECT_EQ(result, value2);
571 }
572 } // namespace NetManagerStandard
573 } // namespace OHOS
574