Add all BENNC message types with unit tests
continuous-integration/drone/push Build is failing Details

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

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"editor.tabSize": 2
}

12369
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,11 +2,13 @@
"name": "bennc-js", "name": "bennc-js",
"version": "1.0.0", "version": "1.0.0",
"description": "A TypeScript/Javascript BENNC implementation.", "description": "A TypeScript/Javascript BENNC implementation.",
"main": "index.js", "main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": { "scripts": {
"lint": "ts-standard", "lint": "ts-standard",
"test": "jest", "test": "jest",
"build": "tsc" "build": "tsc",
"postinstall": "tsc"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -31,7 +33,8 @@
} }
}, },
"dependencies": { "dependencies": {
"@types/websocket": "^1.0.5", "color": "^4.2.0",
"@types/color": "^3.0.3",
"romulus-js": "git+https://git.jacknet.io/TerribleCodeClub/romulus-js.git" "romulus-js": "git+https://git.jacknet.io/TerribleCodeClub/romulus-js.git"
} }
} }

View File

@ -1,23 +0,0 @@
import { IMessageEvent } from 'websocket'
import { IncomingPacket } from './structures/server'
// import { SubscribeMessage } from './structures/subscribe'
export function connect (url: string): void {
const webSocket = new WebSocket(url)
webSocket.binaryType = 'arraybuffer'
webSocket.onmessage = onMessage
webSocket.onopen = () => {
console.log('Connected!')
// const subscribeMessage = new SubscribeMessage(0x0001)
const tmp = Uint8Array.from([0x00, 0x00, 0x00, 0x02, 0x00, 0x01])
console.log(tmp)
webSocket.send(tmp)
}
}
function onMessage (event: IMessageEvent): void {
console.log('New message.')
console.log(event.data)
console.log(new IncomingPacket(event.data as ArrayBuffer))
}

View File

@ -1,6 +1,12 @@
export const MAX_DATA_LENGTH = 1000 export const MAX_DATA_LENGTH = 1000
export enum ByteLength { export const DEFAULT_KEY = Buffer.alloc(16)
UInt16 = 2,
UInt32 = 4 export enum MessageTypes {
Subscribe = 0x0000,
Basic = 0x0001,
UserDataRequest = 0x0002,
UserDataResponse = 0x0003,
Keepalive = 0x0005,
Unsubscribe = 0xFFFF
} }

View File

@ -1,3 +1,7 @@
import { connect } from './bennc' export { unpackIncomingPacket } from './messages/packet'
export { packers, unpackers } from './mapping'
connect('wss://chat.3t.network/BENNC') export { IncomingPacket, OutgoingPacket } from './messages/packet'
export { SubscribeMessage } from './messages/subscribe'
export { BasicMessage } from './messages/basic'
export { UserDataRequestMessage } from './messages/userDataRequest'
export { UserDataResponseMessage } from './messages/userDataResponse'

21
src/mapping.ts Normal file
View File

@ -0,0 +1,21 @@
import { packSubscribeMessage } from './messages/subscribe'
import { packBasicMessage, unpackBasicMessage } from './messages/basic'
import { packUserDataRequestMessage, unpackUserDataRequestMessage } from './messages/userDataRequest'
import { packUserDataResponseMessage, unpackUserDataResponseMessage } from './messages/userDataResponse'
import { packKeepaliveMessage } from './messages/keepalive'
import { packUnsubscribeMessage } from './messages/unsubscribe'
export const packers = {
0x0000: packSubscribeMessage,
0x0001: packBasicMessage,
0x0002: packUserDataRequestMessage,
0x0003: packUserDataResponseMessage,
0x0005: packKeepaliveMessage,
0xffff: packUnsubscribeMessage
}
export const unpackers = {
0x0001: unpackBasicMessage,
0x0002: unpackUserDataRequestMessage,
0x0003: unpackUserDataResponseMessage
}

41
src/messages/basic.ts Normal file
View File

