mqtt 2.18.6 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,12 +71,14 @@ module.exports = function abstractStoreTest (build) {
71
71
 
72
72
  it('should replace a packet when doing put with the same messageId', function (done) {
73
73
  var packet1 = {
74
+ cmd: 'publish', // added
74
75
  topic: 'hello',
75
76
  payload: 'world',
76
77
  qos: 2,
77
78
  messageId: 42
78
79
  }
79
80
  var packet2 = {
81
+ cmd: 'pubrel', // added
80
82
  qos: 2,
81
83
  messageId: 42
82
84
  }
@@ -130,30 +132,4 @@ module.exports = function abstractStoreTest (build) {
130
132
  })
131
133
  })
132
134
  })
133
-
134
- it('should replace a packet when doing put with the same messageId', function (done) {
135
- var packet1 = {
136
- cmd: 'publish', // added
137
- topic: 'hello',
138
- payload: 'world',
139
- qos: 2,
140
- messageId: 42
141
- }
142
- var packet2 = {
143
- cmd: 'pubrel', // added
144
- qos: 2,
145
- messageId: 42
146
- }
147
-
148
- store.put(packet1, function () {
149
- store.put(packet2, function () {
150
- store
151
- .createStream()
152
- .on('data', function (data) {
153
- data.should.eql(packet2)
154
- done()
155
- })
156
- })
157
- })
158
- })
159
135
  }
package/test/client.js CHANGED
@@ -12,6 +12,7 @@ var Buffer = require('safe-buffer').Buffer
12
12
  var Duplex = require('readable-stream').Duplex
13
13
  var Connection = require('mqtt-connection')
14
14
  var Server = require('./server')
15
+ var FastServer = require('./server').FastMqttServer
15
16
  var port = 9876
16
17
  var server
17
18
 
