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, Level} 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 for (let i = 0; i < getReader.length; i++) { 59 if (!getReader[i].isSecureElementPresent()) { 60 console.info("[NFC_test] se not present : " + i); 61 continue; 62 } 63 if (getReader[i].getName() != "eSE") { 64 console.info("[NFC_test] se reader not ese."); 65 continue; 66 } 67 Reader = getReader[i]; 68 console.info("[NFC_test] newSEService openSessionTest getReader name is: " + Reader.getName()); 69 let readerIsPresent = Reader.isSecureElementPresent(); 70 console.info("[NFC_test] isSecureElementPresent status is: " + readerIsPresent); 71 if (readerIsPresent) { 72 Session = Reader.openSession(); 73 let sessionIsClosed = Session.isClosed(); 74 console.info("[NFC_test] Session isConnected The connection status is: " + sessionIsClosed); 75 } 76 } 77 } 78 }).catch((err) =>{ 79 console.info("[NFC_test] getSEService err.code " + err.code + "err.message " + err.message); 80 }) 81 done(); 82 }) 83 beforeEach(function() { 84 console.info('beforeEach called'); 85 }) 86 afterEach(function () { 87 console.info('afterEach called'); 88 }) 89 afterAll(async function (done) { 90 nfcSEService.shutdown(); 91 sleep(900); 92 console.info('[nfc_test] afterAll Se_session shutdown success'); 93 done(); 94 }) 95 96 /** 97 * @tc.number SUB_Communication_NFC_secureElement_js_0600 98 * @tc.name Test getReader 99 * @tc.desc Obtains the reader that provides the session. 100 * @tc.type Function 101 * @tc.level Level 0 102 */ 103 it('SUB_Communication_NFC_secureElement_js_0600', Level.LEVEL0, function () { 104 try { 105 if (getReader == undefined) { 106 console.info("[NFC_test]6 This function is not supported because the phone NFC chip is ST chip."); 107 } else { 108 let getNfcreader = Session.getReader(); 109 console.info("[NFC_test]6 Reader result of this session: " + getNfcreader); 110 expect(getNfcreader instanceof Object).assertTrue(); 111 } 112 } catch (error) { 113 console.info("[NFC_test]6 The reader result of the session is abnormal:" + error); 114 if (error.code == 801){ 115 console.info("[NFC_test]6 getReader is not support"); 116 except(true).assertTrue(); 117 }else{ 118 expect().assertFail(); 119 } 120 } 121 }) 122 123 /** 124 * @tc.number SUB_Communication_NFC_secureElement_js_0700 125 * @tc.name Test getATR 126 * @tc.desc Obtain the ATR of the SE. 127 * @tc.type Function 128 * @tc.level Level 0 129 */ 130 it('SUB_Communication_NFC_secureElement_js_0700', Level.LEVEL0, function () { 131 try { 132 if (getReader == undefined) { 133 console.info("[NFC_test]7 This function is not supported because the phone NFC chip is ST chip."); 134 } else { 135 let nfcGetart = Session.getATR(); 136 expect(nfcGetart).assertInstanceOf('Array'); 137 expect(!nfcGetart == false ).assertTrue(); 138 console.info("[NFC_test]7 Get the ATR of this SE: " + nfcGetart); 139 } 140 } catch (error) { 141 console.info("[NFC_test]7 Get the ATR of this SE occurs exception:" + error + "/" + error.code); 142 expect(801).assertEqual(error.code); 143 } 144 }) 145 146 /** 147 * @tc.number SUB_Communication_NFC_secureElement_js_0800 148 * @tc.name Test close Session 149 * @tc.desc Close a single session. 150 * @tc.type Function 151 * @tc.level Level 0 152 */ 153 it('SUB_Communication_NFC_secureElement_js_0800', Level.LEVEL0, async function (done) { 154 try { 155 if (getReader == undefined) { 156 console.info("[NFC_test]8 This function is not supported because the phone NFC chip is ST chip."); 157 } else { 158 let isopenSession = Session.isClosed(); 159 console.info("[NFC_test]8 Check the first one session is open: " + isopenSession); 160 expect(isopenSession).assertFalse(); 161 Session.close(); 162 sleep(900) 163 console.info("[NFC_test]8 second session is closed successfully"); 164 let iscloseSession = Session.isClosed(); 165 console.info("[NFC_test]8 After close Check the first one session is open: " + iscloseSession); 166 expect(iscloseSession).assertTrue(); 167 } 168 } catch (error) { 169 console.info("[NFC_test]8 0800 occurs exception:" + error + "/" + error.code); 170 expect(801).assertEqual(error.code); 171 } 172 done(); 173 }) 174 175 console.info("*************[nfc_test] start nfc js unit test end*************"); 176 }) 177}