• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17import secureElement from '@ohos.secureElement';
18import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium';
19
20function sleep(delay) { // delay x ms
21    let start = (new Date()).getTime();
22    while ((new Date()).getTime() - start < delay) {
23        continue;
24    }
25}
26
27let nfcSEService = null;
28
29export default function newSEServicetest() {
30    describe('newSEServicetest', function () {
31        beforeAll(function () {
32            try {
33                nfcSEService = secureElement.newSEService("serviceState", (state) => {
34                    if (state == secureElement.ServiceState.DISCONNECTED) {
35                        console.info("[nfc_test] beforeAll newService state is Disconnected");
36                    } else {
37                        console.info("[nfc_test] beforeAll newService state is Connected");
38                    }
39                    expect(state instanceof Object).assertTrue();
40                    console.info("[nfc_test] beforeAll newService state is " + state);
41                });
42                sleep(1000);
43            } catch (e) {
44                console.info("[nfc_test] beforeAll newService occurs exception:" + e.message);
45            }
46        })
47        beforeEach(function() {
48            console.info('beforeEach called');
49        })
50        afterEach(function () {
51            console.info('afterEach called');
52        })
53        afterAll(function () {
54            nfcSEService.shutdown();
55            sleep(5000);
56            console.info('[nfc_test] afterAll newService shutdown success');
57        })
58
59        /**
60         * @tc.number SUB_Communication_NFC_secureElement_js_0100
61         * @tc.name Test isConnected
62         * @tc.desc Check whether the SE service is connected.
63         * @tc.type Function
64         * @tc.level Level 2
65         */
66        it('SUB_Communication_NFC_secureElement_js_0100', 0, function () {
67            try {
68                let nfcisConnected = nfcSEService.isConnected();
69                console.info("[NFC_test]1 SEService The connection status is: " + nfcisConnected);
70                expect(nfcisConnected).assertTrue();
71            } catch (error) {
72                console.info("[NFC_test]1 exception occurred when checking the connection status.:" + error)
73                expect().assertFail();
74            }
75        })
76
77        /**
78         * @tc.number SUB_Communication_NFC_secureElement_js_0200
79         * @tc.name Test getVersion
80         * @tc.desc Returns the version number of the OMA specification.
81         * @tc.type Function
82         * @tc.level Level 2
83         */
84        it('SUB_Communication_NFC_secureElement_js_0200', 0, function ()  {
85            try {
86                let getSEVersion = nfcSEService.getVersion();
87                console.info("[NFC_test]2 OMA The version number is: " + getSEVersion);
88                expect(typeof (getSEVersion)).assertEqual('string');
89            } catch (error) {
90                console.info("[NFC_test]2 Failed to obtain the version.:" + error);
91                expect().assertFail();
92            }
93        })
94
95        /**
96         * @tc.number SUB_Communication_NFC_secureElement_js_0300
97         * @tc.name Test getName
98         * @tc.desc Returns the name of this reader.
99         * @tc.type Function
100         * @tc.level Level 2
101         */
102        it('SUB_Communication_NFC_secureElement_js_0300', 0, function ()  {
103            try {
104                let nfcOmaReaderList = nfcSEService.getReaders();
105                console.info("[nfc_test]3 Result of getReaders:" + nfcOmaReaderList );
106                if (nfcOmaReaderList == undefined) {
107                    console.info("[NFC_test]3 This function is not supported because the phone NFC chip is ST chip.");
108                } else {
109                    expect(nfcOmaReaderList instanceof Object).assertTrue();
110                    let nfcESEReader = nfcOmaReaderList[0];
111                    console.info("[nfc_test]3 getReaders results list 0 is" + nfcESEReader);
112                    expect(nfcESEReader instanceof Object).assertTrue();
113                    let getNfcname = nfcESEReader.getName();
114                    console.info("[NFC_test]3 The reader name is: " + getNfcname);
115                    expect(typeof (getNfcname)).assertEqual('string');
116                }
117            } catch (error) {
118                console.info("[NFC_test]3 getName occurs exception:" + error);
119                expect().assertFail();
120            }
121        })
122
123        /**
124         * @tc.number SUB_Communication_NFC_secureElement_js_0400
125         * @tc.name Test isSecureElementPresent
126         * @tc.desc Indicates whether the corresponding secure element is available.
127         * @tc.type Function
128         * @tc.level Level 2
129         */
130        it('SUB_Communication_NFC_secureElement_js_0400', 0, function ()  {
131            try {
132                let nfcOmaReaderList2 = [];
133                nfcOmaReaderList2 = nfcSEService.getReaders();
134                if (nfcOmaReaderList2 == undefined) {
135                    console.info("[NFC_test]4 This function is not supported because the phone NFC chip is ST chip.");
136                } else {
137                    console.info("[nfc_test]4 Result of getReaders:" + nfcOmaReaderList2);
138                    expect(nfcOmaReaderList2 instanceof Object).assertTrue();
139                    let nfcESEReader2 = nfcOmaReaderList2[0];
140                    console.info("[nfc_test]4 getReaders results list 0 is" + nfcESEReader2);
141                    expect(nfcESEReader2 instanceof Object).assertTrue();
142                    let nfcSePresent = nfcESEReader2.isSecureElementPresent();
143                    console.info("[NFC_test]4 security unit check result is: " + nfcSePresent);
144                    expect(nfcSePresent).assertTrue();
145                    let nfcOmaSession2 = nfcESEReader2.openSession();
146                    sleep(3000)
147                    console.info("[nfc_test]4 Open the second session result" + nfcOmaSession2);
148                    expect(nfcOmaSession2 instanceof Object).assertTrue();
149                    let openSession2 = nfcOmaSession2.isClosed();
150                    console.info("[NFC_test]4 After close Check the second session is open: " + openSession2);
151                    expect(openSession2).assertFalse();
152                    nfcESEReader2.closeSessions();
153                    sleep(3000)
154                    console.info("[NFC_test]4 close the Reader of SE successfully");
155                    let closeSession2 = nfcOmaSession2.isClosed();
156                    console.info("[NFC_test]4 After close Check the second session is open: " + closeSession2);
157                    expect(closeSession2).assertTrue();
158                }
159            } catch (error) {
160                console.info("[NFC_test]4 isSecureElementPresent occurs exception:" + error);
161                expect().assertFail();
162            }
163        })
164
165        /**
166         * @tc.number SUB_Communication_NFC_secureElement_js_0500
167         * @tc.name Test shutdown
168         * @tc.desc Release all SE resources allocated to the service.
169         * @tc.type Function
170         * @tc.level Level 2
171         */
172        it('SUB_Communication_NFC_secureElement_js_0500', 0, function ()  {
173            try {
174                nfcSEService.shutdown();
175                console.info('[NFC_test] 05 eseshutdown pass');
176                let downESEisconnected = nfcSEService.isConnected();
177                console.info("[NFC_test]5 shutdown the SE SEService The connection status is: " + downESEisconnected);
178                expect(downESEisconnected).assertFalse();
179                sleep(5000);
180                let SEService = secureElement.newSEService("serviceState", (state) => {
181                    if (state == secureElement.ServiceState.DISCONNECTED) {
182                        console.info("[nfc_test]5 Service state is Disconnected");
183                    } else {
184                        console.info("[nfc_test]5 Service state is Connected");
185                    }
186                    console.info("[nfc_test]5 Service state is" + state);
187                });
188                sleep(1000);
189                expect(SEService instanceof Object).assertTrue();
190                let openESEisconnected = SEService.isConnected();
191                console.info("[NFC_test]5  SEService The connection status is:: " + openESEisconnected);
192                expect(openESEisconnected).assertTrue();
193                let nfcReadersList = SEService.getReaders();
194                if (nfcReadersList == undefined) {
195                    console.info("[NFC_test]5 This function is not supported because the phone NFC chip is ST chip.");
196                } else {
197                    expect(nfcReadersList instanceof Object).assertTrue();
198                    console.info("[nfc_test]5 Result of getReaders:" + nfcReadersList);
199                }
200            } catch (error) {
201                console.info("[NFC_test]5 getReaders occurs exception:" + error);
202                expect().assertFail();
203            }
204        })
205
206        console.info("*************[nfc_test] start nfc js unit test end*************");
207    })
208}