wifiservice_func_test.c 24.5 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "hctest.h"
M
mamingshuai 已提交
17
#include "ohos_types.h"
W
wenjun 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include "wifi_device.h"
#include "wifi_hotspot.h"
#include "lwip/netif.h"
#include "lwip/netifapi.h"
#include "lwip/ip4_addr.h"
#include "cmsis_os2.h"
#include <unistd.h>

#define DEF_TIMEOUT 15
#define ONE_SECOND 1
#define LEVEL_ERROR (-1)
#define LEVEL_ONE 1
#define LEVEL_TWO 2
#define LEVEL_THREE 3
#define LEVEL_FOUR 4
#define DEF_TASK_STACK 2000
#define DEF_TASK_PRIORITY 20

static int g_apEnableSuccess = 0;
static int g_staScanSuccess = 0;
WifiEvent g_wifiEventHandler = {0};

/**
 * callback task for wifi scan
 */
static void WifiScanStateTask(void)
{
    WifiScanInfo* info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
    if (info == NULL) {
        printf("WifiScanStateTask:malloc fail.\n");
        return;
    }
    unsigned int checkSize = WIFI_SCAN_HOTSPOT_LIMIT;
    WifiErrorCode error = GetScanInfoList(info, &checkSize);
    if (error != WIFI_SUCCESS) {
        printf("WifiScanStateTask:get info fail, error is %d.\n", error);
    } else {
M
mamingshuai 已提交
55
        printf("WifiScanStateTask:get scan size is %u.\n", checkSize);
W
wenjun 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        g_staScanSuccess = 1;
    }
    free(info);
    info = NULL;
}

/**
 * callback task for connection
 */
static void WifiConnectionStateTask(void)
{
    WifiLinkedInfo linkInfo = {0};
    WifiErrorCode error = GetLinkedInfo(&linkInfo);
    if (error != WIFI_SUCCESS) {
        printf("WifiConnectionChanged:get link info fail, error is %d.\n", error);
        return;
    }
    if (linkInfo.connState != WIFI_CONNECTED) {
        printf("WifiConnectionChanged:connect fail!\n");
        return;
    }
    printf("WifiConnectionChanged:connect success.\n");
}

/**
 * callback function for hotspot
 */
static void HotspotStateTask(void)
{
    StationInfo info[WIFI_MAX_STA_NUM] = {0};
    unsigned int size = WIFI_MAX_STA_NUM;
    WifiErrorCode error = GetStationList(info, &size);
    if (error != WIFI_SUCCESS) {
        printf("HotspotStaJoin:get list fail, error is %d.\n", error);
        return;
    }
M
mamingshuai 已提交
92
    printf("HotspotStaJoin:list size is %u.\n", size);
W
wenjun 已提交
93 94 95 96 97 98 99 100 101
    g_apEnableSuccess++;
}

/**
 * callback function for wifi scan
 */
static void OnWifiScanStateChangedHandler(int state, int size)
{
    if (state != WIFI_STATE_AVALIABLE) {
M
mamingshuai 已提交
102
        printf("ScanStateChanged:state is unavailable.\n");
W
wenjun 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    } else {
        printf("ScanStateChanged:state[%d], size[%d].\n", state, size);
        osThreadAttr_t attr;
        attr.name = "WifiScanStateTask";
        attr.attr_bits = 0U;
        attr.cb_mem = NULL;
        attr.cb_size = 0U;
        attr.stack_mem = NULL;
        attr.stack_size = DEF_TASK_STACK;
        attr.priority = DEF_TASK_PRIORITY;
        if (osThreadNew((osThreadFunc_t)WifiScanStateTask, NULL, &attr) == NULL) {
            printf("ScanStateChanged:create task fail!\n");
        }
    }
}

/**
 * callback function for wifi connection
 */
static void OnWifiConnectionChangedHandler(int state, WifiLinkedInfo* info)
{
    if (info == NULL) {
        printf("WifiConnectionChanged:info is null, stat is %d.\n", state);
    } else {
        osThreadAttr_t attr;
        attr.name = "WifiConnectionStateTask";
        attr.attr_bits = 0U;
        attr.cb_mem = NULL;
        attr.cb_size = 0U;
        attr.stack_mem = NULL;
        attr.stack_size = DEF_TASK_STACK;
        attr.priority = DEF_TASK_PRIORITY;
        if (osThreadNew((osThreadFunc_t)WifiConnectionStateTask, NULL, &attr) == NULL) {
            printf("WifiConnectionStateTask:create task fail!\n");
        }
    }
}

