• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "dhcp_common_utils.h"
18 #include "dhcp_logger.h"
19 
20 DEFINE_DHCPLOG_DHCP_LABEL("DhcpCommonUtilsTest");
21 
22 using namespace testing::ext;
23 using namespace OHOS::DHCP;
24 namespace OHOS {
25 constexpr int ONE = 1;
26 constexpr int32_t MAC_LENTH = 6;
27 
28 class DhcpCommonUtilsTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase()
31     {}
TearDownTestCase()32     static void TearDownTestCase()
33     {}
SetUp()34     virtual void SetUp()
35     {}
TearDown()36     virtual void TearDown()
37     {}
38 };
39 
40 /**
41 * @tc.name: Ipv4AnonymizeTest_SUCCESS
42 * @tc.desc: Ipv4AnonymizeTest.
43 * @tc.type: FUNC
44 * @tc.require: AR00000000
45 */
46 HWTEST_F(DhcpCommonUtilsTest, Ipv4AnonymizeTest_SUCCESS, TestSize.Level1)
47 {
48     DHCP_LOGI("enter Ipv4AnonymizeTest_SUCCESS");
49     std::string ipAddr = "1.2.3.4";
50     std::string ret = Ipv4Anonymize(ipAddr);
51     EXPECT_TRUE(!ret.empty());
52     DHCP_LOGI("ret is %{public}s", ret.c_str());
53 }
54 
55 HWTEST_F(DhcpCommonUtilsTest, UintIp4ToStrTest, TestSize.Level1)
56 {
57     DHCP_LOGI("enter UintIp4ToStrTest");
58     uint32_t ip = 4294967295;
59     char *pIp = UintIp4ToStr(ip, false);
60     if (pIp != nullptr) {
61         DHCP_LOGI("pIp:%{public}s", pIp);
62         free(pIp);
63         pIp = nullptr;
64     }
65     char *pIp2 = UintIp4ToStr(ip, true);
66     if (pIp2 != nullptr) {
67         DHCP_LOGI("pIp2:%{public}s", pIp2);
68         free(pIp2);
69         pIp2 = nullptr;
70     }
71     EXPECT_EQ(true, ONE);
72 }
73 
74 HWTEST_F(DhcpCommonUtilsTest, IntIpv4ToAnonymizeStrTest, TestSize.Level1)
75 {
76     DHCP_LOGI("enter IntIpv4ToAnonymizeStrTest");
77     uint32_t ip = 4294967295;
78     std::string ret = IntIpv4ToAnonymizeStr(ip);
79     EXPECT_TRUE(!ret.empty());
80     DHCP_LOGI("ret is %{public}s", ret.c_str());
81 }
82 
83 HWTEST_F(DhcpCommonUtilsTest, MacArray2StrTest, TestSize.Level1)
84 {
85     DHCP_LOGI("enter MacArray2StrTest");
86     uint8_t *macArray = nullptr;
87     int32_t len = 0;
88     EXPECT_TRUE(MacArray2Str(macArray, len).empty());
89     uint8_t mac[MAC_LENTH] = {12, 12, 33, 54, 56, 78};
90     EXPECT_TRUE(MacArray2Str(mac, len).empty());
91     len = MAC_LENTH;
92     EXPECT_TRUE(!MacArray2Str(mac, len).empty());
93 }
94 
95 HWTEST_F(DhcpCommonUtilsTest, ValidHexadecimalNumberTest, TestSize.Level1)
96 {
97     DHCP_LOGI("enter ValidHexadecimalNumberTest");
98     std::string data = "123456";
99     int result = CheckDataLegal(data);
100     EXPECT_EQ(result, 123456);
101 }
102 
103 HWTEST_F(DhcpCommonUtilsTest, InvalidHexadecimalNumberTest, TestSize.Level1)
104 {
105     DHCP_LOGI("enter InvalidHexadecimalNumberTest");
106     std::string data = "abcdef";
107     int result = CheckDataLegal(data);
108     EXPECT_EQ(result, 0);
109 }
110 
111 HWTEST_F(DhcpCommonUtilsTest, EmptyStringTest, TestSize.Level1)
112 {
113     DHCP_LOGI("enter EmptyStringTest");
114     std::string data = "";
115     int result = CheckDataLegal(data);
116     EXPECT_EQ(result, 0);
117 }
118 
119 HWTEST_F(DhcpCommonUtilsTest, GetElapsedSecondsSinceBootTest, TestSize.Level1)
120 {
121     DHCP_LOGI("enter GetElapsedSecondsSinceBootTest");
122     int64_t result = GetElapsedSecondsSinceBoot();
123     EXPECT_GE(result, 0);
124 }
125 
126 HWTEST_F(DhcpCommonUtilsTest, CheckDataToUintTest, TestSize.Level1)
127 {
128     DHCP_LOGI("enter CheckDataToUintTest");
129     std::string data = "abc";
130     int base = 10;
131     unsigned int result = CheckDataToUint(data, base);
132     EXPECT_EQ(result, 0);
133 
134     data = "-123";
135     result = CheckDataToUint(data, base);
136     EXPECT_EQ(result, 0);
137 
138     data = "4294967296"; // 2^32 + 1, out of range for unsigned int
139     result = CheckDataToUint(data, base);
140     EXPECT_EQ(result, 0);
141 
142     data = "123";
143     result = CheckDataToUint(data, base);
144     EXPECT_EQ(result, 123);
145 }
146 
147 HWTEST_F(DhcpCommonUtilsTest, CheckDataTolonglongTest, TestSize.Level1)
148 {
149     DHCP_LOGI("enter CheckDataTolonglongTest");
150     std::string data = "";
151     int base = 10;
152     long long result = CheckDataTolonglong(data, base);
153     EXPECT_EQ(result, 0);
154 
155     data = "abc";
156     result = CheckDataTolonglong(data, base);
157     EXPECT_EQ(result, 0);
158 
159     data = "12345678901234567890";
160     result = CheckDataTolonglong(data, base);
161     EXPECT_EQ(result, 0);
162 
163     data = "12345";
164     result = CheckDataTolonglong(data, base);
165     EXPECT_EQ(result, 12345);
166 
167     data = "9223372036854775808";
168     result = CheckDataTolonglong(data, base);
169     EXPECT_EQ(result, 0);
170 }
171 }