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 int32_t MAC_LENTH = 6;
26
27 class DhcpCommonUtilsTest : public testing::Test {
28 public:
SetUpTestCase()29 static void SetUpTestCase()
30 {}
TearDownTestCase()31 static void TearDownTestCase()
32 {}
SetUp()33 virtual void SetUp()
34 {}
TearDown()35 virtual void TearDown()
36 {}
37 };
38
39 /**
40 * @tc.name: Ipv4AnonymizeTest_SUCCESS
41 * @tc.desc: Ipv4AnonymizeTest.
42 * @tc.type: FUNC
43 * @tc.require: AR00000000
44 */
HWTEST_F(DhcpCommonUtilsTest, Ipv4AnonymizeTest_SUCCESS, TestSize.Level1)45 HWTEST_F(DhcpCommonUtilsTest, Ipv4AnonymizeTest_SUCCESS, TestSize.Level1)
46 {
47 DHCP_LOGI("enter Ipv4AnonymizeTest_SUCCESS");
48 std::string ipAddr = "1.2.3.4";
49 std::string ret = Ipv4Anonymize(ipAddr);
50 EXPECT_TRUE(!ret.empty());
51 DHCP_LOGI("ret is %{public}s", ret.c_str());
52 }
53
HWTEST_F(DhcpCommonUtilsTest, UintIp4ToStrTest, TestSize.Level1)54 HWTEST_F(DhcpCommonUtilsTest, UintIp4ToStrTest, TestSize.Level1)
55 {
56 DHCP_LOGI("enter UintIp4ToStrTest");
57 uint32_t ip = 4294967295;
58 char *pIp = UintIp4ToStr(ip, false);
59 if (pIp != nullptr) {
60 DHCP_LOGI("pIp:%{public}s", pIp);
61 free(pIp);
62 pIp = nullptr;
63 }
64 char *pIp2 = UintIp4ToStr(ip, true);
65 if (pIp2 != nullptr) {
66 DHCP_LOGI("pIp2:%{public}s", pIp2);
67 free(pIp2);
68 pIp2 = nullptr;
69 }
70 }
71
HWTEST_F(DhcpCommonUtilsTest, IntIpv4ToAnonymizeStrTest, TestSize.Level1)72 HWTEST_F(DhcpCommonUtilsTest, IntIpv4ToAnonymizeStrTest, TestSize.Level1)
73 {
74 DHCP_LOGI("enter IntIpv4ToAnonymizeStrTest");
75 uint32_t ip = 4294967295;
76 std::string ret = IntIpv4ToAnonymizeStr(ip);
77 EXPECT_TRUE(!ret.empty());
78 DHCP_LOGI("ret is %{public}s", ret.c_str());
79 }
80
HWTEST_F(DhcpCommonUtilsTest, MacArray2StrTest, TestSize.Level1)81 HWTEST_F(DhcpCommonUtilsTest, MacArray2StrTest, TestSize.Level1)
82 {
83 DHCP_LOGI("enter MacArray2StrTest");
84 uint8_t *macArray = nullptr;
85 int32_t len = 0;
86 EXPECT_TRUE(MacArray2Str(macArray, len).empty());
87 uint8_t mac[MAC_LENTH] = {12, 12, 33, 54, 56, 78};
88 EXPECT_TRUE(MacArray2Str(mac, len).empty());
89 len = MAC_LENTH;
90 EXPECT_TRUE(!MacArray2Str(mac, len).empty());
91 }
92
HWTEST_F(DhcpCommonUtilsTest, ValidHexadecimalNumberTest, TestSize.Level1)93 HWTEST_F(DhcpCommonUtilsTest, ValidHexadecimalNumberTest, TestSize.Level1)
94 {
95 DHCP_LOGI("enter ValidHexadecimalNumberTest");
96 std::string data = "123456";
97 int result = CheckDataLegal(data);
98 EXPECT_EQ(result, 123456);
99 }
100
HWTEST_F(DhcpCommonUtilsTest, InvalidHexadecimalNumberTest, TestSize.Level1)101 HWTEST_F(DhcpCommonUtilsTest, InvalidHexadecimalNumberTest, TestSize.Level1)
102 {
103 DHCP_LOGI("enter InvalidHexadecimalNumberTest");
104 std::string data = "abcdef";
105 int result = CheckDataLegal(data);
106 EXPECT_EQ(result, 0);
107 }
108
HWTEST_F(DhcpCommonUtilsTest, EmptyStringTest, TestSize.Level1)109 HWTEST_F(DhcpCommonUtilsTest, EmptyStringTest, TestSize.Level1)
110 {
111 DHCP_LOGI("enter EmptyStringTest");
112 std::string data = "";
113 int result = CheckDataLegal(data);
114 EXPECT_EQ(result, 0);
115 }
116
HWTEST_F(DhcpCommonUtilsTest, GetElapsedSecondsSinceBootTest, TestSize.Level1)117 HWTEST_F(DhcpCommonUtilsTest, GetElapsedSecondsSinceBootTest, TestSize.Level1)
118 {
119 DHCP_LOGI("enter GetElapsedSecondsSinceBootTest");
120 int64_t result = GetElapsedSecondsSinceBoot();
121 EXPECT_GE(result, 0);
122 }
123 }