WifiJsunit.test1.js 16.5 KB
Newer Older
Q
quanli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 90 91 92 93 94 95 96 97 98 99 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * 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.
 */

import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
import wifi from '@ohos.wifi'

function sleep(delay) {
    return new Promise(resovle => setTimeout(resovle, delay))
}

function checkWifiPowerOn(){
    console.info("wifi_test/wifi status:" + wifi.isWifiActive());
}

function resolveIP(ip) {
    return (ip>>24 & 0xFF) + "." + (ip>>16 & 0xFF) + "." + (ip>>8 & 0xFF) + "." + (ip & 0xFF);
}

let WifiSecurityType = {
    WIFI_SEC_TYPE_INVALID: 0,
    WIFI_SEC_TYPE_OPEN: 1,
    WIFI_SEC_TYPE_WEP: 2,
    WIFI_SEC_TYPE_PSK: 3,
    WIFI_SEC_TYPE_SAE: 4,
}

let ConnState = {
    SCANNING: 0,
    CONNECTING: 1,
    AUTHENTICATING: 2,
    OBTAINING_IPADDR: 3,
    CONNECTED: 4,
    DISCONNECTING: 5,
    DISCONNECTED: 6,
    UNKNOWN: 7,
}

let WifiChannelWidth = {
    WIDTH_20MHZ : 0,
    WIDTH_40MHZ : 1,
    WIDTH_80MHZ : 2,
    WIDTH_160MHZ : 3,
    WIDTH_80MHZ_PLUS : 4,
    WIDTH_INVALID:null,
}

