Add all BENNC message types with unit tests

This commit is contained in:
Jack Hadrill
2022-02-06 20:34:13 +00:00
parent 7d1a0991e4
commit e758de7ef4
28 changed files with 708 additions and 12539 deletions

View File

@@ -0,0 +1,39 @@
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!')
})

View File

@@ -0,0 +1,11 @@
import { MessageTypes } from '../../src/common'
import { packers } from '../../src/mapping'
test('Create a keepalive (0x0005) packet.', () => {
// When
const packedPacket = packers[MessageTypes.Keepalive]()
// Then
const expectedResult = Buffer.from([0x00, 0x05, 0x00, 0x00])
expect(packedPacket).toMatchObject(expectedResult)
})

View File

@@ -0,0 +1,46 @@
import { packOutgoingPacket, unpackIncomingPacket } from '../../src/messages/packet'
test('Pack an outgoing packet.', () => {
// Given
const messageType = Buffer.from([0x12, 0x34])
const data = Buffer.from([0x12, 0x34, 0x56, 0x78])
// When
const packedPacket = packOutgoingPacket({
messageType: messageType,
data: data
})
// Then
const expectedResult = Buffer.from([
// Message type
0x12, 0x34,
// Data length
0x00, 0x04,
// Data
0x12, 0x34, 0x56, 0x78
])
expect(packedPacket).toMatchObject(expectedResult)
})
test('Unpack an incoming packet.', () => {
// Given
const incomingPacket = Buffer.from([
// Message type
0x12, 0x34,
// Sender ID
0xaa, 0xbb, 0xcc, 0xdd,
// Data length
0x00, 0x04,
// Data
0x12, 0x34, 0x56, 0x78
])
// When
const unpackedResult = unpackIncomingPacket(incomingPacket)
// Then
expect(unpackedResult.messageType).toBe(0x1234)
expect(unpackedResult.senderId).toBe(0xaabbccdd)
expect(unpackedResult.data).toMatchObject(Buffer.from([0x12, 0x34, 0x56, 0x78]))
})

View File

@@ -0,0 +1,14 @@
import { MessageTypes } from '../../src/common'
import { packers } from '../../src/mapping'
test('Create a subscribe (0x0000) packet.', () => {
// Given
const messageType = 0xabcd
// When
const packedPacket = packers[MessageTypes.Subscribe]({ messageType: messageType })
// Then
const expectedResult = Buffer.from([0x00, 0x00, 0x00, 0x02, 0xab, 0xcd])
expect(packedPacket).toMatchObject(expectedResult)
})

View File

@@ -0,0 +1,14 @@
import { MessageTypes } from '../../src/common'
import { packers } from '../../src/mapping'
test('Create an unsubscribe (0xffff) packet.', () => {
// Given
const messageType = 0xabcd
// When
const packedPacket = packers[MessageTypes.Unsubscribe]({ messageType: messageType })
// Then
const expectedResult = Buffer.from([0xff, 0xff, 0x00, 0x02, 0xab, 0xcd])
expect(packedPacket).toMatchObject(expectedResult)
})

View File

@@ -0,0 +1,53 @@
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)
})

View File

@@ -0,0 +1,55 @@
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 response (0x0003) packet.', () => {
// Given
const username = 'Butlersaurus'
const colour = Color('#FF4000')
const clientId = 'Mercury'
// When
const packedPacket = packers[MessageTypes.UserDataResponse](
{
username: username,
colour: colour,
clientId: clientId
},
KEY
)
console.log(packedPacket.slice(4).toString('hex'))
// 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, 0x03, 0x00, 0x3A]))
// Check the total length is as expected.
expect(packedPacket.length).toBe(62)
})
test('Parse a user data response (0x0003).', () => {
// Given
const ciphertext = Buffer.from([
0x56, 0x71, 0x08, 0xf9, 0x2c, 0x4e, 0x41, 0x13, 0xbe, 0x44, 0xba, 0xd9, 0xe4, 0x25,
0x14, 0x60, 0x3b, 0x96, 0x7e, 0x0b, 0xbd, 0xac, 0xf0, 0xaf, 0xac, 0xd7, 0x80, 0xe5,
0x62, 0xd4, 0x33, 0x10, 0x23, 0x6d, 0x00, 0x3c, 0xae, 0x40, 0x6c, 0xe9, 0x40, 0xfc,
0x1c, 0xe0, 0xd3, 0xca, 0x65, 0xea, 0x83, 0x73, 0x5e, 0xd2, 0x67, 0xb2, 0x94, 0x58,
0x12, 0x73
])
const username = 'Butlersaurus'
const colour = Color('#FF4000')
const clientId = 'Mercury'
// When
const unpackedPacket = unpackers[MessageTypes.UserDataResponse](ciphertext, KEY)
// Then
expect(unpackedPacket.success).toBe(true)
expect(unpackedPacket.username).toBe(username)
expect(unpackedPacket.colour).toMatchObject(colour)
expect(unpackedPacket.clientId).toBe(clientId)
})