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