• 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 #include <gtest/gtest.h>
16 
17 #include "wifi_log.h"
18 #include "dhcp_client.h"
19 #include "dhcp_function.h"
20 #include "securec.h"
21 
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 class DhcpClientFuncTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase()
28     {}
TearDownTestCase()29     static void TearDownTestCase()
30     {}
SetUp()31     virtual void SetUp()
32     {}
TearDown()33     virtual void TearDown()
34     {}
35 };
36 
37 HWTEST_F(DhcpClientFuncTest, StartProcess_SUCCESS, TestSize.Level1)
38 {
39     struct DhcpClientCfg *pCfg = GetDhcpClientCfg();
40     ASSERT_TRUE(strncpy_s(pCfg->workDir, DIR_MAX_LEN, "./", DIR_MAX_LEN - 1) == EOK);
41     ASSERT_TRUE(strncpy_s(pCfg->ifaceName, INFNAME_SIZE, "wlan0", INFNAME_SIZE - 1) == EOK);
42     ASSERT_TRUE(snprintf_s(pCfg->confFile, DIR_MAX_LEN, DIR_MAX_LEN - 1, "%s%s", pCfg->workDir, DHCPC_CONF) >= 0);
43     ASSERT_TRUE(
44         snprintf_s(pCfg->pidFile, DIR_MAX_LEN, DIR_MAX_LEN - 1, "%s%s.pid", pCfg->workDir, pCfg->ifaceName) >= 0);
45     ASSERT_TRUE(
46         snprintf_s(pCfg->resultFile, DIR_MAX_LEN, DIR_MAX_LEN - 1, "%s%s.result", pCfg->workDir, pCfg->ifaceName) >= 0);
47     ASSERT_TRUE(
48         snprintf_s(pCfg->result6File, DIR_MAX_LEN, DIR_MAX_LEN - 1, "%s%s.result6", pCfg->workDir, pCfg->ifaceName) >= 0);
49     ASSERT_TRUE(
50         snprintf_s(pCfg->leaseFile, DIR_MAX_LEN, DIR_MAX_LEN - 1, "%s%s.lease", pCfg->workDir, pCfg->ifaceName) >= 0);
51     pCfg->getMode = DHCP_IP_TYPE_ALL;
52 
53     EXPECT_EQ(DHCP_OPT_SUCCESS, GetLocalInterface(pCfg->ifaceName, &pCfg->ifaceIndex, pCfg->ifaceMac, NULL));
54 
55     /* Generate format: 1 + ifaceMac. */
56     if (pCfg->pOptClientId != NULL) {
57         free(pCfg->pOptClientId);
58         pCfg->pOptClientId = NULL;
59     }
60     pCfg->pOptClientId = (unsigned char *)malloc(DHCP_OPT_CODE_BYTES + DHCP_OPT_LEN_BYTES + MAC_ADDR_LEN + 1);
61     ASSERT_TRUE(pCfg->pOptClientId != NULL);
62     pCfg->pOptClientId[DHCP_OPT_CODE_INDEX] = CLIENT_IDENTIFIER_OPTION;
63     pCfg->pOptClientId[DHCP_OPT_LEN_INDEX] = MAC_ADDR_LEN + 1;
64     pCfg->pOptClientId[DHCP_OPT_DATA_INDEX] = NUMBER_ONE;
65     ASSERT_TRUE(
66         memcpy_s(pCfg->pOptClientId + DHCP_OPT_DATA_INDEX + 1, MAC_ADDR_LEN, pCfg->ifaceMac, MAC_ADDR_LEN) == EOK);
67     pCfg->timeoutExit = true;
68 
69     EXPECT_EQ(DHCP_OPT_SUCCESS, StartProcess());
70 }
71 
72 HWTEST_F(DhcpClientFuncTest, GetProStatus_SUCCESS, TestSize.Level1)
73 {
74     char workDir[DIR_MAX_LEN] = "./";
75     char pidFile[DIR_MAX_LEN] = "./wlan0.pid";
76     ASSERT_EQ(DHCP_OPT_SUCCESS, InitPidfile(workDir, pidFile, getpid()));
77 
78     EXPECT_EQ(1, GetProStatus(pidFile));
79     unlink(pidFile);
80 }
81 
82 HWTEST_F(DhcpClientFuncTest, GetProStatus_FAILED, TestSize.Level1)
83 {
84     char pidFile[DIR_MAX_LEN] = "./wlan0.pid";
85     EXPECT_EQ(0, GetProStatus(pidFile));
86 }
87 
88 HWTEST_F(DhcpClientFuncTest, StopProcess_SUCCESS, TestSize.Level1)
89 {
90     char pidFile[DIR_MAX_LEN] = "./wlan0.pid";
91     EXPECT_EQ(DHCP_OPT_SUCCESS, StopProcess(pidFile));
92 
93     char workDir[DIR_MAX_LEN] = "./";
94     pid_t testPid = 12345;
95     ASSERT_EQ(DHCP_OPT_SUCCESS, InitPidfile(workDir, pidFile, testPid));
96 
97     EXPECT_EQ(DHCP_OPT_SUCCESS, StopProcess(pidFile));
98     unlink(pidFile);
99 }
100 }  // namespace OHOS