1 /*
2 * Copyright (c) 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 #ifdef GTEST_API_
19 #define private public
20 #define protected public
21 #endif
22
23 #include "net_eap_handler.h"
24 #include "net_eap_callback_stub.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace {
29 constexpr bool TEST_BOOL_VALUE = false;
30 constexpr int32_t TEST_INT32_VALUE = 1;
31 constexpr uint32_t TEST_UINT32_VALUE = 1;
32 constexpr const char *TEST_STRING_VALUE = "test";
33 constexpr const char *TEST_DOMAIN = "test.com";
34 } // namespace
35
36 class NetRegisterEapCallbackTest : public NetRegisterEapCallbackStub {
37 public:
OnRegisterCustomEapCallback(const std::string & regCmd)38 int32_t OnRegisterCustomEapCallback(const std::string ®Cmd) override
39 {
40 return 0;
41 }
42
OnReplyCustomEapDataEvent(int result,const sptr<EapData> & eapData)43 int32_t OnReplyCustomEapDataEvent(int result, const sptr<EapData> &eapData) override
44 {
45 return 0;
46 }
47 };
48
49 class EapPostbackCallbackTest : public NetEapPostbackCallbackStub {
50 public:
OnEapSupplicantPostback(NetType netType,const sptr<EapData> & eapData)51 int32_t OnEapSupplicantPostback(NetType netType, const sptr<EapData> &eapData) override
52 {
53 return 0;
54 }
55 };
56
57 static sptr<INetRegisterEapCallback> g_registerEapCallback = new (std::nothrow) NetRegisterEapCallbackTest();
58 static sptr<INetEapPostbackCallback> g_eapPostbackCallback = new (std::nothrow) EapPostbackCallbackTest();
59
60 using namespace testing::ext;
61 class NetEapHandlerTest : public testing::Test {
62 public:
63 static void SetUpTestCase();
64 static void TearDownTestCase();
65 void SetUp();
66 void TearDown();
67 };
68
SetUpTestCase()69 void NetEapHandlerTest::SetUpTestCase() {}
70
TearDownTestCase()71 void NetEapHandlerTest::TearDownTestCase() {}
72
SetUp()73 void NetEapHandlerTest::SetUp() {}
74
TearDown()75 void NetEapHandlerTest::TearDown() {}
76
77 HWTEST_F(NetEapHandlerTest, RegisterCustomEapCallbackTest001, TestSize.Level1)
78 {
79 NetType netType = NetType::INVALID;
80 int ret1 = NetEapHandler::GetInstance().RegisterCustomEapCallback(netType, g_registerEapCallback);
81 EXPECT_TRUE(ret1 == NETMANAGER_ERR_PARAMETER_ERROR || ret1 == NETMANAGER_SUCCESS);
82 }
83
84 HWTEST_F(NetEapHandlerTest, RegisterCustomEapCallbackTest002, TestSize.Level1)
85 {
86 NetType netType = NetType::WLAN0;
87 sptr<INetRegisterEapCallback> callback = nullptr;
88 int ret1 = NetEapHandler::GetInstance().RegisterCustomEapCallback(netType, callback);
89 EXPECT_TRUE(ret1 == NETMANAGER_ERR_LOCAL_PTR_NULL || ret1 == NETMANAGER_SUCCESS);
90 }
91
92 HWTEST_F(NetEapHandlerTest, RegisterCustomEapCallbackTest003, TestSize.Level1)
93 {
94 NetType netType = NetType::WLAN0;
95 int ret1 = NetEapHandler::GetInstance().RegisterCustomEapCallback(netType, g_registerEapCallback);
96 EXPECT_EQ(ret1, NETMANAGER_SUCCESS);
97 }
98
99 HWTEST_F(NetEapHandlerTest, UnRegisterCustomEapCallbackTest001, TestSize.Level1)
100 {
101 NetType netType = NetType::INVALID;
102 int ret1 = NetEapHandler::GetInstance().UnRegisterCustomEapCallback(netType, g_registerEapCallback);
103 EXPECT_TRUE(ret1 == NETMANAGER_ERR_PARAMETER_ERROR || ret1 == NETMANAGER_SUCCESS);
104 }
105
106 HWTEST_F(NetEapHandlerTest, UnRegisterCustomEapCallbackTest002, TestSize.Level1)
107 {
108 NetType netType = NetType::WLAN0;
109 sptr<INetRegisterEapCallback> callback = nullptr;
110 int ret1 = NetEapHandler::GetInstance().UnRegisterCustomEapCallback(netType, callback);
111 EXPECT_TRUE(ret1 == NETMANAGER_ERR_LOCAL_PTR_NULL || ret1 == NETMANAGER_SUCCESS);
112 }
113
114 HWTEST_F(NetEapHandlerTest, UnRegisterCustomEapCallbackTest003, TestSize.Level1)
115 {
116 NetType netType = NetType::WLAN0;
117 int ret1 = NetEapHandler::GetInstance().UnRegisterCustomEapCallback(netType, g_registerEapCallback);
118 EXPECT_EQ(ret1, NETMANAGER_SUCCESS);
119 }
120
121 HWTEST_F(NetEapHandlerTest, RegCustomEapHandlerTest001, TestSize.Level1)
122 {
123 NetType netType = NetType::INVALID;
124 std::string regCmd = "2:277:278";
125 int ret1 = NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
126 EXPECT_TRUE(ret1 == NETMANAGER_ERR_PARAMETER_ERROR || ret1 == NETMANAGER_SUCCESS);
127 }
128
129 HWTEST_F(NetEapHandlerTest, RegCustomEapHandlerTest002, TestSize.Level1)
130 {
131 NetType netType = NetType::WLAN0;
132 std::string regCmd = "2:277:278";
133 int ret1 = NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
134 EXPECT_TRUE(ret1 == NETMANAGER_ERR_INVALID_PARAMETER || ret1 == NETMANAGER_SUCCESS);
135 }
136
137 HWTEST_F(NetEapHandlerTest, RegCustomEapHandlerTest003, TestSize.Level1)
138 {
139 NetType netType = NetType::WLAN0;
140 std::string regCmd = "2:277:278";
141 int ret1 = NetEapHandler::GetInstance().RegisterCustomEapCallback(netType, nullptr);
142 if (ret1 != NETMANAGER_SUCCESS) {
143 return;
144 }
145 int ret2 = NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
146 EXPECT_TRUE(ret2 == NETMANAGER_ERR_INVALID_PARAMETER || ret2 == NETMANAGER_SUCCESS);
147 }
148
149 HWTEST_F(NetEapHandlerTest, RegCustomEapHandlerTest004, TestSize.Level1)
150 {
151 NetType netType = NetType::WLAN0;
152 std::string regCmd = "2:277:278";
153 int ret1 = NetEapHandler::GetInstance().RegisterCustomEapCallback(netType, g_registerEapCallback);
154 if (ret1 != NETMANAGER_SUCCESS) {
155 return;
156 }
157 int ret2 = NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
158 EXPECT_EQ(ret2, NETMANAGER_SUCCESS);
159 }
160
161 HWTEST_F(NetEapHandlerTest, RegCustomEapHandlerTest005, TestSize.Level1)
162 {
163 NetType netType = NetType::WLAN0;
164 std::string regCmd = "2:277:278";
165 int ret1 = NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, nullptr);
166 EXPECT_TRUE(ret1 == NETMANAGER_ERR_LOCAL_PTR_NULL || ret1 == NETMANAGER_SUCCESS);
167 }
168
169 HWTEST_F(NetEapHandlerTest, NotifyWpaEapInterceptInfoTest001, TestSize.Level1)
170 {
171 NetType netType = NetType::WLAN0;
172 std::string regCmd = "2:277:278";
173 NetEapHandler::GetInstance().RegisterCustomEapCallback(netType, g_registerEapCallback);
174 NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
175 sptr<EapData> eapData =new (std::nothrow) EapData();
176 eapData->eapCode = 1;
177 eapData->eapType = 13;
178 eapData->msgId = 55;
179 eapData->bufferLen = 4;
180 std::vector<uint8_t> tmp = {0x11, 0x12};
181 eapData->eapBuffer = tmp;
182 int ret1 = NetEapHandler::GetInstance().NotifyWpaEapInterceptInfo(netType, eapData);
183 EXPECT_EQ(ret1, NETMANAGER_SUCCESS);
184 }
185
186 HWTEST_F(NetEapHandlerTest, ReplyCustomEapDataTest001, TestSize.Level1)
187 {
188 NetType netType = NetType::WLAN0;
189 std::string regCmd = "2:277:278";
190 NetEapHandler::GetInstance().RegisterCustomEapCallback(netType, g_registerEapCallback);
191 NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
192 sptr<EapData> eapData =new (std::nothrow) EapData();
193 eapData->eapCode = 1;
194 eapData->eapType = 13;
195 eapData->msgId = 55;
196 eapData->bufferLen = 4;
197 std::vector<uint8_t> tmp = {0x11, 0x12};
198 eapData->eapBuffer = tmp;
199 NetEapHandler::GetInstance().NotifyWpaEapInterceptInfo(netType, eapData);
200 eapData->msgId = 54;
201 int ret1 = NetEapHandler::GetInstance().ReplyCustomEapData(1, eapData);
202 EXPECT_TRUE(ret1 == NETMANAGER_ERR_OPERATION_FAILED || ret1 == NETMANAGER_SUCCESS);
203 eapData->msgId = 55;
204 int ret2 = NetEapHandler::GetInstance().ReplyCustomEapData(1, eapData);
205 EXPECT_EQ(ret2, NETMANAGER_SUCCESS);
206 }
207
208 HWTEST_F(NetEapHandlerTest, RegCustomEapHandlerTest006, TestSize.Level1)
209 {
210 #ifdef NET_EXTENSIBLE_AUTHENTICATION
211 NetType netType = NetType::ETH0;
212 std::string regCmd = "2:277:278:279";
213 int ret = NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
214 EXPECT_NE(ret, NETMANAGER_SUCCESS);
215 NetEapHandler::GetInstance().eapHdiWpaManager_ = nullptr;
216 ret = NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
217 EXPECT_EQ(ret, NETMANAGER_EXT_ERR_PARAMETER_ERROR);
218 NetEapHandler::GetInstance().eapHdiWpaManager_ = std::make_shared<EapHdiWpaManager>();
219 ret = NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
220 EXPECT_NE(ret, NETMANAGER_SUCCESS);
221 #endif
222 }
223
224
225 HWTEST_F(NetEapHandlerTest, ReplyCustomEapDataTest002, TestSize.Level1)
226 {
227 #ifdef NET_EXTENSIBLE_AUTHENTICATION
228 NetType netType = NetType::ETH0;
229 std::string regCmd = "2:277:278";
230 NetEapHandler::GetInstance().RegisterCustomEapCallback(netType, g_registerEapCallback);
231 NetEapHandler::GetInstance().RegCustomEapHandler(netType, regCmd, g_eapPostbackCallback);
232 int32_t result = 1;
233 sptr<EapData> eapData =new (std::nothrow) EapData();
234 eapData->eapCode = 2;
235 eapData->eapType = 13;
236 eapData->msgId = 55;
237 eapData->bufferLen = 4;
238 std::vector<uint8_t> tmp = {0x11, 0x12};
239 eapData->eapBuffer = tmp;
240 int ret = NetEapHandler::GetInstance().ReplyCustomEapData(result, eapData);
241 EXPECT_NE(ret, NETMANAGER_SUCCESS);
242 NetEapHandler::GetInstance().eapHdiWpaManager_ = nullptr;
243 ret = NetEapHandler::GetInstance().ReplyCustomEapData(result, eapData);
244 EXPECT_NE(ret, NETMANAGER_SUCCESS);
245 NetEapHandler::GetInstance().eapHdiWpaManager_ = std::make_shared<EapHdiWpaManager>();
246 ret = NetEapHandler::GetInstance().ReplyCustomEapData(result, eapData);
247 EXPECT_NE(ret, NETMANAGER_SUCCESS);
248 #endif
249 }
250
251 HWTEST_F(NetEapHandlerTest, StartEthEapTest001, TestSize.Level1)
252 {
253 #ifdef NET_EXTENSIBLE_AUTHENTICATION
254 int32_t netId = 100;
255 EthEapProfile profile;
256 int ret = NetEapHandler::GetInstance().StartEthEap(netId, profile);
257 EXPECT_NE(ret, NETMANAGER_SUCCESS);
258 NetEapHandler::GetInstance().eapHdiWpaManager_ = nullptr;
259 ret = NetEapHandler::GetInstance().StartEthEap(netId, profile);
260 EXPECT_NE(ret, NETMANAGER_SUCCESS);
261 NetEapHandler::GetInstance().eapHdiWpaManager_ = std::make_shared<EapHdiWpaManager>();
262 #endif
263 }
264
265 HWTEST_F(NetEapHandlerTest, LogOffEthEapTest001, TestSize.Level1)
266 {
267 #ifdef NET_EXTENSIBLE_AUTHENTICATION
268 int32_t netId = 100;
269 int ret = NetEapHandler::GetInstance().LogOffEthEap(netId);
270 EXPECT_NE(ret, NETMANAGER_SUCCESS);
271 NetEapHandler::GetInstance().eapHdiWpaManager_ = nullptr;
272 ret = NetEapHandler::GetInstance().LogOffEthEap(netId);
273 EXPECT_NE(ret, NETMANAGER_SUCCESS);
274 NetEapHandler::GetInstance().eapHdiWpaManager_ = std::make_shared<EapHdiWpaManager>();
275 #endif
276 }
277
278 HWTEST_F(NetEapHandlerTest, StartEthEapTest002, TestSize.Level1)
279 {
280 #ifdef NET_EXTENSIBLE_AUTHENTICATION
281 std::vector<int32_t> netIds = {100, 101, 102, 103, 104, 105};
282 EthEapProfile profile;
283 for (int32_t netId : netIds) {
284 int ret = NetEapHandler::GetInstance().StartEthEap(netId, profile);
285 EXPECT_NE(ret, NETMANAGER_SUCCESS);
286 }
287 #endif
288 }
289
290 HWTEST_F(NetEapHandlerTest, LogOffEthEapTest002, TestSize.Level1)
291 {
292 #ifdef NET_EXTENSIBLE_AUTHENTICATION
293 std::vector<int32_t> netIds = {100, 101, 102, 103, 104, 105};
294 for (int32_t netId : netIds) {
295 int ret = NetEapHandler::GetInstance().LogOffEthEap(netId);
296 EXPECT_NE(ret, NETMANAGER_SUCCESS);
297 }
298 #endif
299 }
300
301 } // namespace NetManagerStandard
302 } // namespace OHOS