| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258 |
- 'use strict'
-
- var test = require('tape')
- var mqtt = require('./')
- var Buffer = require('safe-buffer').Buffer
- var WS = require('readable-stream').Writable
-
- function normalExpectedObject (object) {
- if (object.username != null) object.username = object.username.toString()
- if (object.password != null) object.password = Buffer.from(object.password)
- return object
- }
-
- function testParseGenerate (name, object, buffer, opts) {
- test(name + ' parse', function (t) {
- t.plan(2)
-
- var parser = mqtt.parser(opts)
- var expected = object
- var fixture = buffer
-
- parser.on('packet', function (packet) {
- if (packet.cmd !== 'publish') {
- delete packet.topic
- delete packet.payload
- }
- t.deepEqual(packet, normalExpectedObject(expected), 'expected packet')
- })
-
- parser.on('error', function (err) {
- t.fail(err)
- })
-
- t.equal(parser.parse(fixture), 0, 'remaining bytes')
- })
-
- test(name + ' generate', function (t) {
- t.equal(mqtt.generate(object).toString('hex'), buffer.toString('hex'))
- t.end()
- })
-
- test(name + ' mirror', function (t) {
- t.plan(2)
-
- var parser = mqtt.parser(opts)
- var expected = object
- var fixture = mqtt.generate(object)
-
- parser.on('packet', function (packet) {
- if (packet.cmd !== 'publish') {
- delete packet.topic
- delete packet.payload
- }
- t.deepEqual(packet, normalExpectedObject(expected), 'expected packet')
- })
-
- parser.on('error', function (err) {
- t.fail(err)
- })
-
- t.equal(parser.parse(fixture), 0, 'remaining bytes')
- })
- }
-
- function testParseError (expected, fixture) {
- test(expected, function (t) {
- t.plan(1)
-
- var parser = mqtt.parser()
-
- parser.on('error', function (err) {
- t.equal(err.message, expected, 'expected error message')
- })
-
- parser.on('packet', function () {
- t.fail('parse errors should not be followed by packet events')
- })
-
- parser.parse(fixture)
- })
- }
-
- function testGenerateError (expected, fixture) {
- test(expected, function (t) {
- t.plan(1)
-
- try {
- mqtt.generate(fixture)
- } catch (err) {
- t.equal(expected, err.message)
- }
- })
- }
-
- function testParseGenerateDefaults (name, object, buffer, opts) {
- test(name + ' parse', function (t) {
- var parser = mqtt.parser(opts)
- var expected = object
- var fixture = buffer
-
- t.plan(1 + Object.keys(expected).length)
-
- parser.on('packet', function (packet) {
- Object.keys(expected).forEach(function (key) {
- t.deepEqual(packet[key], expected[key], 'expected packet property ' + key)
- })
- })
-
- t.equal(parser.parse(fixture), 0, 'remaining bytes')
- })
-
- test(name + ' generate', function (t) {
- t.equal(mqtt.generate(object).toString('hex'), buffer.toString('hex'))
- t.end()
- })
- }
-
- function testWriteToStreamError (expected, fixture) {
- test('writeToStream ' + expected + ' error', function (t) {
- t.plan(2)
-
- var stream = WS()
-
- stream.write = () => t.fail('should not have called write')
- stream.on('error', () => t.pass('error emitted'))
-
- var result = mqtt.writeToStream(fixture, stream)
-
- t.false(result, 'result should be false')
- })
- }
-
- test('disabled numbers cache', function (t) {
- var stream = WS()
- var message = {
- cmd: 'publish',
- retain: false,
- qos: 0,
- dup: false,
- length: 10,
- topic: Buffer.from('test'),
- payload: Buffer.from('test')
- }
- var expected = Buffer.from([
- 48, 10, // Header
- 0, 4, // Topic length
- 116, 101, 115, 116, // Topic (test)
- 116, 101, 115, 116 // Payload (test)
- ])
- var written = Buffer.alloc(0)
-
- stream.write = (chunk) => {
- written = Buffer.concat([written, chunk])
- }
- mqtt.writeToStream.cacheNumbers = false
-
- mqtt.writeToStream(message, stream)
-
- t.deepEqual(written, expected, 'written buffer is expected')
-
- mqtt.writeToStream.cacheNumbers = true
-
- stream.end()
- t.end()
- })
-
- testParseGenerate('minimal connect', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 18,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- clean: false,
- keepalive: 30,
- clientId: 'test'
- }, Buffer.from([
- 16, 18, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 0, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116 // Client ID
- ]))
-
- testParseGenerate('no clientId with 3.1.1', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 12,
- protocolId: 'MQTT',
- protocolVersion: 4,
- clean: true,
- keepalive: 30,
- clientId: ''
- }, Buffer.from([
- 16, 12, // Header
- 0, 4, // Protocol ID length
- 77, 81, 84, 84, // Protocol ID
- 4, // Protocol version
- 2, // Connect flags
- 0, 30, // Keepalive
- 0, 0 // Client ID length
- ]))
-
- testParseGenerateDefaults('default connect', {
- cmd: 'connect',
- clientId: 'test'
- }, Buffer.from([
- 16, 16, 0, 4, 77, 81, 84,
- 84, 4, 2, 0, 0,
- 0, 4, 116, 101, 115, 116
- ]))
-
- testParseGenerate('empty will payload', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 47,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: new Buffer(0)
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: new Buffer('password')
- }, Buffer.from([
- 16, 47, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 246, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116, // Client ID
- 0, 5, // Will topic length
- 116, 111, 112, 105, 99, // Will topic
- 0, 0, // Will payload length
- // Will payload
- 0, 8, // Username length
- 117, 115, 101, 114, 110, 97, 109, 101, // Username
- 0, 8, // Password length
- 112, 97, 115, 115, 119, 111, 114, 100 // Password
- ]))
-
- testParseGenerate('empty buffer username payload', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 20,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: new Buffer('')
- }, Buffer.from([
- 16, 20, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 130, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116, // Client ID
- 0, 0 // Username length
- // Empty Username payload
- ]))
-
- testParseGenerate('empty string username payload', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 20,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: ''
- }, Buffer.from([
- 16, 20, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 130, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116, // Client ID
- 0, 0 // Username length
- // Empty Username payload
- ]))
-
- testParseGenerate('empty buffer password payload', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 30,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: new Buffer('')
- }, Buffer.from([
- 16, 30, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 194, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116, // Client ID
- 0, 8, // Username length
- 117, 115, 101, 114, 110, 97, 109, 101, // Username payload
- 0, 0 // Password length
- // Empty password payload
- ]))
-
- testParseGenerate('empty string password payload', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 30,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: ''
- }, Buffer.from([
- 16, 30, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 194, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116, // Client ID
- 0, 8, // Username length
- 117, 115, 101, 114, 110, 97, 109, 101, // Username payload
- 0, 0 // Password length
- // Empty password payload
- ]))
-
- testParseGenerate('empty string username and password payload', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 22,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: '',
- password: new Buffer('')
- }, Buffer.from([
- 16, 22, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 194, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116, // Client ID
- 0, 0, // Username length
- // Empty Username payload
- 0, 0 // Password length
- // Empty password payload
- ]))
-
- testParseGenerate('maximal connect', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: new Buffer('payload')
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: new Buffer('password')
- }, Buffer.from([
- 16, 54, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 246, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116, // Client ID
- 0, 5, // Will topic length
- 116, 111, 112, 105, 99, // Will topic
- 0, 7, // Will payload length
- 112, 97, 121, 108, 111, 97, 100, // Will payload
- 0, 8, // Username length
- 117, 115, 101, 114, 110, 97, 109, 101, // Username
- 0, 8, // Password length
- 112, 97, 115, 115, 119, 111, 114, 100 // Password
- ]))
-
- testParseGenerate('max connect with special chars', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 57,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'tòpic',
- payload: new Buffer('pay£oad')
- },
- clean: true,
- keepalive: 30,
- clientId: 'te$t',
- username: 'u$ern4me',
- password: new Buffer('p4$$w0£d')
- }, Buffer.from([
- 16, 57, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 246, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 36, 116, // Client ID
- 0, 6, // Will topic length
- 116, 195, 178, 112, 105, 99, // Will topic
- 0, 8, // Will payload length
- 112, 97, 121, 194, 163, 111, 97, 100, // Will payload
- 0, 8, // Username length
- 117, 36, 101, 114, 110, 52, 109, 101, // Username
- 0, 9, // Password length
- 112, 52, 36, 36, 119, 48, 194, 163, 100 // Password
- ]))
-
- test('connect all strings generate', function (t) {
- var message = {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 'payload'
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: 'password'
- }
- var expected = Buffer.from([
- 16, 54, // Header
- 0, 6, // Protocol ID length
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 3, // Protocol version
- 246, // Connect flags
- 0, 30, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116, // Client ID
- 0, 5, // Will topic length
- 116, 111, 112, 105, 99, // Will topic
- 0, 7, // Will payload length
- 112, 97, 121, 108, 111, 97, 100, // Will payload
- 0, 8, // Username length
- 117, 115, 101, 114, 110, 97, 109, 101, // Username
- 0, 8, // Password length
- 112, 97, 115, 115, 119, 111, 114, 100 // Password
- ])
-
- t.equal(mqtt.generate(message).toString('hex'), expected.toString('hex'))
- t.end()
- })
-
- testParseError('Cannot parse protocolId', Buffer.from([
- 16, 4,
- 0, 6,
- 77, 81
- ]))
-
- testParseGenerate('connack with return code 0', {
- cmd: 'connack',
- retain: false,
- qos: 0,
- dup: false,
- length: 2,
- sessionPresent: false,
- returnCode: 0
- }, Buffer.from([
- 32, 2, 0, 0
- ]))
-
- testParseGenerate('connack with return code 0 session present bit set', {
- cmd: 'connack',
- retain: false,
- qos: 0,
- dup: false,
- length: 2,
- sessionPresent: true,
- returnCode: 0
- }, Buffer.from([
- 32, 2, 1, 0
- ]))
-
- testParseGenerate('connack with return code 5', {
- cmd: 'connack',
- retain: false,
- qos: 0,
- dup: false,
- length: 2,
- sessionPresent: false,
- returnCode: 5
- }, Buffer.from([
- 32, 2, 0, 5
- ]))
-
- testParseGenerate('minimal publish', {
- cmd: 'publish',
- retain: false,
- qos: 0,
- dup: false,
- length: 10,
- topic: 'test',
- payload: new Buffer('test')
- }, Buffer.from([
- 48, 10, // Header
- 0, 4, // Topic length
- 116, 101, 115, 116, // Topic (test)
- 116, 101, 115, 116 // Payload (test)
- ]))
-
- ;(function () {
- var buffer = new Buffer(2048)
- testParseGenerate('2KB publish packet', {
- cmd: 'publish',
- retain: false,
- qos: 0,
- dup: false,
- length: 2054,
- topic: 'test',
- payload: buffer
- }, Buffer.concat([Buffer.from([
- 48, 134, 16, // Header
- 0, 4, // Topic length
- 116, 101, 115, 116 // Topic (test)
- ]), buffer]))
- })()
-
- ;(function () {
- var buffer = new Buffer(2 * 1024 * 1024)
- testParseGenerate('2MB publish packet', {
- cmd: 'publish',
- retain: false,
- qos: 0,
- dup: false,
- length: 6 + 2 * 1024 * 1024,
- topic: 'test',
- payload: buffer
- }, Buffer.concat([Buffer.from([
- 48, 134, 128, 128, 1, // Header
- 0, 4, // Topic length
- 116, 101, 115, 116 // Topic (test)
- ]), buffer]))
- })()
-
- testParseGenerate('maximal publish', {
- cmd: 'publish',
- retain: true,
- qos: 2,
- length: 12,
- dup: true,
- topic: 'test',
- messageId: 10,
- payload: new Buffer('test')
- }, Buffer.from([
- 61, 12, // Header
- 0, 4, // Topic length
- 116, 101, 115, 116, // Topic
- 0, 10, // Message ID
- 116, 101, 115, 116 // Payload
- ]))
-
- test('publish all strings generate', function (t) {
- var message = {
- cmd: 'publish',
- retain: true,
- qos: 2,
- length: 12,
- dup: true,
- topic: 'test',
- messageId: 10,
- payload: new Buffer('test')
- }
- var expected = Buffer.from([
- 61, 12, // Header
- 0, 4, // Topic length
- 116, 101, 115, 116, // Topic
- 0, 10, // Message ID
- 116, 101, 115, 116 // Payload
- ])
-
- t.equal(mqtt.generate(message).toString('hex'), expected.toString('hex'))
- t.end()
- })
-
- testParseGenerate('empty publish', {
- cmd: 'publish',
- retain: false,
- qos: 0,
- dup: false,
- length: 6,
- topic: 'test',
- payload: new Buffer(0)
- }, Buffer.from([
- 48, 6, // Header
- 0, 4, // Topic length
- 116, 101, 115, 116 // Topic
- // Empty payload
- ]))
-
- test('splitted publish parse', function (t) {
- t.plan(3)
-
- var parser = mqtt.parser()
- var expected = {
- cmd: 'publish',
- retain: false,
- qos: 0,
- dup: false,
- length: 10,
- topic: 'test',
- payload: new Buffer('test')
- }
-
- parser.on('packet', function (packet) {
- t.deepEqual(packet, expected, 'expected packet')
- })
-
- t.equal(parser.parse(Buffer.from([
- 48, 10, // Header
- 0, 4, // Topic length
- 116, 101, 115, 116 // Topic (test)
- ])), 6, 'remaining bytes')
-
- t.equal(parser.parse(Buffer.from([
- 116, 101, 115, 116 // Payload (test)
- ])), 0, 'remaining bytes')
- })
-
- testParseGenerate('puback', {
- cmd: 'puback',
- retain: false,
- qos: 0,
- dup: false,
- length: 2,
- messageId: 2
- }, Buffer.from([
- 64, 2, // Header
- 0, 2 // Message ID
- ]))
-
- testParseGenerate('pubrec', {
- cmd: 'pubrec',
- retain: false,
- qos: 0,
- dup: false,
- length: 2,
- messageId: 2
- }, Buffer.from([
- 80, 2, // Header
- 0, 2 // Message ID
- ]))
-
- testParseGenerate('pubrel', {
- cmd: 'pubrel',
- retain: false,
- qos: 1,
- dup: false,
- length: 2,
- messageId: 2
- }, Buffer.from([
- 98, 2, // Header
- 0, 2 // Message ID
- ]))
-
- testParseGenerate('pubcomp', {
- cmd: 'pubcomp',
- retain: false,
- qos: 0,
- dup: false,
- length: 2,
- messageId: 2
- }, Buffer.from([
- 112, 2, // Header
- 0, 2 // Message ID
- ]))
-
- testParseError('Wrong subscribe header', Buffer.from([
- 128, 9, // Header (subscribeqos=0length=9)
- 0, 6, // Message ID (6)
- 0, 4, // Topic length,
- 116, 101, 115, 116, // Topic (test)
- 0 // Qos (0)
- ]))
-
- testParseGenerate('subscribe to one topic', {
- cmd: 'subscribe',
- retain: false,
- qos: 1,
- dup: false,
- length: 9,
- subscriptions: [
- {
- topic: 'test',
- qos: 0
- }
- ],
- messageId: 6
- }, Buffer.from([
- 130, 9, // Header (subscribeqos=1length=9)
- 0, 6, // Message ID (6)
- 0, 4, // Topic length,
- 116, 101, 115, 116, // Topic (test)
- 0 // Qos (0)
- ]))
-
- testParseGenerate('subscribe to three topics', {
- cmd: 'subscribe',
- retain: false,
- qos: 1,
- dup: false,
- length: 23,
- subscriptions: [
- {
- topic: 'test',
- qos: 0
- }, {
- topic: 'uest',
- qos: 1
- }, {
- topic: 'tfst',
- qos: 2
- }
- ],
- messageId: 6
- }, Buffer.from([
- 130, 23, // Header (publishqos=1length=9)
- 0, 6, // Message ID (6)
- 0, 4, // Topic length,
- 116, 101, 115, 116, // Topic (test)
- 0, // Qos (0)
- 0, 4, // Topic length
- 117, 101, 115, 116, // Topic (uest)
- 1, // Qos (1)
- 0, 4, // Topic length
- 116, 102, 115, 116, // Topic (tfst)
- 2 // Qos (2)
- ]))
-
- testParseGenerate('suback', {
- cmd: 'suback',
- retain: false,
- qos: 0,
- dup: false,
- length: 6,
- granted: [0, 1, 2, 128],
- messageId: 6
- }, Buffer.from([
- 144, 6, // Header
- 0, 6, // Message ID
- 0, 1, 2, 128 // Granted qos (0, 1, 2) and a rejected being 0x80
- ]))
-
- testParseGenerate('unsubscribe', {
- cmd: 'unsubscribe',
- retain: false,
- qos: 1,
- dup: false,
- length: 14,
- unsubscriptions: [
- 'tfst',
- 'test'
- ],
- messageId: 7
- }, Buffer.from([
- 162, 14,
- 0, 7, // Message ID (7)
- 0, 4, // Topic length
- 116, 102, 115, 116, // Topic (tfst)
- 0, 4, // Topic length,
- 116, 101, 115, 116 // Topic (test)
- ]))
-
- testParseGenerate('unsuback', {
- cmd: 'unsuback',
- retain: false,
- qos: 0,
- dup: false,
- length: 2,
- messageId: 8
- }, Buffer.from([
- 176, 2, // Header
- 0, 8 // Message ID
- ]))
-
- testParseGenerate('pingreq', {
- cmd: 'pingreq',
- retain: false,
- qos: 0,
- dup: false,
- length: 0
- }, Buffer.from([
- 192, 0 // Header
- ]))
-
- testParseGenerate('pingresp', {
- cmd: 'pingresp',
- retain: false,
- qos: 0,
- dup: false,
- length: 0
- }, Buffer.from([
- 208, 0 // Header
- ]))
-
- testParseGenerate('disconnect', {
- cmd: 'disconnect',
- retain: false,
- qos: 0,
- dup: false,
- length: 0
- }, Buffer.from([
- 224, 0 // Header
- ]))
-
- testGenerateError('Unknown command', {})
-
- testGenerateError('Invalid protocolId', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 42,
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 'payload'
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: 'password'
- })
-
- testGenerateError('clientId must be supplied before 3.1.1', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 'payload'
- },
- clean: true,
- keepalive: 30,
- username: 'username',
- password: 'password'
- })
-
- testGenerateError('clientId must be given if cleanSession set to 0', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQTT',
- protocolVersion: 4,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 'payload'
- },
- clean: false,
- keepalive: 30,
- username: 'username',
- password: 'password'
- })
-
- testGenerateError('Invalid keepalive', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 'payload'
- },
- clean: true,
- keepalive: 'hello',
- clientId: 'test',
- username: 'username',
- password: 'password'
- })
-
- testGenerateError('Invalid keepalive', {
- cmd: 'connect',
- keepalive: 3.1416
- })
-
- testGenerateError('Invalid will', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: 42,
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: 'password'
- })
-
- testGenerateError('Invalid will topic', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- payload: 'payload'
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: 'password'
- })
-
- testGenerateError('Invalid will payload', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 42
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: 'password'
- })
-
- testGenerateError('Invalid username', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 'payload'
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 42,
- password: 'password'
- })
-
- testGenerateError('Invalid password', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 'payload'
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- username: 'username',
- password: 42
- })
-
- testGenerateError('Username is required to use password', {
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 54,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- will: {
- retain: true,
- qos: 2,
- topic: 'topic',
- payload: 'payload'
- },
- clean: true,
- keepalive: 30,
- clientId: 'test',
- password: 'password'
- })
-
- test('support cork', function (t) {
- t.plan(9)
-
- var dest = WS()
-
- dest._write = function (chunk, enc, cb) {
- t.pass('_write called')
- cb()
- }
-
- mqtt.writeToStream({
- cmd: 'connect',
- retain: false,
- qos: 0,
- dup: false,
- length: 18,
- protocolId: 'MQIsdp',
- protocolVersion: 3,
- clean: false,
- keepalive: 30,
- clientId: 'test'
- }, dest)
-
- dest.end()
- })
-
- // The following test case was designed after experiencing errors
- // when trying to connect with tls on a non tls mqtt port
- // the specific behaviour is:
- // - first byte suggests this is a connect message
- // - second byte suggests message length to be smaller than buffer length
- // thus payload processing starts
- // - the first two bytes suggest a protocol identifier string length
- // that leads the parser pointer close to the end of the buffer
- // - when trying to read further connect flags the buffer produces
- // a "out of range" Error
- //
- testParseError('Packet too short', Buffer.from([
- 16, 9,
- 0, 6,
- 77, 81, 73, 115, 100, 112,
- 3
- ]))
-
- // CONNECT Packets that show other protocol IDs than
- // the valid values MQTT and MQIsdp should cause an error
- // those packets are a hint that this is not a mqtt connection
- testParseError('Invalid protocolId', Buffer.from([
- 16, 18,
- 0, 6,
- 65, 65, 65, 65, 65, 65, // AAAAAA
- 3, // Protocol version
- 0, // Connect flags
- 0, 10, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116 // Client ID
- ]))
-
- // CONNECT Packets that contain an unsupported protocol version
- // Flag (i.e. not `3` or `4`) should cause an error
- testParseError('Invalid protocol version', Buffer.from([
- 16, 18,
- 0, 6,
- 77, 81, 73, 115, 100, 112, // Protocol ID
- 1, // Protocol version
- 0, // Connect flags
- 0, 10, // Keepalive
- 0, 4, // Client ID length
- 116, 101, 115, 116 // Client ID
- ]))
-
- // When a packet contains a string in the variable header and the
- // given string length of this exceeds the overall length of the packet that
- // was specified in the fixed header, parsing must fail.
- // this case simulates this behavior with the protocol ID string of the
- // CONNECT packet. The fixed header suggests a remaining length of 8 bytes
- // which would be exceeded by the string length of 15
- // in this case, a protocol ID parse error is expected
- testParseError('Cannot parse protocolId', Buffer.from([
- 16, 8, // Fixed header
- 0, 15, // string length 15 --> 15 > 8 --> error!
- 77, 81, 73, 115, 100, 112,
- 77, 81, 73, 115, 100, 112,
- 77, 81, 73, 115, 100, 112,
- 77, 81, 73, 115, 100, 112,
- 77, 81, 73, 115, 100, 112,
- 77, 81, 73, 115, 100, 112,
- 77, 81, 73, 115, 100, 112,
- 77, 81, 73, 115, 100, 112
- ]))
-
- test('stops parsing after first error', function (t) {
- t.plan(4)
-
- var parser = mqtt.parser()
-
- var packetCount = 0
- var errorCount = 0
- var expectedPackets = 1
- var expectedErrors = 1
-
- parser.on('packet', function (packet) {
- t.ok(++packetCount <= expectedPackets, 'expected <= ' + expectedPackets + ' packets')
- })
-
- parser.on('error', function (erroneous) {
- t.ok(++errorCount <= expectedErrors, 'expected <= ' + expectedErrors + ' errors')
- })
-
- parser.parse(Buffer.from([
- // First, a valid connect packet:
-
- 16, 12, // Header
- 0, 4, // Protocol ID length
- 77, 81, 84, 84, // Protocol ID
- 4, // Protocol version
- 2, // Connect flags
- 0, 30, // Keepalive
- 0, 0, // Client ID length
-
- // Then an invalid subscribe packet:
-
- 128, 9, // Header (subscribeqos=0length=9)
- 0, 6, // Message ID (6)
- 0, 4, // Topic length,
- 116, 101, 115, 116, // Topic (test)
- 0, // Qos (0)
-
- // And another invalid subscribe packet:
-
- 128, 9, // Header (subscribeqos=0length=9)
- 0, 6, // Message ID (6)
- 0, 4, // Topic length,
- 116, 101, 115, 116, // Topic (test)
- 0, // Qos (0)
-
- // Finally, a valid disconnect packet:
-
- 224, 0 // Header
- ]))
-
- // Calling parse again clears the error and continues parsing
- packetCount = 0
- errorCount = 0
- expectedPackets = 2
- expectedErrors = 0
-
- parser.parse(Buffer.from([
- // Connect:
-
- 16, 12, // Header
- 0, 4, // Protocol ID length
- 77, 81, 84, 84, // Protocol ID
- 4, // Protocol version
- 2, // Connect flags
- 0, 30, // Keepalive
- 0, 0, // Client ID length
-
- // Disconnect:
-
- 224, 0 // Header
- ]))
- })
-
- testWriteToStreamError('Invalid protocolId', {
- cmd: 'connect',
- protocolId: {}
- })
-
- testWriteToStreamError('Invalid topic', {
- cmd: 'publish',
- topic: {}
- })
-
- testWriteToStreamError('Invalid messageId', {
- cmd: 'subscribe',
- mid: {}
- })
|