/**
 * callback function for STA join AP
 */
static void OnHotspotStaJoinHandler(StationInfo* info)
{
    if (info == NULL) {
        printf("HotspotStaJoin:info is null.\n");
    }  else {
        osThreadAttr_t attr;
        attr.name = "HotspotStateTask";
        attr.attr_bits = 0U;
        attr.cb_mem = NULL;
        attr.cb_size = 0U;
        attr.stack_mem = NULL;
        attr.stack_size = DEF_TASK_STACK;
        attr.priority = DEF_TASK_PRIORITY;
        if (osThreadNew((osThreadFunc_t)HotspotStateTask, NULL, &attr) == NULL) {
            printf("HotspotStaJoin:create task fail!\n");
        }
    }
}

/**
 * callback function for STA leave AP
 */
static void OnHotspotStaLeaveHandler(StationInfo* info)
{
    if (info == NULL) {
        printf("HotspotStaLeave:info is null.\n");
    } else {
        g_apEnableSuccess--;
    }
}

/**
 * callback function for AP
 */
static void OnHotspotStateChangedHandler(int state)
{
    printf("HotspotStateChanged:state is %d.\n", state);
}

/**
 * common wait scan result
 */
M
mamingshuai 已提交
186
static void WaitScanResult(void)
W
wenjun 已提交
187 188 189 190 191 192
{
    int scanTimeout = DEF_TIMEOUT;
    while (scanTimeout > 0) {
        sleep(ONE_SECOND);
        scanTimeout--;
        if (g_staScanSuccess == 1) {
M
mamingshuai 已提交
193
            printf("WaitScanResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout));
W
wenjun 已提交
194 195 196 197
            break;
        }
    }
    if (scanTimeout <= 0) {
M
mamingshuai 已提交
198
        printf("WaitScanResult:timeout!\n");
W
wenjun 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    }
}

/**
 * @tc.desc      : register a test suite, this test suite is used to test basic functions
 * @param        : subsystem name is communication
 * @param        : module name is wifiaware
 * @param        : test suit name is WifiAwareReliTestSuite
 */
LITE_TEST_SUIT(communication, wifiservice, WifiServiceFuncTestSuite);

/**
 * @tc.setup     : setup for all testcases
 * @return       : setup result, TRUE is success, FALSE is fail
 */
static BOOL WifiServiceFuncTestSuiteSetUp(void)
{
216 217 218 219
    WifiErrorCode error;
    // check wifi stat
    int ret = IsWifiActive();
    if (ret == WIFI_STATE_AVALIABLE) {
M
mamingshuai 已提交
220
        printf("[Setup]wifi is active, disable now...\n");
221 222
        error = DisableWifi();
        if (error == WIFI_SUCCESS) {
M
mamingshuai 已提交
223
            printf("[Setup]disable wifi success\n");
224
        } else {
M
mamingshuai 已提交
225 226
            TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
            printf("[Setup]disable wifi fail, please disable wifi, then run test cases!\n");
227 228 229 230 231 232 233
            return FALSE;
        }
    }

    // check AP stat
    ret = IsHotspotActive();
    if (ret == WIFI_HOTSPOT_ACTIVE) {
M
mamingshuai 已提交
234
        printf("[Setup]AP is active, disable now...\n");
235 236
        error = DisableHotspot();
        if (error == WIFI_SUCCESS) {
M
mamingshuai 已提交
237
            printf("[Setup]disable AP success\n");
238
        } else {
M
mamingshuai 已提交
239 240
            TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
            printf("[Setup]disable AP fail, please disable ap, then run test cases!\n");
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
            return FALSE;
        }
    }

    // check device config
    WifiDeviceConfig config[WIFI_MAX_CONFIG_SIZE] = {0};
    unsigned int size = WIFI_MAX_CONFIG_SIZE;
    error = GetDeviceConfigs(config, &size);
    if (error != ERROR_WIFI_NOT_AVAILABLE) {
        printf("[Setup]there is device config, clear now...\n");
        int count = 0;
        for (int i = 0; i < WIFI_MAX_CONFIG_SIZE; i++) {
            if (&config[i] != NULL) {
                RemoveDevice(config[i].netId);
                count++;
            }
        }
        printf("[Setup]clear count [%d]\n", count);
    }

    // register wifi event
W
wenjun 已提交
262 263 264 265 266
    g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
    g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
    g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
    g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
    g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
267
    error = RegisterWifiEvent(&g_wifiEventHandler);
268
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
269
    if (error != WIFI_SUCCESS) {
270
        printf("[Setup]register wifi event fail!\n");
W
wenjun 已提交
271 272 273 274 275 276 277 278 279 280 281 282
        return FALSE;
    }
    return TRUE;
}

/**
 * @tc.teardown  : teardown for all testcases
 * @return       : teardown result, TRUE is success, FALSE is fail
 */
static BOOL WifiServiceFuncTestSuiteTearDown(void)
{
    WifiErrorCode error = UnRegisterWifiEvent(&g_wifiEventHandler);
M
mamingshuai 已提交
283
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
284 285 286 287 288 289 290 291 292 293 294 295
    printf("+-------------------------------------------+\n");
    if (error != WIFI_SUCCESS) {
        return FALSE;
    }
    return TRUE;
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0100
 * @tc.name      : Test enable and disable wifi interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
296
LITE_TEST_CASE(WifiServiceFuncTestSuite, testEnableDisableWifi, Function | MediumTest | Level2)
W
wenjun 已提交
297 298
{
    int stat = IsWifiActive();
M
mamingshuai 已提交
299
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
300 301

    WifiErrorCode error = EnableWifi();
M
mamingshuai 已提交
302
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
303
    stat = IsWifiActive();
M
mamingshuai 已提交
304
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_AVALIABLE, stat);
W
wenjun 已提交
305 306

    error = EnableWifi();
M
mamingshuai 已提交
307
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_BUSY, error);
W
wenjun 已提交
308
    stat = IsWifiActive();
M
mamingshuai 已提交
309
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_AVALIABLE, stat);
W
wenjun 已提交
310 311

    error = DisableWifi();