@ -0,0 +1,41 @@
import { decrypt, encrypt } from 'romulus-js'
import { DEFAULT_KEY, MessageTypes } from '../common'
import { numberToUint16BE } from '../utilities/number'
import { packOutgoingPacket } from './packet'
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Basic)
export interface BasicMessage {
message: string
success?: boolean
}
/**
* Create an outgoing basic message (0x0001) packet.
* @param properties The properties for the message.
* @param key The key to encrypt the data with.
* @returns An outgoing basic message (0x0001) packet.
*/
export function packBasicMessage (properties: BasicMessage, key: Buffer = DEFAULT_KEY): Buffer {
const message = Buffer.from(properties.message, 'utf-8')
const data = encrypt(message, MESSAGE_TYPE, key)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
data: data
})
}
/**
* Unpack the data section of an incoming basic message (0x0001) message.
* @param data The data section of an incoming basic message (0x0001) message.
* @param key The key to decrypt the data with.
* @returns An unpacked basic message (0x0001) message.
*/
export function unpackBasicMessage (data: Buffer, key: Buffer = DEFAULT_KEY): BasicMessage {
const message = decrypt(data, MESSAGE_TYPE, key)
return {
message: message.plaintext.toString(),
success: message.success
}
}

16
src/messages/keepalive.ts Normal file
View File

@ -0,0 +1,16 @@
import { MessageTypes } from '../common'
import { numberToUint16BE } from '../utilities/number'
import { packOutgoingPacket } from './packet'
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Keepalive)
/**
* Create an outgoing keepalive (0x0005) packet.
* @returns An outgoing keepalive (0x0005) packet.
*/
export function packKeepaliveMessage (): Buffer {
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
data: Buffer.alloc(0)
})
}

55
src/messages/packet.ts Normal file
View File

@ -0,0 +1,55 @@
import { MAX_DATA_LENGTH } from '../common'
import { numberToUint16BE } from '../utilities/number'
import { SmartBuffer } from '../utilities/smart-buffer'
export interface IncomingPacket {
messageType: number
senderId: number
data: Buffer
}
export interface OutgoingPacket {
messageType: Buffer
data: Buffer
}
/**
* Create an outgoing packet ready-to-send over a WebSocket in line with the BENNC basic message strucure specification.
* @param outgoingPacket The message type and data to send.
* @returns A buffer containing the ready-to-send packet.
*/
export function packOutgoingPacket (outgoingPacket: OutgoingPacket): Buffer {
// Verify that the data does not exceed the maximum data length.
if (outgoingPacket.data.length > MAX_DATA_LENGTH) {
throw RangeError(`Specified data of length ${outgoingPacket.data.length} exceeds max data length ${MAX_DATA_LENGTH}.`)
}
// Prepare the outgoing packet.
const buffer = Buffer.concat([
outgoingPacket.messageType,
numberToUint16BE(outgoingPacket.data.length),
outgoingPacket.data
])
return buffer
}
/**
* Unpack an incoming packet coming from a WebSocket in line with the BENNC basic message strucure specification.
* @param incomingPacket The incoming buffer from a WebSocket to unpack.
* @returns The unpacked data.
*/
export function unpackIncomingPacket (incomingPacket: ArrayBuffer): IncomingPacket {
const buffer = SmartBuffer.from(incomingPacket)
const messageType = buffer.readUInt16()
const senderId = buffer.readUInt32()
const length = buffer.readUInt16()
const data = buffer.readBytes(length)
return {
messageType: messageType,
senderId: senderId,
data: data
}
}

22
src/messages/subscribe.ts Normal file
View File

@ -0,0 +1,22 @@
import { MessageTypes } from '../common'
import { numberToUint16BE } from '../utilities/number'
import { packOutgoingPacket } from './packet'
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Subscribe)
export interface SubscribeMessage {
messageType: number
}
/**
* Create an outgoing subscribe (0x0000) packet.
* @param properties The properties for the message.
* @returns An outgoing subscribe (0x0000) packet.
*/
export function packSubscribeMessage (properties: SubscribeMessage): Buffer {
const data = numberToUint16BE(properties.messageType)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
data: data
})
}