export default function actsWifiTestNew() {
    describe('actsWifiTestNew', function () {
        beforeEach(function () {
            checkWifiPowerOn();
        })
        afterEach(function () {
        })

        /**
        * @tc.number     CandidateNetWork_0001
        * @tc.name       SUB_Communication_WiFi_XTS_CandidateNetWork_0001
        * @since 9
        * @tc.desc       Test add OPEN and WEP CandidateConfig Promise API functionality.
        * @syscap SystemCapability.Communication.WiFi.STA
        * @permission ohos.permission.SET_WIFI_INFO
        */
        it('SUB_Communication_WiFi_XTS_CandidateNetWork_0001', 0, async function (done) {
            let wifiDeviceConfig = {
                "ssid": "TEST_OPEN",
                "bssid": "",
                "preSharedKey": "",
                "isHiddenSsid": false,
                "securityType": WifiSecurityType.WIFI_SEC_TYPE_OPEN,
            };

            await wifi.addCandidateConfig(wifiDeviceConfig)
                .then(netWorkId => {
                    console.info("[wifi_test]add OPEN CandidateConfig promise : " + JSON.stringify(netWorkId));
                    expect(true).assertEqual(netWorkId = -1);
                }).catch((error) => {
                    console.error('[wifi_js] add OPEN CandidateConfig promise failed -> ' + JSON.stringify(error));
                    expect(false).assertFalse();
                });

            let getconfig = wifi.getCandidateConfigs();
            console.info("[wifi_test] wifi get OPEN CandidateConfigs result : " + JSON.stringify(getconfig));

            let wifiDeviceConfig1 = {
                "ssid": "TEST_WEP",
                "bssid": "",
                "preSharedKey": "ABCDEF1234",
                "isHiddenSsid": false,
                "securityType": WifiSecurityType.WIFI_SEC_TYPE_WEP,
            };

            await wifi.addCandidateConfig(wifiDeviceConfig1)
                .then(netWorkId => {
                    console.info("[wifi_test]add WEP CandidateConfig promise : " + JSON.stringify(netWorkId));
                    expect(true).assertEqual(netWorkId = -1);
                }).catch((error) => {
                    console.error('[wifi_js] add WEP CandidateConfig promise failed -> ' + JSON.stringify(error));
                    expect(false).assertFalse();
                });
            console.info("[wifi_test] wifi get WEP CandidateConfigs result : " + JSON.stringify(getconfig));
            done();
        })

        /**
        * @tc.number     CandidateNetWork_0001
        * @tc.name       SUB_Communication_WiFi_XTS_CandidateNetWork_0002
        * @since 8
        * @tc.desc       Test add PSK CandidateConfig and removeCandidateConfig Promise API functionality.
        * @syscap SystemCapability.Communication.WiFi.STA
        * @permission ohos.permission.SET_WIFI_INFO
        */
        it('SUB_Communication_WiFi_XTS_CandidateNetWork_0002', 0, async function (done) {
            let wifiDeviceConfig = {
                "ssid": "TEST_PSK",
                "bssid": "",
                "preSharedKey": "12345678",
                "isHiddenSsid": false,
                "securityType": WifiSecurityType.WIFI_SEC_TYPE_PSK,
                "netId": "",
            };
            await wifi.addCandidateConfig(wifiDeviceConfig)
                .then(netWorkId => {
                    console.info("[wifi_test]add PSK CandidateConfig promise : " + JSON.stringify(netWorkId));
                    expect(true).assertEqual(netWorkId != -1);
                }).catch((error) => {
                    console.error('[wifi_js] add PSK CandidateConfig promise failed -> ' + JSON.stringify(error));
                    expect().assertFail();
                });
            let getconfig = wifi.getCandidateConfigs();
            console.info("[wifi_test] wifi get PSK CandidateConfigs result : " + JSON.stringify(getconfig));
            expect(true).assertEqual(getconfig[0].securityType == wifiDeviceConfig.securityType);
            expect(true).assertEqual(getconfig[0].isHiddenSsid == wifiDeviceConfig.isHiddenSsid);
            expect(true).assertEqual(getconfig[0].ssid == wifiDeviceConfig.ssid);
            var networkId = getconfig[0].netId;
            console.info("[wifi_test] wifi get networkId result : " + JSON.stringify(networkId));
            await wifi.removeCandidateConfig(networkId)
                .then(ret => {
                    console.info("[wifi_test]remove CandidateConfig promise" + JSON.stringify(ret));
                    expect(false).assertFalse();
                    let getconfig1 = wifi.getCandidateConfigs();
                    console.info("[wifi_test] wifi get CandidateConfigs result : " + JSON.stringify(getconfig1));
                    console.info("[wifi_test] wifi  getconfig.length result : " + JSON.stringify(getconfig1.length));
                    expect(true).assertEqual(getconfig1.length == 0);
                }).catch((error) => {
                    console.error('[wifi_js] remove CandidateConfig promise failed -> ' + JSON.stringify(error));
                    expect().assertFail();
                });
            done();
        })

        /**
        * @tc.number     CandidateNetWork_0003
        * @tc.name       SUB_Communication_WiFi_XTS_CandidateNetWork_0003
        * @since 8
        * @tc.desc       Test add SAE CandidateConfig Promise API functionality.
        * @syscap SystemCapability.Communication.WiFi.STA
        * @permission ohos.permission.SET_WIFI_INFO
        */
        it('SUB_Communication_WiFi_XTS_CandidateNetWork_0003', 0, async function (done) {
            let wifiDeviceConfig = {
                "ssid": "TEST_SAE",
                "bssid": "",
                "preSharedKey": "12345678",
                "isHiddenSsid": false,
                "securityType": WifiSecurityType.WIFI_SEC_TYPE_SAE,
            };
            await wifi.addCandidateConfig(wifiDeviceConfig)
                .then(netWorkId => {
                    console.info("[wifi_test]add SAE CandidateConfig promise : " + JSON.stringify(netWorkId));
                    expect(true).assertEqual(netWorkId = -1);
                }).catch((error) => {
                    console.error('[wifi_js] add SAE CandidateConfig promise failed -> ' + JSON.stringify(error));
                });
            let getconfig = wifi.getCandidateConfigs();
            console.info("[wifi_test] wifi get SAE CandidateConfigs result : " + JSON.stringify(getconfig));
            done();
        })

        /**
        * @tc.number     CandidateNetWork_0005
        * @tc.name       SUB_Communication_WiFi_XTS_CandidateNetWork_0005
        * @since 8
        * @tc.desc       Test add CandidateConfig and removeCandidateConfig callback API functionality.
        * @syscap SystemCapability.Communication.WiFi.STA
        * @permission ohos.permission.SET_WIFI_INFO
        */
        it('SUB_Communication_WiFi_XTS_CandidateNetWork_0005', 0, async function (done) {
            let wifiDeviceConfig = {
                "ssid": "TYPE_PSK1",
                "bssid": "",
                "preSharedKey": "12345678",
                "isHiddenSsid": false,
                "securityType": WifiSecurityType.WIFI_SEC_TYPE_PSK,
            }
            function addCandidate() {
                return new Promise((resolve, reject) => {
                    wifi.addCandidateConfig(wifiDeviceConfig,
                        (err, netWorkId) => {
                            if (err) {
                                console.info("[wifi_test]add CandidateConfig callback failed : " + JSON.stringify(err));
                                return;
                            }
                            console.info("[wifi_test]addCandidateConfig callback result: " + JSON.stringify(netWorkId));
                            expect(true).assertEqual(netWorkId != -1);
                            resolve();
                        });
                });
            }
            await addCandidate();
            let configs = wifi.getCandidateConfigs();
            console.info("[wifi_test] wifi getCandidateConfigs result : " + JSON.stringify(configs));
            expect(true).assertEqual(configs[0].securityType == wifiDeviceConfig.securityType);
            expect(true).assertEqual(configs[0].isHiddenSsid == wifiDeviceConfig.isHiddenSsid);
            expect(true).assertEqual(configs[0].ssid == wifiDeviceConfig.ssid);
            var networkId = configs[0].netId;
            function removeCandidate() {
                return new Promise((resolve, reject) => {
                    wifi.removeCandidateConfig(networkId,
                        (err, ret) => {
                            if (err) {
                                console.info("[wifi_test]removeCandidate callback failed : " + JSON.stringify(err));
                                return;
                            }
                            console.info("[wifi_test] removeCandidateConfig callback result:" + JSON.stringify(ret));
                            expect(ret).assertTrue();
                            let configs1 = wifi.getCandidateConfigs();
                            console.info("[wifi_test] wifi get  CandidateConfigs result : " + JSON.stringify(configs1));
                            console.info("[wifi_test] getconfig.length result : " + JSON.stringify(configs1.length));
                            expect(true).assertEqual(configs1.length == 0);
                            resolve();
                        });
                });
            }
            await removeCandidate();
            done();
        })

        /**
        * @tc.number     CandidateNetWork_0006
        * @tc.name       SUB_Communication_WiFi_XTS_SUB_Communication_WiFi_XTS_CandidateNetWork_0006
        * @tc.desc       Test connect To CandidateConfig API functionality.
        * @since 9
        * @syscap SystemCapability.Communication.WiFi.STA
        * @permission ohos.permission.SET_WIFI_INFO
        */
        it('SUB_Communication_WiFi_XTS_CandidateNetWork_0006', 0, async function (done) {
            let WifiSecurityType = {
                WIFI_SEC_TYPE_INVALID: 0,
                WIFI_SEC_TYPE_OPEN: 1,
                WIFI_SEC_TYPE_WEP: 2,
                WIFI_SEC_TYPE_PSK: 3,
                WIFI_SEC_TYPE_SAE: 4,
            }
            let wifiDeviceConfig = {
                "ssid": "HONOR 3000",
                "bssid": "",
                "preSharedKey": "12345678",
                "isHiddenSsid": false,
                "securityType": WifiSecurityType.WIFI_SEC_TYPE_PSK,
            };
            await wifi.addCandidateConfig(wifiDeviceConfig)
                .then(netWorkId => {
                    console.info("[wifi_test]add CandidateConfig promise : " + JSON.stringify(netWorkId));
                    expect(true).assertEqual(netWorkId != -1);
                }).catch((error) => {
                    console.error('[wifi_js] add CandidateConfig promise failed -> ' + JSON.stringify(error));
                    expect().assertFail();
                });
            let getconfig = wifi.getCandidateConfigs();
            console.info("[wifi_test] wifi get  CandidateConfigs result : " + JSON.stringify(getconfig));
            let connectToCandidate = wifi.connectToCandidateConfig(getconfig[0].netId);
            console.info("[wifi_test] connect To CandidateConfig result : " + JSON.stringify(connectToCandidate));
            await sleep(3000);
            await wifi.getLinkedInfo()
                .then((result) => {
                    console.info("[wifi_test] get wifi link [promise] -> " + JSON.stringify(result));
                    expect(JSON.stringify(result)).assertContain('band');
                    done();
                }).catch((error) => {
                    console.info("[wifi_test] promise then error." + JSON.stringify(error));
                    expect().assertFail();
                });
            done();
        })


        /**
        * @tc.number     Sta_0002
        * @tc.name       SUB_Communication_WiFi_XTS_Sta_0002
        * @tc.desc       Test  get to ScanInfos promise and callback API functionality.
        * @since 6
        * @syscap SystemCapability.Communication.WiFi.STA
        * @permission ohos.permission.GET_WIFI_INFO and (ohos.permission.GET_WIFI_PEERS_MAC or ohos.permission.LOCATION)
        */
        it('SUB_Communication_WiFi_XTS_Sta_0002', 0, async function (done) {
            await wifi.getScanInfos()
                .then(result => {
                    let clen = Object.keys(result).length;
                    expect(clen).assertLarger(0);
                    console.info("[wifi_test] getScanInfos promise result " + JSON.stringify(result));
                });
            function getScanInfos() {
                return new Promise((resolve, reject) => {
                    wifi.getScanInfos(
                        (err, result) => {
                            if (err) {
                                console.log("[wifi_test] wifi getScanInfos failed " + err);
                            }
                            let clen = Object.keys(result).length;
                            if (!(clen == 0)) {
                                expect(clen).assertLarger(0);
                                console.info("[wifi_test] getScanInfos callback result: " + JSON.stringify(result));
                                for (let j = 0; j < clen; ++j) {
                                    console.info("ssid: " + result[j].ssid + "bssid: " + result[j].bssid +
                                    "securityType: " + result[j].securityType +
                                    "rssi: " + result[j].rssi + "band: " + result[j].band +
                                    "frequency: " + result[j].frequency + "channelWidth: " + result[j].channelWidth +
                                    "timestamp" + result[j].timestamp + "capabilities" + result[j].capabilities
                                    + "centerFrequency0: " + result[j].centerFrequency0
                                    + "centerFrequency1: " + result[j].centerFrequency1
                                    + "infoElems: " + result[j].infoElems);
                                }
                            }
                            resolve();
                        });
                });
            }
            await getScanInfos();
            done();
        })

        /**
        * @tc.number     Sta_0034
        * @tc.name       SUB_Communication_WiFi_XTS_Sta_0034
        * @tc.desc       Test  get to ScanInfos  Sync API functionality.
        * @since 9
        * @syscap SystemCapability.Communication.WiFi.STA
        * @permission ohos.permission.GET_WIFI_INFO and (ohos.permission.GET_WIFI_PEERS_MAC or ohos.permission.LOCATION)
        */
        it('SUB_Communication_WiFi_XTS_Sta_0034', 0, async function (done) {
            let getScanInfos = wifi.getScanInfosSync();
            console.info("[wifi_test] wifi get to ScanInfosSync  result : " + JSON.stringify(getScanInfos));
            let lenth = Object.keys(getScanInfos).length;
            console.info("[wifi_test] wifi ScanInfosSync length  result : " + JSON.stringify(lenth));
            expect(lenth).assertLarger(0);
            done();
        })
    })
}