Fix outbound user data request/response
This commit is contained in:
		
							parent
							
								
									0b19b83271
								
							
						
					
					
						commit
						3a163df991
					
				@ -33,9 +33,11 @@ export function packUserDataRequestMessage (properties: UserDataRequestMessage,
 | 
			
		||||
  const packedData = new SmartBuffer()
 | 
			
		||||
  packedData.writeBytes(usernameLength)
 | 
			
		||||
  packedData.writeBytes(username)
 | 
			
		||||
  packedData.pad(32 - username.length)
 | 
			
		||||
  packedData.writeBytes(colour)
 | 
			
		||||
  packedData.writeBytes(clientIdLength)
 | 
			
		||||
  packedData.writeBytes(clientId)
 | 
			
		||||
  packedData.pad(32 - clientId.length)
 | 
			
		||||
 | 
			
		||||
  // Encrypt the data.
 | 
			
		||||
  const data = encrypt(packedData.data, MESSAGE_TYPE, key)
 | 
			
		||||
 | 
			
		||||
@ -33,9 +33,11 @@ export function packUserDataResponseMessage (properties: UserDataResponseMessage
 | 
			
		||||
  const packedData = new SmartBuffer()
 | 
			
		||||
  packedData.writeBytes(usernameLength)
 | 
			
		||||
  packedData.writeBytes(username)
 | 
			
		||||
  packedData.pad(32 - username.length)
 | 
			
		||||
  packedData.writeBytes(colour)
 | 
			
		||||
  packedData.writeBytes(clientIdLength)
 | 
			
		||||
  packedData.writeBytes(clientId)
 | 
			
		||||
  packedData.pad(32 - clientId.length)
 | 
			
		||||
 | 
			
		||||
  // Return encrypted data.
 | 
			
		||||
  const data = encrypt(packedData.data, MESSAGE_TYPE, key)
 | 
			
		||||
 | 
			
		||||
@ -79,7 +79,8 @@ export class SmartBuffer {
 | 
			
		||||
  * @param length The number of bytes to pad.
 | 
			
		||||
  */
 | 
			
		||||
  pad (length: number): void {
 | 
			
		||||
    this._data.push(...Array<number>(length))
 | 
			
		||||
    this._data.push(...Array<number>(length).fill(0))
 | 
			
		||||
    this.cursor += length
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
@ -100,7 +101,7 @@ export class SmartBuffer {
 | 
			
		||||
   */
 | 
			
		||||
  splice (start: number, deleteCount: number, ...items: number[]): void {
 | 
			
		||||
    if (this.length < start) {
 | 
			
		||||
      this.pad(start)
 | 
			
		||||
      this._data.push(...Array<number>(start))
 | 
			
		||||
    }
 | 
			
		||||
    this._data.splice(this.cursor, deleteCount, ...items)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -23,10 +23,10 @@ test('Create a user data request (0x0002) packet.', () => {
 | 
			
		||||
  // 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(new Uint8Array([0x00, 0x02, 0x00, 0x3A]))
 | 
			
		||||
  expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x02, 0x00, 0x67]))
 | 
			
		||||
 | 
			
		||||
  // Check the total length is as expected.
 | 
			
		||||
  expect(packedPacket.length).toBe(62)
 | 
			
		||||
  expect(packedPacket.length).toBe(107)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test('Parse a user data request (0x0002).', () => {
 | 
			
		||||
 | 
			
		||||
@ -23,10 +23,10 @@ test('Create a user data response (0x0003) packet.', () => {
 | 
			
		||||
  // 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(new Uint8Array([0x00, 0x03, 0x00, 0x3A]))
 | 
			
		||||
  expect(packedPacket.slice(0, 4)).toMatchObject(new Uint8Array([0x00, 0x03, 0x00, 0x67]))
 | 
			
		||||
 | 
			
		||||
  // Check the total length is as expected.
 | 
			
		||||
  expect(packedPacket.length).toBe(62)
 | 
			
		||||
  expect(packedPacket.length).toBe(107)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test('Parse a user data response (0x0003).', () => {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user