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