View File

@ -0,0 +1,22 @@
import { MessageTypes } from '../common'
import { numberToUint16BE } from '../utilities/number'
import { packOutgoingPacket } from './packet'
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Unsubscribe)
export interface UnsubscribeMessage {
messageType: number
}
/**
* Create an outgoing unsubscribe (0xFFFF) packet.
* @param properties The properties for the message.
* @returns An outgoing unsubscribe (0xFFFF) packet.
*/
export function packUnsubscribeMessage (properties: UnsubscribeMessage): Buffer {
const data = numberToUint16BE(properties.messageType)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
data: data
})
}

View File

@ -0,0 +1,85 @@
import Color from 'color'
import { decrypt, encrypt } from 'romulus-js'
import { DEFAULT_KEY, MessageTypes } from '../common'
import { numberToUint16BE } from '../utilities/number'
import { SmartBuffer } from '../utilities/smart-buffer'
import { packOutgoingPacket } from './packet'
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataRequest)
export interface UserDataRequestMessage {
username: string
colour: Color
clientId: string
success?: boolean
}
/**
* Create an outgoing user data request (0x0002) packet.
* @param properties The properties for the message.
* @param key The key to encrypt the data with.
* @returns An outgoing user data request (0x0002) packet.
*/
export function packUserDataRequestMessage (properties: UserDataRequestMessage, key: Buffer = DEFAULT_KEY): Buffer {
// Prepare data in correct format.
const username = Buffer.from(properties.username, 'utf-8')
const usernameLength = numberToUint16BE(username.length)
const colour = Buffer.from(properties.colour.array())
const clientId = Buffer.from(properties.clientId, 'utf-8')
const clientIdLength = numberToUint16BE(clientId.length)
// Pack data.
const packedData = Buffer.concat([
usernameLength,
username,
colour,
clientIdLength,
clientId
])
// Encrypt the data.
const data = encrypt(packedData, MESSAGE_TYPE, key)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
data: data
})
}
/**
* Unpack the data section of an incoming user data request (0x0002) message
* @param data The data section of an incoming user data request (0x0002) message
* @param key The key to decrypt the data with.
* @returns An unpacked user data request (0x0002) message
*/
export function unpackUserDataRequestMessage (data: Buffer, key: Buffer = DEFAULT_KEY): UserDataRequestMessage {
// Decrypt the incoming data.
const message = decrypt(data, MESSAGE_TYPE, key)
// Guard to check if decryption was successful.
if (!message.success) {
return {
username: '',
colour: Color('black'),
clientId: '',
success: false
}
}
// Unpack and read data in correct format.
const packedData = SmartBuffer.from(message.plaintext)
const usernameLength = packedData.readUInt16()
const username = packedData.readBytes(usernameLength)
const colour = packedData.readBytes(3)
const clientIdLength = packedData.readUInt16()
const clientId = packedData.readBytes(clientIdLength)
// Return data in correct format.
return {
username: username.toString(),
colour: Color.rgb(colour),
clientId: clientId.toString(),
success: message.success
}
}

View File