M
mamingshuai 已提交
312
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
313
    stat = IsWifiActive();
M
mamingshuai 已提交
314
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
315 316

    error = DisableWifi();
M
mamingshuai 已提交
317
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_NOT_STARTED, error);
W
wenjun 已提交
318
    stat = IsWifiActive();
M
mamingshuai 已提交
319
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
320 321 322 323 324 325 326
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0200
 * @tc.name      : Test scan and get scan info interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
327
LITE_TEST_CASE(WifiServiceFuncTestSuite, testScan, Function | MediumTest | Level2)
W
wenjun 已提交
328 329 330
{
    unsigned int size = WIFI_SCAN_HOTSPOT_LIMIT;
    WifiErrorCode error = EnableWifi();
M
mamingshuai 已提交
331
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
332
    int stat = IsWifiActive();
M
mamingshuai 已提交
333
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_AVALIABLE, stat);
W
wenjun 已提交
334 335 336 337

    WifiScanInfo* info = malloc(sizeof(WifiScanInfo) * WIFI_SCAN_HOTSPOT_LIMIT);
    TEST_ASSERT_NOT_NULL(info);
    error = GetScanInfoList(info, &size);
M
mamingshuai 已提交
338 339
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    TEST_ASSERT_EQUAL_INT(0, size);
W
wenjun 已提交
340 341 342

    g_staScanSuccess = 0;
    error = Scan();
M
mamingshuai 已提交
343 344 345
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
346 347 348

    size = WIFI_SCAN_HOTSPOT_LIMIT;
    error = GetScanInfoList(info, &size);
M
mamingshuai 已提交
349 350
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    TEST_ASSERT_NOT_EQUAL(WIFI_SCAN_HOTSPOT_LIMIT, size);
W
wenjun 已提交
351 352 353
    free(info);

    error = DisableWifi();
M
mamingshuai 已提交
354
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
355
    stat = IsWifiActive();
M
mamingshuai 已提交
356
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
357 358 359 360 361 362 363
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0300
 * @tc.name      : Test connect and disconnect interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
364
LITE_TEST_CASE(WifiServiceFuncTestSuite, testConnectDisConnect, Function | MediumTest | Level2)
W
wenjun 已提交
365 366 367
{
    int netId = 0;
    WifiDeviceConfig config = {0};
368 369
    const char* ssid = "xts_execute";
    int ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid, strlen(ssid));
