• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 
18 #include "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 uint32_t ADDREDD_LEN = 16;
29 } // namespace
30 class UtNetmanagerBaseCommon : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp();
35     void TearDown();
36 };
SetUpTestCase()37 void UtNetmanagerBaseCommon::SetUpTestCase() {}
38 
TearDownTestCase()39 void UtNetmanagerBaseCommon::TearDownTestCase() {}
40 
SetUp()41 void UtNetmanagerBaseCommon::SetUp() {}
42 
TearDown()43 void UtNetmanagerBaseCommon::TearDown() {}
44 
45 /**
46  * @tc.name: UtNetmanagerBaseCommon001
47  * @tc.desc: Test UtNetmanagerBaseCommon ForkExec.
48  * @tc.type: FUNC
49  */
50 HWTEST_F(UtNetmanagerBaseCommon, UtNetmanagerBaseCommon001, TestSize.Level1)
51 {
52     std::string out;
53     CommonUtils::ForkExec("/system/bin/ls -a", &out);
54     ASSERT_FALSE(out.empty());
55     std::cout << "out: " << out << std::endl;
56 }
57 
58 /**
59  * @tc.name: UtNetmanagerBaseCommon002
60  * @tc.desc: Test UtNetmanagerBaseCommon ForkExec.
61  * @tc.type: FUNC
62  */
63 HWTEST_F(UtNetmanagerBaseCommon, UtNetmanagerBaseCommon002, TestSize.Level1)
64 {
65     std::string out;
66     CommonUtils::ForkExec("/system/bin/ls -l", &out);
67     ASSERT_FALSE(out.empty());
68     std::cout << "out: " << out << std::endl;
69 }
70 
71 /**
72  * @tc.name: UtNetmanagerBaseCommon003
73  * @tc.desc: Test UtNetmanagerBaseCommon ForkExec.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(UtNetmanagerBaseCommon, UtNetmanagerBaseCommon003, TestSize.Level1)
77 {
78     CommonUtils::ForkExec("/system/bin/mount -o rw,remount /");
79     CommonUtils::ForkExec("/system/bin/mkdir uttest");
80     std::string out;
81     CommonUtils::ForkExec("/system/bin/ls -a", &out);
82     ASSERT_TRUE(out.find("uttest") != std::string::npos);
83     CommonUtils::ForkExec("/system/bin/rm -rf uttest");
84 }
85 
86 /**
87  * @tc.name: SplitTest001
88  * @tc.desc: Test UtNetmanagerBaseCommon Split.
89  * @tc.type: FUNC
90  */
91 HWTEST_F(UtNetmanagerBaseCommon, SplitTest001, TestSize.Level1)
92 {
93     std::vector<std::string> result = CommonUtils::Split(TEST_TEXT, SPLIT);
94     ASSERT_FALSE(result.empty());
95 }
96 
97 /**
98  * @tc.name: SplitTest002
99  * @tc.desc: Test UtNetmanagerBaseCommon Split.
100  * @tc.type: FUNC
101  */
102 HWTEST_F(UtNetmanagerBaseCommon, SplitTest002, TestSize.Level1)
103 {
104     std::vector<std::string> result = CommonUtils::Split({}, SPLIT);
105     ASSERT_TRUE(result.empty());
106 }
107 
108 /**
109  * @tc.name: StripTest001
110  * @tc.desc: Test UtNetmanagerBaseCommon Strip.
111  * @tc.type: FUNC
112  */
113 HWTEST_F(UtNetmanagerBaseCommon, StripTest001, TestSize.Level1)
114 {
115     auto result = CommonUtils::Strip(TEST_TEXT, '?');
116     ASSERT_FALSE(result.empty());
117 }
118 
119 /**
120  * @tc.name: IsValidIPV4Test001
121  * @tc.desc: Test UtNetmanagerBaseCommon IsValidIPV4.
122  * @tc.type: FUNC
123  */
124 HWTEST_F(UtNetmanagerBaseCommon, IsValidIPV4Test001, TestSize.Level1)
125 {
126     auto result = CommonUtils::IsValidIPV4(TEST_TEXT);
127     ASSERT_FALSE(result);
128 }
129 
130 /**
131  * @tc.name: IsValidIPV4Test002
132  * @tc.desc: Test UtNetmanagerBaseCommon IsValidIPV4.
133  * @tc.type: FUNC
134  */
135 HWTEST_F(UtNetmanagerBaseCommon, IsValidIPV4Test002, TestSize.Level1)
136 {
137     auto result = CommonUtils::IsValidIPV4({});
138     ASSERT_FALSE(result);
139 }
140 
141 /**
142  * @tc.name: IsValidIPV6Test001
143  * @tc.desc: Test UtNetmanagerBaseCommon IsValidIPV6.
144  * @tc.type: FUNC
145  */
146 HWTEST_F(UtNetmanagerBaseCommon, IsValidIPV6Test001, TestSize.Level1)
147 {
148     auto result = CommonUtils::IsValidIPV6(TEST_TEXT);
149     ASSERT_FALSE(result);
150 }
151 
152 /**
153  * @tc.name: IsValidIPV6Test002
154  * @tc.desc: Test UtNetmanagerBaseCommon IsValidIPV6.
155  * @tc.type: FUNC
156  */
157 HWTEST_F(UtNetmanagerBaseCommon, IsValidIPV6Test002, TestSize.Level1)
158 {
159     auto result = CommonUtils::IsValidIPV6({});
160     ASSERT_FALSE(result);
161 }
162 
163 /**
164  * @tc.name: GetAddrFamilyTest001
165  * @tc.desc: Test UtNetmanagerBaseCommon GetAddrFamily.
166  * @tc.type: FUNC
167  */
168 HWTEST_F(UtNetmanagerBaseCommon, GetAddrFamilyTest001, TestSize.Level1)
169 {
170     auto result = CommonUtils::GetAddrFamily(TEST_IP);
171     ASSERT_NE(result, 0);
172 }
173 
174 /**
175  * @tc.name: GetMaskLengthTest001
176  * @tc.desc: Test UtNetmanagerBaseCommon GetMaskLength.
177  * @tc.type: FUNC
178  */
179 HWTEST_F(UtNetmanagerBaseCommon, GetMaskLengthTest001, TestSize.Level1)
180 {
181     auto result = CommonUtils::GetMaskLength(TEST_TEXT);
182     ASSERT_NE(result, 0);
183 }
184 
185 /**
186  * @tc.name: ConvertIpv4AddressTest001
187  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
188  * @tc.type: FUNC
189  */
190 HWTEST_F(UtNetmanagerBaseCommon, ConvertIpv4AddressTest001, TestSize.Level1)
191 {
192     auto result = CommonUtils::ConvertIpv4Address(0);
193     ASSERT_TRUE(result.empty());
194 }
195 
196 /**
197  * @tc.name: ConvertIpv4AddressTest002
198  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
199  * @tc.type: FUNC
200  */
201 HWTEST_F(UtNetmanagerBaseCommon, ConvertIpv4AddressTest002, TestSize.Level1)
202 {
203     auto result = CommonUtils::ConvertIpv4Address(ADDREDD_LEN);
204     ASSERT_FALSE(result.empty());
205 }
206 
207 /**
208  * @tc.name: ConvertIpv4AddressTest003
209  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
210  * @tc.type: FUNC
211  */
212 HWTEST_F(UtNetmanagerBaseCommon, ConvertIpv4AddressTest003, TestSize.Level1)
213 {
214     auto result = CommonUtils::ConvertIpv4Address(TEST_IP);
215     ASSERT_NE(result, static_cast<uint32_t>(0));
216 }
217 
218 /**
219  * @tc.name: ConvertIpv4AddressTest004
220  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
221  * @tc.type: FUNC
222  */
223 HWTEST_F(UtNetmanagerBaseCommon, ConvertIpv4AddressTest004, TestSize.Level1)
224 {
225     std::string addr;
226     auto result = CommonUtils::ConvertIpv4Address(addr);
227     ASSERT_EQ(result, static_cast<uint32_t>(0));
228 }
229 
230 /**
231  * @tc.name: Ipv4PrefixLenTest001
232  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
233  * @tc.type: FUNC
234  */
235 HWTEST_F(UtNetmanagerBaseCommon, Ipv4PrefixLenTest001, TestSize.Level1)
236 {
237     std::string addr;
238     auto result = CommonUtils::ConvertIpv4Address(addr);
239     ASSERT_EQ(result, static_cast<uint32_t>(0));
240 }
241 
242 /**
243  * @tc.name: Ipv4PrefixLenTest002
244  * @tc.desc: Test UtNetmanagerBaseCommon ConvertIpv4Address.
245  * @tc.type: FUNC
246  */
247 HWTEST_F(UtNetmanagerBaseCommon, Ipv4PrefixLenTest002, TestSize.Level1)
248 {
249     auto result = CommonUtils::ConvertIpv4Address(TEST_IP);
250     ASSERT_NE(result, static_cast<uint32_t>(0));
251 }
252 
253 /**
254  * @tc.name: ParseIntTest001
255  * @tc.desc: Test UtNetmanagerBaseCommon ParseInt.
256  * @tc.type: FUNC
257  */
258 HWTEST_F(UtNetmanagerBaseCommon, ParseIntTest001, TestSize.Level1)
259 {
260     std::string testStr = "123";
261     int32_t value = 0;
262     auto result = CommonUtils::ParseInt(testStr, &value);
263     ASSERT_NE(value, 0);
264     ASSERT_TRUE(result);
265 }
266 
267 /**
268  * @tc.name: ParseIntTest002
269  * @tc.desc: Test UtNetmanagerBaseCommon ParseInt.
270  * @tc.type: FUNC
271  */
272 HWTEST_F(UtNetmanagerBaseCommon, ParseIntTest002, TestSize.Level1)
273 {
274     std::string testStr = "abcdfagdshrfsth";
275     int32_t value = 0;
276     auto result = CommonUtils::ParseInt(testStr, &value);
277     ASSERT_EQ(value, 0);
278     ASSERT_FALSE(result);
279 }
280 
281 /**
282  * @tc.name: ParseIntTest003
283  * @tc.desc: Test UtNetmanagerBaseCommon ParseInt.
284  * @tc.type: FUNC
285  */
286 HWTEST_F(UtNetmanagerBaseCommon, ParseIntTest003, TestSize.Level1)
287 {
288     std::string testStr = "44514564121561456745456891564564894";
289     int32_t value = 0;
290     auto result = CommonUtils::ParseInt(testStr, &value);
291     ASSERT_EQ(value, 0);
292     ASSERT_FALSE(result);
293 }
294 
295 /**
296  * @tc.name: ParseIntTest004
297  * @tc.desc: Test UtNetmanagerBaseCommon ParseInt.
298  * @tc.type: FUNC
299  */
300 HWTEST_F(UtNetmanagerBaseCommon, ParseIntTest004, TestSize.Level1)
301 {
302     std::string testStr = "-156423456123512423456146";
303     int32_t value = 0;
304     auto result = CommonUtils::ParseInt(testStr, &value);
305     ASSERT_EQ(value, 0);
306     ASSERT_FALSE(result);
307 }
308 
309 /**
310  * @tc.name: ConvertToInt64Test001
311  * @tc.desc: Test UtNetmanagerBaseCommon ConvertToInt64.
312  * @tc.type: FUNC
313  */
314 HWTEST_F(UtNetmanagerBaseCommon, ConvertToInt64Test001, TestSize.Level1)
315 {
316     std::string testStr = "145689";
317     auto result = CommonUtils::ConvertToInt64(testStr);
318     ASSERT_NE(result, 0);
319 }
320 
321 /**
322  * @tc.name: ToAnonymousIpTest001
323  * @tc.desc: Test UtNetmanagerBaseCommon ToAnonymousIp.
324  * @tc.type: FUNC
325  */
326 HWTEST_F(UtNetmanagerBaseCommon, ToAnonymousIpTest001, TestSize.Level1)
327 {
328     auto result = CommonUtils::ToAnonymousIp(TEST_IPV4);
329     ASSERT_FALSE(result.empty());
330 }
331 
332 /**
333  * @tc.name: ToAnonymousIpTest002
334  * @tc.desc: Test UtNetmanagerBaseCommon ToAnonymousIp.
335  * @tc.type: FUNC
336  */
337 HWTEST_F(UtNetmanagerBaseCommon, ToAnonymousIpTest002, TestSize.Level1)
338 {
339     auto result = CommonUtils::ToAnonymousIp(TEST_IP);
340     ASSERT_FALSE(result.empty());
341 }
342 
343 /*
344  * @tc.name: ToAnonymousIpTest003
345  * @tc.desc: Test UtNetmanagerBaseCommon ToAnonymousIp.
346  * @tc.type: FUNC
347  */
348 HWTEST_F(UtNetmanagerBaseCommon, ToAnonymousIpTest003, TestSize.Level1)
349 {
350     auto result = CommonUtils::ToAnonymousIp({});
351     ASSERT_TRUE(result.empty());
352 }
353 
354 /**
355  * @tc.name: StrToIntTest001
356  * @tc.desc: Test UtNetmanagerBaseCommon StrToInt.
357  * @tc.type: FUNC
358  */
359 HWTEST_F(UtNetmanagerBaseCommon, StrToIntTest001, TestSize.Level1)
360 {
361     std::string testStr = "145689";
362     auto result = CommonUtils::StrToInt(testStr);
363     ASSERT_NE(result, 0);
364 }
365 
366 /**
367  * @tc.name: StrToUintTest001
368  * @tc.desc: Test UtNetmanagerBaseCommon StrToUint.
369  * @tc.type: FUNC
370  */
371 HWTEST_F(UtNetmanagerBaseCommon, StrToUintTest001, TestSize.Level1)
372 {
373     std::string testStr = "145689";
374     auto result = CommonUtils::StrToUint(testStr);
375     ASSERT_NE(result, static_cast<uint32_t>(0));
376 }
377 
378 /**
379  * @tc.name: StrToBoolTest001
380  * @tc.desc: Test UtNetmanagerBaseCommon StrToBool.
381  * @tc.type: FUNC
382  */
383 HWTEST_F(UtNetmanagerBaseCommon, StrToBoolTest001, TestSize.Level1)
384 {
385     std::string testStr = "145689";
386     auto result = CommonUtils::StrToBool(testStr);
387     ASSERT_TRUE(result);
388 }
389 
390 /**
391  * @tc.name: StrToLongTest001
392  * @tc.desc: Test UtNetmanagerBaseCommon StrToUint.
393  * @tc.type: FUNC
394  */
395 HWTEST_F(UtNetmanagerBaseCommon, StrToLongTest001, TestSize.Level1)
396 {
397     std::string testStr = "145689";
398     auto result = CommonUtils::StrToLong(testStr);
399     ASSERT_NE(result, 0);
400 }
401 } // namespace NetManagerStandard
402 } // namespace OHOS
403