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 24async function getSEService() { 25 console.info("SecureElementDemo createService start getSEService"); 26 await secureElement.createService().then( (data) => { 27 console.info("start createService data is" + data); 28 Service = data; 29 console.info("start createService Service is" + Service); 30 let connect = Service.isConnected() 31 console.info("SecureElementDemo createService success = " + connect); 32 }) 33 .catch((err) => { 34 console.info("createService createService err = " + err); 35 }) 36 37} 38 39let Service = null; 40let getReader = null; 41 42export default function newSEServicetest() { 43 describe('newSEServicetest', function () { 44 beforeAll(async function (done) { 45 await secureElement.createService().then( (data) => { 46 console.info("SecureElementDemo newSEServicetest data =" + data); 47 Service = data; 48 let connect = Service.isConnected(); 49 console.info("SecureElementDemo newSEServicetest connect = " + connect); 50 return Service; 51 }) 52 .catch((err) => { 53 console.info("createService newSEServicetest err = " + err); 54 }) 55 done(); 56 console.info('beforeAll called'); 57 }) 58 59 beforeEach(function() { 60 console.info('beforeEach called'); 61 }) 62 63 afterEach(function () { 64 console.info('afterEach called'); 65 }) 66 67 afterAll(async function (done) { 68 nfcSEService.shutdown(); 69 sleep(900); 70 console.info('[nfc_test] afterAll newService shutdown success'); 71 done(); 72 }) 73 74 /** 75 * @tc.number SUB_Communication_NFC_secureElement_js_0600 76 * @tc.name Test isConnected 77 * @tc.desc Check whether the SE service is connected. 78 * @tc.type Function 79 * @tc.level Level 2 80 */ 81 it('SUB_Communication_NFC_secureElement_js_0600', 0, function () { 82 try { 83 let nfcisConnected = Service.isConnected(); 84 console.info("[NFC_test]6 SEService The connection status is: " + nfcisConnected); 85 expect(nfcisConnected).assertTrue(); 86 } catch (error) { 87 console.info("[NFC_test]6 exception occurred when checking the connection status.:" + error) 88 expect().assertFail(); 89 } 90 }) 91 92 /** 93 * @tc.number SUB_Communication_NFC_secureElement_js_0700 94 * @tc.name Test getVersion 95 * @tc.desc Returns the version number of the OMA specification. 96 * @tc.type Function 97 * @tc.level Level 2 98 */ 99 it('SUB_Communication_NFC_secureElement_js_0700', 0, function () { 100 try { 101 let getSEVersion = Service.getVersion(); 102 console.info("[NFC_test]7 OMA The version number is: " + getSEVersion); 103 expect(typeof (getSEVersion)).assertEqual('string'); 104 } catch (error) { 105 console.info("[NFC_test]7 Failed to obtain the version.:" + error); 106 expect().assertFail(); 107 } 108 }) 109 110 /** 111 * @tc.number SUB_Communication_NFC_secureElement_js_0800 112 * @tc.name Test getName 113 * @tc.desc Returns the name of this reader. 114 * @tc.type Function 115 * @tc.level Level 2 116 */ 117 it('SUB_Communication_NFC_secureElement_js_0800', 0, function () { 118 try { 119 let nfcOmaReaderList = Service.getReaders(); 120 console.info("[nfc_test]8 Result of getReaders:" + nfcOmaReaderList ); 121 if (nfcOmaReaderList == undefined) { 122 console.info("[NFC_test]8 This function is not supported because the phone NFC chip is ST chip."); 123 } else { 124 expect(nfcOmaReaderList instanceof Object).assertTrue(); 125 let nfcESEReader = nfcOmaReaderList[0]; 126 console.info("[nfc_test]8 getReaders results list 0 is" + nfcESEReader); 127 expect(nfcESEReader instanceof Object).assertTrue(); 128 let getNfcname = nfcESEReader.getName(); 129 console.info("[NFC_test]8 The reader name is: " + getNfcname); 130 expect(typeof (getNfcname)).assertEqual('string'); 131 } 132 } catch (error) { 133 console.info("[NFC_test]8 getName occurs exception:" + error); 134 expect().assertFail(); 135 } 136 }) 137 138 /** 139 * @tc.number SUB_Communication_NFC_secureElement_js_0900 140 * @tc.name Test isSecureElementPresent 141 * @tc.desc Indicates whether the corresponding secure element is available. 142 * @tc.type Function 143 * @tc.level Level 2 144 */ 145 it('SUB_Communication_NFC_secureElement_js_0900', 0, async function (done) { 146 try { 147 let nfcOmaReaderList2 = []; 148 getReader = Service.getReaders(); 149 console.info("[nfc_test]9 getReader value is: " + getReader); 150 let getReader00 = getReader[0]; 151 let getReader01 = getReader[1]; 152 console.info("[nfc_test]9 getReaders results list 0 is" + getReader00); 153 let getReader000 = getReader00.getName(); 154 let getReader001 = getReader01.getName(); 155 console.info("[nfc_test]9 The reader name is: " + getReader000 + "/" + getReader001); 156 try { 157 if ( getReader000 == 'eSE'){ 158 nfcOmaReaderList2 = getReader00; 159 }else { 160 console.info("[nfc_test]9 info"); 161 nfcOmaReaderList2 = getReader01; 162 } 163 164 } catch (error) { 165 console.info("[nfc_test]9 error " + error); 166 } 167 if (nfcOmaReaderList2 == undefined) { 168 console.info("[NFC_test]9 This function is not supported because the phone NFC chip is ST chip."); 169 } else { 170 console.info("[nfc_test]9 Result of getReaders:" + nfcOmaReaderList2); 171 expect(nfcOmaReaderList2 instanceof Object).assertTrue(); 172 let nfcESEReader2 = nfcOmaReaderList2; 173 console.info("[nfc_test]9 getReaders results list 0 is" + nfcESEReader2); 174 expect(nfcESEReader2 instanceof Object).assertTrue(); 175 let nfcSePresent = nfcESEReader2.isSecureElementPresent(); 176 console.info("[NFC_test]9 security unit check result is: " + nfcSePresent); 177 expect(nfcSePresent).assertTrue(); 178 let nfcOmaSession2 = nfcESEReader2.openSession(); 179 sleep(900) 180 console.info("[nfc_test]9 Open the second session result" + nfcOmaSession2); 181 expect(nfcOmaSession2 instanceof Object).assertTrue(); 182 let openSession2 = nfcOmaSession2.isClosed(); 183 console.info("[NFC_test]9 After close Check the second session is open: " + openSession2); 184 expect(openSession2).assertFalse(); 185 nfcESEReader2.closeSessions(); 186 sleep(900) 187 console.info("[NFC_test]9 close the Reader of SE successfully"); 188 let closeSession2 = nfcOmaSession2.isClosed(); 189 console.info("[NFC_test]9 After close Check the second session is open: " + closeSession2); 190 expect(closeSession2).assertTrue(); 191 } 192 } catch (error) { 193 console.info("[NFC_test]9 isSecureElementPresent occurs exception:" + error); 194 expect().assertFail(); 195 } 196 done(); 197 }) 198 199 /** 200 * @tc.number SUB_Communication_NFC_secureElement_js_1000 201 * @tc.name Test shutdown 202 * @tc.desc Release all SE resources allocated to the service. 203 * @tc.type Function 204 * @tc.level Level 2 205 */ 206 it('SUB_Communication_NFC_secureElement_js_1000', 0, async function (done) { 207 try { 208 Service.shutdown(); 209 console.info('[NFC_test]10 eseshutdown pass'); 210 let downESEisconnected = Service.isConnected(); 211 console.info("[NFC_test]10 shutdown the SE SEService The connection status is: " + downESEisconnected); 212 expect(downESEisconnected).assertFalse(); 213 sleep(900); 214 await getSEService().then(async (data) => { 215 Service = data; 216 let seIsConnected = Service.isConnected(); 217 console.info("[NFC_test] SEService isConnected The connection status is: " + seIsConnected); 218 if (seIsConnected) { 219 getReader = Service.getReaders(); 220 if (getReader == undefined) { 221 console.info("[NFC_test]10 This function is not supported because the phone NFC chip is ST chip."); 222 } else { 223 expect(getReader instanceof Object).assertTrue(); 224 console.info("[nfc_test]10 Result of getReaders:" + getReader); 225 } 226 } 227 }).catch((err) =>{ 228 console.info("[NFC_test] getSEService err.code " + err.code + "err.message " + err.message); 229 }) 230 } catch (error) { 231 console.info("[NFC_test]10 getReaders occurs exception:" + error); 232 expect().assertFail(); 233 } 234 done(); 235 }) 236 237 console.info("*************[nfc_test] start nfc js unit test end*************"); 238 }) 239}