From fe886dc88fbe578dcd19a581afe85a923a74124e Mon Sep 17 00:00:00 2001 From: tomkp Date: Sun, 28 Dec 2025 17:05:12 +0000 Subject: [PATCH] Add tests for readRecord(), getData(), and card getter - Add test for readRecord() verifying correct APDU structure - Add test for getData() verifying correct APDU structure - Add test for card getter Increases iso7816-application.ts coverage from 86% to 100%. Fixes #38 --- src/iso7816-application.test.ts | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/iso7816-application.test.ts b/src/iso7816-application.test.ts index 57e8fd3..c9cc7e7 100644 --- a/src/iso7816-application.test.ts +++ b/src/iso7816-application.test.ts @@ -95,4 +95,60 @@ describe('Iso7816', () => { expect(mockCard.transmit).toHaveBeenCalledTimes(2); }); }); + + describe('readRecord()', () => { + it('should send correct APDU for reading a record', async () => { + const mockCard: Card = { + atr: Buffer.from([0x3b, 0x00]), + transmit: vi.fn((buffer: Buffer) => { + expect(buffer[0]).toBe(0x00); // CLA + expect(buffer[1]).toBe(0xb2); // INS (READ RECORD) + expect(buffer[2]).toBe(0x01); // P1 (record number) + expect(buffer[3]).toBe(0x14); // P2 (SFI 2 << 3) + 4 = 20 = 0x14 + return Promise.resolve(Buffer.from([0x01, 0x02, 0x03, 0x90, 0x00])); + }), + }; + + const app = createIso7816(mockCard); + const response = await app.readRecord(2, 1); + + expect(response.isOk()).toBe(true); + expect(mockCard.transmit).toHaveBeenCalledTimes(1); + }); + }); + + describe('getData()', () => { + it('should send correct APDU for getting data', async () => { + const mockCard: Card = { + atr: Buffer.from([0x3b, 0x00]), + transmit: vi.fn((buffer: Buffer) => { + expect(buffer[0]).toBe(0x00); // CLA + expect(buffer[1]).toBe(0xca); // INS (GET DATA) + expect(buffer[2]).toBe(0x9f); // P1 + expect(buffer[3]).toBe(0x36); // P2 + return Promise.resolve(Buffer.from([0x9f, 0x36, 0x02, 0x00, 0x60, 0x90, 0x00])); + }), + }; + + const app = createIso7816(mockCard); + const response = await app.getData(0x9f, 0x36); + + expect(response.isOk()).toBe(true); + expect(mockCard.transmit).toHaveBeenCalledTimes(1); + }); + }); + + describe('card getter', () => { + it('should return the underlying card', () => { + const mockCard: Card = { + atr: Buffer.from([0x3b, 0x00]), + transmit: vi.fn(), + }; + + const app = createIso7816(mockCard); + + expect(app.card).toBe(mockCard); + expect(app.card.atr).toEqual(Buffer.from([0x3b, 0x00])); + }); + }); });