M
mamingshuai 已提交
370
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
371

W
wenjun 已提交
372
    config.securityType = WIFI_SEC_TYPE_OPEN;
373
    WifiErrorCode error = AddDeviceConfig(&config, &netId);
M
mamingshuai 已提交
374
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
375 376

    error = ConnectTo(netId);
M
mamingshuai 已提交
377
    TEST_ASSERT_NOT_EQUAL(WIFI_SUCCESS, error);
W
wenjun 已提交
378 379
    unsigned char mac[WIFI_MAC_LEN];
    error = GetDeviceMacAddress((unsigned char *)mac);
M
mamingshuai 已提交
380
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
381
    error = Disconnect();
M
mamingshuai 已提交
382
    TEST_ASSERT_NOT_EQUAL(WIFI_SUCCESS, error);
W
wenjun 已提交
383
    error = RemoveDevice(netId);
M
mamingshuai 已提交
384
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
385 386 387 388 389 390 391
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0400
 * @tc.name      : Test handle device config interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
392
LITE_TEST_CASE(WifiServiceFuncTestSuite, testHandleDeviceConfig, Function | MediumTest | Level2)
W
wenjun 已提交
393 394
{
    int netId = 0;
395 396 397 398
    const char* ssid1 = "XtsTestWifi1";
    const char* ssid2 = "XtsTestWifi2";
    const char* ssid3 = "XtsTestWifi3";
    const char* info = "12345678";
W
wenjun 已提交
399 400
    unsigned char bssid[WIFI_MAC_LEN] = {0xac, 0x75, 0x1d, 0xd8, 0x55, 0xc1};
    WifiDeviceConfig config = {0};
401
    config.freq = 20;
W
wenjun 已提交
402 403
    config.securityType = WIFI_SEC_TYPE_SAE;
    config.wapiPskType = WIFI_PSK_TYPE_ASCII;
404
    int ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid1, strlen(ssid1));
M
mamingshuai 已提交
405
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
406
    ret = strncpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, info, strlen(info));
M
mamingshuai 已提交
407
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
408
    ret = memcpy_s(config.bssid, WIFI_MAC_LEN, bssid, WIFI_MAC_LEN);
M
mamingshuai 已提交
409
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
410
    WifiErrorCode error = AddDeviceConfig(&config, &netId);
M
mamingshuai 已提交
411
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
412

413
    for (int i = 0; i < WIFI_MAX_CONFIG_SIZE - 1; i++) {
W
wenjun 已提交
414 415
        config.securityType = WIFI_SEC_TYPE_PSK;
        config.wapiPskType = WIFI_PSK_TYPE_HEX;
416
        ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid2, sizeof(ssid2));
M
mamingshuai 已提交
417
        TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
418
        ret = strncpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, info, strlen(info));
M
mamingshuai 已提交
419
        TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
420
        error = AddDeviceConfig(&config, &netId);
M
mamingshuai 已提交
421
        TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
422 423 424 425 426 427
        if (error != WIFI_SUCCESS) {
            printf("Add fail[%d].\n", i);
            break;
        }
    }

428
    ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid3, strlen(ssid3));
M
mamingshuai 已提交
429
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
430
    ret = strncpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, info, strlen(info));
M
mamingshuai 已提交
431
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
432 433
    config.securityType = WIFI_SEC_TYPE_PSK;
    error = AddDeviceConfig(&config, &netId);
M
mamingshuai 已提交
434
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_BUSY, error);
W
wenjun 已提交
435 436 437 438

    WifiDeviceConfig allConfig[WIFI_MAX_CONFIG_SIZE] = {0};
    unsigned int size = WIFI_MAX_CONFIG_SIZE;
    error = GetDeviceConfigs(allConfig, &size);
M
mamingshuai 已提交
439 440
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    TEST_ASSERT_EQUAL_INT(WIFI_MAX_CONFIG_SIZE, size);
W
wenjun 已提交
441 442 443

    for (int i = 0; i < WIFI_MAX_CONFIG_SIZE; i++) {
        error = RemoveDevice(allConfig[i].netId);
M
mamingshuai 已提交
444
        TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
445 446 447
    }

    error = GetDeviceConfigs(allConfig, &size);
