1 /*
2 * Copyright (c) 2021-2023 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 #include <osal_mem.h>
18 #include "wifi_driver_client.h"
19 #include "securec.h"
20
21 using namespace testing::ext;
22
23 namespace ClientTest {
24 const uint32_t DEFAULT_COMBO_SIZE = 10;
25 const char *WLAN_IFNAME = "wlan0";
26 const uint32_t RESET_TIME = 3;
27 class WifiClientTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp();
32 void TearDown();
33 };
34
SetUpTestCase()35 void WifiClientTest::SetUpTestCase()
36 {
37 }
38
TearDownTestCase()39 void WifiClientTest::TearDownTestCase()
40 {
41 }
42
SetUp()43 void WifiClientTest::SetUp()
44 {
45 WifiDriverClientInit();
46 }
47
TearDown()48 void WifiClientTest::TearDown()
49 {
50 WifiDriverClientDeinit();
51 }
52
Hid2dFunCb(const uint8_t * recvMsg,uint32_t recvMsgLen)53 static int32_t Hid2dFunCb(const uint8_t *recvMsg, uint32_t recvMsgLen)
54 {
55 (void)recvMsg;
56 (void)recvMsgLen;
57 return RET_CODE_SUCCESS;
58 }
59
60 /**
61 * @tc.name: WifiClientSetResetDriver001
62 * @tc.desc: Wifi client reset driver function test
63 * @tc.type: FUNC
64 * @tc.require:
65 */
66 HWTEST_F(WifiClientTest, WifiClientSetResetDriver001, TestSize.Level1)
67 {
68 int32_t ret;
69 uint8_t chipId = 0;
70
71 ret = AcquireChipId(WLAN_IFNAME, &chipId);
72 ASSERT_TRUE(chipId < MAX_WLAN_DEVICE);
73 EXPECT_EQ(RET_CODE_SUCCESS, ret);
74
75 ret = SetResetDriver(chipId, WLAN_IFNAME);
76 EXPECT_EQ(RET_CODE_SUCCESS, ret);
77 sleep(RESET_TIME);
78 }
79
80 /**
81 * @tc.name: WifiClientSetCountryCode001
82 * @tc.desc: Wifi client set country code function test
83 * @tc.type: FUNC
84 * @tc.require: AR000FRMJC
85 */
86 HWTEST_F(WifiClientTest, WifiClientSetCountryCode001, TestSize.Level1)
87 {
88 int32_t ret;
89 const char *code = "CN";
90 const char *codeDigital = "99";
91 const char *ifNameInvalid = "wlanTest";
92 uint32_t len = 2;
93
94 ret = WifiSetCountryCode(ifNameInvalid, code, len);
95 EXPECT_EQ(RET_CODE_FAILURE, ret);
96 ret = WifiSetCountryCode(WLAN_IFNAME, code, len);
97 EXPECT_EQ(RET_CODE_SUCCESS, ret);
98 ret = WifiSetCountryCode(WLAN_IFNAME, codeDigital, len);
99 bool flag = (ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
100 ASSERT_TRUE(flag);
101 }
102
103 /**
104 * @tc.name: WifiClientGetUsableNetworkInfo001
105 * @tc.desc: Wifi client get usable networkInfo function test
106 * @tc.type: FUNC
107 * @tc.require: AR000FRMJC
108 */
109 HWTEST_F(WifiClientTest, WifiClientGetUsableNetworkInfo001, TestSize.Level1)
110 {
111 int32_t ret;
112 struct NetworkInfoResult networkInfo;
113
114 ret = GetUsableNetworkInfo(&networkInfo);
115 EXPECT_EQ(RET_CODE_SUCCESS, ret);
116 }
117
118 /**
119 * @tc.name: WifiClientIsSupportCombo001
120 * @tc.desc: Wifi client is support combo function test
121 * @tc.type: FUNC
122 * @tc.require: AR000FRMJC
123 */
124 HWTEST_F(WifiClientTest, WifiClientIsSupportCombo001, TestSize.Level1)
125 {
126 int32_t ret;
127 uint8_t isSupportCombo;
128
129 ret = IsSupportCombo(&isSupportCombo);
130 EXPECT_EQ(RET_CODE_SUCCESS, ret);
131 }
132
133 /**
134 * @tc.name: WifiClientGetComboInfo001
135 * @tc.desc: Wifi client get combo info function test
136 * @tc.type: FUNC
137 * @tc.require: AR000FRMJC
138 */
139 HWTEST_F(WifiClientTest, WifiClientGetComboInfo001, TestSize.Level1)
140 {
141 int32_t ret;
142 uint64_t comboInfo[DEFAULT_COMBO_SIZE] = {};
143
144 ret = GetComboInfo(comboInfo, DEFAULT_COMBO_SIZE);
145 bool flag = (ret == RET_CODE_SUCCESS || ret == RET_CODE_NOT_SUPPORT);
146 ASSERT_TRUE(flag);
147 }
148
149 /**
150 * @tc.name: WifiClientSetMacAddr001
151 * @tc.desc: Wifi client set mac addr function test
152 * @tc.type: FUNC
153 * @tc.require: AR000FRMJC
154 */
155 HWTEST_F(WifiClientTest, WifiClientSetMacAddr001, TestSize.Level1)
156 {
157 int32_t ret;
158 const char *ifNameInvalid = "wlanTest";
159 unsigned char mac[ETH_ADDR_LEN] = {0x12, 0x34, 0x56, 0x78, 0xab, 0xcd};
160 unsigned char errorMac[ETH_ADDR_LEN] = {0x11, 0x34, 0x56, 0x78, 0xab, 0xcd};
161
162 ret = SetMacAddr(WLAN_IFNAME, mac, ETH_ADDR_LEN);
163 bool flag = (ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT || ret == HDF_ERR_DEVICE_BUSY);
164 ASSERT_TRUE(flag);
165 ret = SetMacAddr(WLAN_IFNAME, errorMac, ETH_ADDR_LEN);
166 EXPECT_NE(RET_CODE_SUCCESS, ret);
167 ret = SetMacAddr(ifNameInvalid, mac, ETH_ADDR_LEN);
168 EXPECT_NE(RET_CODE_SUCCESS, ret);
169 }
170
171 /**
172 * @tc.name: WifiClientGetDevMacAddr001
173 * @tc.desc: Wifi client get mac addr function test
174 * @tc.type: FUNC
175 * @tc.require: AR000FRMJC
176 */
177 HWTEST_F(WifiClientTest, WifiClientGetDevMacAddr001, TestSize.Level1)
178 {
179 int32_t ret;
180 unsigned char mac[ETH_ADDR_LEN] = {};
181 int32_t type = WIFI_IFTYPE_STATION;
182 const char *ifNameInvalid = "wlanTest";
183
184 ret = GetDevMacAddr(WLAN_IFNAME, type, mac, ETH_ADDR_LEN);
185 EXPECT_NE(RET_CODE_FAILURE, ret);
186 ret = GetDevMacAddr(ifNameInvalid, type, mac, ETH_ADDR_LEN);
187 EXPECT_NE(RET_CODE_SUCCESS, ret);
188 }
189
190 /**
191 * @tc.name: WifiClientGetDevMacAddr002
192 * @tc.desc: Wifi client get mac addr function test
193 * @tc.type: FUNC
194 * @tc.require: AR000H603L
195 */
196 HWTEST_F(WifiClientTest, WifiClientGetDevMacAddr002, TestSize.Level1)
197 {
198 int32_t ret;
199 unsigned char mac[ETH_ADDR_LEN] = {};
200 int32_t type = WIFI_IFTYPE_AP;
201
202 ret = GetDevMacAddr(WLAN_IFNAME, type, mac, ETH_ADDR_LEN);
203 EXPECT_NE(RET_CODE_FAILURE, ret);
204 }
205
206 /**
207 * @tc.name: WifiClientGetValidFreqByBand001
208 * @tc.desc: Wifi client get valid freq function test
209 * @tc.type: FUNC
210 * @tc.require: AR000FRMJC
211 */
212 HWTEST_F(WifiClientTest, WifiClientGetValidFreqByBand001, TestSize.Level1)
213 {
214 int32_t ret;
215 int32_t band = IEEE80211_BAND_2GHZ;
216 int32_t bandNotSupport = IEEE80211_NUM_BANDS;
217 struct FreqInfoResult result;
218 uint32_t size = 14;
219 uint32_t i;
220 const char *ifNameInvalid = "wlanTest";
221
222 result.freqs = (uint32_t *)OsalMemCalloc(35 * sizeof(uint32_t));
223 if (result.freqs == NULL) {
224 printf("%s: OsalMemCalloc failed", __FUNCTION__);
225 return;
226 }
227
228 result.txPower = (uint32_t *)OsalMemCalloc(35 * sizeof(uint32_t));
229 if (result.txPower == NULL) {
230 printf("%s: OsalMemCalloc failed", __FUNCTION__);
231 OsalMemFree(result.freqs);
232 return;
233 }
234
235 ret = GetValidFreqByBand(WLAN_IFNAME, bandNotSupport, &result, size);
236 EXPECT_NE(RET_CODE_SUCCESS, ret);
237 ret = GetValidFreqByBand(WLAN_IFNAME, band, nullptr, size);
238 EXPECT_NE(RET_CODE_SUCCESS, ret);
239 ret = GetValidFreqByBand(ifNameInvalid, band, &result, size);
240 EXPECT_NE(RET_CODE_SUCCESS, ret);
241 ret = GetValidFreqByBand(WLAN_IFNAME, band, &result, size);
242 EXPECT_EQ(RET_CODE_SUCCESS, ret);
243 if (ret == RET_CODE_SUCCESS) {
244 printf("%s: num = %u\n", __func__, result.nums);
245 for (i = 0; i < result.nums; i++) {
246 printf("%s: freq[%d] = %d\n", __func__, i, result.freqs[i]);
247 }
248 }
249
250 OsalMemFree(result.txPower);
251 OsalMemFree(result.freqs);
252 }
253
254 /**
255 * @tc.name: WifiClientSetTxPower001
256 * @tc.desc: Wifi client set tx power function test
257 * @tc.type: FUNC
258 * @tc.require: AR000FRMJC
259 */
260 HWTEST_F(WifiClientTest, WifiClientSetTxPower001, TestSize.Level1)
261 {
262 int32_t ret;
263 int32_t power = 10;
264 const char *ifNameInvalid = "wlanTest";
265
266 ret = SetTxPower(ifNameInvalid, power);
267 EXPECT_NE(RET_CODE_SUCCESS, ret);
268 ret = SetTxPower(WLAN_IFNAME, power);
269 EXPECT_EQ(RET_CODE_SUCCESS, ret);
270 }
271
272 /**
273 * @tc.name: WifiClientGetAssociatedStas001
274 * @tc.desc: Wifi client get associated status function test
275 * @tc.type: FUNC
276 * @tc.require: AR000H603L
277 */
278 HWTEST_F(WifiClientTest, WifiClientGetAssociatedStas001, TestSize.Level1)
279 {
280 int32_t ret;
281 struct AssocStaInfoResult result;
282
283 ret = GetAssociatedStas(WLAN_IFNAME, &result);
284 EXPECT_EQ(RET_CODE_SUCCESS, ret);
285 }
286
287 /**
288 * @tc.name: WifiClientSetScanMacAddr001
289 * @tc.desc: Wifi client set scan MAC address function test
290 * @tc.type: FUNC
291 * @tc.require: AR000H603L
292 */
293 HWTEST_F(WifiClientTest, WifiClientSetScanMacAddr001, TestSize.Level1)
294 {
295 int32_t ret;
296 const char *ifNameInvalid = "wlanTest";
297 unsigned char scanMac[ETH_ADDR_LEN] = {0x12, 0x34, 0x56, 0x78, 0xab, 0xcd};
298
299 ret = SetScanMacAddr(ifNameInvalid, scanMac, ETH_ADDR_LEN);
300 EXPECT_EQ(RET_CODE_FAILURE, ret);
301 ret = SetScanMacAddr(WLAN_IFNAME, scanMac, ETH_ADDR_LEN);
302 bool flag = (ret == RET_CODE_SUCCESS || ret == RET_CODE_NOT_SUPPORT);
303 ASSERT_TRUE(flag);
304 }
305
306 /**
307 * @tc.name: WifiClientAcquireChipId001
308 * @tc.desc: Wifi client get chipId function test
309 * @tc.type: FUNC
310 * @tc.require: AR000H603L
311 */
312 HWTEST_F(WifiClientTest, WifiClientAcquireChipId001, TestSize.Level1)
313 {
314 int32_t ret;
315 const char *ifNameInvalid = "wlanTest";
316 uint8_t chipId = 0;
317
318 ret = AcquireChipId(nullptr, &chipId);
319 EXPECT_EQ(RET_CODE_INVALID_PARAM, ret);
320 ret = AcquireChipId(WLAN_IFNAME, nullptr);
321 EXPECT_EQ(RET_CODE_INVALID_PARAM, ret);
322 ret = AcquireChipId(ifNameInvalid, &chipId);
323 EXPECT_NE(RET_CODE_SUCCESS, ret);
324 ret = AcquireChipId(WLAN_IFNAME, &chipId);
325 ASSERT_TRUE(chipId < MAX_WLAN_DEVICE);
326 EXPECT_EQ(RET_CODE_SUCCESS, ret);
327 }
328
329 /**
330 * @tc.name: WifiClientGetIfNamesByChipId001
331 * @tc.desc: Wifi client get ifName by chipId function test
332 * @tc.type: FUNC
333 * @tc.require: AR000H603L
334 */
335 HWTEST_F(WifiClientTest, WifiClientGetIfNamesByChipId001, TestSize.Level1)
336 {
337 int32_t ret;
338 uint8_t chipId = 0;
339 uint8_t chipIdInvalid = 100;
340 char *ifNames = nullptr;
341 uint32_t num = 0;
342
343 ret = AcquireChipId(WLAN_IFNAME, &chipId);
344 ASSERT_TRUE(chipId < MAX_WLAN_DEVICE);
345 EXPECT_EQ(RET_CODE_SUCCESS, ret);
346 ret = GetIfNamesByChipId(chipIdInvalid, &ifNames, &num);
347 EXPECT_NE(RET_CODE_SUCCESS, ret);
348 ret = GetIfNamesByChipId(chipId, &ifNames, &num);
349 EXPECT_NE(ifNames, nullptr);
350 EXPECT_EQ(RET_CODE_SUCCESS, ret);
351 ret = GetIfNamesByChipId(chipId, &ifNames, nullptr);
352 EXPECT_EQ(RET_CODE_INVALID_PARAM, ret);
353 ret = GetIfNamesByChipId(chipId, nullptr, &num);
354 EXPECT_EQ(RET_CODE_INVALID_PARAM, ret);
355 free(ifNames);
356 }
357
358 /**
359 * @tc.name: WifiClientGetNetDeviceInfo001
360 * @tc.desc: Wifi client get netDevice information function test
361 * @tc.type: FUNC
362 * @tc.require: AR000H603L
363 */
364 HWTEST_F(WifiClientTest, WifiClientGetNetDeviceInfo001, TestSize.Level1)
365 {
366 int32_t ret;
367 struct NetDeviceInfoResult netDeviceInfoResult;
368
369 ret = GetNetDeviceInfo(&netDeviceInfoResult);
370 EXPECT_EQ(RET_CODE_SUCCESS, ret);
371 }
372
373 /**
374 * @tc.name: WifiClientGetCurrentPowerMode001
375 * @tc.desc: Wifi client get current power mode function test
376 * @tc.type: FUNC
377 * @tc.require: AR000H603L
378 */
379 HWTEST_F(WifiClientTest, WifiClientGetCurrentPowerMode001, TestSize.Level1)
380 {
381 int32_t ret;
382 uint8_t mode = 0;
383 const char *ifNameInvalid = "wlanTest";
384
385 ret = GetCurrentPowerMode(ifNameInvalid, &mode);
386 EXPECT_NE(RET_CODE_SUCCESS, ret);
387 ret = GetCurrentPowerMode(WLAN_IFNAME, &mode);
388 bool flag = (ret == RET_CODE_SUCCESS || ret == RET_CODE_NOT_SUPPORT);
389 ASSERT_TRUE(flag);
390 }
391
392 /**
393 * @tc.name: WifiClientSetPowerMode001
394 * @tc.desc: Wifi client set power mode function test
395 * @tc.type: FUNC
396 * @tc.require: AR000H603L
397 */
398 HWTEST_F(WifiClientTest, WifiClientSetPowerMode001, TestSize.Level1)
399 {
400 int32_t ret;
401 const char *ifNameInvalid = "wlanTest";
402
403 ret = SetPowerMode(ifNameInvalid, WIFI_POWER_MODE_SLEEPING);
404 EXPECT_NE(RET_CODE_SUCCESS, ret);
405 ret = SetPowerMode(WLAN_IFNAME, WIFI_POWER_MODE_SLEEPING);
406 bool flag = (ret == RET_CODE_SUCCESS || ret == RET_CODE_NOT_SUPPORT);
407 ASSERT_TRUE(flag);
408 }
409
410 /**
411 * @tc.name: WifiClientSetPowerMode002
412 * @tc.desc: Wifi client set power mode function test
413 * @tc.type: FUNC
414 * @tc.require: AR000H603L
415 */
416 HWTEST_F(WifiClientTest, WifiClientSetPowerMode002, TestSize.Level1)
417 {
418 int32_t ret;
419
420 ret = SetPowerMode(WLAN_IFNAME, WIFI_POWER_MODE_GENERAL);
421 bool flag = (ret == RET_CODE_SUCCESS || ret == RET_CODE_NOT_SUPPORT);
422 ASSERT_TRUE(flag);
423 }
424
425 /**
426 * @tc.name: WifiClientSetPowerMode003
427 * @tc.desc: Wifi client set power mode function test
428 * @tc.type: FUNC
429 * @tc.require: AR000H603L
430 */
431 HWTEST_F(WifiClientTest, WifiClientSetPowerMode003, TestSize.Level1)
432 {
433 int32_t ret;
434
435 ret = SetPowerMode(WLAN_IFNAME, WIFI_POWER_MODE_THROUGH_WALL);
436 bool flag = (ret == RET_CODE_SUCCESS || ret == RET_CODE_NOT_SUPPORT);
437 ASSERT_TRUE(flag);
438 }
439
440 /**
441 * @tc.name: WifiClientSetPowerMode004
442 * @tc.desc: Wifi client set power mode function test
443 * @tc.type: FUNC
444 * @tc.require: AR000H603L
445 */
446 HWTEST_F(WifiClientTest, WifiClientSetPowerMode004, TestSize.Level1)
447 {
448 int32_t ret;
449
450 ret = SetPowerMode(WLAN_IFNAME, WIFI_POWER_MODE_NUM);
451 EXPECT_NE(RET_CODE_SUCCESS, ret);
452 }
453
454 /**
455 * @tc.name: WifiRegisterHid2dCallback001
456 * @tc.desc: Wifi register hid2d callback function test
457 * @tc.type: FUNC
458 * @tc.require:
459 */
460 HWTEST_F(WifiClientTest, WifiRegisterHid2dCallback001, TestSize.Level1)
461 {
462 int32_t ret;
463
464 ret = WifiRegisterHid2dCallback(nullptr, WLAN_IFNAME);
465 EXPECT_EQ(RET_CODE_INVALID_PARAM, ret);
466 ret = WifiRegisterHid2dCallback(Hid2dFunCb, nullptr);
467 EXPECT_EQ(RET_CODE_INVALID_PARAM, ret);
468 ret = WifiRegisterHid2dCallback(Hid2dFunCb, WLAN_IFNAME);
469 EXPECT_EQ(RET_CODE_SUCCESS, ret);
470 WifiUnregisterHid2dCallback(nullptr, WLAN_IFNAME);
471 WifiUnregisterHid2dCallback(Hid2dFunCb, nullptr);
472 }
473
474 /**
475 * @tc.name: WifiGetSignalPollInfo001
476 * @tc.desc: Wifi get signal poll info function test
477 * @tc.type: FUNC
478 * @tc.require:
479 */
480 HWTEST_F(WifiClientTest, WifiGetSignalPollInfo001, TestSize.Level1)
481 {
482 int32_t ret;
483 const char *ifNameInvalid = "wlanTest";
484 struct SignalResult signalResult;
485 (void)memset_s(&signalResult, sizeof(signalResult), 0, sizeof(signalResult));
486
487 ret = WifiGetSignalPollInfo(nullptr, &signalResult);
488 EXPECT_EQ(RET_CODE_FAILURE, ret);
489 ret = WifiGetSignalPollInfo(ifNameInvalid, &signalResult);
490 EXPECT_EQ(RET_CODE_FAILURE, ret);
491 }
492
WifiEventCb(uint32_t event,void * respData,const char * ifName)493 static int32_t WifiEventCb(uint32_t event, void *respData, const char *ifName)
494 {
495 (void)event;
496 (void)respData;
497 (void)ifName;
498 return RET_CODE_SUCCESS;
499 }
500
501 /**
502 * @tc.name: WifiRegisterEventCallback001
503 * @tc.desc: Wifi register event callback function test
504 * @tc.type: FUNC
505 * @tc.require:
506 */
507 HWTEST_F(WifiClientTest, WifiRegisterEventCallback001, TestSize.Level1)
508 {
509 int32_t ret;
510
511 ret = WifiRegisterEventCallback(nullptr, WIFI_KERNEL_TO_HAL_CLIENT, WLAN_IFNAME);
512 EXPECT_EQ(RET_CODE_INVALID_PARAM, ret);
513 ret = WifiRegisterEventCallback(WifiEventCb, WIFI_KERNEL_TO_HAL_CLIENT, nullptr);
514 EXPECT_EQ(RET_CODE_INVALID_PARAM, ret);
515 WifiUnregisterEventCallback(nullptr, WIFI_KERNEL_TO_HAL_CLIENT, WLAN_IFNAME);
516 WifiUnregisterEventCallback(WifiEventCb, WIFI_KERNEL_TO_HAL_CLIENT, nullptr);
517 }
518 };
519