1 /*
2 * Copyright (c) 2023-2024 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
18 #include "gtest/gtest-message.h"
19 #include "gtest/gtest-test-part.h"
20 #include "gtest/hwext/gtest-ext.h"
21 #include "gtest/hwext/gtest-tag.h"
22
23 #define private public
24 #include "ethernet_configuration.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace {
29 using namespace testing::ext;
30 constexpr const char *IFACE = "iface0";
31 std::string IFACE_NAME = "iface0";
32 constexpr const char *FILE_PATH = "./BUILD.gn";
33 constexpr const char *DIR_PATH = "./BUILD.gn";
34 std::string REAL_PATH = "./BUILD.gn";
35 std::string FILE_CONTENT = "./BUILD.gn";
36 constexpr const char *LAN_STATIC_KEY = "LAN_STATIC";
37 constexpr const char *STATIC_KEY = "STATIC";
38 constexpr const char *DHCP_KEY = "DHCP";
39 constexpr const char *LAN_DHCP_KEY = "LAN_DHCP";
40 constexpr const char *USER_CONFIG_DIR_TEST = "/data/service/el1/public/netmanager/ethernet";
41 } // namespace
42
43 class EthernetConfigurationTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49 };
50
SetUpTestCase()51 void EthernetConfigurationTest::SetUpTestCase() {}
52
TearDownTestCase()53 void EthernetConfigurationTest::TearDownTestCase() {}
54
SetUp()55 void EthernetConfigurationTest::SetUp() {}
56
TearDown()57 void EthernetConfigurationTest::TearDown() {}
58
59 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration001, TestSize.Level1)
60 {
61 EthernetConfiguration ethernetConfiguration;
62 std::map<std::string, std::set<NetCap>> devCaps;
63 std::map<std::string, sptr<InterfaceConfiguration>> devCfgs;
64 bool ret = ethernetConfiguration.ReadSystemConfiguration(devCaps, devCfgs);
65 EXPECT_TRUE(ret);
66 ret = ethernetConfiguration.ReadUserConfiguration(devCfgs);
67 EXPECT_TRUE(ret);
68 sptr<InterfaceConfiguration> cfg = (std::make_unique<InterfaceConfiguration>()).release();
69 ret = ethernetConfiguration.WriteUserConfiguration(IFACE, cfg);
70 EXPECT_TRUE(ret);
71 ret = ethernetConfiguration.ClearAllUserConfiguration();
72 EXPECT_TRUE(ret);
73 EthernetDhcpCallback::DhcpResult dhcpResult;
74 sptr<StaticConfiguration> config = (std::make_unique<StaticConfiguration>()).release();
75 ret = ethernetConfiguration.ConvertToConfiguration(dhcpResult, config);
76 EXPECT_FALSE(ret);
77 std::string strRet = ethernetConfiguration.ReadJsonFile(FILE_PATH);
78 ret = ethernetConfiguration.IsDirExist(DIR_PATH);
79 ret = ethernetConfiguration.CreateDir(DIR_PATH);
80 ret = ethernetConfiguration.DelDir(DIR_PATH);
81 ret = ethernetConfiguration.IsFileExist(FILE_PATH, REAL_PATH);
82 EXPECT_FALSE(ret);
83 ret = ethernetConfiguration.ReadFile(FILE_PATH, FILE_CONTENT);
84 EXPECT_FALSE(ret);
85 ret = ethernetConfiguration.WriteFile(FILE_PATH, FILE_CONTENT);
86 EXPECT_TRUE(ret);
87 ethernetConfiguration.ParserFileConfig(FILE_CONTENT, IFACE_NAME, cfg);
88 ethernetConfiguration.GenCfgContent(IFACE, cfg, FILE_CONTENT);
89 sptr<InterfaceConfiguration> cfg2 = nullptr;
90 ret = ethernetConfiguration.WriteUserConfiguration(IFACE, cfg2);
91 EXPECT_FALSE(ret);
92 }
93
94 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration002, TestSize.Level1)
95 {
96 EthernetConfiguration ethernetConfiguration;
97 EthernetDhcpCallback::DhcpResult dhcpResult;
98 sptr<StaticConfiguration> config = nullptr;
99 bool ret = ethernetConfiguration.ConvertToConfiguration(dhcpResult, config);
100 EXPECT_FALSE(ret);
101
102 std::string filePath = "";
103 std::string fileContent = "";
104 sptr<InterfaceConfiguration> configSptr = nullptr;
105 ethernetConfiguration.ParserIfaceIpAndRoute(configSptr, filePath);
106
107 std::string iface = "";
108 ethernetConfiguration.GenCfgContent(iface, nullptr, fileContent);
109
110 ret = ethernetConfiguration.IsDirExist(filePath);
111 EXPECT_FALSE(ret);
112
113 configSptr = ethernetConfiguration.MakeInterfaceConfiguration(nullptr, nullptr);
114 EXPECT_TRUE(configSptr == nullptr);
115
116 ret = ethernetConfiguration.DelDir(filePath);
117 EXPECT_FALSE(ret);
118
119 ret = ethernetConfiguration.ReadFile(filePath, fileContent);
120 EXPECT_FALSE(ret);
121
122 ret = ethernetConfiguration.WriteFile(filePath, fileContent);
123 EXPECT_FALSE(ret);
124
125 ret = ethernetConfiguration.IsValidDhcpResult(dhcpResult, config);
126 EXPECT_FALSE(ret);
127 }
128
129 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration003, TestSize.Level1)
130 {
131 EthernetConfiguration ethernetConfiguration;
132 EthernetDhcpCallback::DhcpResult dhcpResult;
133 dhcpResult.ipAddr = "test";
134 sptr<StaticConfiguration> config = (std::make_unique<StaticConfiguration>()).release();
135 ethernetConfiguration.IsValidDhcpResult(dhcpResult, config);
136
137 std::string result = ethernetConfiguration.GetIfaceMode(IPSetMode::LAN_STATIC);
138 EXPECT_TRUE(result == LAN_STATIC_KEY);
139
140 result = ethernetConfiguration.GetIfaceMode(IPSetMode::LAN_DHCP);
141 EXPECT_TRUE(result == LAN_DHCP_KEY);
142
143 result = ethernetConfiguration.GetIfaceMode(IPSetMode::STATIC);
144 EXPECT_TRUE(result == STATIC_KEY);
145
146 result = ethernetConfiguration.GetIfaceMode(IPSetMode::DHCP);
147 EXPECT_TRUE(result == DHCP_KEY);
148 }
149
150 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration004, TestSize.Level1)
151 {
152 EthernetConfiguration ethernetConfiguration;
153 std::map<std::string, sptr<InterfaceConfiguration>> devCfgs;
154 std::string fileContent = "";
155 bool ret = ethernetConfiguration.CreateDir(USER_CONFIG_DIR_TEST);
156 ret = ethernetConfiguration.WriteFile(std::string(USER_CONFIG_DIR_TEST) + "/ethernet_user_interfaces.json",
157 fileContent);
158 ret = ethernetConfiguration.ReadUserConfiguration(devCfgs);
159 EXPECT_TRUE(ret);
160 ret = ethernetConfiguration.DelDir(USER_CONFIG_DIR_TEST);
161 EXPECT_TRUE(ret);
162 }
163
164 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration005, TestSize.Level1)
165 {
166 EthernetConfiguration ethernetConfiguration;
167 std::map<std::string, sptr<InterfaceConfiguration>> devCfgs;
168 std::string path = "/data/service/el1/public/netmanager/ethernet_test";
169 bool ret = ethernetConfiguration.CreateDir(path);
170 std::string fileContent = "";
171 ret = ethernetConfiguration.WriteFile(path + "ethernet_user_interfaces.json", fileContent);
172 ret = ethernetConfiguration.ReadUserConfiguration(devCfgs);
173 EXPECT_TRUE(ret);
174 ret = ethernetConfiguration.DelDir(path);
175 EXPECT_TRUE(ret);
176 }
177
178 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration006, TestSize.Level1)
179 {
180 EthernetConfiguration ethernetConfiguration;
181 std::map<std::string, sptr<InterfaceConfiguration>> devCfgs;
182 std::string fileContent = "DEVICE=eth0\nBOOTPROTO=LAN_STATIC\n";
183 bool ret = ethernetConfiguration.CreateDir(USER_CONFIG_DIR_TEST);
184 ret = ethernetConfiguration.WriteFile(std::string(USER_CONFIG_DIR_TEST) + "/ethernet_user_interfaces.json",
185 fileContent);
186 ret = ethernetConfiguration.ReadUserConfiguration(devCfgs);
187 fileContent = "DEVICE=eth0\nBOOTPROTO=LAN_DHCP\n";
188 ret = ethernetConfiguration.WriteFile(std::string(USER_CONFIG_DIR_TEST) + "/ethernet_user_interfaces.json",
189 fileContent);
190 ret = ethernetConfiguration.ReadUserConfiguration(devCfgs);
191 fileContent = "DEVICE=eth0\nBOOTPROTO=STATIC\n";
192 ret = ethernetConfiguration.WriteFile(std::string(USER_CONFIG_DIR_TEST) + "/ethernet_user_interfaces.json",
193 fileContent);
194 ret = ethernetConfiguration.ReadUserConfiguration(devCfgs);
195 fileContent = "DEVICE=eth0\nBOOTPROTO=DHCP\n";
196 ret = ethernetConfiguration.WriteFile(std::string(USER_CONFIG_DIR_TEST) + "/ethernet_user_interfaces.json",
197 fileContent);
198 ret = ethernetConfiguration.ReadUserConfiguration(devCfgs);
199 EXPECT_TRUE(ret);
200 ret = ethernetConfiguration.DelDir(USER_CONFIG_DIR_TEST);
201 EXPECT_TRUE(ret);
202 }
203
204 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration007, TestSize.Level1)
205 {
206 EthernetConfiguration ethernetConfiguration;
207 std::map<std::string, sptr<InterfaceConfiguration>> devCfgs;
208 std::string fileContent =
209 "DEVICE=eth0\n\
210 BOOTPROTO=STATIC\n\
211 IPADDR=123456\n\
212 NETMASK=123456\n\
213 GATEWAY=123456\n\
214 ROUTE=123456\n\
215 ROUTE_NETMASK=123456\n\
216 DNS=123456\n\
217 PROXY_HOST=123456\n\
218 PROXY_PORT=123456\n\
219 PROXY_EXCLUSIONS=\"127.0.0.1\", \"127.0.0.1\", \"127.0.0.1\"\n";
220 bool ret = ethernetConfiguration.CreateDir(USER_CONFIG_DIR_TEST);
221 ret = ethernetConfiguration.WriteFile(std::string(USER_CONFIG_DIR_TEST) + "/ethernet_user_interfaces.json",
222 fileContent);
223 ret = ethernetConfiguration.ReadUserConfiguration(devCfgs);
224 EXPECT_TRUE(ret);
225 ret = ethernetConfiguration.DelDir(USER_CONFIG_DIR_TEST);
226 EXPECT_TRUE(ret);
227 }
228
229 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration008, TestSize.Level1)
230 {
231 EthernetConfiguration ethernetConfiguration;
232 EthernetDhcpCallback::DhcpResult dhcpResult;
233 dhcpResult.ipAddr = "test1";
234 dhcpResult.gateWay = "test2";
235 INetAddr ipv4Addr;
236 ipv4Addr.address_ = "test1";
237 sptr<StaticConfiguration> config = (std::make_unique<StaticConfiguration>()).release();
238 config->ipAddrList_.push_back(ipv4Addr);
239 bool ret = ethernetConfiguration.IsValidDhcpResult(dhcpResult, config);
240 EXPECT_TRUE(ret);
241
242 ipv4Addr.address_ = "test2";
243 config->gatewayList_.push_back(ipv4Addr);
244 ret = ethernetConfiguration.IsValidDhcpResult(dhcpResult, config);
245 EXPECT_FALSE(ret);
246 }
247
248 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration009, TestSize.Level1)
249 {
250 EthernetConfiguration ethernetConfiguration;
251 EthernetDhcpCallback::DhcpResult dhcpResult;
252 dhcpResult.ipAddr = "test1";
253 dhcpResult.gateWay = "test2";
254 dhcpResult.route1 = "test3";
255 dhcpResult.route2 = "test4";
256 sptr<StaticConfiguration> config = (std::make_unique<StaticConfiguration>()).release();
257 bool ret = ethernetConfiguration.ConvertToConfiguration(dhcpResult, config);
258 EXPECT_TRUE(ret);
259 }
260
261 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration010, TestSize.Level1)
262 {
263 EthernetConfiguration ethernetConfiguration;
264 EthernetDhcpCallback::DhcpResult dhcpResult;
265 dhcpResult.ipAddr = "test1";
266 dhcpResult.gateWay = "test2";
267 dhcpResult.route1 = "*";
268 dhcpResult.route2 = "test4";
269 sptr<StaticConfiguration> config = (std::make_unique<StaticConfiguration>()).release();
270 bool ret = ethernetConfiguration.ConvertToConfiguration(dhcpResult, config);
271 EXPECT_TRUE(ret);
272 }
273
274 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration011, TestSize.Level1)
275 {
276 EthernetConfiguration ethernetConfiguration;
277 EthernetDhcpCallback::DhcpResult dhcpResult;
278 dhcpResult.ipAddr = "test1";
279 dhcpResult.gateWay = "test2";
280 dhcpResult.route1 = "*";
281 dhcpResult.route2 = "*";
282 sptr<StaticConfiguration> config = (std::make_unique<StaticConfiguration>()).release();
283 bool ret = ethernetConfiguration.ConvertToConfiguration(dhcpResult, config);
284 EXPECT_TRUE(ret);
285 }
286
287 HWTEST_F(EthernetConfigurationTest, EthernetConfiguration012, TestSize.Level1)
288 {
289 EthernetConfiguration ethernetConfiguration;
290 sptr<InterfaceConfiguration> configSptr = nullptr;
291 sptr<InterfaceConfiguration> devCfg = (std::make_unique<InterfaceConfiguration>()).release();
292 devCfg->mode_ = STATIC;
293 sptr<NetLinkInfo> devLinkInfo = (std::make_unique<NetLinkInfo>()).release();
294 configSptr = ethernetConfiguration.MakeInterfaceConfiguration(devCfg, devLinkInfo);
295 EXPECT_TRUE(configSptr->mode_ == STATIC);
296 }
297 } // namespace NetManagerStandard
298 } // namespace OHOS