1 /*
2 * Copyright (c) 2025-2025 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 "ethernet_client.h"
19 #include "net_manager_constants.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
23 using namespace testing::ext;
24 namespace {
25 using namespace testing::ext;
26 constexpr const char *DEV_NAME = "eth0";
27 constexpr const char *IFACE_NAME = "wlan0";
28 } // namespace
29
30 class EthernetClientTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 sptr<InterfaceConfiguration> GetIfaceConfig();
35 void SetUp();
36 void TearDown();
37 };
38
39 class NetEapPostBackCallbackTest : public NetEapPostbackCallbackStub {
40 public:
OnEapSupplicantPostback(NetType netType,const sptr<EapData> & eapData)41 int32_t OnEapSupplicantPostback(NetType netType, const sptr<EapData> &eapData) override
42 {
43 return 0;
44 }
45 };
46
47 class NetRegisterEapCallbackTest : public NetRegisterEapCallbackStub {
48 public:
OnRegisterCustomEapCallback(const std::string & regCmd)49 int32_t OnRegisterCustomEapCallback(const std::string ®Cmd) override
50 {
51 return 0;
52 }
OnReplyCustomEapDataEvent(int result,const sptr<EapData> & eapData)53 int32_t OnReplyCustomEapDataEvent(int result, const sptr<EapData> &eapData) override
54 {
55 return 0;
56 }
57 };
58
GetIfaceConfig()59 sptr<InterfaceConfiguration> EthernetClientTest::GetIfaceConfig()
60 {
61 sptr<InterfaceConfiguration> ic = (std::make_unique<InterfaceConfiguration>()).release();
62 if (!ic) {
63 return ic;
64 }
65 INetAddr ipv4Addr;
66 ipv4Addr.type_ = INetAddr::IPV4;
67 ipv4Addr.family_ = 0x01;
68 ipv4Addr.prefixlen_ = 0x01;
69 ipv4Addr.address_ = "172.17.5.234";
70 ipv4Addr.netMask_ = "255.255.254.0";
71 ipv4Addr.hostName_ = "netAddr";
72 ic->ipStatic_.ipAddrList_.push_back(ipv4Addr);
73 INetAddr route;
74 route.type_ = INetAddr::IPV4;
75 route.family_ = 0x01;
76 route.prefixlen_ = 0x01;
77 route.address_ = "0.0.0.0";
78 route.netMask_ = "0.0.0.0";
79 route.hostName_ = "netAddr";
80 ic->ipStatic_.routeList_.push_back(route);
81 INetAddr gateway;
82 gateway.type_ = INetAddr::IPV4;
83 gateway.family_ = 0x01;
84 gateway.prefixlen_ = 0x01;
85 gateway.address_ = "172.17.4.1";
86 gateway.netMask_ = "0.0.0.0";
87 gateway.hostName_ = "netAddr";
88 ic->ipStatic_.gatewayList_.push_back(gateway);
89 INetAddr netMask;
90 netMask.type_ = INetAddr::IPV4;
91 netMask.family_ = 0x01;
92 netMask.address_ = "255.255.255.0";
93 netMask.hostName_ = "netAddr";
94 ic->ipStatic_.netMaskList_.push_back(netMask);
95 INetAddr dns1;
96 dns1.type_ = INetAddr::IPV4;
97 dns1.family_ = 0x01;
98 dns1.address_ = "8.8.8.8";
99 dns1.hostName_ = "netAddr";
100 INetAddr dns2;
101 dns2.type_ = INetAddr::IPV4;
102 dns2.family_ = 0x01;
103 dns2.address_ = "114.114.114.114";
104 dns2.hostName_ = "netAddr";
105 ic->ipStatic_.dnsServers_.push_back(dns1);
106 ic->ipStatic_.dnsServers_.push_back(dns2);
107 return ic;
108 }
109
SetUpTestCase()110 void EthernetClientTest::SetUpTestCase() {}
111
TearDownTestCase()112 void EthernetClientTest::TearDownTestCase() {}
113
SetUp()114 void EthernetClientTest::SetUp() {}
115
TearDown()116 void EthernetClientTest::TearDown() {}
117
118 /**
119 * @tc.name: GetMacAddressTest001
120 * @tc.desc: Test GetMacAddress.
121 * @tc.type: FUNC
122 */
123 HWTEST_F(EthernetClientTest, GetMacAddressTest001, TestSize.Level1)
124 {
125 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
126 std::vector<MacAddressInfo> macAddrList;
127 int32_t ret = ethernetClient->GetMacAddress(macAddrList);
128 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PERMISSION_DENIED);
129 }
130
131 /**
132 * @tc.name: SetIfaceConfigTest001
133 * @tc.desc: Test SetIfaceConfig.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(EthernetClientTest, SetIfaceConfigTest001, TestSize.Level1)
137 {
138 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
139 sptr<InterfaceConfiguration> ic = GetIfaceConfig();
140 int32_t ret = ethernetClient->SetIfaceConfig(DEV_NAME, ic);
141 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PERMISSION_DENIED);
142 }
143
144 /**
145 * @tc.name: GetIfaceConfigTest001
146 * @tc.desc: Test GetIfaceConfig.
147 * @tc.type: FUNC
148 */
149 HWTEST_F(EthernetClientTest, GetIfaceConfigTest001, TestSize.Level1)
150 {
151 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
152 std::string iface = DEV_NAME;
153 sptr<InterfaceConfiguration> ifaceConfig = GetIfaceConfig();
154 int32_t ret = ethernetClient->GetIfaceConfig(iface, ifaceConfig);
155 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PERMISSION_DENIED);
156 }
157
158 /**
159 * @tc.name: ResetFactoryTest001
160 * @tc.desc: Test ResetFactory.
161 * @tc.type: FUNC
162 */
163 HWTEST_F(EthernetClientTest, ResetFactoryTest001, TestSize.Level1)
164 {
165 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
166 int32_t ret = ethernetClient->ResetFactory();
167 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
168 }
169
170 /**
171 * @tc.name: SetInterfaceUpTest001
172 * @tc.desc: Test SetInterfaceUp.
173 * @tc.type: FUNC
174 */
175 HWTEST_F(EthernetClientTest, SetInterfaceUpTest001, TestSize.Level1)
176 {
177 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
178 int32_t ret = ethernetClient->SetInterfaceUp(DEV_NAME);
179 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
180 }
181
182 /**
183 * @tc.name: SetInterfaceDownTest001
184 * @tc.desc: Test SetInterfaceDown.
185 * @tc.type: FUNC
186 */
187 HWTEST_F(EthernetClientTest, SetInterfaceDownTest001, TestSize.Level1)
188 {
189 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
190 int32_t ret = ethernetClient->SetInterfaceDown(DEV_NAME);
191 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
192 }
193
194 /**
195 * @tc.name: SetInterfaceConfigTest001
196 * @tc.desc: Test SetInterfaceConfig.
197 * @tc.type: FUNC
198 */
199 HWTEST_F(EthernetClientTest, SetInterfaceConfigTest001, TestSize.Level1)
200 {
201 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
202 std::string iface = DEV_NAME;
203 OHOS::nmd::InterfaceConfigurationParcel cfg;
204 cfg.ifName = "eth0";
205 cfg.hwAddr = "";
206 cfg.ipv4Addr = "172.17.5.234";
207 cfg.prefixLength = 24;
208 cfg.flags.push_back("up");
209 cfg.flags.push_back("broadcast");
210 int32_t ret = ethernetClient->SetInterfaceConfig(iface, cfg);
211 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
212 }
213
214 /**
215 * @tc.name: GetDeviceInformationTest001
216 * @tc.desc: Test GetDeviceInformation.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(EthernetClientTest, GetDeviceInformationTest001, TestSize.Level1)
220 {
221 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
222 std::vector<EthernetDeviceInfo> devInfoList;
223 int32_t ret = ethernetClient->GetDeviceInformation(devInfoList);
224 EXPECT_NE(ret, NETMANAGER_EXT_SUCCESS);
225 }
226
227 /**
228 * @tc.name: RegCustomEapHandlerTest001
229 * @tc.desc: Test RegCustomEapHandler.
230 * @tc.type: FUNC
231 */
232 HWTEST_F(EthernetClientTest, RegCustomEapHandlerTest001, TestSize.Level1)
233 {
234 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
235 NetType netType = NetType::ETH0;
236 std::string regCmd = "";
237 sptr<INetEapPostbackCallback> callback = (std::make_unique<NetEapPostBackCallbackTest>()).release();
238 int32_t ret = ethernetClient->RegCustomEapHandler(netType, regCmd, callback);
239 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
240 }
241
242 /**
243 * @tc.name: ReplyCustomEapDataTest001
244 * @tc.desc: Test ReplyCustomEapData.
245 * @tc.type: FUNC
246 */
247 HWTEST_F(EthernetClientTest, ReplyCustomEapDataTest001, TestSize.Level1)
248 {
249 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
250 int result = -1;
251 sptr<EapData> eapData = (std::make_unique<EapData>()).release();
252 int32_t ret = ethernetClient->ReplyCustomEapData(result, eapData);
253 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
254 }
255
256 /**
257 * @tc.name: RegisterCustomEapCallbackTest001
258 * @tc.desc: Test RegisterCustomEapCallback.
259 * @tc.type: FUNC
260 */
261 HWTEST_F(EthernetClientTest, RegisterCustomEapCallbackTest001, TestSize.Level1)
262 {
263 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
264 NetType netType = NetType::ETH0;
265 sptr<INetRegisterEapCallback> callback = (std::make_unique<NetRegisterEapCallbackTest>()).release();
266 int32_t ret = ethernetClient->RegisterCustomEapCallback(netType, callback);
267 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
268 }
269
270 /**
271 * @tc.name: UnRegisterCustomEapCallbackTest001
272 * @tc.desc: Test UnRegisterCustomEapCallback.
273 * @tc.type: FUNC
274 */
275 HWTEST_F(EthernetClientTest, UnRegisterCustomEapCallbackTest001, TestSize.Level1)
276 {
277 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
278 NetType netType = NetType::ETH0;
279 sptr<INetRegisterEapCallback> callback = (std::make_unique<NetRegisterEapCallbackTest>()).release();
280 int32_t ret = ethernetClient->UnRegisterCustomEapCallback(netType, callback);
281 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
282 }
283
284 /**
285 * @tc.name: NotifyWpaEapInterceptInfoTest001
286 * @tc.desc: Test NotifyWpaEapInterceptInfo.
287 * @tc.type: FUNC
288 */
289 HWTEST_F(EthernetClientTest, NotifyWpaEapInterceptInfoTest001, TestSize.Level1)
290 {
291 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
292 NetType netType = NetType::ETH0;
293 sptr<EapData> eapData = (std::make_unique<EapData>()).release();
294 int32_t ret = ethernetClient->NotifyWpaEapInterceptInfo(netType, eapData);
295 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
296 }
297
298 /**
299 * @tc.name: StartEthEapTest001
300 * @tc.desc: Test StartEthEap.
301 * @tc.type: FUNC
302 */
303 HWTEST_F(EthernetClientTest, StartEthEapTest001, TestSize.Level1)
304 {
305 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
306 int32_t netId = 100;
307 EthEapProfile profile;
308 int32_t ret = ethernetClient->StartEthEap(netId, profile);
309 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
310 }
311
312 /**
313 * @tc.name: LogOffEthEapTest001
314 * @tc.desc: Test LogOffEthEap.
315 * @tc.type: FUNC
316 */
317 HWTEST_F(EthernetClientTest, LogOffEthEapTest001, TestSize.Level1)
318 {
319 auto ethernetClient = DelayedSingleton<EthernetClient>::GetInstance();
320 int32_t netId = 100;
321 int32_t ret = ethernetClient->LogOffEthEap(netId);
322 EXPECT_EQ(ret, NETMANAGER_EXT_SUCCESS);
323 }
324
325 } // namespace NetManagerStandard
326 } // namespace OHOS