wifiservice_func_test.c 25.3 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 394 395
    const char* ssid1 = "XtsTestWifi01";
    const char* ssid[TEST_SSID_COUNT] = {"XtsTestWifi02", "XtsTestWifi03", "XtsTestWifi04", "XtsTestWifi05", "XtsTestWifi06", "XtsTestWifi07", "XtsTestWifi08", "XtsTestWifi09", 		"XtsTestWifi10"};
    const char* ssid11 = "XtsTestWifi11";
396
    const char* info = "12345678";
W
wenjun 已提交
397 398
    unsigned char bssid[WIFI_MAC_LEN] = {0xac, 0x75, 0x1d, 0xd8, 0x55, 0xc1};
    WifiDeviceConfig config = {0};
399
    config.freq = 20;
W
wenjun 已提交
400 401
    config.securityType = WIFI_SEC_TYPE_SAE;
    config.wapiPskType = WIFI_PSK_TYPE_ASCII;
402
    int ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid1, strlen(ssid1));
M
mamingshuai 已提交
403
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
404
    ret = strncpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, info, strlen(info));
M
mamingshuai 已提交
405
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
406
    ret = memcpy_s(config.bssid, WIFI_MAC_LEN, bssid, WIFI_MAC_LEN);
M
mamingshuai 已提交
407
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
408
    WifiErrorCode error = AddDeviceConfig(&config, &netId);
M
mamingshuai 已提交
409
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
410

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

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

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

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

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

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

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

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

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

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

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

    int stat = IsHotspotActive();
M
mamingshuai 已提交
511
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_NOT_ACTIVE, stat);
W
wenjun 已提交
512
    error = EnableHotspot();
M
mamingshuai 已提交
513
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
514
    stat = IsHotspotActive();
M
mamingshuai 已提交
515
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_ACTIVE, stat);
W
wenjun 已提交
516
    error = EnableHotspot();
M
mamingshuai 已提交
517
    TEST_ASSERT_NOT_EQUAL(WIFI_SUCCESS, error);
W
wenjun 已提交
518
    stat = IsHotspotActive();
M
mamingshuai 已提交
519
    TEST_ASSERT_EQUAL_INT(WIFI_HOTSPOT_ACTIVE, stat);
W
wenjun 已提交
520 521 522 523

    int timeout = 3;
    g_apEnableSuccess = 0;
    while (timeout > 0) {
524
        osDelay(ONE_SECOND);
W
wenjun 已提交
525 526 527 528 529 530 531 532
        timeout--;
        if (g_apEnableSuccess >= 1) {
            printf("Wait %d seconds.\n", (DEF_TIMEOUT - timeout));
            break;
        }
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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 已提交
696 697 698

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

    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 已提交
724 725

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

732
    int errorType = -1; //Unnormal Type Val -> Default Type Val
W
wenjun 已提交
733
    scanParams->scanType = errorType;
734
    g_staScanSuccess = 0;
W
wenjun 已提交
735
    error = AdvanceScan(scanParams);
M
mamingshuai 已提交
736
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
737 738
    WaitScanResult();
    TEST_ASSERT_EQUAL_INT(1, g_staScanSuccess);
W
wenjun 已提交
739 740 741 742

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

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

    free(scanParams);
}

RUN_TEST_SUITE(WifiServiceFuncTestSuite);