bennc-js/tests/messages/basic.test.ts

40 lines
1.3 KiB
TypeScript
Raw Normal View History

import { MessageTypes } from '../../src/common'
import { packers, unpackers } from '../../src/mapping'
const KEY = Buffer.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F])
test('Create a basic message (0x0001) packet.', () => {
// Given
const message = 'Hello, World!'
// When
const packedPacket = packers[MessageTypes.Basic](
{ message: message },
KEY
)
// Then
// We can't check the contents of the data as it's encrypted with a random nonce.
// Check the message type and length.
expect(packedPacket.slice(0, 4)).toMatchObject(Buffer.from([0x00, 0x01, 0x00, 0x2D]))
// Check the total length is as expected.
expect(packedPacket.length).toBe(49)
})
test('Parse a basic message (0x0001).', () => {
// Given
const ciphertext = Buffer.from([
0x84, 0xa2, 0x3a, 0xe2, 0xa8, 0xff, 0x43, 0x56, 0x96, 0x94, 0xf6, 0xe5, 0x1a, 0x30, 0x53,
0x39, 0xb9, 0xc4, 0x2c, 0xab, 0x21, 0x52, 0x8f, 0xc2, 0xba, 0x6c, 0x8b, 0x96, 0x82, 0x9d,
0x51, 0x27, 0x0c, 0xb6, 0x3b, 0x7f, 0x3e, 0x2a, 0x7b, 0x70, 0xbe, 0x01, 0x7b, 0x71, 0x9d
])
// When
const unpackedPacket = unpackers[MessageTypes.Basic](ciphertext, KEY)
// Then
expect(unpackedPacket.success).toBe(true)
expect(unpackedPacket.message).toBe('Hello, World!')
})