M
mamingshuai 已提交
448
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_NOT_AVAILABLE, error);
W
wenjun 已提交
449 450 451 452 453 454 455
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0500
 * @tc.name      : Test handle AP config interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
456
LITE_TEST_CASE(WifiServiceFuncTestSuite, testHandleHotspotConfig, Function | MediumTest | Level2)
W
wenjun 已提交
457
{
458 459
    const char* ssid = "XtsTestAp";
    const char* info = "12345678";
W
wenjun 已提交
460
    HotspotConfig config = {0};
461
    int ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid, strlen(ssid));
M
mamingshuai 已提交
462
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
463
    ret = strncpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, info, strlen(info));
M
mamingshuai 已提交
464
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
465 466
    config.securityType = WIFI_SEC_TYPE_PSK;
    WifiErrorCode error = SetHotspotConfig(&config);
M
mamingshuai 已提交
467
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
468 469 470

    HotspotConfig getConfig = {0};
    error = GetHotspotConfig(&getConfig);
M
mamingshuai 已提交
471
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
472 473 474 475 476
    TEST_ASSERT_EQUAL_INT(config.securityType, WIFI_SEC_TYPE_PSK);

    int band = 11;
    int bandOrig = 11;
    error = SetBand(band);
M
mamingshuai 已提交
477
    TEST_ASSERT_NOT_EQUAL(WIFI_SUCCESS, error);
W
wenjun 已提交
478
    error = GetBand(&band);
M
mamingshuai 已提交
479 480
    TEST_ASSERT_NOT_EQUAL(WIFI_SUCCESS, error);
    TEST_ASSERT_EQUAL_INT(bandOrig, band);
W
wenjun 已提交
481 482

    error = SetBand(HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
483
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
484
    error = GetBand(&band);
M
mamingshuai 已提交
485 486
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    TEST_ASSERT_EQUAL_INT(HOTSPOT_BAND_TYPE_2G, band);
W
wenjun 已提交
487 488 489

    HotspotConfig getConfigAgain = {0};
    error = GetHotspotConfig(&getConfigAgain);
M
mamingshuai 已提交
490 491
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    TEST_ASSERT_EQUAL_INT(HOTSPOT_BAND_TYPE_2G, getConfigAgain.band);
W
wenjun 已提交
492 493 494 495 496 497 498
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0600
 * @tc.name      : Test enable and disable AP interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
499
LITE_TEST_CASE(WifiServiceFuncTestSuite, testEnableDisableHotSpot, Function | MediumTest | Level2)
W
wenjun 已提交
500
{
501 502
    const char* ssid = "XtsTestAp";
    const char* info = "12345678";
W
wenjun 已提交
503
    HotspotConfig config = {0};
504
    int ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid, strlen(ssid));
M
mamingshuai 已提交
505
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
506
    ret = strncpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, info, strlen(info));
M
mamingshuai 已提交
507
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
508 509
    config.securityType = WIFI_SEC_TYPE_PSK;
    WifiErrorCode error = SetHotspotConfig(&config);
M
mamingshuai 已提交
510
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
511 512

    int stat = IsHotspotActive();
M
mamingshuai 已提交
513
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_NOT_ACTIVE, stat);
W
wenjun 已提交
514
    error = EnableHotspot();
M
mamingshuai 已提交
515
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
516
    stat = IsHotspotActive();
M
mamingshuai 已提交
517
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_ACTIVE, stat);
W
wenjun 已提交
518
    error = EnableHotspot();
M
mamingshuai 已提交
519
    TEST_ASSERT_NOT_EQUAL(WIFI_SUCCESS, error);
W
wenjun 已提交
520
    stat = IsHotspotActive();
M
mamingshuai 已提交
521
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_ACTIVE, stat);
W
wenjun 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534

    int timeout = 3;
    g_apEnableSuccess = 0;
    while (timeout > 0) {
        sleep(ONE_SECOND);
        timeout--;
        if (g_apEnableSuccess >= 1) {
            printf("Wait %d seconds.\n", (DEF_TIMEOUT - timeout));
            break;
        }
    }

    error = DisableHotspot();
M
mamingshuai 已提交
535
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
536
    stat = IsHotspotActive();
M
mamingshuai 已提交
537
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_NOT_ACTIVE, stat);
W
wenjun 已提交
538
    error = DisableHotspot();
M
mamingshuai 已提交
539
    TEST_ASSERT_NOT_EQUAL(WIFI_SUCCESS, error);
