1 /* 2 * Copyright (C) 2021-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 #include <string> 18 #include <securec.h> 19 #include <cstdlib> 20 #include <cstdio> 21 #include "dhcp_argument.h" 22 #include "dhcp_s_define.h" 23 #include "dhcp_logger.h" 24 25 DEFINE_DHCPLOG_DHCP_LABEL("DhcpArgumentTest"); 26 27 using namespace testing::ext; 28 29 namespace OHOS { 30 namespace DHCP { 31 HWTEST(DhcpArgumentTest, ParseArgumentsTest, TestSize.Level1) 32 { 33 PrintRequiredArguments(); 34 ShowHelp(2); 35 std::string ifname = "eth0"; 36 std::string netMask = "192.168.1.2"; 37 std::string ipRange = "192.168.1.1 , 192.168.1.100"; 38 std::string localIp = "192.168.62.1"; 39 int result = ParseArguments(ifname, netMask, ipRange, localIp); 40 EXPECT_EQ(result, RET_SUCCESS); 41 } 42 43 HWTEST(DhcpArgumentTest, InitArgumentsTest, TestSize.Level1) 44 { 45 EXPECT_TRUE(InitArguments() == RET_SUCCESS); 46 } 47 48 HWTEST(DhcpArgumentTest, PutArgumentTest, TestSize.Level1) 49 { 50 EXPECT_TRUE(PutArgument(NULL, NULL) == RET_FAILED); 51 const char *argu = "lease"; 52 const char *val = "4000"; 53 EXPECT_TRUE(PutArgument(NULL, val) == RET_FAILED); 54 EXPECT_TRUE(PutArgument(argu, NULL) == RET_FAILED); 55 EXPECT_TRUE(PutArgument(argu, val) == RET_SUCCESS); 56 argu = "xxxx"; 57 val = "nothing"; 58 EXPECT_TRUE(PutArgument(argu, val) == RET_SUCCESS); 59 EXPECT_TRUE(PutArgument(argu, val) == RET_FAILED); 60 argu = "longlongvalue"; 61 val = "verylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvaluevery" 62 "longvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvaluev" 63 "erylongvalueverylongvalueverylongvalueverylongvalue"; 64 EXPECT_TRUE(PutArgument(argu, val) == RET_ERROR); 65 } 66 67 HWTEST(DhcpArgumentTest, GetArgumentTest, TestSize.Level1) 68 { 69 DHCP_LOGE("enter GetArgumentTest"); 70 ArgumentInfo *arg = GetArgument("lease"); 71 EXPECT_TRUE(arg); 72 EXPECT_EQ(0, strncmp(arg->name, "lease", strlen("lease"))); 73 EXPECT_EQ(0, strncmp(arg->value, "4000", strlen("4000"))); 74 75 arg = GetArgument("xxxx"); 76 EXPECT_TRUE(arg); 77 EXPECT_EQ(0, strncmp(arg->name, "xxxx", strlen("xxxx"))); 78 EXPECT_EQ(0, strncmp(arg->value, "nothing", strlen("nothing"))); 79 } 80 81 HWTEST(DhcpArgumentTest, HasArgumentTest, TestSize.Level1) 82 { 83 const char *name = "xxx"; 84 EXPECT_TRUE(HasArgument(name) == 0); 85 name = "lease"; 86 EXPECT_TRUE(HasArgument(name) == 1); 87 name = nullptr; 88 EXPECT_TRUE(HasArgument(name) == 0); 89 FreeArguments(); 90 } 91 } 92 }