@ -0,0 +1,84 @@
import Color from 'color'
import { decrypt, encrypt } from 'romulus-js'
import { DEFAULT_KEY, MessageTypes } from '../common'
import { numberToUint16BE } from '../utilities/number'
import { SmartBuffer } from '../utilities/smart-buffer'
import { packOutgoingPacket } from './packet'
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.UserDataResponse)
export interface UserDataResponseMessage {
username: string
colour: Color
clientId: string
success?: boolean
}
/**
* Pack the data section of an outgoing user data response (0x0003) message.
* @param properties The properties for the message.
* @param key The key to encrypt the data with.
* @returns The data section of an outgoing user data response (0x0003) message.
*/
export function packUserDataResponseMessage (properties: UserDataResponseMessage, key: Buffer = DEFAULT_KEY): Buffer {
// Prepare data in correct format.
const username = Buffer.from(properties.username, 'utf-8')
const usernameLength = numberToUint16BE(username.length)
const colour = Buffer.from(properties.colour.array())
const clientId = Buffer.from(properties.clientId, 'utf-8')
const clientIdLength = numberToUint16BE(clientId.length)
// Pack data.
const packedData = Buffer.concat([
usernameLength,
username,
colour,
clientIdLength,
clientId
])
// Return encrypted data.
const data = encrypt(packedData, MESSAGE_TYPE, key)
return packOutgoingPacket({
messageType: MESSAGE_TYPE,
data: data
})
}
/**
* Unpack the data section of an incoming user data response (0x0003) message
* @param data The data section of an incoming user data response (0x0003) message
* @param key The key to decrypt the data with.
* @returns An unpacked user data response (0x0003) message
*/
export function unpackUserDataResponseMessage (data: Buffer, key: Buffer = DEFAULT_KEY): UserDataResponseMessage {
// Decrypt the incoming data.
const message = decrypt(data, MESSAGE_TYPE, key)
// Guard to check if decryption was successful.
if (!message.success) {
return {
username: '',
colour: Color('black'),
clientId: '',
success: false
}
}
// Unpack and read data in correct format.
const packedData = SmartBuffer.from(message.plaintext)
const usernameLength = packedData.readUInt16()
const username = packedData.readBytes(usernameLength)
const colour = packedData.readBytes(3)
const clientIdLength = packedData.readUInt16()
const clientId = packedData.readBytes(clientIdLength)
// Return data in correct format.
return {
username: username.toString(),
colour: Color.rgb(colour),
clientId: clientId.toString(),
success: message.success
}
}

View File

@ -1,35 +0,0 @@
import { MAX_DATA_LENGTH } from '../common'
import { SmartBuffer } from '../smart-buffer'
export class IncomingPacket extends SmartBuffer {
fields: {
messageType: number
senderId: number
length: number
data: Uint8Array
}
constructor (data: ArrayBuffer) {
super()
this.data = new Uint8Array(data)
this.fields = {
messageType: this.readUInt16(),
senderId: this.readUInt32(),
length: this.readUInt16(),
data: this.readBytes(this.length)
}
}
}
export class OutgoingPacket extends SmartBuffer {
constructor (messageType: number, data: Uint8Array = new Uint8Array(0)) {
super()
if (data.length > MAX_DATA_LENGTH) {
throw RangeError(`Specified data of length ${data.length} exceeds max data length ${MAX_DATA_LENGTH}.`)
}
this.writeUInt16(messageType)
this.writeUInt16(data.length)
this.writeBytes(data)
}
}

View File

@ -1,22 +0,0 @@
import { SmartBuffer } from '../smart-buffer'
import { OutgoingPacket } from './server'
export class SubscribeMessage extends SmartBuffer {
static readonly messageType = 0x0000
constructor (id: number) {
super()
const outgoingPacket = new OutgoingPacket(SubscribeMessage.messageType)
this.data = outgoingPacket.data
this.writeUInt16(id)
}
}
export class Unsubscribe extends OutgoingPacket {
static readonly messageType = 0xffff
constructor (id: number) {
super(Unsubscribe.messageType)
this.writeUInt16(id)
}
}

21
src/utilities/number.ts Normal file
View File

@ -0,0 +1,21 @@
/**
* Pack a number to a 2 byte Uint16 buffer (big endian).
* @param number The number to pack.
* @returns The packed buffer.
*/
export function numberToUint16BE (number: number): Buffer {
const ret = Buffer.alloc(2)
ret.writeUInt16BE(number)
return ret
}
/**
* Pack a number to a 4 byte Uint32 buffer (big endian).
* @param number The number to pack.
* @returns The packed buffer.
*/
export function numberToUint32BE (number: number): Buffer {
const ret = Buffer.alloc(4)
ret.writeUInt32BE(number)
return ret
}

