• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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    return new Promise((resolve, reject) => {
26        Service = secureElement.newSEService("serviceState", (state) => {
27            if (state == secureElement.ServiceState.DISCONNECTED) {
28                console.info("[nfc_test] getSEService newService state is Disconnected");
29                let err = null;
30                err.code = -1;
31                err.message = "newSEService failed, service is not connected"
32                reject(err);
33            } else {
34                console.info("[nfc_test] getSEService newService state is Connected");
35                resolve(Service);
36            }
37        });
38    });
39}
40
41let Service = null;
42let Reader = null;
43let Session = null;
44let getReader = null;
45let nfcSEService = null;
46let aidArray = [160, 0, 0, 1, 81, 0, 0, 0];
47let p2 = 0x00;
48let command = [128, 202, 159, 127, 0];
49let readerIsPresent = null;
50
51export default function channelCallbacktest() {
52    describe('channelCallbacktest', function () {
53        beforeAll(async function (done) {
54            await getSEService().then(async (data) => {
55                Service = data;
56                let seIsConnected = Service.isConnected();
57                console.info("[NFC_test] Logical SEService isConnected The connection status is: " + seIsConnected);
58                if (seIsConnected) {
59                            getReader = Service.getReaders();
60                            console.info("channelCallbacktest getReader value  is: " + getReader);
61                            let getReader00 = getReader[0];
62                            let getReader01 = getReader[1];
63                            console.info("channelCallbacktest getReaders results list 0 is" + getReader00);
64                            let getReader000 = getReader00.getName();
65                            let getReader001 = getReader01.getName();
66                            console.info("channelCallbacktest getReader name is: " + getReader000 + "/" + getReader001);
67                    try {
68                        if (getReader000 == 'eSE'){
69                             readerIsPresent = getReader00.isSecureElementPresent();
70                             console.info("channelCallbacktest getReader00 readerIsPresent " + readerIsPresent);
71                             Reader = getReader00;
72                   }else {
73                             readerIsPresent = getReader01.isSecureElementPresent();
74                             console.info("channelCallbacktest getReader01 readerIsPresent " + readerIsPresent);
75                             Reader = getReader01;
76                   }
77
78                    } catch (error) {
79                             console.info("channelCallbacktest this is error " + error);
80                    }
81                    console.info("[NFC_test] Logical Reader isConnected The connection status is: " + readerIsPresent);
82                    if (readerIsPresent) {
83                        Session = Reader.openSession();
84                        let sessionIsClosed = Session.isClosed();
85                        console.info("[NFC_test] Logical Session isConnected The connection status is: " + sessionIsClosed);
86                    }
87                }
88            }).catch((err) =>{
89                console.info("[NFC_test] getSEService err.code " + err.code + "err.message " + err.message);
90            })
91            done();
92            console.info('beforeAll called');
93        })
94
95        beforeEach(function() {
96            console.info('beforeEach called');
97        })
98
99        afterEach(function () {
100
101            console.info('afterEach success');
102        })
103
104        afterAll(async function (done) {
105            nfcSEService.shutdown();
106            sleep(900);
107            console.info('[nfc_test] afterAll channelcallback shutdown success');
108            done();
109        })
110
111       /**
112         * @tc.number SUB_Communication_Ese_LogicalChannel_js_0900
113         * @tc.name Test isBasicChannel
114         * @tc.desc open BasicChannel Check whether the channel is a basic channel.
115         * @tc.type Function
116         * @tc.level Level 2
117         */
118        it('SUB_Communication_Ese_LogicalChannel_js_0900', 0, async function (done) {
119            try {
120                if (getReader == undefined) {
121                    console.info("[NFC_test]09 This function is not supported because the phone NFC chip is ST chip.");
122                } else {
123                    function P2BasicChannel_callback(){
124                        return new Promise((resolve, reject) => {
125                            Session.openBasicChannel(aidArray, p2,
126                                (err, result) => {
127                                    if(err) {
128                                        console.info("[NFC_test]09 openBasicChannel data == null" + err.code + "---" + err);
129                                        reject(err.code);
130                                    } else {
131                                        console.info("[NFC_test]09 openBasicChannel data != null " + result);
132                                        let P2BasicChannel = result;
133                                        let isBasic = P2BasicChannel.isBasicChannel();
134                                        console.info("[NFC_test]09 Check whether the channel is a basic channel: " + isBasic);
135                                        expect(isBasic).assertTrue();
136                                        P2BasicChannel.close();
137                                        console.info("[NFC_test]09 Data received by the application select command: " );
138                                        resolve();
139                                    }
140                                });
141                        });
142                    }
143                    await P2BasicChannel_callback().then((data) => {
144                        console.info("[NFC_test]09 openBasicChannel done");
145                    })
146                    .catch(e => {
147                        console.info("[NFC_test]09 openBasicChannel failed" + e);
148                        expect(3300103).assertEqual(e);
149                    })
150                }
151            } catch (error) {
152                console.info("[NFC_test]09 openBasicChannel_p2_callback occurs exception:" + error.code + "---" + error);
153                expect().assertFail();
154            }
155            sleep(900);
156            done();
157        })
158
159        /**
160         * @tc.number SUB_Communication_Ese_LogicalChannel_js_1000
161         * @tc.name Test isClosed
162         * @tc.desc open LogicalChannel Check whether the channel is closed.
163         * @tc.type Function
164         * @tc.level Level 2
165         */
166        it('SUB_Communication_Ese_LogicalChannel_js_1000', 0, async function (done) {
167            try {
168                if (getReader == undefined) {
169                    console.info("[NFC_test]10 This function is not supported because the phone NFC chip is ST chip.");
170                } else {
171                    function LogicalChannel_callback(){
172                        return new Promise((resolve, reject) => {
173                            Session.openLogicalChannel(aidArray, p2,
174                                (err, result) => {
175                                    if(err) {
176                                        console.info("[NFC_test]10 openLogicalChannel data == null" + err);
177                                        reject(err.code);
178                                    } else {
179                                        console.info("[NFC_test]10 openLogicalChannel data == null" + result);
180                                        let P2LogicalChannel = result;
181                                        P2LogicalChannel.close();
182                                        console.info("[NFC_test]10 Data received by the application select command: " );
183                                        let isChannelClosed = P2LogicalChannel.isClosed();
184                                        console.info("[NFC_test]10 Check whether the channel is closed: " + isChannelClosed);
185                                        expect(isChannelClosed).assertTrue();
186                                    }
187                                    resolve();
188                                });
189                        });
190                    }
191                    await LogicalChannel_callback().then((data) => {
192                        console.info("[NFC_test]10 openBasicChannel done");
193                    })
194                    .catch(e => {
195                        console.info("[NFC_test]10 openBasicChannel failed" + e);
196                        expect(3300103).assertEqual(e);
197                    })
198                }
199            } catch (error) {
200                console.info("[NFC_test]10 openLogicalChannel_P2_callback occurs exception:" + error.code + "---" + error);
201                expect().assertFail();
202            }
203            sleep(900);
204            done();
205        })
206
207
208        /**
209         * @tc.number SUB_Communication_Ese_LogicalChannel_js_1100
210         * @tc.name Test getSelectResponse
211         * @tc.desc open BasicChannel Returns the data received from the application selection command.
212         * @tc.type Function
213         * @tc.level Level 2
214         */
215        it('SUB_Communication_Ese_LogicalChannel_js_1100', 0, async function (done) {
216            try {
217                if (getReader == undefined) {
218                    console.info("[NFC_test]11 This function is not supported because the phone NFC chip is ST chip.");
219                } else {
220                    function BasicChannel_callback(){
221                        return new Promise((resolve, reject) => {
222                            Session.openBasicChannel(aidArray,
223                                (err, result) => {
224                                    if(err) {
225                                        console.info("[NFC_test]11 to get openBasicChannel:" + JSON.stringify(err));
226                                        reject(err.code);
227                                    } else {
228                                        console.info("[NFC_test]11 openBasicChannel data != null " + result);
229                                        let nfcBasicChannel = result;
230                                        let getResponse = nfcBasicChannel.getSelectResponse();
231                                        console.info("[NFC_test]11 getSelectResponse data: " + getResponse);
232                                        nfcBasicChannel.close();
233                                        console.info("[NFC_test]11 Data received by the application select command: " );
234                                        expect(getResponse).assertInstanceOf('Array');
235                                    }
236                                    resolve();
237                                });
238                        });
239                    }
240                    await BasicChannel_callback().then((data) => {
241                        console.info("[NFC_test]11 openBasicChannel done");
242                    })
243                    .catch(e => {
244                        console.info("[NFC_test]11 openBasicChannel failed" + e);
245                        expect(3300103).assertEqual(e);
246                    })
247            }
248            } catch (error) {
249                console.info("[NFC_test]11 openBasicChannel_callback occurs exception:" + error.code + "---" + error);
250                expect().assertFail();
251            }
252            sleep(900);
253            done();
254        })
255
256        /**
257         * @tc.number SUB_Communication_Ese_LogicalChannel_js_1200
258         * @tc.name Test transmit
259         * @tc.desc open LogicalChannel Send an APDU command to the SE
260         * @tc.type Function
261         * @tc.level Level 2
262         */
263        it('SUB_Communication_Ese_LogicalChannel_js_1200', 0, async function (done) {
264            try {
265                if (getReader == undefined) {
266                    console.info("[NFC_test]12 This function is not supported because the phone NFC chip is ST chip.");
267                } else {
268                    function LogicalChannel_callback(){
269                        return new Promise((resolve, reject) => {
270                            Session.openLogicalChannel(aidArray,
271                                (err, result) => {
272                                    if(err) {
273                                        console.info("[NFC_test]12 failed to get openLogicalChannel:" + JSON.stringify(err));
274                                        reject(err.code);
275                                    } else {
276                                        console.info("[NFC_test]12 openLogicalChannel data != null " + result);
277                                        let nfcLogicalChannel = result;
278                                        nfcLogicalChannel.transmit(command, (err, data) => {
279                                            if (err) {
280                                                console.info("[NFC_test]12 Send error an APDU command to the SE: " + err);
281                                                expect().assertFail();
282                                            } else {
283                                                console.info("[NFC_test]12 Send an APDU command to the SE: " + data);
284                                                expect(data).assertInstanceOf('Array');
285                                            }
286                                        });
287                                        sleep(900);
288                                        nfcLogicalChannel.close();
289                                        console.info("[NFC_test]12 Data received by the application select command: " );
290                                    }
291                                    resolve();
292                                });
293                        });
294                    }
295                    await LogicalChannel_callback().then((data) => {
296                        console.info("[NFC_test]12 openBasicChannel done");
297                    })
298                    .catch(e => {
299                        console.info("[NFC_test]12 openBasicChannel failed" + e);
300                        expect(3300103).assertEqual(e);
301                    })
302                }
303            } catch (error) {
304                console.info("[NFC_test]12 openLogicalChannel_callback occurs exception:" + error.code + "---" + error);
305                expect().assertFail();
306            }
307            sleep(900);
308            done();
309        })
310
311       /**
312         * @tc.number SUB_Communication_Ese_LogicalChannel_js_1300
313         * @tc.name Test transmit
314         * @tc.desc open LogicalChannel Send an APDU command to the SE
315         * @tc.type Function
316         * @tc.level Level 2
317         */
318        it('SUB_Communication_Ese_LogicalChannel_js_1300', 0, async function (done) {
319            try {
320                if (getReader == undefined) {
321                    console.info("[NFC_test]13 This function is not supported because the phone NFC chip is ST chip.");
322                } else {
323                    let getPromise = Session.openBasicChannel(aidArray);
324                    await getPromise.then((data) => {
325                        console.info("[NFC_test]13 openBasicChannel openBasicChannel data: " + data);
326                        if ( data != undefined && data != null){
327                            console.info("[NFC_test]13 openBasicChannel data != null");
328                            let OmaBasicChannel = data;
329                            OmaBasicChannel.close();
330                            console.info("[NFC_test]13 Data received by the application select command: " );
331                        }
332                        else {
333                            console.info("[NFC_test]13 openBasicChannel data == null");
334                        }
335                    }).catch((error)=> {
336                        console.info("[NFC_test]13 openBasicChannel catch error" + error);
337                        expect(3300103).assertEqual(error.code)
338                    });
339                    sleep(5000);
340                }
341            } catch (error) {
342                console.info("[NFC_test]13 openBasicChannel_Promise occurs exception:" + error.code + "---" + error);
343                expect().assertFail();
344            }
345            done();
346        })
347
348        /**
349         * @tc.number SUB_Communication_Ese_LogicalChannel_js_1400
350         * @tc.name Test isClosed
351         * @tc.desc open LogicalChannel Check whether the channel is a basic channel.
352         * @tc.type Function
353         * @tc.level Level 2
354         */
355        it('SUB_Communication_Ese_LogicalChannel_js_1400', 0, async function (done) {
356            try {
357                if (getReader == undefined) {
358                    console.info("[NFC_test]14 This function is not supported because the phone NFC chip is ST chip.");
359                } else {
360                    let getPromise = Session.openLogicalChannel(aidArray);
361                    await getPromise.then((data) => {
362                        console.info("[NFC_test]14  openLogicalChannel data: " + data);
363                        if ( data != undefined && data != null){
364                            console.info("[NFC_test]14 openLogicalChannel data != null");
365                            let OmaLogicalChannel = data;
366                            OmaLogicalChannel.close();
367                            let isChannelClosed = OmaLogicalChannel.isClosed();
368                            console.info("[NFC_test]14 Check whether the channel is disabled: " + isChannelClosed);
369                            expect(isChannelClosed).assertTrue();
370                        }
371                        else {
372                            console.info("[NFC_test]14 openLogicalChannel data == null");
373                        }
374                    }).catch((error)=> {
375                        console.info("[NFC_test]14 openLogicalChannel catch error" + error.code + "---" + error);
376                        expect(3300103).assertEqual(error.code)
377                    });
378                    sleep(5000);
379                }
380            } catch (error) {
381                console.info("[NFC_test]14 openLogicalChannel_Promise occurs exception:" + error);
382                expect().assertFail();
383            }
384            done();
385        })
386
387        /**
388         * @tc.number SUB_Communication_Ese_LogicalChannel_js_1500
389         * @tc.name Test isClosed
390         * @tc.desc open BasicChannel Check whether the channel is a basic channel.
391         * @tc.type Function
392         * @tc.level Level 2
393         */
394        it('SUB_Communication_Ese_LogicalChannel_js_1500', 0, async function (done) {
395            try {
396                if (getReader == undefined) {
397                    console.info("[NFC_test]15 This function is not supported because the phone NFC chip is ST chip.");
398                } else {
399                    let getPromise = Session.openBasicChannel(aidArray,p2);
400                    await getPromise.then((data) => {
401                        console.info("[NFC_test]15  openBasicChannel data: " + data);
402                        if ( data != undefined && data != null){
403                            console.info("[NFC_test]15 openBasicChannel data != null");
404                            let OmaLogicalChannel = data;
405                            OmaLogicalChannel.close();
406                            let isChannelClosed = OmaLogicalChannel.isClosed();
407                            console.info("[NFC_test]15 Check whether the channel is disabled: " + isChannelClosed);
408                            expect(isChannelClosed).assertTrue();
409                        }
410                        else {
411                            console.info("[NFC_test]15 openBasicChannel data == null");
412                        }
413                    }).catch((error)=> {
414                        console.info("[NFC_test]15 openBasicChannel catch error" + error.code + "---" + error);
415                        expect(3300103).assertEqual(error.code)
416                    });
417                    sleep(5000);
418                }
419            } catch (error) {
420                console.info("[NFC_test]15 openBasicChannel occurs exception:" + error);
421                expect().assertFail();
422            }
423            done();
424        })
425
426        /**
427         * @tc.number SUB_Communication_Ese_LogicalChannel_js_1600
428         * @tc.name Test isClosed
429         * @tc.desc Check whether the channel is closed.
430         * @tc.type Function
431         * @tc.level Level 2
432         */
433        it('SUB_Communication_Ese_LogicalChannel_js_1600', 0, async function (done) {
434            try {
435                if (getReader == undefined) {
436                    console.info("[NFC_test]16 This function is not supported because the phone NFC chip is ST chip.");
437                } else {
438                    let getPromise = Session.openLogicalChannel(aidArray,p2);
439                    await getPromise.then((data) => {
440                        console.info("[NFC_test]16  openLogicalChannel data: " + data);
441                        if ( data != undefined && data != null){
442                            console.info("[NFC_test]16 openLogicalChannel data != null");
443                            let OmaLogicalChannel = data;
444                            OmaLogicalChannel.close();
445                            let isChannelClosed = OmaLogicalChannel.isClosed();
446                            console.info("[NFC_test]16 Check whether the channel is disabled: " + isChannelClosed);
447                            expect(isChannelClosed).assertTrue();
448                        }
449                        else {
450                            console.info("[NFC_test]16 openLogicalChannel data == null");
451                        }
452                    }).catch((error)=> {
453                        console.info("[NFC_test]16 openLogicalChannel catch error" + error.code + "---" + error);
454                        expect(3300103).assertEqual(error.code)
455                    });
456                    sleep(5000);
457                }
458            } catch (error) {
459                console.info("[NFC_test]16 openLogicalChannel_Promise occurs exception:" + error);
460                expect().assertFail();
461            }
462            done();
463        })
464
465        console.info("*************[nfc_test] start nfc js unit test end*************");
466    })
467}