@@ -26,13 +27,35 @@ function connOnlyServer () {
26
27
  /**
27
28
  * Test server
28
29
  */
29
- function buildServer () {
30
- return new Server(function (client) {
30
+ function buildServer (fastFlag) {
31
+ var handler = function (client) {
32
+ client.on('auth', function (packet) {
33
+ var rc = 'reasonCode'
34
+ var connack = {}
35
+ connack[rc] = 0
36
+ client.connack(connack)
37
+ })
31
38
  client.on('connect', function (packet) {
32
- if (packet.clientId === 'invalid') {
33
- client.connack({returnCode: 2})
39
+ var rc = 'returnCode'
40
+ var connack = {}
41
+ if (client.options && client.options.protocolVersion === 5) {
42
+ rc = 'reasonCode'
43
+ if (packet.clientId === 'invalid') {
44
+ connack[rc] = 128
45
+ } else {
46
+ connack[rc] = 0
47
+ }
48
+ } else {
49
+ if (packet.clientId === 'invalid') {
50
+ connack[rc] = 2
51
+ } else {
52
+ connack[rc] = 0
53
+ }
54
+ }
55
+ if (packet.properties && packet.properties.authenticationMethod) {
56
+ return false
34
57
  } else {
35
- client.connack({returnCode: 0})
58
+ client.connack(connack)
36
59
  }
37
60
  })
38
61
 
@@ -73,13 +96,19 @@ function buildServer () {
73
96
  })
74
97
 
75
98
  client.on('unsubscribe', function (packet) {
99
+ packet.granted = packet.unsubscriptions.map(function () { return 0 })
76
100
  client.unsuback(packet)
77
101
  })
78
102
 
79
103
  client.on('pingreq', function () {
80
104
  client.pingresp()
81
105
  })
82
- })
106
+ }
107
+ if (fastFlag) {
108
+ return new FastServer(handler)
109
+ } else {
110
+ return new Server(handler)
111
+ }
83
112
  }
84
113
 
85
114
  server = buildServer().listen(port)
@@ -263,14 +292,15 @@ describe('MqttClient', function () {
263
292
  it('should reconnect to multiple host-ports-protocol combinations if servers is passed', function (done) {
264
293
  this.timeout(15000)
265
294
 
266
- var server2 = buildServer().listen(port + 42)
295
+ var server = buildServer(true).listen(port + 41)
296
+ var server2 = buildServer(true).listen(port + 42)
267
297
 
268
298
  server2.on('listening', function () {
269
299
  var client = mqtt.connect({
270
300
  protocol: 'wss',
271
301
  servers: [
272
302
  { port: port + 42, host: 'localhost', protocol: 'ws' },
273
- { port: port, host: 'localhost' }
303
+ { port: port + 41, host: 'localhost' }
274
304
  ],
275
305
  keepalive: 50
276
306
  })
@@ -281,7 +311,7 @@ describe('MqttClient', function () {
281
311
  })
282
312
 
283
313
  server.once('client', function () {
284
- should.equal(client.stream.socket.url, 'wss://localhost:9876/', 'Protocol for second client should use the default protocol: wss, on port: port + 42.')
314
+ should.equal(client.stream.socket.url, 'wss://localhost:9917/', 'Protocol for second client should use the default protocol: wss, on port: port + 42.')
285
315
  client.end()
286
316
  done()
287
317
  })
@@ -541,4 +571,559 @@ describe('MqttClient', function () {
541
571
  })
542
572
  })
543
573
  })
574
+
575
+ it('check emit error on checkDisconnection w/o callback', function (done) {
576
+ this.timeout(15000)
577
+ var server118 = new Server(function (client) {
578
+ client.on('connect', function (packet) {
579
+ client.connack({
580
+ reasonCode: 0
581
+ })
582
+ })
583
+ client.on('publish', function (packet) {
584
+ setImmediate(function () {
585
+ packet.reasonCode = 0
586
+ client.puback(packet)
587
+ })
588
+ })
589
+ }).listen(port + 118)
590
+ var opts = {
591
+ host: 'localhost',
592
+ port: port + 118,
593
+ protocolVersion: 5
594
+ }
595
+ var client = mqtt.connect(opts)
596
+ client.on('error', function (error) {
597
+ should(error.message).be.equal('client disconnecting')
598
+ server118.close()
599
+ done()
600
+ })
601
+ client.on('connect', function () {
602
+ client.end(function () {
603
+ client._checkDisconnecting()
604
+ })
605
+ server118.close()
606
+ })
607
+ })
608
+
609
+ describe('MQTT 5.0', function () {
610
+ var server = buildServer().listen(port + 115)
611
+ var config = { protocol: 'mqtt', port: port + 115, protocolVersion: 5, properties: { maximumPacketSize: 200 } }
612
+ abstractClientTests(server, config)
613
+ it('should has Auth method with Auth data', function (done) {
614
+ this.timeout(5000)
615
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { authenticationData: Buffer.from([1, 2, 3, 4]) }}
616
+ try {
617
+ mqtt.connect(opts)
618
+ } catch (error) {
619
+ should(error.message).be.equal('Packet has no Authentication Method')
620
+ }
621
+ done()
622
+ })
623
+ it('auth packet', function (done) {
624
+ this.timeout(15000)
625
+ server.once('client', function (client) {
626
+ client.on('auth', function (packet) {
627
+ done()
628
+ })
629
+ })
630
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { authenticationMethod: 'json' }, authPacket: {}}
631
+ mqtt.connect(opts)
632
+ })
633
+ it('Maximum Packet Size', function (done) {
634
+ this.timeout(15000)
635
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { maximumPacketSize: 1 }}
636
+ var client = mqtt.connect(opts)
637
+ client.on('error', function (error) {
638
+ should(error.message).be.equal('exceeding packets size connack')
639
+ done()
640
+ })
641
+ })
642
+ describe('Topic Alias', function () {
643
+ it('topicAlias > topicAliasMaximum', function (done) {
644
+ this.timeout(15000)
645
+ var maximum = 15
646
+ var current = 22
647
+ server.once('client', function (client) {
648
+ client.on('publish', function (packet) {
649
+ if (packet.properties && packet.properties.topicAlias) {
650
+ done(new Error('Packet should not have topicAlias'))
651
+ return false
652
+ }
653
+ done()
654
+ })
655
+ })
656
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5, properties: { topicAliasMaximum: maximum }}
657
+ var client = mqtt.connect(opts)
658
+ client.publish('t/h', 'Message', { properties: { topicAlias: current } })
659
+ })
660
+ it('topicAlias w/o topicAliasMaximum in settings', function (done) {
661
+ this.timeout(15000)
662
+ server.once('client', function (client) {
663
+ client.on('publish', function (packet) {
664
+ if (packet.properties && packet.properties.topicAlias) {
665
+ done(new Error('Packet should not have topicAlias'))
666
+ return false
667
+ }
668
+ done()
669
+ })
670
+ })
671
+ var opts = {host: 'localhost', port: port + 115, protocolVersion: 5}
672
+ var client = mqtt.connect(opts)
673
+ client.publish('t/h', 'Message', { properties: { topicAlias: 22 } })
674
+ })
675
+ })
676
+ it('Change values of some properties by server response', function (done) {
677
+ this.timeout(15000)
678
+ var server116 = new Server(function (client) {
679
+ client.on('connect', function (packet) {
680
+ client.connack({
681
+ reasonCode: 0,
682
+ properties: {
683
+ topicAliasMaximum: 15,
684
+ serverKeepAlive: 16,
685
+ maximumPacketSize: 95
686
+ }
687
+ })
688
+ })
689
+ }).listen(port + 116)
690
+ var opts = {
691
+ host: 'localhost',
692
+ port: port + 116,
693
+ protocolVersion: 5,
694
+ properties: {
695
+ topicAliasMaximum: 10,
696
+ serverKeepAlive: 11,
697
+ maximumPacketSize: 100
698
+ }
699
+ }
700
+ var client = mqtt.connect(opts)
701
+ client.on('connect', function () {
702
+ should(client.options.keepalive).be.equal(16)
703
+ should(client.options.properties.topicAliasMaximum).be.equal(15)
704
+ should(client.options.properties.maximumPacketSize).be.equal(95)
705
+ server116.close()
706
+ done()
707
+ })
708
+ })
709
+
710
+ it('should resubscribe when reconnecting with protocolVersion 5 and Session Present flag is false', function (done) {
711
+ this.timeout(15000)
712
+ var tryReconnect = true
713
+ var reconnectEvent = false
714
+ var server316 = new Server(function (client) {
715
+ client.on('connect', function (packet) {
716
+ client.connack({
717
+ reasonCode: 0,
718
+ sessionPresent: false
719
+ })
720
+ client.on('subscribe', function () {
721
+ if (!tryReconnect) {
722
+ client.end()
723
+ server316.close()
724
+ done()
725
+ }
726
+ })
727
+ })
728
+ }).listen(port + 316)
729
+ var opts = {
730
+ host: 'localhost',
731
+ port: port + 316,
732
+ protocolVersion: 5
733
+ }
734
+ var client = mqtt.connect(opts)
735
+
736
+ client.on('reconnect', function () {
737
+ reconnectEvent = true
738
+ })
739
+
740
+ client.on('connect', function (connack) {
741
+ should(connack.sessionPresent).be.equal(false)
742
+ if (tryReconnect) {
743
+ client.subscribe('hello', function () {
744
+ client.stream.end()
745
+ })
746
+
747
+ tryReconnect = false
748
+ } else {
749
+ reconnectEvent.should.equal(true)
750
+ }
751
+ })
752
+ })
753
+
754
+ it('should resubscribe when reconnecting with protocolVersion 5 and properties', function (done) {
755
+ this.timeout(15000)
756
+ var tryReconnect = true
757
+ var reconnectEvent = false
758
+ var server326 = new Server(function (client) {
759
+ client.on('connect', function (packet) {
760
+ client.on('subscribe', function (packet) {
761
+ if (!reconnectEvent) {
762
+ client.suback({
763
+ messageId: packet.messageId,
764
+ granted: packet.subscriptions.map(function (e) {
765
+ return e.qos
766
+ })
767
+ })
768
+ } else {
769
+ if (!tryReconnect) {
770
+ should(packet.properties.userProperties.test).be.equal('test')
771
+ client.end()
772
+ server326.close()
773
+ done()
774
+ }
775
+ }
776
+ })
777
+ client.connack({
778
+ reasonCode: 0,
779
+ sessionPresent: false
780
+ })
781
+ })
782
+ }).listen(port + 326)
783
+ var opts = {
784
+ host: 'localhost',
785
+ port: port + 326,
786
+ protocolVersion: 5
787
+ }
788
+ var client = mqtt.connect(opts)
789
+
790
+ client.on('reconnect', function () {
791
+ reconnectEvent = true
792
+ })
793
+
794
+ client.on('connect', function (connack) {
795
+ should(connack.sessionPresent).be.equal(false)
796
+ if (tryReconnect) {
797
+ client.subscribe('hello', { properties: { userProperties: { test: 'test' } } }, function () {
798
+ client.stream.end()
799
+ })
800
+
801
+ tryReconnect = false
802
+ } else {
803
+ reconnectEvent.should.equal(true)
804
+ }
805
+ })
806
+ })
807
+
808
+ var serverErr = new Server(function (client) {
809
+ client.on('connect', function (packet) {
810
+ client.connack({
811
+ reasonCode: 0
812
+ })
813
+ })
814
+ client.on('publish', function (packet) {
815
+ setImmediate(function () {
816
+ switch (packet.qos) {
817
+ case 0:
818
+ break
819
+ case 1:
820
+ packet.reasonCode = 142
821
+ delete packet.cmd
822
+ client.puback(packet)
823
+ break
824
+ case 2:
825
+ packet.reasonCode = 142
826
+ delete packet.cmd
827
+ client.pubrec(packet)
828
+ break
829
+ }
830
+ })
831
+ })
832
+
833
+ client.on('pubrel', function (packet) {
834
+ packet.reasonCode = 142
835
+ delete packet.cmd
836
+ client.pubcomp(packet)
837
+ })
838
+ })
839
+ it('Subscribe properties', function (done) {
840
+ this.timeout(15000)
841
+ var opts = {
842
+ host: 'localhost',
843
+ port: port + 119,
844
+ protocolVersion: 5
845
+ }
846
+ var subOptions = { properties: { subscriptionIdentifier: 1234 } }
847
+ var server119 = new Server(function (client) {
848
+ client.on('connect', function (packet) {
849
+ client.connack({
850
+ reasonCode: 0
851
+ })
852
+ })
853
+ client.on('subscribe', function (packet) {
854
+ should(packet.properties.subscriptionIdentifier).be.equal(subOptions.properties.subscriptionIdentifier)
855
+ server119.close()
856
+ done()
857
+ })
858
+ }).listen(port + 119)
859
+
860
+ var client = mqtt.connect(opts)
861
+ client.on('connect', function () {
862
+ client.subscribe('a/b', subOptions)
863
+ })
864
+ })
865
+
866
+ it('puback handling errors check', function (done) {
867
+ this.timeout(15000)
868
+ serverErr.listen(port + 117)
869
+ var opts = {
870
+ host: 'localhost',
871
+ port: port + 117,
872
+ protocolVersion: 5
873
+ }
874
+ var client = mqtt.connect(opts)
875
+ client.once('connect', () => {
876
+ client.publish('a/b', 'message', {qos: 1}, function (err, packet) {
877
+ should(err.message).be.equal('Publish error: Session taken over')
878
+ should(err.code).be.equal(142)
879
+ })
880
+ serverErr.close()
881
+ done()
882
+ })
883
+ })
884
+ it('pubrec handling errors check', function (done) {
885
+ this.timeout(15000)
886
+ serverErr.listen(port + 118)
887
+ var opts = {
888
+ host: 'localhost',
889
+ port: port + 118,
890
+ protocolVersion: 5
891
+ }
892
+ var client = mqtt.connect(opts)
893
+ client.once('connect', () => {
894
+ client.publish('a/b', 'message', {qos: 2}, function (err, packet) {
895
+ should(err.message).be.equal('Publish error: Session taken over')
896
+ should(err.code).be.equal(142)
897
+ })
898
+ serverErr.close()
899
+ done()
900
+ })
901
+ })
902
+ it('puback handling custom reason code', function (done) {
903
+ this.timeout(15000)
904
+ serverErr.listen(port + 117)
905
+ var opts = {
906
+ host: 'localhost',
907
+ port: port + 117,
908
+ protocolVersion: 5,
909
+ customHandleAcks: function (topic, message, packet, cb) {
910
+ var code = 0
911
+ if (topic === 'a/b') {
912
+ code = 128
913
+ }
914
+ cb(code)
915
+ }
916
+ }
917
+
918
+ serverErr.once('client', function (c) {
919
+ c.once('subscribe', function () {
920
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
921
+ })
922
+
923
+ c.on('puback', function (packet) {
924
+ should(packet.reasonCode).be.equal(128)
925
+ client.end()
926
+ c.destroy()
927
+ serverErr.close()
928
+ done()
929
+ })
930
+ })
931
+
932
+ var client = mqtt.connect(opts)
933
+ client.once('connect', function () {
934
+ client.subscribe('a/b', {qos: 1})
935
+ })
936
+ })
937
+ it('server side disconnect', function (done) {
938
+ this.timeout(15000)
939
+ var server327 = new Server(function (client) {
940
+ client.on('connect', function (packet) {
941
+ client.connack({
942
+ reasonCode: 0
943
+ })
944
+ client.disconnect({reasonCode: 128})
945
+ server327.close()
946
+ })
947
+ })
948
+ server327.listen(port + 327)
949
+ var opts = {
950
+ host: 'localhost',
951
+ port: port + 327,
952
+ protocolVersion: 5
953
+ }
954
+
955
+ var client = mqtt.connect(opts)
956
+ client.once('disconnect', function (disconnectPacket) {
957
+ should(disconnectPacket.reasonCode).be.equal(128)
958
+ done()
959
+ })
960
+ })
961
+ it('pubrec handling custom reason code', function (done) {
962
+ this.timeout(15000)
963
+ serverErr.listen(port + 117)
964
+ var opts = {
965
+ host: 'localhost',
966
+ port: port + 117,
967
+ protocolVersion: 5,
968
+ customHandleAcks: function (topic, message, packet, cb) {
969
+ var code = 0
970
+ if (topic === 'a/b') {
971
+ code = 128
972
+ }
973
+ cb(code)
974
+ }
975
+ }
976
+
977
+ serverErr.once('client', function (c) {
978
+ c.once('subscribe', function () {
979
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
980
+ })
981
+
982
+ c.on('pubrec', function (packet) {
983
+ should(packet.reasonCode).be.equal(128)
984
+ client.end()
985
+ c.destroy()
986
+ serverErr.close()
987
+ done()
988
+ })
989
+ })
990
+
991
+ var client = mqtt.connect(opts)
992
+ client.once('connect', function () {
993
+ client.subscribe('a/b', {qos: 1})
994
+ })
995
+ })
996
+ it('puback handling custom reason code with error', function (done) {
997
+ this.timeout(15000)
998
+ serverErr.listen(port + 117)
999
+ var opts = {
1000
+ host: 'localhost',
1001
+ port: port + 117,
1002
+ protocolVersion: 5,
1003
+ customHandleAcks: function (topic, message, packet, cb) {
1004
+ var code = 0
1005
+ if (topic === 'a/b') {
1006
+ cb(new Error('a/b is not valid'))
1007
+ }
1008
+ cb(code)
1009
+ }
1010
+ }
1011
+
1012
+ serverErr.once('client', function (c) {
1013
+ c.once('subscribe', function () {
1014
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
1015
+ })
1016
+ })
1017
+
1018
+ var client = mqtt.connect(opts)
1019
+ client.on('error', function (error) {
1020
+ should(error.message).be.equal('a/b is not valid')
1021
+ client.end()
1022
+ serverErr.close()
1023
+ done()
1024
+ })
1025
+ client.once('connect', function () {
1026
+ client.subscribe('a/b', {qos: 1})
1027
+ })
1028
+ })
1029
+ it('pubrec handling custom reason code with error', function (done) {
1030
+ this.timeout(15000)
1031
+ serverErr.listen(port + 117)
1032
+ var opts = {
1033
+ host: 'localhost',
1034
+ port: port + 117,
1035
+ protocolVersion: 5,
1036
+ customHandleAcks: function (topic, message, packet, cb) {
1037
+ var code = 0
1038
+ if (topic === 'a/b') {
1039
+ cb(new Error('a/b is not valid'))
1040
+ }
1041
+ cb(code)
1042
+ }
1043
+ }
1044
+
1045
+ serverErr.once('client', function (c) {
1046
+ c.once('subscribe', function () {
1047
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
1048
+ })
1049
+ })
1050
+
1051
+ var client = mqtt.connect(opts)
1052
+ client.on('error', function (error) {
1053
+ should(error.message).be.equal('a/b is not valid')
1054
+ client.end()
1055
+ serverErr.close()
1056
+ done()
1057
+ })
1058
+ client.once('connect', function () {
1059
+ client.subscribe('a/b', {qos: 1})
1060
+ })
1061
+ })
1062
+ it('puback handling custom invalid reason code', function (done) {
1063
+ this.timeout(15000)
1064
+ serverErr.listen(port + 117)
1065
+ var opts = {
1066
+ host: 'localhost',
1067
+ port: port + 117,
1068
+ protocolVersion: 5,
1069
+ customHandleAcks: function (topic, message, packet, cb) {
1070
+ var code = 0
1071
+ if (topic === 'a/b') {
1072
+ code = 124124
1073
+ }
1074
+ cb(code)
1075
+ }
1076
+ }
1077
+
1078
+ serverErr.once('client', function (c) {
1079
+ c.once('subscribe', function () {
1080
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 1, messageId: 1 })
1081
+ })
1082
+ })
1083
+
1084
+ var client = mqtt.connect(opts)
1085
+ client.on('error', function (error) {
1086
+ should(error.message).be.equal('Wrong reason code for puback')
1087
+ client.end()
1088
+ serverErr.close()
1089
+ done()
1090
+ })
1091
+ client.once('connect', function () {
1092
+ client.subscribe('a/b', {qos: 1})
1093
+ })
1094
+ })
1095
+ it('pubrec handling custom invalid reason code', function (done) {
1096
+ this.timeout(15000)
1097
+ serverErr.listen(port + 117)
1098
+ var opts = {
1099
+ host: 'localhost',
1100
+ port: port + 117,
1101
+ protocolVersion: 5,
1102
+ customHandleAcks: function (topic, message, packet, cb) {
1103
+ var code = 0
1104
+ if (topic === 'a/b') {
1105
+ code = 34535
1106
+ }
1107
+ cb(code)
1108
+ }
1109
+ }
1110
+
1111
+ serverErr.once('client', function (c) {
1112
+ c.once('subscribe', function () {
1113
+ c.publish({ topic: 'a/b', payload: 'payload', qos: 2, messageId: 1 })
1114
+ })
1115
+ })
1116
+
1117
+ var client = mqtt.connect(opts)
1118
+ client.on('error', function (error) {
1119
+ should(error.message).be.equal('Wrong reason code for pubrec')
1120
+ client.end()
1121
+ serverErr.close()
1122
+ done()
1123
+ })
1124
+ client.once('connect', function () {
1125
+ client.subscribe('a/b', {qos: 1})
1126
+ })
1127
+ })
1128
+ })
544
1129
  })