Skip to content

Commit

Permalink
Merge 33a6136 into b870b1d
Browse files Browse the repository at this point in the history
  • Loading branch information
justusjonas74 committed Feb 21, 2024
2 parents b870b1d + 33a6136 commit 7a73a53
Show file tree
Hide file tree
Showing 23 changed files with 241 additions and 478 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

name: Node.js CI

on:
push:
branches: ['master']
pull_request:
branches: ['master']
on: [push, pull_request]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"semi": true,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 80,
"printWidth": 120,
"overrides": [
{
"files": ["*.ts", "*.mts"],
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ const options = {
};

uic.readBarcode(file_path, options).then((ticket) => {
console.log(ticket.isSignatureValid); // Returns 'true' or 'false'.
console.log(ticket.validityOfSignature); // Returns "VALID", "INVALID" or "Public Key not found"
// ticket.isSignatureValid is deprecated. Use validityOfSignature instead.
});
//
```
Expand Down
14 changes: 4 additions & 10 deletions __tests__/barcode-dataTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@ describe('barcode-data', () => {
expect(interpretBarcode(ticket).version).toBe(2);
});
test('should return an empty array if input param is an empty buffer.', () => {
expect(
Array.isArray(interpretBarcode(Buffer.from('')).ticketContainers)
).toBe(true);
expect(interpretBarcode(Buffer.from('')).ticketContainers).toHaveLength(
0
); // eslint-disable-line no-unused-expressions
expect(Array.isArray(interpretBarcode(Buffer.from('')).ticketContainers)).toBe(true);
expect(interpretBarcode(Buffer.from('')).ticketContainers).toHaveLength(0); // eslint-disable-line no-unused-expressions
});

describe('on unknown data fields', () => {
let results: TicketDataContainer[];
beforeEach((done) => {
const ticket = dummyTicket('MYID!!', '01', 'Test');
results = interpretBarcode(ticket)
.ticketContainers as TicketDataContainer[];
results = interpretBarcode(ticket).ticketContainers as TicketDataContainer[];
done();
});
test('should ignore unkown data fields', () => {
Expand All @@ -53,8 +48,7 @@ describe('barcode-data', () => {
let results: TicketDataContainer[];
beforeEach((done) => {
const ticket = dummyTicket('U_HEAD', '03', 'Test');
results = interpretBarcode(ticket)
.ticketContainers as TicketDataContainer[];
results = interpretBarcode(ticket).ticketContainers as TicketDataContainer[];
done();
});
test('should ignore unkown versions of data fields', () => {
Expand Down
65 changes: 23 additions & 42 deletions __tests__/block-typesTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,26 @@ describe('block-types.js', () => {
describe('STRING', () => {
test('should return an String from a Buffer', () => {
// const dataTypeArr = bt[0].versions['01'][0]
const dataTypeArr = bt.find(
(container) => container.name == 'U_HEAD' && container.version == '01'
)?.dataFields[0];
const dataTypeArr = bt.find((container) => container.name == 'U_HEAD' && container.version == '01')
?.dataFields[0];
const res = dataTypeArr?.interpreterFn;
const testValue = 'GAUF';
expect(res!(Buffer.from(testValue))).toBe(testValue);
});
});
describe('HEX', () => {
test('should return a hexadecimal encoded string representation from a Buffer', () => {
const dataTypeArr = bt.find(
(container) => container.name == 'U_HEAD' && container.version == '01'
)?.dataFields[2];
const dataTypeArr = bt.find((container) => container.name == 'U_HEAD' && container.version == '01')
?.dataFields[2];
const res = dataTypeArr?.interpreterFn;
const testValue = '0123456789abcdef';
expect(res!(Buffer.from(testValue, 'hex'))).toBe(testValue);
});
});
describe('STR_INT', () => {
test('should return a number from a Buffer encoded string', () => {
const dataTypeArr = bt.find(
(container) => container.name == 'U_TLAY' && container.version == '01'
)?.dataFields[1];
const dataTypeArr = bt.find((container) => container.name == 'U_TLAY' && container.version == '01')
?.dataFields[1];
const res = dataTypeArr?.interpreterFn;
const testValue = 1234;
const testValueBuf = Buffer.from(testValue.toString(10));
Expand All @@ -52,9 +49,8 @@ describe('block-types.js', () => {
});
describe('DB_DATETIME', () => {
test('should return a Date from a Buffer encoded string', () => {
const dataTypeArr = bt.find(
(container) => container.name == 'U_HEAD' && container.version == '01'
)?.dataFields[3];
const dataTypeArr = bt.find((container) => container.name == 'U_HEAD' && container.version == '01')
?.dataFields[3];
const res = dataTypeArr?.interpreterFn;
const str = '130419871215';
const dummyDateBuf = Buffer.from(str);
Expand All @@ -69,17 +65,10 @@ describe('block-types.js', () => {
let res2: IEFS_DATA;
let resDc10: IEFS_DATA;
beforeAll((done) => {
const fn = bt.find(
(container) => container.name == '0080VU' && container.version == '01'
)?.dataFields[4].interpreterFn;
const testBuf = Buffer.from(
'130791f0187407d018763821000138221800000000130791f008dc060d18767a131c',
'hex'
);
const testBufDc10 = Buffer.from(
'130791f0187407d018763821000138221800000000130791f008dc061018767a131c',
'hex'
);
const fn = bt.find((container) => container.name == '0080VU' && container.version == '01')?.dataFields[4]
.interpreterFn;
const testBuf = Buffer.from('130791f0187407d018763821000138221800000000130791f008dc060d18767a131c', 'hex');
const testBufDc10 = Buffer.from('130791f0187407d018763821000138221800000000130791f008dc061018767a131c', 'hex');
const doubleTestBuf = Buffer.from(
'130791f0187407d018763821000138221800000000130791f008dc060d18767a131c130791f0187407d018763821000138221800000000130791f008dc060d18767a131c',
'hex'
Expand All @@ -106,16 +95,12 @@ describe('block-types.js', () => {
expect(resDc10['1']).toHaveProperty('Liste_DC.typ_DC', '10');
});
test('should parse the DateTime correctly', () => {
expect(res['1']).toHaveProperty(
'valid_from',
new Date(2018, 0, 1, 0, 0, 0)
); // .and.be.instanceof(Date);
expect(res['1']).toHaveProperty('valid_from', new Date(2018, 0, 1, 0, 0, 0)); // .and.be.instanceof(Date);
});
});
describe('RCT2_TEST_DATA', () => {
const fn = bt.find(
(container) => container.name == 'U_TLAY' && container.version == '01'
)?.dataFields[2].interpreterFn;
const fn = bt.find((container) => container.name == 'U_TLAY' && container.version == '01')?.dataFields[2]
.interpreterFn;
const RCT2_TEST_DATA = Buffer.from(
'303030303031373130303031384d617274696e61204d75737465726d616e6e3031303030313731303030313654616765735469636b657420506c757330323030303137313030303239507265697373747566652031302c2056474e20476573616d747261756d3033303030313731303030333332372e30352e323031372030303a30302d32392e30352e323031372030333a30303035303030313731303030313030312e30312e31393930',
'hex'
Expand Down Expand Up @@ -147,9 +132,8 @@ describe('block-types.js', () => {
});
});
describe('auftraegeSblocks_V3', () => {
const fn = bt.find(
(container) => container.name == '0080BL' && container.version == '03'
)?.dataFields[1].interpreterFn;
const fn = bt.find((container) => container.name == '0080BL' && container.version == '03')?.dataFields[1]
.interpreterFn;
const TEST_DATA = Buffer.from(
'313031303132303138303130313230313832373839343134353200313653303031303030395370617270726569735330303230303031325330303330303031415330303930303036312d312d3439533031323030303130533031343030303253325330313530303035526965736153303136303031344ec3bc726e626572672b4369747953303231303033304e562a4c2d4862662031353a343820494345313531332d494345313731335330323330303133446f656765204672616e6369735330323630303032313353303238303031334672616e63697323446f656765533033313030313030312e30312e32303138533033323030313030312e30312e32303138533033353030303531303239375330333630303033323834',
'hex'
Expand All @@ -168,9 +152,8 @@ describe('block-types.js', () => {
});
});
describe('auftraegeSblocks_V3.sblocks', () => {
const fn = bt.find(
(container) => container.name == '0080BL' && container.version == '03'
)?.dataFields[1].interpreterFn;
const fn = bt.find((container) => container.name == '0080BL' && container.version == '03')?.dataFields[1]
.interpreterFn;
const TEST_DATA = Buffer.from(
'313031303132303138303130313230313832373839343134353200313653303031303030395370617270726569735330303230303031325330303330303031415330303930303036312d312d3439533031323030303130533031343030303253325330313530303035526965736153303136303031344ec3bc726e626572672b4369747953303231303033304e562a4c2d4862662031353a343820494345313531332d494345313731335330323330303133446f656765204672616e6369735330323630303032313353303238303031334672616e63697323446f656765533033313030313030312e30312e32303138533033323030313030312e30312e32303138533033353030303531303239375330333630303033323834',
'hex'
Expand All @@ -182,9 +165,8 @@ describe('block-types.js', () => {
});
});
describe('auftraegeSblocks_V2', () => {
const fn = bt.find(
(container) => container.name == '0080BL' && container.version == '02'
)?.dataFields[1].interpreterFn;
const fn = bt.find((container) => container.name == '0080BL' && container.version == '02')?.dataFields[1]
.interpreterFn;
const TEST_DATA = Buffer.from(
'3130313233343536373839213031323334353637383921303130343230313830313034323031383031303432303138313653303031303030395370617270726569735330303230303031325330303330303031415330303930303036312d312d3439533031323030303130533031343030303253325330313530303035526965736153303136303031344ec3bc726e626572672b4369747953303231303033304e562a4c2d4862662031353a343820494345313531332d494345313731335330323330303133446f656765204672616e6369735330323630303032313353303238303031334672616e63697323446f656765533033313030313030312e30312e32303138533033323030313030312e30312e32303138533033353030303531303239375330333630303033323834',
'hex'
Expand All @@ -206,9 +188,8 @@ describe('block-types.js', () => {
});
});
describe('AUSWEIS_TYP', () => {
const fn = bt.find(
(container) => container.name == '0080ID' && container.version == '01'
)?.dataFields[0].interpreterFn;
const fn = bt.find((container) => container.name == '0080ID' && container.version == '01')?.dataFields[0]
.interpreterFn;
const TEST_DATA = Buffer.from('09');
const result = fn!(TEST_DATA);
test('should return a string', () => {
Expand Down
97 changes: 43 additions & 54 deletions __tests__/checkInputTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { describe, expect, test, beforeAll } from '@jest/globals';
import path from 'path';
import fs from 'fs';

import {
fileExists,
fileWillExists,
loadFileOrBuffer,
readFileAsync
} from '../src/checkInput';
import { loadFileOrBuffer } from '../src/checkInput';

describe('checkInput.js', () => {
const filePath = {
Expand All @@ -27,66 +22,60 @@ describe('checkInput.js', () => {
// done()
});

describe('fileExists', () => {
test("should return false if a file with relative path isn't found", () => {
expect(fileExists(filePath.relative_false)).toBe(false);
});
test('should return true if a file with relative path is found', () => {
expect(fileExists(filePath.relative_true)).toBe(true);
});
test("should return false if a file with absolute path isn't found", () => {
expect(fileExists(filePath.absolute_false)).toBe(false);
});
test('should return true if a file with absolute path is found', () => {
expect(fileExists(filePath.absolute_true)).toBe(true);
});
});
// describe('fileExists', () => {
// test("should return false if a file with relative path isn't found", () => {
// expect(fileExists(filePath.relative_false)).toBe(false);
// });
// test('should return true if a file with relative path is found', () => {
// expect(fileExists(filePath.relative_true)).toBe(true);
// });
// test("should return false if a file with absolute path isn't found", () => {
// expect(fileExists(filePath.absolute_false)).toBe(false);
// });
// test('should return true if a file with absolute path is found', () => {
// expect(fileExists(filePath.absolute_true)).toBe(true);
// });
// });

describe('fileWillExists', () => {
test('should return true if a file with relative path is found', () => {
return expect(fileWillExists(filePath.relative_true)).resolves.toBe(
filePath.relative_true
);
});
// describe('fileWillExists', () => {
// test('should return true if a file with relative path is found', () => {
// return expect(fileWillExists(filePath.relative_true)).resolves.toBe(filePath.relative_true);
// });

test('should return true if a file with absolute path is found', () => {
return expect(fileWillExists(filePath.absolute_true)).resolves.toBe(
filePath.absolute_true
);
});
});
describe('readFileAsync', () => {
test('should return true if a file with relative path is found', () => {
return expect(
readFileAsync(filePath.relative_true)
).resolves.toStrictEqual(fs.readFileSync(filePath.relative_true));
});
test("should return false if a file with absolute path isn't found", () => {
return expect(readFileAsync(filePath.absolute_false)).rejects.toThrow();
});
test('should return true if a file with absolute path is found', () => {
return expect(
readFileAsync(filePath.absolute_true)
).resolves.toStrictEqual(fs.readFileSync(filePath.absolute_true));
});
});
// test('should return true if a file with absolute path is found', () => {
// return expect(fileWillExists(filePath.absolute_true)).resolves.toBe(filePath.absolute_true);
// });
// });
// describe('readFileAsync', () => {
// test('should return true if a file with relative path is found', () => {
// return expect(readFileAsync(filePath.relative_true)).resolves.toStrictEqual(
// fs.readFileSync(filePath.relative_true)
// );
// });
// test("should return false if a file with absolute path isn't found", () => {
// return expect(readFileAsync(filePath.absolute_false)).rejects.toThrow();
// });
// test('should return true if a file with absolute path is found', () => {
// return expect(readFileAsync(filePath.absolute_true)).resolves.toStrictEqual(
// fs.readFileSync(filePath.absolute_true)
// );
// });
// });

describe('isBufferOrString', () => {
describe('loadFileOrBuffer', () => {
describe('with no optional parameters', () => {
test('should be fulfilled with a string', () => {
return expect(
loadFileOrBuffer(filePath.relative_true)
).resolves.toStrictEqual(fs.readFileSync(filePath.relative_true)); // eslint-disable-line no-unused-expressions
return expect(loadFileOrBuffer(filePath.relative_true)).resolves.toStrictEqual(
fs.readFileSync(filePath.relative_true)
); // eslint-disable-line no-unused-expressions
});
test('should be fulfilled with a Buffer', () => {
const buf = Buffer.from('01125684');

return expect(loadFileOrBuffer(buf)).resolves.toBe(buf); // eslint-disable-line no-unused-expressions
});
test('should be rejected with a wrong file path', () => {
return expect(
loadFileOrBuffer(filePath.relative_false)
).rejects.toThrow();
return expect(loadFileOrBuffer(filePath.relative_false)).rejects.toThrow();
});
});
});
Expand Down
33 changes: 28 additions & 5 deletions __tests__/check_signatureTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeAll, describe, expect, test } from '@jest/globals';

import { verifyTicket } from '../src/check_signature';
import { TicketSignatureVerficationStatus, verifyTicket } from '../src/check_signature';
import { ParsedUIC918Barcode, TicketDataContainer } from '../src/barcode-data';
import { updateLocalCerts, filePath } from '../src/updateLocalCerts';
import { existsSync } from 'node:fs';
Expand All @@ -12,7 +12,7 @@ beforeAll(async () => {

describe('check_signature.js', () => {
describe('verifyTicket()', () => {
test('should return true if a valid signature is given', () => {
test('should return VALID if a valid signature is given', () => {
const ticket: ParsedUIC918Barcode = {
signature: Buffer.from(
'302c02146b646f806c2cbc1f16977166e626c3a251c30b5602144917f4e606dfa8150eb2fa4c174378972623e47400000000',
Expand All @@ -33,9 +33,9 @@ describe('check_signature.js', () => {
ticketDataLength: Buffer.from(''),
ticketDataUncompressed: Buffer.from('')
};
return expect(verifyTicket(ticket)).resolves.toBe(true);
return expect(verifyTicket(ticket)).resolves.toBe(TicketSignatureVerficationStatus.VALID);
});
test('should return false if an invalid message is given', () => {
test('should return INVALID if an invalid message is given', () => {
const ticket = {
signature: Buffer.from(
'302c02146b646f806c2cbc1f16977166e626c3a251c30b5602144917f4e606dfa8150eb2fa4c174378972623e47400000000',
Expand All @@ -53,7 +53,30 @@ describe('check_signature.js', () => {
ticketDataLength: Buffer.from(''),
ticketDataUncompressed: Buffer.from('')
};
return expect(verifyTicket(ticket)).resolves.toBe(false);
return expect(verifyTicket(ticket)).resolves.toBe(TicketSignatureVerficationStatus.INVALID);
});
test("should return NOPUBLICKEY if a valid signature is given, but the public key isn't found", () => {
const ticket: ParsedUIC918Barcode = {
signature: Buffer.from(
'302c02146b646f806c2cbc1f16977166e626c3a251c30b5602144917f4e606dfa8150eb2fa4c174378972623e47400000000',
'hex'
),
ticketDataRaw: Buffer.from(
'789c6d90cd4ec24010c78b07f5e2c5534f86c48350539cd98f523c9014ba056285c40ae1661a056c2484b495a8275fc877f1017c1867900307770ffbdfdffee76367fcd037410808a025800f919a7ad3c095d6de124d04411ba5d2109ad0b0b1138304891a04a204147caabf532bbfa93ca5b5855e029c1b5ad172f6b6ce6759414010404142b20848b4486874c1858453700c0945422464a42a80789316c56c79d9cdca77421ee789f274f5327fcdcbda6d9aadeabb374115154e06c175b5371ede3bb58ee9387d73973851e8f44c3cbcea8e4cecc4a338767a833a05c86d438fcf79362fab715a94c43caece6d0a9f5f999eef2c097d9c7b44d9006cf09789882d517b84ba06c59c3467a320cda39b8c79267ed37aa2e1560e2ebe6a73bb3cfab6376dd7aab41b36cf9ce1f1cfe189bdf938fba4cbe23fc762e738cd7e01b9e06a43',
'hex'
),
header: {
umid: Buffer.from('235554', 'hex'),
mt_version: Buffer.from('3031', 'hex'),
rics: Buffer.from('38383838', 'hex'), // Not existing RICS CODE
key_id: Buffer.from('3838383838', 'hex')
},
version: 1,
ticketContainers: [],
ticketDataLength: Buffer.from(''),
ticketDataUncompressed: Buffer.from('')
};
return expect(verifyTicket(ticket)).resolves.toBe(TicketSignatureVerficationStatus.NOPUBLICKEY);
});
});
});
Loading

0 comments on commit 7a73a53

Please sign in to comment.