View File

@ -1,4 +1,4 @@
import { ByteLength } from './common' import { numberToUint16BE, numberToUint32BE } from './number'
export class SmartBuffer { export class SmartBuffer {
private _data: number[] private _data: number[]
@ -8,23 +8,23 @@ export class SmartBuffer {
* Wrap a buffer to track position and provide useful read / write functionality. * Wrap a buffer to track position and provide useful read / write functionality.
* @param data Buffer to wrap (optional). * @param data Buffer to wrap (optional).
*/ */
constructor (data?: Uint8Array) { constructor (length: number = 0) {
this._data = data === undefined ? [] : [...data] this._data = new Array<number>(length)
this._cursor = 0 this._cursor = 0
} }
/** /**
* Return a regular buffer. * Return a regular buffer.
*/ */
get data (): Uint8Array { get data (): Buffer {
return Uint8Array.from(this._data) return Buffer.from(this._data)
} }
/** /**
* Update the smart buffer to wrap new data. * Update the smart buffer to wrap new data.
*/ */
set data (data: Uint8Array) { set data (data: Buffer) {
this._data = [...data] this._data = Array.from(data)
this.cursor = 0 this.cursor = 0
} }
@ -59,12 +59,27 @@ export class SmartBuffer {
this._cursor = position this._cursor = position
} }
/**
* Create a new SmartBuffer from an existing object.
* @param data The object to convert to a new SmartBuffer.
* @returns A new SmartBuffer.
*/
static from (data: number[] | ArrayBuffer | Buffer): SmartBuffer {
const smartBuffer = new SmartBuffer()
if (data instanceof ArrayBuffer) {
smartBuffer._data = Array.from(Buffer.from(data))
} else {
smartBuffer._data = Array.from(data)
}
return smartBuffer
}
/** /**
* Pads bytes to the end of the smart buffer. * Pads bytes to the end of the smart buffer.
* @param length The number of bytes to pad. * @param length The number of bytes to pad.
*/ */
pad (length: number): void { pad (length: number): void {
this._data.push(...new Uint8Array(length)) this._data.push(...Array<number>(length))
} }
/** /**
@ -73,8 +88,8 @@ export class SmartBuffer {
* @param end The end position. * @param end The end position.
* @returns A new buffer containing data from the specified range. * @returns A new buffer containing data from the specified range.
*/ */
slice (start: number, end: number): Uint8Array { slice (start: number, end: number): Buffer {
return Uint8Array.from(this._data.slice(start, end)) return Buffer.from(this._data.slice(start, end))
} }
/** /**
@ -87,7 +102,7 @@ export class SmartBuffer {
if (this.length < start) { if (this.length < start) {
this.pad(start) this.pad(start)
} }
this._data.splice(this.cursor, ByteLength.UInt16, ...items) this._data.splice(this.cursor, deleteCount, ...items)
} }
/** /**
@ -95,9 +110,8 @@ export class SmartBuffer {
* @returns A number represented by the bytes at the current cursor position. * @returns A number represented by the bytes at the current cursor position.
*/ */
readUInt16 (): number { readUInt16 (): number {
this.cursor += ByteLength.UInt16 this.cursor += 2
const bytes = this.slice(this.cursor - ByteLength.UInt16, this.cursor) return Buffer.from(this._data).readUInt16BE(this.cursor - 2)
return (bytes[0] << 8) | bytes[1]
} }
/** /**
@ -105,16 +119,15 @@ export class SmartBuffer {
* @returns A number represented by the bytes at the current cursor position. * @returns A number represented by the bytes at the current cursor position.
*/ */
readUInt32 (): number { readUInt32 (): number {
this.cursor += ByteLength.UInt32 this.cursor += 4
const bytes = this.slice(this.cursor - ByteLength.UInt32, this.cursor) return Buffer.from(this._data).readUInt32BE(this.cursor - 4)
return (((bytes[0] << 24) | (bytes[1] << 16)) | (bytes[2] << 8) | bytes[3])
} }
/** /**
* Read the specified number of bytes from the smart buffer at the current cursor position, and increment the cursor. * Read the specified number of bytes from the smart buffer at the current cursor position, and increment the cursor.
* @returns A buffer containing the bytes read from the current cursor position. * @returns A buffer containing the bytes read from the current cursor position.
*/ */
readBytes (length: number): Uint8Array { readBytes (length: number): Buffer {
this.cursor += length this.cursor += length
return this.slice(this.cursor - length, this.cursor) return this.slice(this.cursor - length, this.cursor)
} }
@ -124,12 +137,8 @@ export class SmartBuffer {
* @param number The number to write in UInt16 format at the current cursor position. * @param number The number to write in UInt16 format at the current cursor position.
*/ */
writeUInt16 (number: number): void { writeUInt16 (number: number): void {
const bytes = [ this.splice(this.cursor, 2, ...numberToUint16BE(number))
(number & 0xff00) >> 8, this.cursor += 2
(number & 0x00ff)
]
this.splice(this.cursor, ByteLength.UInt16, ...bytes)
this.cursor += ByteLength.UInt16
} }
/** /**
@ -137,21 +146,15 @@ export class SmartBuffer {
* @param number The number to write in UInt32 format at the current cursor position. * @param number The number to write in UInt32 format at the current cursor position.
*/ */
writeUInt32 (number: number): void { writeUInt32 (number: number): void {
const bytes = [ this.splice(this.cursor, 4, ...numberToUint32BE(number))
(number & 0xff000000) >> 24, this.cursor += 4
(number & 0x00ff0000) >> 16,
(number & 0x0000ff00) >> 8,
(number & 0x000000ff)
]
this.splice(this.cursor, ByteLength.UInt32, ...bytes)
this.cursor += ByteLength.UInt32
} }
/** /**
* Write the specified bytes to the smart buffer at the current cursor position, and increment the cursor. * Write the specified bytes to the smart buffer at the current cursor position, and increment the cursor.
* @param buffer The bytes to write at the current cursor position. * @param buffer The bytes to write at the current cursor position.
*/ */
writeBytes (buffer: Uint8Array): void { writeBytes (buffer: number[] | Buffer): void {
this.splice(this.cursor, buffer.length, ...buffer) this.splice(this.cursor, buffer.length, ...buffer)
this.cursor += buffer.length this.cursor += buffer.length
} }

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)
})

