Skip to content

Commit

Permalink
Correctly generate special chars in CONNECT packet.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Oct 8, 2015
1 parent 032c660 commit fa6e75f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
6 changes: 3 additions & 3 deletions generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function connect(opts) {
if (!will.topic || 'string' !== typeof will.topic) {
throw new Error('Invalid will topic')
} else {
length += will.topic.length + 2
length += Buffer.byteLength(will.topic) + 2
}

// Payload
Expand All @@ -133,7 +133,7 @@ function connect(opts) {
// Username
if (username) {
if (username.length) {
length += username.length + 2
length += Buffer.byteLength(username) + 2
} else {
throw new Error('Invalid username')
}
Expand All @@ -142,7 +142,7 @@ function connect(opts) {
// Password
if (password) {
if (password.length) {
length += password.length + 2
length += Buffer.byteLength(password) + 2
} else {
throw new Error('Invalid password')
}
Expand Down
42 changes: 38 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,44 @@ testParseGenerate('maximal connect', {
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')
}, new Buffer([
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'
Expand Down Expand Up @@ -813,7 +851,3 @@ testGenerateError('Invalid password', {
, username: 'username'
, password: 42
})




0 comments on commit fa6e75f

Please sign in to comment.