secure_client.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict'
  2. var mqtt = require('..')
  3. var path = require('path')
  4. var abstractClientTests = require('./abstract_client')
  5. var fs = require('fs')
  6. var port = 9899
  7. var KEY = path.join(__dirname, 'helpers', 'tls-key.pem')
  8. var CERT = path.join(__dirname, 'helpers', 'tls-cert.pem')
  9. var WRONG_CERT = path.join(__dirname, 'helpers', 'wrong-cert.pem')
  10. var Server = require('./server')
  11. var server = new Server.SecureServer({
  12. key: fs.readFileSync(KEY),
  13. cert: fs.readFileSync(CERT)
  14. }, function (client) {
  15. client.on('connect', function (packet) {
  16. if (packet.clientId === 'invalid') {
  17. client.connack({returnCode: 2})
  18. } else {
  19. server.emit('connect', client)
  20. client.connack({returnCode: 0})
  21. }
  22. })
  23. client.on('publish', function (packet) {
  24. setImmediate(function () {
  25. /* jshint -W027 */
  26. /* eslint default-case:0 */
  27. switch (packet.qos) {
  28. case 0:
  29. break
  30. case 1:
  31. client.puback(packet)
  32. break
  33. case 2:
  34. client.pubrec(packet)
  35. break
  36. }
  37. /* jshint +W027 */
  38. })
  39. })
  40. client.on('pubrel', function (packet) {
  41. client.pubcomp(packet)
  42. })
  43. client.on('pubrec', function (packet) {
  44. client.pubrel(packet)
  45. })
  46. client.on('pubcomp', function () {
  47. // Nothing to be done
  48. })
  49. client.on('subscribe', function (packet) {
  50. client.suback({
  51. messageId: packet.messageId,
  52. granted: packet.subscriptions.map(function (e) {
  53. return e.qos
  54. })
  55. })
  56. })
  57. client.on('unsubscribe', function (packet) {
  58. client.unsuback(packet)
  59. })
  60. client.on('pingreq', function () {
  61. client.pingresp()
  62. })
  63. }).listen(port)
  64. describe('MqttSecureClient', function () {
  65. var config = { protocol: 'mqtts', port: port, rejectUnauthorized: false }
  66. abstractClientTests(server, config)
  67. describe('with secure parameters', function () {
  68. it('should validate successfully the CA', function (done) {
  69. var client = mqtt.connect({
  70. protocol: 'mqtts',
  71. port: port,
  72. ca: [fs.readFileSync(CERT)],
  73. rejectUnauthorized: true
  74. })
  75. client.on('error', function (err) {
  76. done(err)
  77. })
  78. server.once('connect', function () {
  79. done()
  80. })
  81. })
  82. it('should validate successfully the CA using URI', function (done) {
  83. var client = mqtt.connect('mqtts://localhost:' + port, {
  84. ca: [fs.readFileSync(CERT)],
  85. rejectUnauthorized: true
  86. })
  87. client.on('error', function (err) {
  88. done(err)
  89. })
  90. server.once('connect', function () {
  91. done()
  92. })
  93. })
  94. it('should validate successfully the CA using URI with path', function (done) {
  95. var client = mqtt.connect('mqtts://localhost:' + port + '/', {
  96. ca: [fs.readFileSync(CERT)],
  97. rejectUnauthorized: true
  98. })
  99. client.on('error', function (err) {
  100. done(err)
  101. })
  102. server.once('connect', function () {
  103. done()
  104. })
  105. })
  106. it('should validate unsuccessfully the CA', function (done) {
  107. var client = mqtt.connect({
  108. protocol: 'mqtts',
  109. port: port,
  110. ca: [fs.readFileSync(WRONG_CERT)],
  111. rejectUnauthorized: true
  112. })
  113. client.once('error', function () {
  114. done()
  115. client.end()
  116. client.on('error', function () {})
  117. })
  118. })
  119. it('should emit close on TLS error', function (done) {
  120. var client = mqtt.connect({
  121. protocol: 'mqtts',
  122. port: port,
  123. ca: [fs.readFileSync(WRONG_CERT)],
  124. rejectUnauthorized: true
  125. })
  126. client.on('error', function () {})
  127. // TODO node v0.8.x emits multiple close events
  128. client.once('close', function () {
  129. done()
  130. })
  131. })
  132. })
  133. })