Add all BENNC message types with unit tests
This commit is contained in:
39
tests/messages/basic.test.ts
Normal file
39
tests/messages/basic.test.ts
Normal 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!')
|
||||
})
|
||||
11
tests/messages/keepalive.test.ts
Normal file
11
tests/messages/keepalive.test.ts
Normal 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)
|
||||
})
|
||||
46
tests/messages/packet.test.ts
Normal file
46
tests/messages/packet.test.ts
Normal 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]))
|
||||
})
|
||||
14
tests/messages/subscribe.test.ts
Normal file
14
tests/messages/subscribe.test.ts
Normal 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)
|
||||
})
|
||||
14
tests/messages/unsubscribe.test.ts
Normal file
14
tests/messages/unsubscribe.test.ts
Normal 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)
|
||||
})
|
||||
53
tests/messages/userDataRequest.test.ts
Normal file
53
tests/messages/userDataRequest.test.ts
Normal 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)
|
||||
})
|
||||
55
tests/messages/userDataResponse.test.ts
Normal file
55
tests/messages/userDataResponse.test.ts
Normal 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)
|
||||
})
|
||||
19
tests/utilities/number.test.ts
Normal file
19
tests/utilities/number.test.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { numberToUint16BE, numberToUint32BE } from '../../src/utilities/number'
|
||||
|
||||
test('Test number conversion to Uint16 big endian buffer.', () => {
|
||||
// When
|
||||
const result = numberToUint16BE(1234)
|
||||
|
||||
// Then
|
||||
const expectedResult = Buffer.from([0x04, 0xd2])
|
||||
expect(result).toMatchObject(expectedResult)
|
||||
})
|
||||
|
||||
test('Test number conversion to Uint32 big endian buffer.', () => {
|
||||
// When
|
||||
const result = numberToUint32BE(123456)
|
||||
|
||||
// Then
|
||||
const expectedResult = Buffer.from([0x00, 0x01, 0xE2, 0x40])
|
||||
expect(result).toMatchObject(expectedResult)
|
||||
})
|
||||
@@ -1,11 +1,11 @@
|
||||
import { SmartBuffer } from '../src/smart-buffer'
|
||||
import { SmartBuffer } from '../../src/utilities/smart-buffer'
|
||||
|
||||
test('Read a UInt16.', () => {
|
||||
// Given
|
||||
const buffer = Uint8Array.from([0x30, 0x39])
|
||||
const buffer = [0x30, 0x39]
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readUInt16()).toBe(12345)
|
||||
@@ -13,10 +13,10 @@ test('Read a UInt16.', () => {
|
||||
|
||||
test('Read a UInt32.', () => {
|
||||
// Given
|
||||
const buffer = Uint8Array.from([0x49, 0x96, 0x02, 0xD2])
|
||||
const buffer = [0x49, 0x96, 0x02, 0xD2]
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readUInt32()).toBe(1234567890)
|
||||
@@ -24,21 +24,22 @@ test('Read a UInt32.', () => {
|
||||
|
||||
test('Read a buffer.', () => {
|
||||
// Given
|
||||
const buffer = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readBytes(4)).toMatchObject(Uint8Array.from([0, 1, 2, 3]))
|
||||
const result = smartBuffer.readBytes(4)
|
||||
expect(result).toMatchObject(Buffer.from([0, 1, 2, 3]))
|
||||
})
|
||||
|
||||
test('Read a UInt16 from an offset.', () => {
|
||||
// Given
|
||||
const buffer = Uint8Array.from([0x00, 0x00, 0x30, 0x39])
|
||||
const buffer = [0x00, 0x00, 0x30, 0x39]
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
smartBuffer.cursor = 2
|
||||
|
||||
// Then
|
||||
@@ -47,10 +48,10 @@ test('Read a UInt16 from an offset.', () => {
|
||||
|
||||
test('Read a UInt32 from an offset.', () => {
|
||||
// Given
|
||||
const buffer = Uint8Array.from([0x00, 0x00, 0x49, 0x96, 0x02, 0xD2])
|
||||
const buffer = [0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
smartBuffer.cursor = 2
|
||||
|
||||
// Then
|
||||
@@ -59,14 +60,14 @@ test('Read a UInt32 from an offset.', () => {
|
||||
|
||||
test('Read a buffer from an offset.', () => {
|
||||
// Given
|
||||
const buffer = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
smartBuffer.cursor = 2
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readBytes(4)).toMatchObject(Uint8Array.from([2, 3, 4, 5]))
|
||||
expect(smartBuffer.readBytes(4)).toMatchObject(Buffer.from([2, 3, 4, 5]))
|
||||
})
|
||||
|
||||
test('Write a UInt16.', () => {
|
||||
@@ -77,7 +78,7 @@ test('Write a UInt16.', () => {
|
||||
smartBuffer.writeUInt16(12345)
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(Uint8Array.from([0x30, 0x39]))
|
||||
expect(smartBuffer.data).toMatchObject(Buffer.from([0x30, 0x39]))
|
||||
})
|
||||
|
||||
test('Write a UInt32.', () => {
|
||||
@@ -88,7 +89,7 @@ test('Write a UInt32.', () => {
|
||||
smartBuffer.writeUInt32(1234567890)
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(Uint8Array.from([0x49, 0x96, 0x02, 0xD2]))
|
||||
expect(smartBuffer.data).toMatchObject(Buffer.from([0x49, 0x96, 0x02, 0xD2]))
|
||||
})
|
||||
|
||||
test('Write a buffer.', () => {
|
||||
@@ -96,10 +97,10 @@ test('Write a buffer.', () => {
|
||||
const smartBuffer = new SmartBuffer()
|
||||
|
||||
// When
|
||||
smartBuffer.writeBytes(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
expect(smartBuffer.data).toMatchObject(Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
})
|
||||
|
||||
test('Write a UInt16 at an offset.', () => {
|
||||
@@ -111,7 +112,7 @@ test('Write a UInt16 at an offset.', () => {
|
||||
smartBuffer.writeUInt16(12345)
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(Uint8Array.from([0x00, 0x00, 0x30, 0x39]))
|
||||
expect(smartBuffer.data).toMatchObject(Buffer.from([0x00, 0x00, 0x30, 0x39]))
|
||||
})
|
||||
|
||||
test('Write a UInt32 at an offset.', () => {
|
||||
@@ -123,7 +124,7 @@ test('Write a UInt32 at an offset.', () => {
|
||||
smartBuffer.writeUInt32(1234567890)
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(Uint8Array.from([0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]))
|
||||
expect(smartBuffer.data).toMatchObject(Buffer.from([0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]))
|
||||
})
|
||||
|
||||
test('Write a buffer at an offset.', () => {
|
||||
@@ -132,10 +133,10 @@ test('Write a buffer at an offset.', () => {
|
||||
|
||||
// When
|
||||
smartBuffer.cursor = 2
|
||||
smartBuffer.writeBytes(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(Uint8Array.from([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
expect(smartBuffer.data).toMatchObject(Buffer.from([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
})
|
||||
|
||||
test('Cursor is correctly incremented after reading a UInt16.', () => {
|
||||
@@ -143,7 +144,7 @@ test('Cursor is correctly incremented after reading a UInt16.', () => {
|
||||
const buffer = new Uint8Array(4)
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
|
||||
// Then
|
||||
smartBuffer.readUInt16()
|
||||
@@ -155,7 +156,7 @@ test('Cursor is correctly incremented after reading a UInt32.', () => {
|
||||
const buffer = new Uint8Array(4)
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
|
||||
// Then
|
||||
smartBuffer.readUInt32()
|
||||
@@ -167,7 +168,7 @@ test('Cursor is correctly incremented after reading a buffer.', () => {
|
||||
const buffer = new Uint8Array(8)
|
||||
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
|
||||
// Then
|
||||
smartBuffer.readBytes(4)
|
||||
@@ -201,7 +202,7 @@ test('Cursor is correctly incremented after writing a buffer.', () => {
|
||||
const smartBuffer = new SmartBuffer()
|
||||
|
||||
// When
|
||||
smartBuffer.writeBytes(Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.cursor).toBe(10)
|
||||
@@ -217,7 +218,7 @@ test('Seek to position below 0 throws range error.', () => {
|
||||
}).toThrow(RangeError)
|
||||
})
|
||||
|
||||
test('Pad some data..', () => {
|
||||
test('Pad some data.', () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
|
||||
Reference in New Issue
Block a user