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

54 lines
1.7 KiB
TypeScript
Raw Normal View History

import Color from 'color'
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 user data request (0x0002) packet.', () => {
// Given
const username = 'Butlersaurus'
const colour = Color('#FF4000')
const clientId = 'Mercury'
// When
const packedPacket = packers[MessageTypes.UserDataRequest](
{
username: username,
colour: colour,
clientId: clientId
},
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, 0x02, 0x00, 0x3A]))
// Check the total length is as expected.
expect(packedPacket.length).toBe(62)
})
test('Parse a user data request (0x0002).', () => {
// Given
const ciphertext = Buffer.from([
0x6e, 0x3f, 0xe8, 0x45, 0x65, 0x59, 0x45, 0x85, 0xb4, 0xb2, 0xbc, 0x7a, 0x03, 0xc5,
0x6d, 0xf4, 0x23, 0x3e, 0xe9, 0x3b, 0x08, 0x6d, 0x67, 0x85, 0xc1, 0xda, 0xc6, 0x3f,
0xef, 0xaf, 0x4f, 0xd8, 0x63, 0xe6, 0xc1, 0x6c, 0x98, 0x45, 0x46, 0x4a, 0x3b, 0x61,
0x2c, 0x1e, 0x05, 0x03, 0x65, 0xe8, 0x8d, 0x82, 0x59, 0x56, 0x38, 0x58, 0x2e, 0xc4,
0x6f, 0xed
])
const username = 'Butlersaurus'
const colour = Color('#FF4000')
const clientId = 'Mercury'
// When
const unpackedPacket = unpackers[MessageTypes.UserDataRequest](ciphertext, KEY)
// Then
expect(unpackedPacket.success).toBe(true)
expect(unpackedPacket.username).toBe(username)
expect(unpackedPacket.colour).toMatchObject(colour)
expect(unpackedPacket.clientId).toBe(clientId)
})