• 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 #include <string>
17 #include <securec.h>
18 #include "dhcp_config.h"
19 #include "dhcp_logger.h"
20 
21 DEFINE_DHCPLOG_DHCP_LABEL("DhcpConfigTest");
22 
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace Wifi {
26 class DhcpConfigTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase()
29     {}
TearDownTestCase()30     static void TearDownTestCase()
31     {}
SetUp()32     virtual void SetUp()
33     {}
TearDown()34     virtual void TearDown()
35     {}
36 };
37 
38 class MockConfigFile {
39 public:
MockConfigFile()40     MockConfigFile() : mFilePath("./unittest_dhcp_config.conf")
41     {}
42 
~MockConfigFile()43     ~MockConfigFile()
44     {
45         unlink(mFilePath.c_str());
46     }
47 
ClearAndWriteFile(const std::string & line)48     void ClearAndWriteFile(const std::string &line)
49     {
50         FILE *fp = fopen(mFilePath.c_str(), "w");
51         if (fp == nullptr) {
52             return;
53         }
54         (void)fprintf(fp, "%s\n", line.c_str());
55         (void)fclose(fp);
56         return;
57     }
58 
AppendFile(const std::string & line)59     void AppendFile(const std::string &line)
60     {
61         FILE *fp = fopen(mFilePath.c_str(), "w+");
62         if (fp == nullptr) {
63             return;
64         }
65         (void)fprintf(fp, "%s\n", line.c_str());
66         (void)fclose(fp);
67         return;
68     }
69 
GetConfigFile() const70     std::string GetConfigFile() const
71     {
72         return mFilePath;
73     }
74 
75 private:
76     std::string mFilePath;
77 };
78 
79 HWTEST(DhcpConfigTest, LoadConfigTest, TestSize.Level1)
80 {
81     DHCP_LOGE("enter LoadConfigTest");
82     EXPECT_TRUE(LoadConfig(nullptr, nullptr, nullptr) == RET_FAILED);
83     std::string configFile;
84     EXPECT_TRUE(LoadConfig(configFile.c_str(), nullptr, nullptr) == RET_FAILED);
85     std::string ifName;
86     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), nullptr) == RET_FAILED);
87     DhcpConfig config;
88     ASSERT_TRUE(memset_s(&config, sizeof(config), 0, sizeof(config)) == EOK);
89     MockConfigFile mockConfigFile;
90     configFile = mockConfigFile.GetConfigFile();
91     ifName = "veryveryveryveryveryveryveryveryveryverylanginterface";
92     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
93     std::string content = "# comment \ninterface=" + ifName;
94     mockConfigFile.AppendFile(content);
95     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
96     ifName = "wlan0";
97     content = "interface=wlan0\ndns=255.255.255.256";
98     mockConfigFile.ClearAndWriteFile(content);
99     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
100     content = "interface=wlan0\ndns=8.8.8.8,114.114.114.114\npool=error_poll_msg";
101     mockConfigFile.ClearAndWriteFile(content);
102     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
103     content = "interface=wlan0\ndns=8.8.8.8,114.114.114.114\npool=a,b";
104     mockConfigFile.ClearAndWriteFile(content);
105     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
106     content = "interface=wlan0\ndns=8.8.8.8,114.114.114.114\npool=192.168.1.1,b";
107     mockConfigFile.ClearAndWriteFile(content);
108     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
109     content = "interface=wlan0\ndns=8.8.8.8,114.114.114.114\npool=192.168.1.10,192.168.1.100";
110     mockConfigFile.ClearAndWriteFile(content);
111     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
112     content = "interface=wlan0\ndns=8.8.8.8,114.114.114.114\npool=192.168.1.1,192.168.1.100\n"
113               "server=192.168.1.256";
114     mockConfigFile.ClearAndWriteFile(content);
115     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
116     content = "interface=wlan0\ndns=8.8.8.8,114.114.114.114\npool=192.168.1.10,192.168.1.100\n"
117               "server=192.168.1.2\ngateway=192.168.1.1\nnetmask=255.255.255.0\nleaseTime=0";
118     mockConfigFile.ClearAndWriteFile(content);
119     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
120     content = "interface=wlan0\ndns=8.8.8.8,114.114.114.114\npool=192.168.1.10,192.168.1.100\n"
121               "server=192.168.1.2\ngateway=192.168.1.1\nnetmask=255.255.255.0\nleaseTime=60\n"
122               "renewalTime=10\nrebindingTime=10\ndistribution=3";
123     mockConfigFile.ClearAndWriteFile(content);
124     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_FAILED);
125     content = "interface=wlan0\ndns=8.8.8.8,114.114.114.114\npool=192.168.1.10,192.168.1.100\n"
126               "server=192.168.1.2\ngateway=192.168.1.1\nnetmask=255.255.255.0\nleaseTime=60\n"
127               "renewalTime=10\nrebindingTime=10\ndistribution=0\nbroadcast=1\ninvalid_key=haha";
128     mockConfigFile.ClearAndWriteFile(content);
129     EXPECT_TRUE(LoadConfig(configFile.c_str(), ifName.c_str(), &config) == RET_SUCCESS);
130 }
131 }
132 }