wifiservice_func_test.c 24.4 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
#include "wifi_device.h"
#include "wifi_hotspot.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 已提交
52
        printf("WifiScanStateTask:get scan size is %u.\n", checkSize);
W
wenjun 已提交
53 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
        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 已提交
89
    printf("HotspotStaJoin:list size is %u.\n", size);
W
wenjun 已提交
90 91 92 93 94 95 96 97 98
    g_apEnableSuccess++;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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 已提交
532
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
533
    stat = IsHotspotActive();
M
mamingshuai 已提交
534
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_NOT_ACTIVE, stat);
W
wenjun 已提交
535
    error = DisableHotspot();
M
mamingshuai 已提交
536
    TEST_ASSERT_NOT_EQUAL(WIFI_SUCCESS, error);
W
wenjun 已提交
537
    stat = IsHotspotActive();
M
mamingshuai 已提交
538
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_NOT_ACTIVE, stat);
W
wenjun 已提交
539 540 541 542 543 544 545
}

/**
 * @tc.number    : SUB_COMMUNICATION_WIFISERVICE_SDK_0700
 * @tc.name      : Test get signal Level interface
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
546
LITE_TEST_CASE(WifiServiceFuncTestSuite, testGetSignalLevel, Function | MediumTest | Level2)
W
wenjun 已提交
547 548 549 550 551 552 553 554 555 556 557 558
{
    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 已提交
559
    TEST_ASSERT_EQUAL_INT(LEVEL_ERROR, level);
W
wenjun 已提交
560
    level = GetSignalLevel(rssiOf2gLevel1, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
561
    TEST_ASSERT_EQUAL_INT(LEVEL_ONE, level);
W
wenjun 已提交
562
    level = GetSignalLevel(rssiOf2gLevel2, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
563
    TEST_ASSERT_EQUAL_INT(LEVEL_TWO, level);
W
wenjun 已提交
564
    level = GetSignalLevel(rssiOf2gLevel3, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
565
    TEST_ASSERT_EQUAL_INT(LEVEL_THREE, level);
W
wenjun 已提交
566
    level = GetSignalLevel(rssiBothLevel4, HOTSPOT_BAND_TYPE_2G);
M
mamingshuai 已提交
567
    TEST_ASSERT_EQUAL_INT(LEVEL_FOUR, level);
W
wenjun 已提交
568 569

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

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

    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 已提交
604 605 606
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
607 608 609 610

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

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

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

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

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

    g_staScanSuccess = 0;
    error = AdvanceScan(NULL);
M
mamingshuai 已提交
649 650
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
651 652 653

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

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

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

    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 已提交
681 682
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
683 684 685

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

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

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

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

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

    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 已提交
714 715 716
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
717 718 719 720 721 722

    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 已提交
723 724
    TEST_ASSERT_EQUAL_INT(ERROR_WIFI_UNKNOWN, error);
    TEST_ASSERT_EQUAL_INT(0, g_staScanSuccess);
W
wenjun 已提交
725 726

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

    free(scanParams);
}

RUN_TEST_SUITE(WifiServiceFuncTestSuite);