2022-02-06 20:34:13 +00:00
|
|
|
import Color from 'color'
|
|
|
|
import { MessageTypes } from '../../src/common'
|
|
|
|
import { packers, unpackers } from '../../src/mapping'
|
|
|
|
|
2022-02-22 22:35:55 +00:00
|
|
|
const KEY = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F])
|
2022-02-06 20:34:13 +00:00
|
|
|
|
|
|
|
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.
|
2022-02-22 22:35:55 +00:00
|
|
|
expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x02, 0x00, 0x3A]))
|
2022-02-06 20:34:13 +00:00
|
|
|
|
|
|
|
// Check the total length is as expected.
|
|
|
|
expect(packedPacket.length).toBe(62)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Parse a user data request (0x0002).', () => {
|
|
|
|
// Given
|
2022-02-22 22:35:55 +00:00
|
|
|
const ciphertext = new Uint8Array([
|
2022-02-06 20:34:13 +00:00
|
|
|
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)
|
|
|
|
})
|