From 2703db0134029f4436c1d64dc818c47e84051a36 Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Thu, 28 Sep 2023 16:04:12 +0100 Subject: [PATCH] chore: add tests to check receiving a broken message --- src/__tests__/connection.spec.ts | 160 +++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/src/__tests__/connection.spec.ts b/src/__tests__/connection.spec.ts index 90c227b9..5dd0ee08 100644 --- a/src/__tests__/connection.spec.ts +++ b/src/__tests__/connection.spec.ts @@ -192,5 +192,165 @@ describe('connection', () => { conn.disconnect() } }) + + it('receive fast responses', async () => { + const conn = new Connection('127.0.0.1', 5250, true) + try { + expect(conn).toBeTruthy() + + const onConnError = jest.fn() + const onConnData = jest.fn() + conn.on('error', onConnError) + conn.on('data', onConnData) + + const sockets = SocketMock.openSockets() + expect(sockets).toHaveLength(1) + + // Dispatch a command + const sendError = await conn.sendCommand( + { + command: Commands.Info, + params: {}, + }, + 'cmd1' + ) + expect(sendError).toBeFalsy() + const sendError2 = await conn.sendCommand( + { + command: Commands.Play, + params: { + channel: 1, + layer: 10, + }, + }, + 'cmd2' + ) + expect(sendError2).toBeFalsy() + expect(onConnError).toHaveBeenCalledTimes(0) + expect(onConnData).toHaveBeenCalledTimes(0) + + // Info was sent + expect(onSocketWrite).toHaveBeenCalledTimes(2) + expect(onSocketWrite).toHaveBeenNthCalledWith(1, 'REQ cmd1 INFO\r\n', 'utf-8') + expect(onSocketWrite).toHaveBeenNthCalledWith(2, 'REQ cmd2 PLAY 1-10\r\n', 'utf-8') + + // Send replies + sockets[0].mockData( + Buffer.from( + `RES cmd1 201 INFO OK\r\n\n\r\n\r\n` + ) + ) + sockets[0].mockData(Buffer.from(`RES cmd2 202 PLAY OK\r\n`)) + + // Wait for deserializer to run + await new Promise(process.nextTick.bind(process)) + await new Promise(process.nextTick.bind(process)) + await new Promise(process.nextTick.bind(process)) + + expect(onConnError).toHaveBeenCalledTimes(0) + expect(onConnData).toHaveBeenCalledTimes(2) + + // Check result looks good + expect(onConnData).toHaveBeenNthCalledWith(1, { + command: 'INFO', + data: [ + { + channel: { + test: [''], + }, + }, + ], + message: 'The command has been executed and data is being returned.', + reqId: 'cmd1', + responseCode: 201, + type: 'OK', + }) + expect(onConnData).toHaveBeenNthCalledWith(2, { + command: 'PLAY', + data: [], + message: 'The command has been executed.', + reqId: 'cmd2', + responseCode: 202, + type: 'OK', + }) + } finally { + // Ensure cleaned up + conn.disconnect() + } + }) + + it('receive broken response', async () => { + const conn = new Connection('127.0.0.1', 5250, true) + try { + expect(conn).toBeTruthy() + + const onConnError = jest.fn() + const onConnData = jest.fn() + conn.on('error', onConnError) + conn.on('data', onConnData) + + const sockets = SocketMock.openSockets() + expect(sockets).toHaveLength(1) + + // Dispatch a command + const sendError = await conn.sendCommand( + { + command: Commands.Info, + params: {}, + }, + 'cmd1' + ) + expect(sendError).toBeFalsy() + const sendError2 = await conn.sendCommand( + { + command: Commands.Play, + params: { + channel: 1, + layer: 10, + }, + }, + 'cmd2' + ) + expect(sendError2).toBeFalsy() + expect(onConnError).toHaveBeenCalledTimes(0) + expect(onConnData).toHaveBeenCalledTimes(0) + + // Info was sent + expect(onSocketWrite).toHaveBeenCalledTimes(2) + expect(onSocketWrite).toHaveBeenNthCalledWith(1, 'REQ cmd1 INFO\r\n', 'utf-8') + expect(onSocketWrite).toHaveBeenNthCalledWith(2, 'REQ cmd2 PLAY 1-10\r\n', 'utf-8') + + // Reply with a blob designed to crash the xml parser + sockets[0].mockData(Buffer.from(`201 INFO OK\r\n