Skip to content

Commit

Permalink
Merge 6860e12 into be13dff
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinDmello authored Aug 2, 2016
2 parents be13dff + 6860e12 commit e194063
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/handlers/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,26 @@ function authenticate (arg, done) {
client.broker.registerClient(client)
return done()
} else if (err) {
write(client, {
cmd: 'connack',
returnCode: 4
}, client.close.bind(client, done))
if (err && !err.returnCode) {
write(client, {
cmd: 'connack',
returnCode: 4
}, client.close.bind(client, done))
return
} else {
if (err.returnCode >= 1 && err.returnCode <= 3) {
write(client, {
cmd: 'connack',
returnCode: err.returnCode
}, client.close.bind(client, done))
} else {
// If errorCode is 4 or not a number
write(client, {
cmd: 'connack',
returnCode: 4
}, client.close.bind(client, done))
}
}
} else {
write(client, {
cmd: 'connack',
Expand Down
168 changes: 168 additions & 0 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,174 @@ test('authenticate errors', function (t) {
})
})

test('authentication error when return code 1 (unacceptable protocol version) is passed', function (t) {
t.plan(5)

var s = setup()

s.broker.authenticate = function (client, username, password, cb) {
t.ok(client instanceof Client, 'client is there')
t.equal(username, 'my username', 'username is there')
t.deepEqual(password, new Buffer('my pass'), 'password is there')
cb({ returnCode: 1 }, null)
}

s.outStream.on('data', function (packet) {
t.deepEqual(packet, {
cmd: 'connack',
returnCode: 1,
length: 2,
qos: 0,
retain: false,
dup: false,
topic: null,
payload: null,
sessionPresent: false
}, 'unsuccessful connack,unacceptable protocol version')
})

eos(s.outStream, function () {
t.pass('ended')
})

s.inStream.write({
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-client',
username: 'my username',
password: 'my pass',
keepalive: 0
})
})

test('authentication error when return code 2 (identifier rejected) is passed', function (t) {
t.plan(5)

var s = setup()

s.broker.authenticate = function (client, username, password, cb) {
t.ok(client instanceof Client, 'client is there')
t.equal(username, 'my username', 'username is there')
t.deepEqual(password, new Buffer('my pass'), 'password is there')
cb({ returnCode: 2 }, null)
}

s.outStream.on('data', function (packet) {
t.deepEqual(packet, {
cmd: 'connack',
returnCode: 2,
length: 2,
qos: 0,
retain: false,
dup: false,
topic: null,
payload: null,
sessionPresent: false
}, 'unsuccessful connack, identifier rejected')
})

eos(s.outStream, function () {
t.pass('ended')
})

s.inStream.write({
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-client',
username: 'my username',
password: 'my pass',
keepalive: 0
})
})

test('authentication error when return code 3 (Server unavailable) is passed', function (t) {
t.plan(5)

var s = setup()

s.broker.authenticate = function (client, username, password, cb) {
t.ok(client instanceof Client, 'client is there')
t.equal(username, 'my username', 'username is there')
t.deepEqual(password, new Buffer('my pass'), 'password is there')
cb({ returnCode: 3 }, null)
}

s.outStream.on('data', function (packet) {
t.deepEqual(packet, {
cmd: 'connack',
returnCode: 3,
length: 2,
qos: 0,
retain: false,
dup: false,
topic: null,
payload: null,
sessionPresent: false
}, 'unsuccessful connack, Server unavailable')
})

eos(s.outStream, function () {
t.pass('ended')
})

s.inStream.write({
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-client',
username: 'my username',
password: 'my pass',
keepalive: 0
})
})

test('authentication error when non numeric return code is passed', function (t) {
t.plan(5)

var s = setup()

s.broker.authenticate = function (client, username, password, cb) {
t.ok(client instanceof Client, 'client is there')
t.equal(username, 'my username', 'username is there')
t.deepEqual(password, new Buffer('my pass'), 'password is there')
cb({ returnCode: 'returnCode' }, null)
}

s.outStream.on('data', function (packet) {
t.deepEqual(packet, {
cmd: 'connack',
returnCode: 4,
length: 2,
qos: 0,
retain: false,
dup: false,
topic: null,
payload: null,
sessionPresent: false
}, 'unsuccessful connack, bad user name or password')
})

eos(s.outStream, function () {
t.pass('ended')
})

s.inStream.write({
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-client',
username: 'my username',
password: 'my pass',
keepalive: 0
})
})

test('authorize publish', function (t) {
t.plan(3)

Expand Down

0 comments on commit e194063

Please sign in to comment.