W
wenjun 已提交
540
    stat = IsHotspotActive();
M
mamingshuai 已提交
541
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_NOT_ACTIVE, stat);
W
wenjun 已提交
542 543 544 545 546 547 548
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0700
 * @tc.name      : Test get signal Level interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
549
LITE_TEST_CASE(WifiServiceFuncTestSuite, testGetSignalLevel, Function | MediumTest | Level2)
W
wenjun 已提交
550 551 552 553 554 555 556 557 558 559 560 561
{
    int level;
    int rssiNoLevel = -90;
    int rssiOf2gLevel1 = -88;
    int rssiOf2gLevel2 = -82;
    int rssiOf2gLevel3 = -75;
    int rssiOf5gLevel1 = -85;
    int rssiOf5gLevel2 = -79;
    int rssiOf5gLevel3 = -72;
    int rssiBothLevel4 = -65;

    level = GetSignalLevel(rssiNoLevel, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
562
    TEST_ASSERT_EQUAL_INT(LEVEL_ERROR, level);
W
wenjun 已提交
563
    level = GetSignalLevel(rssiOf2gLevel1, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
564
    TEST_ASSERT_EQUAL_INT(LEVEL_ONE, level);
W
wenjun 已提交
565
    level = GetSignalLevel(rssiOf2gLevel2, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
566
    TEST_ASSERT_EQUAL_INT(LEVEL_TWO, level);
W
wenjun 已提交
567
    level = GetSignalLevel(rssiOf2gLevel3, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
568
    TEST_ASSERT_EQUAL_INT(LEVEL_THREE, level);
W
wenjun 已提交
569
    level = GetSignalLevel(rssiBothLevel4, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
570
    TEST_ASSERT_EQUAL_INT(LEVEL_FOUR, level);
W
wenjun 已提交
571 572

    level = GetSignalLevel(rssiNoLevel, HOTSPOT_BAND_TYPE_5G);
M
mamingshuai 已提交
573
    TEST_ASSERT_EQUAL_INT(-1, level);
W
wenjun 已提交
574
    level = GetSignalLevel(rssiOf5gLevel1, HOTSPOT_BAND_TYPE_5G);
M
mamingshuai 已提交
575
    TEST_ASSERT_EQUAL_INT(LEVEL_ONE, level);
W
wenjun 已提交
576
    level = GetSignalLevel(rssiOf5gLevel2, HOTSPOT_BAND_TYPE_5G);
M
mamingshuai 已提交
577
    TEST_ASSERT_EQUAL_INT(LEVEL_TWO, level);
W
wenjun 已提交
578
    level = GetSignalLevel(rssiOf5gLevel3, HOTSPOT_BAND_TYPE_5G);
M
mamingshuai 已提交
579
    TEST_ASSERT_EQUAL_INT(LEVEL_THREE, level);
W
wenjun 已提交
580
    level = GetSignalLevel(rssiBothLevel4, HOTSPOT_BAND_TYPE_5G);
M
mamingshuai 已提交
581
    TEST_ASSERT_EQUAL_INT(LEVEL_FOUR, level);
W
wenjun 已提交
582 583 584 585 586 587 588
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0800
 * @tc.name      : test adavance scan interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
589
LITE_TEST_CASE(WifiServiceFuncTestSuite, testAdvanceScanType, Function | MediumTest | Level2)
W
wenjun 已提交
590 591
{
    WifiErrorCode error = EnableWifi();
M
mamingshuai 已提交
592
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
593
    int stat = IsWifiActive();
M
mamingshuai 已提交
594
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_AVALIABLE, stat);
W
wenjun 已提交
595 596 597 598 599 600 601 602 603 604 605 606

    int freq = 2460;
    WifiScanParams scanParams = {0};
    char bssid[WIFI_MAC_LEN] = {0xac, 0x75, 0x1d, 0xd8, 0x55, 0xc1};
    strcpy_s(scanParams.ssid, sizeof(scanParams.ssid), "wifi_service_xts");
    scanParams.ssidLen = strlen(scanParams.ssid);
    scanParams.freqs = freq;
    memcpy_s(scanParams.bssid, sizeof(scanParams.bssid), bssid, sizeof(bssid));

    scanParams.scanType = WIFI_SSID_SCAN;
    g_staScanSuccess = 0;
    error = AdvanceScan(&scanParams);
M
mamingshuai 已提交
607 608 609
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
610 611 612 613

    scanParams.scanType = WIFI_FREQ_SCAN;
    g_staScanSuccess = 0;
    error = AdvanceScan(&scanParams);
M
mamingshuai 已提交
614 615 616
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
617 618 619 620

    scanParams.scanType = WIFI_BSSID_SCAN;
    g_staScanSuccess = 0;
    error = AdvanceScan(&scanParams);
M
mamingshuai 已提交
621 622 623
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
624 625 626 627

    scanParams.scanType = WIFI_BAND_SCAN;
    g_staScanSuccess = 0;
    error = AdvanceScan(&scanParams);
M
mamingshuai 已提交
628 629 630
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
631 632

    error = DisableWifi();
M
mamingshuai 已提交
633
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
634
    stat = IsWifiActive();
M
mamingshuai 已提交
635
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
636 637 638 639 640 641 642
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0900
 * @tc.name      : test adavance scan interface with invalid parameter
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
643
LITE_TEST_CASE(WifiServiceFuncTestSuite, testAdvanceScanInvalidParam01, Function | MediumTest | Level2)
W
wenjun 已提交
644 645
{
    WifiErrorCode error = EnableWifi();
M
mamingshuai 已提交
646
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
647
    int stat = IsWifiActive();
M
mamingshuai 已提交
648
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_AVALIABLE, stat);
W
wenjun 已提交
649 650 651

    g_staScanSuccess = 0;
    error = AdvanceScan(NULL);
M
mamingshuai 已提交
652 653
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
654 655 656

    WifiScanParams scanParams = {0};
    error = AdvanceScan(&scanParams);
M
mamingshuai 已提交
657 658
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
659 660

    error = DisableWifi();
M
mamingshuai 已提交
661
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
662
    stat = IsWifiActive();
M
mamingshuai 已提交
663
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
664 665 666 667
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_1000
668
 * @tc.name      : test adavance scan interface with different invalid scantype
W
wenjun 已提交
669 670
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
671
LITE_TEST_CASE(WifiServiceFuncTestSuite, testAdvanceScanInvalidParam02, Function | MediumTest | Level2)
W
wenjun 已提交
672 673
{
    WifiErrorCode error = EnableWifi();
M
mamingshuai 已提交
674
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
675
    int stat = IsWifiActive();
M
mamingshuai 已提交
676
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_AVALIABLE, stat);
W
wenjun 已提交
677 678 679 680 681 682 683

    WifiScanParams* scanParams = malloc(sizeof(WifiScanParams));
    TEST_ASSERT_NOT_NULL(scanParams);
    memset_s(scanParams, sizeof(WifiScanParams), 0, sizeof(WifiScanParams));

    g_staScanSuccess = 0;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
684 685
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
686 687 688

    scanParams->scanType = WIFI_BSSID_SCAN;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
689 690
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
691 692 693

    scanParams->scanType = WIFI_SSID_SCAN;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
694 695
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
696 697 698

    scanParams->scanType = WIFI_FREQ_SCAN;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
699 700
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
701 702 703

    scanParams->scanType = WIFI_BAND_SCAN;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
704 705
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
706 707 708 709

    int errorType = -1;
    scanParams->scanType = errorType;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
710 711
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
712 713 714 715 716

    char bssid[WIFI_MAC_LEN] = {0xac, 0x75, 0x1d, 0xd8, 0x55, 0xc1};
    memcpy_s(scanParams->bssid, sizeof(scanParams->bssid), bssid, sizeof(bssid));
    scanParams->scanType = WIFI_BSSID_SCAN;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
717 718 719
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
720 721 722 723 724 725

    memset_s(scanParams, sizeof(WifiScanParams), 0, sizeof(WifiScanParams));
    strcpy_s(scanParams->ssid, sizeof(scanParams->ssid), "wifi_service_xts");
    scanParams->scanType = WIFI_SSID_SCAN;
    g_staScanSuccess = 0;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
726 727
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
728 729

    error = DisableWifi();
M
mamingshuai 已提交
730
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
731
    stat = IsWifiActive();
M
mamingshuai 已提交
732
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
733 734 735 736 737

    free(scanParams);
}

RUN_TEST_SUITE(WifiServiceFuncTestSuite);