channelCallback.test.js 15.6 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
/*
 * Copyright (C) 2023 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 secureElement from '@ohos.secureElement';
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium';

function sleep(delay) { // delay x ms
    let start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
        continue;
    }
}

let nfcSEService = null;
let nfcESEReader = null;
let nfcOmaSession = null;
let nfcOmaReaderList = [];
let aidArray = [160, 0, 0, 1, 81, 0, 0, 0];
let p2 = 0x00;
let command = [128, 202, 159, 127, 0];

export default function channelCallbacktest() {
    describe('channelCallbacktest', function () {
        beforeAll(function () {
            try {
                nfcSEService = secureElement.newSEService("serviceState", (state) => {
                    if (state == secureElement.ServiceState.DISCONNECTED) {
                        console.info("[nfc_test] beforeAll Se_channel state is Disconnected");
                    } else {
                        console.info("[nfc_test] beforeAll Se_channel state is Connected");
                    }
                    expect(state instanceof Object).assertTrue();
                    console.info("[nfc_test] beforeAll Se_channel state is getReaders" + state);
                });
                sleep(1000);
                nfcOmaReaderList = nfcSEService.getReaders();
                if (nfcOmaReaderList.length === 0) {
                    console.info("[NFC_test]This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    console.info("[nfc_test] beforeAll Se_channel Result of getReaders:" + nfcOmaReaderList.length);
                    nfcESEReader = nfcOmaReaderList[0];
                    console.info("[nfc_test] beforeAll Se_channel getReaders results list 0 is" + nfcESEReader);
                    nfcOmaSession = nfcESEReader.openSession();
                    console.info("[nfc_test] beforeAll Se_channel openSession The result is" + nfcOmaSession);
                }
            } catch (e) {
                console.info("[nfc_test] beforeAll Se_channel occurs exception:" + e.message);
            }
        })

        beforeEach(function() {
            console.info('beforeEach called');
        })

        afterEach(function () {
            console.info('shutdown success');
        })

        afterAll(function () {
            nfcSEService.shutdown();
            sleep(5000);
            console.info('[nfc_test] afterAll channelcallback shutdown success');
        })

       /**
         * @tc.number SUB_Communication_Ese_LogicalChannel_js_2400
         * @tc.name Test isBasicChannel
         * @tc.desc open BasicChannel Check whether the channel is a basic channel.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_Ese_LogicalChannel_js_2400', 0, async function (done) {
            try {
                if (nfcOmaReaderList == undefined) {
                    console.info("[NFC_test]24 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    function P2BasicChannel_callback(){
                        return new Promise((resolve, reject) => {
                            nfcOmaSession.openBasicChannel(aidArray, p2,
                                (err, result) => {
                                    if(err) {
                                        console.info("[NFC_test]24 openBasicChannel data == null" + err);
                                        expect().assertFail();
                                    }
                                    console.info("[NFC_test]24 openBasicChannel data != null " + result);
                                    let P2BasicChannel = result;
                                    let isBasic = P2BasicChannel.isBasicChannel();
                                    console.info("[NFC_test]24 Check whether the channel is a basic channel: " + isBasic);
                                    expect(isBasic).assertTrue();
                                    P2BasicChannel.close();
                                    console.info("[NFC_test]24 Data received by the application select command: " );
                                    resolve();
                                });
                        });
                    }
                    await P2BasicChannel_callback();
                }
            } catch (error) {
                console.info("[NFC_test]24 openBasicChannel_p2_callback occurs exception:" + error);
                expect().assertFail();
            }
            sleep(3000);
            done();
        })

        /**
         * @tc.number SUB_Communication_Ese_LogicalChannel_js_2500
         * @tc.name Test isClosed
         * @tc.desc open LogicalChannel Check whether the channel is closed.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_Ese_LogicalChannel_js_2500', 0, async function (done) {
            try {
                if (nfcOmaReaderList == undefined) {
                    console.info("[NFC_test]25 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    function LogicalChannel_callback(){
                        return new Promise((resolve, reject) => {
                            nfcOmaSession.openLogicalChannel(aidArray, p2,
                                (err, result) => {
                                    if(err) {
                                        console.info("[NFC_test]25 openLogicalChannel data == null" + err);
                                        expect().assertFail();
                                    }
                                    console.info("[NFC_test]25 openLogicalChannel data == null" + result);
                                    let P2LogicalChannel = result;
                                    P2LogicalChannel.close();
                                    console.info("[NFC_test]25 Data received by the application select command: " );
                                    let isChannelClosed = P2LogicalChannel.isClosed();
                                    console.info("[NFC_test]25 Check whether the channel is closed: " + isChannelClosed);
                                    expect(isChannelClosed).assertTrue();
                                    resolve();
                                });
                        });
                    }
                    await LogicalChannel_callback();
                }
            } catch (error) {
                console.info("[NFC_test]25 openLogicalChannel_P2_callback occurs exception:" + error);
                expect().assertFail();
            }
            sleep(3000);
            done();
        })


        /**
         * @tc.number SUB_Communication_Ese_LogicalChannel_js_2600
         * @tc.name Test getSelectResponse
         * @tc.desc open BasicChannel Returns the data received from the application selection command.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_Ese_LogicalChannel_js_2600', 0, async function (done) {
            try {
                if (nfcOmaReaderList == undefined) {
                    console.info("[NFC_test]26 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    function BasicChannel_callback(){
                        return new Promise((resolve, reject) => {
                            nfcOmaSession.openBasicChannel(aidArray,
                                (err, result) => {
                                    if(err) {
                                        console.info("[NFC_test]26 to get openBasicChannel:" + JSON.stringify(err));
                                        expect().assertFail();
                                    }
                                    console.info("[NFC_test]26 openBasicChannel data != null " + result);
                                    let nfcBasicChannel = result;
                                    let getResponse = nfcBasicChannel.getSelectResponse();
                                    console.info("[NFC_test]26 getSelectResponse data: " + getResponse);
                                    nfcBasicChannel.close();
                                    console.info("[NFC_test]26 Data received by the application select command: " );
                                    expect(getResponse).assertInstanceOf('Array');
                                    resolve();
                                });
                        });
                    }
                    await BasicChannel_callback();
            }
            } catch (error) {
                console.info("[NFC_test]26 openBasicChannel_callback occurs exception:" + error);
                expect().assertFail();
            }
            sleep(5000);
            done();
        })

        /**
         * @tc.number SUB_Communication_Ese_LogicalChannel_js_2700
         * @tc.name Test transmit
         * @tc.desc open LogicalChannel Send an APDU command to the SE 
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_Ese_LogicalChannel_js_2700', 0, async function (done) {
            try {
                if (nfcOmaReaderList == undefined) {
                    console.info("[NFC_test]27 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    function LogicalChannel_callback(){
                        return new Promise((resolve, reject) => {
                            nfcOmaSession.openLogicalChannel(aidArray,
                                (err, result) => {
                                    if(err) {
                                        console.info("[NFC_test]27 failed to get openLogicalChannel:" + JSON.stringify(err));
                                        expect().assertFail();
                                    }
                                    console.info("[NFC_test]27 openLogicalChannel data != null " + result);
                                    let nfcLogicalChannel = result;
                                    nfcLogicalChannel.transmit(command, (err, data) => {
                                        if (err) {
                                            console.info("[NFC_test]27 Send error an APDU command to the SE: " + err);
                                            expect().assertFail();
                                        } else {
                                            console.info("[NFC_test]27 Send an APDU command to the SE: " + data);
                                            expect(data).assertInstanceOf('Array');
                                        }
                                    });
                                    sleep(5000);
                                    nfcLogicalChannel.close();
                                    console.info("[NFC_test]27 Data received by the application select command: " );
                                    resolve();
                                });
                        });
                    }
                    await LogicalChannel_callback();
                }
            } catch (error) {
                console.info("[NFC_test]27 openLogicalChannel_callback occurs exception:" + error);
                expect().assertFail();
            }
            sleep(3000);
            done();
        })

       /**
         * @tc.number SUB_Communication_Ese_LogicalChannel_js_2800
         * @tc.name Test transmit
         * @tc.desc open LogicalChannel Send an APDU command to the SE 
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_Ese_LogicalChannel_js_2800', 0, async function (done) {
            try {
                if (nfcOmaReaderList == undefined) {
                    console.info("[NFC_test]28 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    let getPromise = nfcOmaSession.openBasicChannel(aidArray);
                    await getPromise.then((data) => {
                        console.info("[NFC_test]28 openBasicChannel openBasicChannel data: " + data);
                        if ( data != undefined && data != null){
                            console.info("[NFC_test]28 openBasicChannel data != null");
                            let OmaBasicChannel = data;
                            OmaBasicChannel.close();
                            console.info("[NFC_test]28 Data received by the application select command: " );
                        }
                        else {
                            console.info("[NFC_test]28 openBasicChannel data == null");
                        }
                    }).catch((error)=> {
                        console.info("[NFC_test]28 openBasicChannel catch error" + error);
                        expect().assertFail();
                    });
                    sleep(5000);
                }
            } catch (error) {
                console.info("[NFC_test]28 openBasicChannel_Promise occurs exception:" + error);
                expect().assertFail();
            }
            done();
        })

        /**
         * @tc.number SUB_Communication_Ese_LogicalChannel_js_2900
         * @tc.name Test isClosed
         * @tc.desc Check whether the channel is closed.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_Ese_LogicalChannel_js_2900', 0, async function (done) {
            try {
                if (nfcOmaReaderList == undefined) {
                    console.info("[NFC_test]29 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    let getPromise = nfcOmaSession.openLogicalChannel(aidArray);
                    await getPromise.then((data) => {
                        console.info("[NFC_test]29  openLogicalChannel data: " + data);
                        if ( data != undefined && data != null){
                            console.info("[NFC_test]29 openLogicalChannel data != null");
                            let OmaLogicalChannel = data;
                            let isChannelClosed = OmaLogicalChannel.isClosed();
                            console.info("[NFC_test]29 Check whether the channel is disabled: " + isChannelClosed);
                            expect(isChannelClosed).assertFalse();
                        }
                        else {
                            console.info("[NFC_test]29 openLogicalChannel data == null");
                        }
                    }).catch((error)=> {
                        console.info("[NFC_test]29 openLogicalChannel catch error" + error);
                        expect().assertFail();
                    });
                    sleep(5000);
                }
            } catch (error) {
                console.info("[NFC_test]29 openLogicalChannel_Promise occurs exception:" + error);
                expect().assertFail();
            }
            done();
        })

        console.info("*************[nfc_test] start nfc js unit test end*************");
    })
}