From c4793e536d9f9c2295b31d61f9e0d63be5624da2 Mon Sep 17 00:00:00 2001 From: Siarhei Buntsevich Date: Thu, 1 Nov 2018 15:19:18 +0300 Subject: [PATCH] will payload and properties fix + test --- package.json | 3 +- test.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ writeToStream.js | 5 ++-- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 4d64ee2..2f98695 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "Matteo Collina (https://github.com/mcollina)", "Adam Rudd ", "Peter Sorowka (https://github.com/psorowka)", - "Wouter Klijn (https://github.com/wuhkuh)" + "Wouter Klijn (https://github.com/wuhkuh)", + "Siarhei Buntsevich (https://github.com/scarry1992)" ], "scripts": { "test": "tape test.js | tap-spec && standard", diff --git a/test.js b/test.js index b842c35..0f0abc4 100644 --- a/test.js +++ b/test.js @@ -260,6 +260,79 @@ testParseGenerate('connect MQTT 5.0', { 4, 3, 2, 1// Will payload ])) +testParseGenerate('connect MQTT 5.0 with will properties but w/o will payload', { + cmd: 'connect', + retain: false, + qos: 0, + dup: false, + length: 121, + protocolId: 'MQTT', + protocolVersion: 5, + will: { + retain: true, + qos: 2, + properties: { + willDelayInterval: 1234, + payloadFormatIndicator: false, + messageExpiryInterval: 4321, + contentType: 'test', + responseTopic: 'topic', + correlationData: Buffer.from([1, 2, 3, 4]), + userProperties: { + 'test': 'test' + } + }, + topic: 'topic', + payload: Buffer.from([]) + }, + clean: true, + keepalive: 30, + properties: { + sessionExpiryInterval: 1234, + receiveMaximum: 432, + maximumPacketSize: 100, + topicAliasMaximum: 456, + requestResponseInformation: true, + requestProblemInformation: true, + userProperties: { + 'test': 'test' + }, + authenticationMethod: 'test', + authenticationData: Buffer.from([1, 2, 3, 4]) + }, + clientId: 'test' +}, Buffer.from([ + 16, 121, // Header + 0, 4, // Protocol ID length + 77, 81, 84, 84, // Protocol ID + 5, // Protocol version + 54, // Connect flags + 0, 30, // Keepalive + 47, // properties length + 17, 0, 0, 4, 210, // sessionExpiryInterval + 33, 1, 176, // receiveMaximum + 39, 0, 0, 0, 100, // maximumPacketSize + 34, 1, 200, // topicAliasMaximum + 25, 1, // requestResponseInformation + 23, 1, // requestProblemInformation, + 38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116, // userProperties, + 21, 0, 4, 116, 101, 115, 116, // authenticationMethod + 22, 0, 4, 1, 2, 3, 4, // authenticationData + 0, 4, // Client ID length + 116, 101, 115, 116, // Client ID + 47, // will properties + 24, 0, 0, 4, 210, // will delay interval + 1, 0, // payload format indicator + 2, 0, 0, 16, 225, // message expiry interval + 3, 0, 4, 116, 101, 115, 116, // content type + 8, 0, 5, 116, 111, 112, 105, 99, // response topic + 9, 0, 4, 1, 2, 3, 4, // corelation data + 38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116, // user properties + 0, 5, // Will topic length + 116, 111, 112, 105, 99, // Will topic + 0, 0 // Will payload length +])) + testParseGenerate('connect MQTT 5.0 w/o will properties', { cmd: 'connect', retain: false, diff --git a/writeToStream.js b/writeToStream.js index 6042eae..4cc0702 100644 --- a/writeToStream.js +++ b/writeToStream.js @@ -160,12 +160,13 @@ function connect (packet, stream, opts) { } // Payload + length += 2 // payload length if (will.payload) { if (will.payload.length >= 0) { if (typeof will.payload === 'string') { - length += Buffer.byteLength(will.payload) + 2 + length += Buffer.byteLength(will.payload) } else { - length += will.payload.length + 2 + length += will.payload.length } } else { stream.emit('error', new Error('Invalid will payload'))