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 1
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
        sleep(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 396
    const char* ssid0 = "TestWifi01";
    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
    int ret = strncpy_s(config.ssid, WIFI_MAX_SSID_LEN, ssid0, strlen(ssid0));
M
mamingshuai 已提交
404
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
405
    ret = strncpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, info, strlen(info));
M
mamingshuai 已提交
406
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
407
    ret = memcpy_s(config.bssid, WIFI_MAC_LEN, bssid, WIFI_MAC_LEN);
M
mamingshuai 已提交
408
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, ret);
W
wenjun 已提交
409
    WifiErrorCode error = AddDeviceConfig(&config, &netId);
M
mamingshuai 已提交
410
    TEST_ASSERT_EQUAL_INT(WIFI_SUCCESS, error);
W
wenjun 已提交
411

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    free(scanParams);
}

RUN_TEST_SUITE(WifiServiceFuncTestSuite);