wifiservice_func_test.c 25.2 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
#include "wifi_device.h"
#include "wifi_hotspot.h"
#include "cmsis_os2.h"
#include <unistd.h>

#define DEF_TIMEOUT 15
24
#define ONE_SECOND 100
W
wenjun 已提交
25 26 27 28 29 30 31
#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
32
#define TEST_SSID_COUNT 9
W
wenjun 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

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 已提交
53
        printf("WifiScanStateTask:get scan size is %u.\n", checkSize);
W
wenjun 已提交
54 55 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
        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 已提交
90
    printf("HotspotStaJoin:list size is %u.\n", size);
W
wenjun 已提交
91 92 93 94 95 96 97 98 99
    g_apEnableSuccess++;
}

/**
 * callback function for wifi scan
 */
static void OnWifiScanStateChangedHandler(int state, int size)
{
    if (state != WIFI_STATE_AVALIABLE) {
M
mamingshuai 已提交
100
        printf("ScanStateChanged:state is unavailable.\n");
W
wenjun 已提交
101 102 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
    } 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 已提交
184
static void WaitScanResult(void)
W
wenjun 已提交
185 186 187
{
    int scanTimeout = DEF_TIMEOUT;
    while (scanTimeout > 0) {
188
        osDelay(ONE_SECOND);
W
wenjun 已提交
189 190
        scanTimeout--;
        if (g_staScanSuccess == 1) {
M
mamingshuai 已提交
191
            printf("WaitScanResult:wait success[%d]s\n", (DEF_TIMEOUT - scanTimeout));
W
wenjun 已提交
192 193 194 195
            break;
        }
    }
    if (scanTimeout <= 0) {
M
mamingshuai 已提交
196
        printf("WaitScanResult:timeout!\n");
W
wenjun 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    }
}

/**
 * @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)
{
214 215 216 217
    WifiErrorCode error;
    // check wifi stat
    int ret = IsWifiActive();
    if (ret == WIFI_STATE_AVALIABLE) {
M
mamingshuai 已提交
218
        printf("[Setup]wifi is active, disable now...\n");
219 220
        error = DisableWifi();
        if (error == WIFI_SUCCESS) {
M
mamingshuai 已提交
221
            printf("[Setup]disable wifi success\n");
222
        } else {
M
mamingshuai 已提交
223 224
            TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
            printf("[Setup]disable wifi fail, please disable wifi, then run test cases!\n");
225 226 227 228 229 230 231
            return FALSE;
        }
    }

    // check AP stat
    ret = IsHotspotActive();
    if (ret == WIFI_HOTSPOT_ACTIVE) {
M
mamingshuai 已提交
232
        printf("[Setup]AP is active, disable now...\n");
233 234
        error = DisableHotspot();
        if (error == WIFI_SUCCESS) {
M
mamingshuai 已提交
235
            printf("[Setup]disable AP success\n");
236
        } else {
M
mamingshuai 已提交
237 238
            TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
            printf("[Setup]disable AP fail, please disable ap, then run test cases!\n");
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
            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 已提交
260 261 262 263 264
    g_wifiEventHandler.OnWifiScanStateChanged = OnWifiScanStateChangedHandler;
    g_wifiEventHandler.OnWifiConnectionChanged = OnWifiConnectionChangedHandler;
    g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
    g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
    g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
265
    error = RegisterWifiEvent(&g_wifiEventHandler);
266
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
267
    if (error != WIFI_SUCCESS) {
268
        printf("[Setup]register wifi event fail!\n");
W
wenjun 已提交
269 270 271 272 273 274 275 276 277 278 279 280
        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 已提交
281
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
282 283 284 285 286 287 288 289 290 291 292 293
    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 已提交
294
LITE_TEST_CASE(WifiServiceFuncTestSuite, testEnableDisableWifi, Function | MediumTest | Level2)
W
wenjun 已提交
295 296
{
    int stat = IsWifiActive();
M
mamingshuai 已提交
297
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
298 299

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

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

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

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

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

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

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

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

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

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

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

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

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0400
 * @tc.name      : Test handle device config interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
390
LITE_TEST_CASE(WifiServiceFuncTestSuite, testHandleDeviceConfig, Function | MediumTest | Level2)
W
wenjun 已提交
391 392
{
    int netId = 0;
393
    const char* ssid0 = "TestWifi01";
394 395 396
    const char* ssid[TEST_SSID_COUNT] = {"TestWifi02", "TestWifi03", "TestWifi04", "TestWifi05", "TestWifi06",
    		"TestWifi07", "TestWifi08", "TestWifi09", "TestWifi10"};
    const char* ssid10 = "TestWifi11";
397
    const char* info = "12345678";
W
wenjun 已提交
398 399
    unsigned char bssid[WIFI_MAC_LEN] = {0xac, 0x75, 0x1d, 0xd8, 0x55, 0xc1};
    WifiDeviceConfig config = {0};
400
    config.freq = 20;
W
wenjun 已提交
401 402
    config.securityType = WIFI_SEC_TYPE_SAE;
    config.wapiPskType = WIFI_PSK_TYPE_ASCII;
403

404
    int ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid0, strlen(ssid0));
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, ssid[i], sizeof(ssid[i]));
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
        if (error != WIFI_SUCCESS) {
            printf("Add fail[%d].\n", i);
            break;
        }
    }
427

428
    ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid10, strlen(ssid10));
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

    int timeout = 3;
    g_apEnableSuccess = 0;
    while (timeout > 0) {
526
        osDelay(ONE_SECOND);
W
wenjun 已提交
527 528 529 530 531 532 533 534
        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

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

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

    scanParams->scanType = WIFI_BSSID_SCAN;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
687
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
W
wenjun 已提交
688 689 690

    scanParams->scanType = WIFI_SSID_SCAN;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
691
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
692 693 694 695 696 697

    memset_s(scanParams, sizeof(WifiScanParams), 0, sizeof(WifiScanParams));
    strcpy_s(scanParams->ssid, sizeof(scanParams->ssid), "wifi_service_xts");
    scanParams->scanType = WIFI_SSID_SCAN;
    error = AdvanceScan(scanParams);
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
W
wenjun 已提交
698 699 700

    scanParams->scanType = WIFI_FREQ_SCAN;
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
701
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

    error = DisableWifi();
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    stat = IsWifiActive();
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);

    free(scanParams);
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_1100
 * @tc.name      : test adavance scan interface with different invalid scantype
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(WifiServiceFuncTestSuite, testAdvanceScanInvalidParam03, Function | MediumTest | Level2)
{
    WifiErrorCode error = EnableWifi();
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    int stat = IsWifiActive();
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_AVALIABLE, stat);

    WifiScanParams* scanParams = malloc(sizeof(WifiScanParams));
    TEST_ASSERT_NOT_NULL(scanParams);
    memset_s(scanParams, sizeof(WifiScanParams), 0, sizeof(WifiScanParams));
W
wenjun 已提交
726 727

    scanParams->scanType = WIFI_BAND_SCAN;
728
    g_staScanSuccess = 0;
W
wenjun 已提交
729
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
730
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
731 732
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
733 734 735

    int errorType = -1;
    scanParams->scanType = errorType;
736
    g_staScanSuccess = 0;
W
wenjun 已提交
737
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
738
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
739 740
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
741 742 743 744

    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;
745
    g_staScanSuccess = 0;
W
wenjun 已提交
746
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
747 748 749
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
750 751

    error = DisableWifi();
M
mamingshuai 已提交
752
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
753
    stat = IsWifiActive();
M
mamingshuai 已提交
754
    TEST_ASSERT_EQUAL_INT(WIFI_STATE_NOT_AVALIABLE, stat);
W
wenjun 已提交
755 756 757 758

    free(scanParams);
}

759
RUN_TEST_SUITE(WifiServiceFuncTestSuite);