tests(protocols): add unit tests for @trezor/protocols package

This commit is contained in:
Szymon Lesisz
2023-12-04 17:57:05 +01:00
committed by martin
parent e00cf70ac3
commit 5073f49215
4 changed files with 114 additions and 1 deletions

View File

@@ -18,7 +18,7 @@
],
"scripts": {
"lint:js": "yarn g:eslint '**/*.{ts,tsx,js}'",
"test:unit": "jest -c ../../jest.config.base.js --passWithNoTests",
"test:unit": "jest -c ../../jest.config.base.js",
"type-check": "tsc --build",
"build:lib": "rimraf ./lib && yarn tsc --build tsconfig.lib.json",
"prepublishOnly": "yarn tsx ../../scripts/prepublishNPM.js",

View File

@@ -0,0 +1,28 @@
import ByteBuffer from 'bytebuffer';
import { bridge } from '../src/index';
describe('protocol-bridge', () => {
it('encode', () => {
let chunks;
// encode small chunk, message without data
chunks = bridge.encode(new ByteBuffer(0), { messageType: 55 });
expect(chunks.limit).toEqual(6);
// encode big chunk, message with data
chunks = bridge.encode(new ByteBuffer(371), { messageType: 55 });
expect(chunks.slice(0, 6).toString('hex')).toEqual('003700000173');
expect(chunks.readUint32(2)).toEqual(371);
expect(chunks.buffer.length).toEqual(371 + 6);
});
it('decode', () => {
const getFeatures = Buffer.from('0037', 'hex');
const data = Buffer.allocUnsafe(385).fill(0);
data.fill(getFeatures, 0, 2);
data.writeUint32BE(379, 2);
const read = bridge.decode(ByteBuffer.fromHex(data.toString('hex')));
expect(read.typeId).toEqual(55);
});
});

View File

@@ -0,0 +1,46 @@
import { trzd } from '../src/index';
describe('protocol-v1', () => {
it('decode', () => {
// Testnet chain
let data = Buffer.from(
'74727a643100585a6865130008011203455448183c2208457468657265756dDEAD',
'hex',
);
let read;
read = trzd.decode(data);
expect(read.magic).toEqual('trzd1');
expect(read.definitionType).toEqual(0);
expect(read.protobufLength).toEqual(19);
expect(read.protobufPayload.toBuffer().toString('hex')).toEqual(
'08011203455448183c2208457468657265756d',
);
// Testnet token
data = Buffer.from(
'74727a643101585a686532000a147af963cf6d228e564e2a0aa0ddbf06210b38615d10051a0354535420122a11676f65726c69205465737420746f6b656eDEAD',
'hex',
);
read = trzd.decode(data);
expect(read.magic).toEqual('trzd1');
expect(read.definitionType).toEqual(1);
expect(read.protobufLength).toEqual(50);
expect(read.protobufPayload.toBuffer().toString('hex')).toEqual(
'0a147af963cf6d228e564e2a0aa0ddbf06210b38615d10051a0354535420122a11676f65726c69205465737420746f6b656e',
);
// chain name > 256
const longName = Buffer.from('LongName'.repeat(50)); // 400 bytes
const len = Buffer.alloc(2);
len.writeInt16LE(longName.length);
data = Buffer.from(
`74727a643100585a6865${len.toString('hex')}${longName.toString('hex')}DEAD`,
'hex',
);
console.warn(len.toString('hex'));
read = trzd.decode(data);
expect(read.protobufLength).toEqual(400);
expect(read.protobufPayload.toBuffer()).toEqual(longName);
});
});

View File

@@ -0,0 +1,39 @@
import ByteBuffer from 'bytebuffer';
import { v1 } from '../src/index';
describe('protocol-v1', () => {
it('encode', () => {
let chunks;
// encode only one chunk, message without data
chunks = v1.encode(new ByteBuffer(0), { messageType: 55 });
expect(chunks.length).toEqual(1);
expect(chunks[0].length).toEqual(64);
// encode multiple chunks, message with data
chunks = v1.encode(new ByteBuffer(371), { messageType: 55 });
expect(chunks.length).toEqual(7);
chunks.forEach((chunk, index) => {
expect(chunk.length).toEqual(64);
if (index === 0) {
// first chunk with additional data
expect(chunk.slice(0, 9).toString('hex')).toEqual('3f2323003700000173');
expect(chunk.readUInt32BE(5)).toEqual(371);
} else {
// following chunk starts with MESSAGE_MAGIC_HEADER_BYTE
expect(chunk.slice(0, 5).toString('hex')).toEqual('3f00000000');
}
});
});
it('decode', () => {
const getFeatures = Buffer.from('3f23230037', 'hex');
const data = Buffer.allocUnsafe(360).fill(0);
data.fill(getFeatures, 0, 5);
data.writeUint32BE(379, 5);
const read = v1.decodeChunked(data);
expect(read.typeId).toEqual(55);
expect(read.length).toEqual(379);
});
});