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) { 21 return new Promise(resovle => setTimeout(resovle, delay)) 22} 23 24let Service = null; 25let Reader = null; 26let Session = null; 27let getReader = null; 28let nfcSEService = null; 29let readerIsPresent = null; 30 31async function getSEService() { 32 return new Promise((resolve, reject) => { 33 Service = secureElement.newSEService("serviceState", (state) => { 34 if (state == secureElement.ServiceState.DISCONNECTED) { 35 console.info("[nfc_test] getSEService newService state is Disconnected"); 36 let err = null; 37 err.code = -1; 38 err.message = "newSEService failed, service is not connected" 39 reject(err); 40 } else { 41 console.info("[nfc_test] getSEService newService state is Connected"); 42 resolve(Service); 43 } 44 }); 45 }); 46} 47 48export default function openSessionTest() { 49 describe('openSessionTest', function () { 50 beforeAll(async function (done) { 51 await getSEService().then(async (data) => { 52 Service = data; 53 let seIsConnected = Service.isConnected(); 54 console.info("[NFC_test] SEService isConnected The connection status is: " + seIsConnected); 55 if (seIsConnected) { 56 getReader = Service.getReaders(); 57 console.info("openSessionTest getReader value is: " + getReader); 58 let getReader00 = getReader[0]; 59 let getReader01 = getReader[1]; 60 console.info("openSessionTest getReaders results list 0 is" + getReader00); 61 let getReader000 = getReader00.getName(); 62 let getReader001 = getReader01.getName(); 63 console.info("openSessionTest getReader name is: " + getReader000 + "/" + getReader001); 64 try { 65 if (getReader000 == 'eSE'){ 66 readerIsPresent = getReader00.isSecureElementPresent(); 67 console.info("openSessionTest getReader00 readerIsPresent " + readerIsPresent); 68 Reader = getReader00; 69 }else { 70 readerIsPresent = getReader01.isSecureElementPresent(); 71 console.info("openSessionTest getReader01 readerIsPresent " + readerIsPresent); 72 Reader = getReader01; 73 } 74 75 } catch (error) { 76 console.info("openSessionTest this is error " + error); 77 } 78 console.info("[NFC_test] Reader isConnected The connection status is: " + readerIsPresent); 79 if (readerIsPresent) { 80 Session = Reader.openSession(); 81 let sessionIsClosed = Session.isClosed(); 82 console.info("[NFC_test] Session isConnected The connection status is: " + sessionIsClosed); 83 } 84 } 85 }).catch((err) =>{ 86 console.info("[NFC_test] getSEService err.code " + err.code + "err.message " + err.message); 87 }) 88 done(); 89 }) 90 beforeEach(function() { 91 console.info('beforeEach called'); 92 }) 93 afterEach(function () { 94 console.info('afterEach called'); 95 }) 96 afterAll(async function (done) { 97 nfcSEService.shutdown(); 98 sleep(900); 99 console.info('[nfc_test] afterAll Se_session shutdown success'); 100 done(); 101 }) 102 103 /** 104 * @tc.number SUB_Communication_NFC_secureElement_js_0600 105 * @tc.name Test getReader 106 * @tc.desc Obtains the reader that provides the session. 107 * @tc.type Function 108 * @tc.level Level 2 109 */ 110 it('SUB_Communication_NFC_secureElement_js_0600', 0, function () { 111 try { 112 if (getReader == undefined) { 113 console.info("[NFC_test]6 This function is not supported because the phone NFC chip is ST chip."); 114 } else { 115 let getNfcreader = Session.getReader(); 116 console.info("[NFC_test]6 Reader result of this session: " + getNfcreader); 117 expect(getNfcreader instanceof Object).assertTrue(); 118 } 119 } catch (error) { 120 console.info("[NFC_test]6 The reader result of the session is abnormal:" + error); 121 expect().assertFail(); 122 } 123 }) 124 125 /** 126 * @tc.number SUB_Communication_NFC_secureElement_js_0700 127 * @tc.name Test getATR 128 * @tc.desc Obtain the ATR of the SE. 129 * @tc.type Function 130 * @tc.level Level 2 131 */ 132 it('SUB_Communication_NFC_secureElement_js_0700', 0, function () { 133 try { 134 if (getReader == undefined) { 135 console.info("[NFC_test]7 This function is not supported because the phone NFC chip is ST chip."); 136 } else { 137 let nfcGetart = Session.getATR(); 138 expect(nfcGetart).assertInstanceOf('Array'); 139 expect(!nfcGetart == false ).assertTrue(); 140 console.info("[NFC_test]7 Get the ATR of this SE: " + nfcGetart); 141 } 142 } catch (error) { 143 console.info("[NFC_test]7 Get the ATR of this SE occurs exception:" + error); 144 expect().assertFail(); 145 } 146 }) 147 148 /** 149 * @tc.number SUB_Communication_NFC_secureElement_js_0800 150 * @tc.name Test close Session 151 * @tc.desc Close a single session. 152 * @tc.type Function 153 * @tc.level Level 2 154 */ 155 it('SUB_Communication_NFC_secureElement_js_0800', 0, async function (done) { 156 try { 157 if (getReader == undefined) { 158 console.info("[NFC_test]8 This function is not supported because the phone NFC chip is ST chip."); 159 } else { 160 let isopenSession = Session.isClosed(); 161 console.info("[NFC_test]8 Check the first one session is open: " + isopenSession); 162 expect(isopenSession).assertFalse(); 163 Session.close(); 164 sleep(900) 165 console.info("[NFC_test]8 second session is closed successfully"); 166 let iscloseSession = Session.isClosed(); 167 console.info("[NFC_test]8 After close Check the first one session is open: " + iscloseSession); 168 expect(iscloseSession).assertTrue(); 169 } 170 } catch (error) { 171 console.info("[NFC_test]8 0800 occurs exception:" + error); 172 expect().assertFail(); 173 } 174 done(); 175 }) 176 177 console.info("*************[nfc_test] start nfc js unit test end*************"); 178 }) 179}