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_define.h" 18 #include "common_util.h" 19 20 using namespace testing::ext; 21 HWTEST(CommonUtilTest, LogTimeTest, TestSize.Level1) 22 { 23 LogTime(); 24 uint64_t begin = Tmspsec(); 25 EXPECT_TRUE(begin > 0); 26 sleep(1); 27 uint64_t curr = Tmspsec(); 28 EXPECT_TRUE(curr > begin); 29 begin = Tmspusec(); 30 EXPECT_TRUE(begin > 0); 31 sleep(1); 32 curr = Tmspusec(); 33 EXPECT_TRUE(begin > 0); 34 } 35 36 HWTEST(CommonUtilTest, LeftTirmTest, TestSize.Level1) 37 { 38 char src1[] = " aabbccdd"; 39 LeftTrim(src1); 40 EXPECT_STREQ("aabbccdd", src1); 41 } 42 43 HWTEST(CommonUtilTest, RightTrimTest, TestSize.Level1) 44 { 45 char src1[] = "aabbccdd "; 46 RightTrim(src1); 47 EXPECT_STREQ("aabbccdd", src1); 48 } 49 50 HWTEST(CommonUtilTest, TrimStringTest, TestSize.Level1) 51 { 52 char src1[] = "aabbccdd "; 53 char src2[] = " aabbccdd"; 54 char src3[] = " aabbccdd "; 55 TrimString(src1); 56 TrimString(src2); 57 TrimString(src3); 58 EXPECT_STREQ("aabbccdd", src1); 59 EXPECT_STREQ("aabbccdd", src2); 60 EXPECT_STREQ("aabbccdd", src3); 61 } 62 63 HWTEST(CommonUtilTest, GetFilePathTest, TestSize.Level1) 64 { 65 const char *testPath = "/etc/dhcp/dhcp_server.conf"; 66 EXPECT_TRUE(GetFilePath(NULL) == NULL); 67 EXPECT_TRUE(GetFilePath("") == NULL); 68 EXPECT_STREQ("/etc/dhcp", GetFilePath(testPath)); 69 } 70 71 HWTEST(CommonUtilTest, GetLeaseFileTest, TestSize.Level1) 72 { 73 const char *leaseFile = "/etc/dhcp/dhcp_server.lease"; 74 const char *ifname = "test_if0"; 75 EXPECT_TRUE(GetLeaseFile(leaseFile, NULL) == NULL); 76 EXPECT_TRUE(GetLeaseFile(NULL, NULL) == NULL); 77 EXPECT_STREQ("/etc/dhcp/dhcp_server.lease.test_if0", GetLeaseFile(leaseFile, ifname)); 78 } 79 80 HWTEST(CommonUtilTest, CreatePathTest, TestSize.Level1) 81 { 82 const char *testPath = "./test/data/testpath"; 83 ASSERT_EQ(RET_FAILED, CreatePath(NULL)); 84 ASSERT_EQ(RET_FAILED, CreatePath("")); 85 ASSERT_EQ(RET_SUCCESS, CreatePath(testPath)); 86 EXPECT_EQ(0, remove(testPath)); 87 }