View 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)
})

View File

@ -1,11 +1,11 @@
import { SmartBuffer } from '../src/smart-buffer' import { SmartBuffer } from '../../src/utilities/smart-buffer'
test('Read a UInt16.', () => { test('Read a UInt16.', () => {
// Given // Given
const buffer = Uint8Array.from([0x30, 0x39]) const buffer = [0x30, 0x39]
// When // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
// Then // Then
expect(smartBuffer.readUInt16()).toBe(12345) expect(smartBuffer.readUInt16()).toBe(12345)
@ -13,10 +13,10 @@ test('Read a UInt16.', () => {
test('Read a UInt32.', () => { test('Read a UInt32.', () => {
// Given // Given
const buffer = Uint8Array.from([0x49, 0x96, 0x02, 0xD2]) const buffer = [0x49, 0x96, 0x02, 0xD2]
// When // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
// Then // Then
expect(smartBuffer.readUInt32()).toBe(1234567890) expect(smartBuffer.readUInt32()).toBe(1234567890)
@ -24,21 +24,22 @@ test('Read a UInt32.', () => {
test('Read a buffer.', () => { test('Read a buffer.', () => {
// Given // 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 // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
// Then // 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.', () => { test('Read a UInt16 from an offset.', () => {
// Given // Given
const buffer = Uint8Array.from([0x00, 0x00, 0x30, 0x39]) const buffer = [0x00, 0x00, 0x30, 0x39]
// When // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
smartBuffer.cursor = 2 smartBuffer.cursor = 2
// Then // Then
@ -47,10 +48,10 @@ test('Read a UInt16 from an offset.', () => {
test('Read a UInt32 from an offset.', () => { test('Read a UInt32 from an offset.', () => {
// Given // Given
const buffer = Uint8Array.from([0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]) const buffer = [0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]
// When // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
smartBuffer.cursor = 2 smartBuffer.cursor = 2
// Then // Then
@ -59,14 +60,14 @@ test('Read a UInt32 from an offset.', () => {
test('Read a buffer from an offset.', () => { test('Read a buffer from an offset.', () => {
// Given // 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 // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
smartBuffer.cursor = 2 smartBuffer.cursor = 2
// Then // 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.', () => { test('Write a UInt16.', () => {
@ -77,7 +78,7 @@ test('Write a UInt16.', () => {
smartBuffer.writeUInt16(12345) smartBuffer.writeUInt16(12345)
// Then // Then
expect(smartBuffer.data).toMatchObject(Uint8Array.from([0x30, 0x39])) expect(smartBuffer.data).toMatchObject(Buffer.from([0x30, 0x39]))
}) })
test('Write a UInt32.', () => { test('Write a UInt32.', () => {
@ -88,7 +89,7 @@ test('Write a UInt32.', () => {
smartBuffer.writeUInt32(1234567890) smartBuffer.writeUInt32(1234567890)
// Then // 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.', () => { test('Write a buffer.', () => {
@ -96,10 +97,10 @@ test('Write a buffer.', () => {
const smartBuffer = new SmartBuffer() const smartBuffer = new SmartBuffer()
// When // 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 // 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.', () => { test('Write a UInt16 at an offset.', () => {
@ -111,7 +112,7 @@ test('Write a UInt16 at an offset.', () => {
smartBuffer.writeUInt16(12345) smartBuffer.writeUInt16(12345)
// Then // 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.', () => { test('Write a UInt32 at an offset.', () => {
@ -123,7 +124,7 @@ test('Write a UInt32 at an offset.', () => {
smartBuffer.writeUInt32(1234567890) smartBuffer.writeUInt32(1234567890)
// Then // 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.', () => { test('Write a buffer at an offset.', () => {
@ -132,10 +133,10 @@ test('Write a buffer at an offset.', () => {
// When // When
smartBuffer.cursor = 2 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 // 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.', () => { 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) const buffer = new Uint8Array(4)
// When // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
// Then // Then
smartBuffer.readUInt16() smartBuffer.readUInt16()
@ -155,7 +156,7 @@ test('Cursor is correctly incremented after reading a UInt32.', () => {
const buffer = new Uint8Array(4) const buffer = new Uint8Array(4)
// When // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
// Then // Then
smartBuffer.readUInt32() smartBuffer.readUInt32()
@ -167,7 +168,7 @@ test('Cursor is correctly incremented after reading a buffer.', () => {
const buffer = new Uint8Array(8) const buffer = new Uint8Array(8)
// When // When
const smartBuffer = new SmartBuffer(buffer) const smartBuffer = SmartBuffer.from(buffer)
// Then // Then
smartBuffer.readBytes(4) smartBuffer.readBytes(4)
@ -201,7 +202,7 @@ test('Cursor is correctly incremented after writing a buffer.', () => {
const smartBuffer = new SmartBuffer() const smartBuffer = new SmartBuffer()
// When // 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 // Then
expect(smartBuffer.cursor).toBe(10) expect(smartBuffer.cursor).toBe(10)
@ -217,7 +218,7 @@ test('Seek to position below 0 throws range error.', () => {
}).toThrow(RangeError) }).toThrow(RangeError)
}) })
test('Pad some data..', () => { test('Pad some data.', () => {
// Given // Given
const smartBuffer = new SmartBuffer() const smartBuffer = new SmartBuffer()

View File

@ -1,20 +0,0 @@
const path = require('path')
module.exports = {
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
}