Skip to content

Commit

Permalink
Minor changes and more elaborate test docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Sorowka committed Jan 15, 2016
1 parent 2c5b0cc commit 38e00e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
23 changes: 8 additions & 15 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,14 @@ Parser.prototype.parse = function (buf) {

this._list.append(buf)

try {
while ((this.packet.length != -1 || this._list.length > 0) &&
(noError = this[this._states[this._stateCounter]]())) {
this._stateCounter++

while ((this.packet.length != -1 || this._list.length > 0) &&
(noError = this[this._states[this._stateCounter]]())) {
this._stateCounter++

if (this._stateCounter >= this._states.length) {
this._stateCounter = 0
}
if (this._stateCounter >= this._states.length) {
this._stateCounter = 0
}
}
catch (e) {

return this.emit('error', e);
}

return this._list.length
}
Expand Down Expand Up @@ -101,7 +94,7 @@ Parser.prototype._parseLength = function () {
this._list.consume(bytes)
}

return result
return result
}

Parser.prototype._parsePayload = function () {
Expand Down Expand Up @@ -171,7 +164,7 @@ Parser.prototype._parseConnect = function () {
if (protocolId === null)
return this.emit('error', new Error('cannot parse protocol id'))

if (['MQTT', 'MQIsdp'].indexOf(protocolId) < 0) {
if (protocolId != 'MQTT' && protocolId != 'MQIsdp') {

return this.emit('error', new Error('invalid protocol id'))
}
Expand All @@ -184,7 +177,7 @@ Parser.prototype._parseConnect = function () {

packet.protocolVersion = this._list.readUInt8(this._pos)

if([3,4].indexOf(packet.protocolVersion) < 0) {
if(packet.protocolVersion != 3 && packet.protocolVersion != 4) {

return this.emit('error', new Error('invalid protocol version'))
}
Expand Down
16 changes: 14 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,9 @@ testParseError('packet too short', new Buffer([
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 protocol id', new Buffer([
16, 18,
0, 6,
Expand All @@ -909,6 +912,8 @@ testParseError('invalid protocol id', new Buffer([
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', new Buffer([
16, 18,
0, 6,
Expand All @@ -920,9 +925,16 @@ testParseError('invalid protocol version', new Buffer([
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 protocol id', new Buffer([
16, 8,
0, 15,
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,
Expand Down

0 comments on commit 38e00e1

Please sign in to comment.