• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Level0)
32 {
33     std::string ifname = "eth0";
34     std::string netMask = "192.168.1.2";
35     std::string ipRange = "192.168.1.1 , 192.168.1.100";
36     std::string localIp = "192.168.62.1";
37     int result = ParseArguments(ifname, netMask, ipRange, localIp);
38     EXPECT_EQ(result, RET_SUCCESS);
39 }
40 
41 HWTEST(DhcpArgumentTest, InitArgumentsTest, TestSize.Level0)
42 {
43     EXPECT_TRUE(InitArguments() == RET_SUCCESS);
44 }
45 
46 HWTEST(DhcpArgumentTest, PutArgumentTest, TestSize.Level0)
47 {
48     EXPECT_TRUE(PutArgument(NULL, NULL) == RET_FAILED);
49     const char *argu = "lease";
50     const char *val = "4000";
51     EXPECT_TRUE(PutArgument(NULL, val) == RET_FAILED);
52     EXPECT_TRUE(PutArgument(argu, NULL) == RET_FAILED);
53     EXPECT_TRUE(PutArgument(argu, val) == RET_SUCCESS);
54     argu = "xxxx";
55     val = "nothing";
56     EXPECT_TRUE(PutArgument(argu, val) == RET_SUCCESS);
57     EXPECT_TRUE(PutArgument(argu, val) == RET_FAILED);
58     argu = "longlongvalue";
59     val = "verylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvaluevery"
60     "longvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvalueverylongvaluev"
61     "erylongvalueverylongvalueverylongvalueverylongvalue";
62     EXPECT_TRUE(PutArgument(argu, val) == RET_ERROR);
63 }
64 
65 HWTEST(DhcpArgumentTest, GetArgumentTest, TestSize.Level0)
66 {
67     DHCP_LOGE("enter GetArgumentTest");
68     ArgumentInfo *arg = GetArgument("lease");
69     EXPECT_TRUE(arg);
70     EXPECT_EQ(0, strncmp(arg->name, "lease", strlen("lease")));
71     EXPECT_EQ(0, strncmp(arg->value, "4000", strlen("4000")));
72 
73     arg = GetArgument("xxxx");
74     EXPECT_TRUE(arg);
75     EXPECT_EQ(0, strncmp(arg->name, "xxxx", strlen("xxxx")));
76     EXPECT_EQ(0, strncmp(arg->value, "nothing", strlen("nothing")));
77 }
78 
79 HWTEST(DhcpArgumentTest, HasArgumentTest, TestSize.Level0)
80 {
81     const char *name = "xxx";
82     EXPECT_TRUE(HasArgument(name) == 0);
83     name = "lease";
84     EXPECT_TRUE(HasArgument(name) == 1);
85     name = nullptr;
86     EXPECT_TRUE(HasArgument(name) == 0);
87     FreeArguments();
88 }
89 }
90 }