newSEService.test.js 9.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
/*
 * 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;

export default function newSEServicetest() {
    describe('newSEServicetest', function () {
        beforeAll(function () {
            try {
                nfcSEService = secureElement.newSEService("serviceState", (state) => {
                    if (state == secureElement.ServiceState.DISCONNECTED) {
                        console.info("[nfc_test] beforeAll newService state is Disconnected");
                    } else {
                        console.info("[nfc_test] beforeAll newService state is Connected");
                    }
                    expect(state instanceof Object).assertTrue();
                    console.info("[nfc_test] beforeAll newService state is " + state);
                });
                sleep(1000);
            } catch (e) {
                console.info("[nfc_test] beforeAll newService occurs exception:" + e.message);
            }
        })
        beforeEach(function() {
            console.info('beforeEach called');
        })
        afterEach(function () {
            console.info('afterEach called');
        })
        afterAll(function () {
            nfcSEService.shutdown();
            sleep(5000);
            console.info('[nfc_test] afterAll newService shutdown success');
        })

        /**
         * @tc.number SUB_Communication_NFC_secureElement_js_0100
         * @tc.name Test isConnected
         * @tc.desc Check whether the SE service is connected.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_NFC_secureElement_js_0100', 0, function () {
            try {
                let nfcisConnected = nfcSEService.isConnected();
                console.info("[NFC_test]1 SEService The connection status is: " + nfcisConnected);
                expect(nfcisConnected).assertTrue();
            } catch (error) {
                console.info("[NFC_test]1 exception occurred when checking the connection status.:" + error)
                expect().assertFail();
            }
        })

        /**
         * @tc.number SUB_Communication_NFC_secureElement_js_0200
         * @tc.name Test getVersion
         * @tc.desc Returns the version number of the OMA specification.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_NFC_secureElement_js_0200', 0, function ()  {
            try {
                let getSEVersion = nfcSEService.getVersion();
                console.info("[NFC_test]2 OMA The version number is: " + getSEVersion);
                expect(typeof (getSEVersion)).assertEqual('string');
            } catch (error) {
                console.info("[NFC_test]2 Failed to obtain the version.:" + error);
                expect().assertFail();
            }
        })

        /**
         * @tc.number SUB_Communication_NFC_secureElement_js_0300
         * @tc.name Test getName
         * @tc.desc Returns the name of this reader.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_NFC_secureElement_js_0300', 0, function ()  {
            try {
                let nfcOmaReaderList = nfcSEService.getReaders();
                console.info("[nfc_test]3 Result of getReaders:" + nfcOmaReaderList );
                if (nfcOmaReaderList == undefined) {
                    console.info("[NFC_test]3 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    expect(nfcOmaReaderList instanceof Object).assertTrue();
                    let nfcESEReader = nfcOmaReaderList[0];
                    console.info("[nfc_test]3 getReaders results list 0 is" + nfcESEReader);
                    expect(nfcESEReader instanceof Object).assertTrue();
                    let getNfcname = nfcESEReader.getName();
                    console.info("[NFC_test]3 The reader name is: " + getNfcname);
                    expect(typeof (getNfcname)).assertEqual('string');
                }
            } catch (error) {
                console.info("[NFC_test]3 getName occurs exception:" + error);
                expect().assertFail();
            }
        })

        /**
         * @tc.number SUB_Communication_NFC_secureElement_js_0400
         * @tc.name Test isSecureElementPresent
         * @tc.desc Indicates whether the corresponding secure element is available.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_NFC_secureElement_js_0400', 0, function ()  {
            try {
                let nfcOmaReaderList2 = [];
                nfcOmaReaderList2 = nfcSEService.getReaders();
                if (nfcOmaReaderList2 == undefined) {
                    console.info("[NFC_test]4 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    console.info("[nfc_test]4 Result of getReaders:" + nfcOmaReaderList2);
                    expect(nfcOmaReaderList2 instanceof Object).assertTrue();
                    let nfcESEReader2 = nfcOmaReaderList2[0];
                    console.info("[nfc_test]4 getReaders results list 0 is" + nfcESEReader2);
                    expect(nfcESEReader2 instanceof Object).assertTrue();
                    let nfcSePresent = nfcESEReader2.isSecureElementPresent();
                    console.info("[NFC_test]4 security unit check result is: " + nfcSePresent);
                    expect(nfcSePresent).assertTrue();
                    let nfcOmaSession2 = nfcESEReader2.openSession();
                    sleep(3000)
                    console.info("[nfc_test]4 Open the second session result" + nfcOmaSession2);
                    expect(nfcOmaSession2 instanceof Object).assertTrue();
                    let openSession2 = nfcOmaSession2.isClosed();
                    console.info("[NFC_test]4 After close Check the second session is open: " + openSession2);
                    expect(openSession2).assertFalse();
                    nfcESEReader2.closeSessions();
                    sleep(3000)
                    console.info("[NFC_test]4 close the Reader of SE successfully");
                    let closeSession2 = nfcOmaSession2.isClosed();
                    console.info("[NFC_test]4 After close Check the second session is open: " + closeSession2);
                    expect(closeSession2).assertTrue();
                }
            } catch (error) {
                console.info("[NFC_test]4 isSecureElementPresent occurs exception:" + error);
                expect().assertFail();
            }
        })
        
        /**
         * @tc.number SUB_Communication_NFC_secureElement_js_0500
         * @tc.name Test shutdown
         * @tc.desc Release all SE resources allocated to the service.
         * @tc.type Function
         * @tc.level Level 2
         */
        it('SUB_Communication_NFC_secureElement_js_0500', 0, function ()  {
            try {
                nfcSEService.shutdown();
                console.info('[NFC_test] 05 eseshutdown pass');
                let downESEisconnected = nfcSEService.isConnected();
                console.info("[NFC_test]5 shutdown the SE SEService The connection status is: " + downESEisconnected);
                expect(downESEisconnected).assertFalse();
                sleep(5000);
                let SEService = secureElement.newSEService("serviceState", (state) => {
                    if (state == secureElement.ServiceState.DISCONNECTED) {
                        console.info("[nfc_test]5 Service state is Disconnected");
                    } else {
                        console.info("[nfc_test]5 Service state is Connected");
                    }
                    console.info("[nfc_test]5 Service state is" + state);
                });
                sleep(1000);
                expect(SEService instanceof Object).assertTrue();
                let openESEisconnected = SEService.isConnected();
                console.info("[NFC_test]5  SEService The connection status is:: " + openESEisconnected);
                expect(openESEisconnected).assertTrue();
                let nfcReadersList = SEService.getReaders();
                if (nfcReadersList == undefined) {
                    console.info("[NFC_test]5 This function is not supported because the phone NFC chip is ST chip.");
                } else {
                    expect(nfcReadersList instanceof Object).assertTrue();
                    console.info("[nfc_test]5 Result of getReaders:" + nfcReadersList);
                }
            } catch (error) {
                console.info("[NFC_test]5 getReaders occurs exception:" + error);
                expect().assertFail();
            }
        })

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