mqtt 0.2.0 → 0.3.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.
- data/NEWS.md +22 -2
- data/README.md +126 -7
- data/lib/mqtt.rb +5 -5
- data/lib/mqtt/client.rb +92 -55
- data/lib/mqtt/packet.rb +305 -185
- data/lib/mqtt/proxy.rb +28 -28
- data/lib/mqtt/version.rb +2 -1
- data/spec/mqtt_client_spec.rb +252 -175
- data/spec/mqtt_packet_spec.rb +823 -401
- data/spec/mqtt_version_spec.rb +3 -3
- data/spec/zz_client_integration_spec.rb +16 -16
- metadata +102 -85
data/spec/mqtt_packet_spec.rb
CHANGED
@@ -9,168 +9,203 @@ require 'mqtt'
|
|
9
9
|
describe MQTT::Packet do
|
10
10
|
|
11
11
|
describe "when creating a new packet" do
|
12
|
-
it "should allow you to set the packet
|
13
|
-
packet = MQTT::Packet.new( :
|
14
|
-
packet.
|
12
|
+
it "should allow you to set the packet flags as a hash parameter" do
|
13
|
+
packet = MQTT::Packet.new( :flags => [true, false, true, false] )
|
14
|
+
expect(packet.flags).to eq([true, false, true, false])
|
15
15
|
end
|
16
16
|
|
17
|
-
it "should
|
18
|
-
packet = MQTT::Packet.new
|
19
|
-
packet.
|
17
|
+
it "should have a custom inspect method" do
|
18
|
+
packet = MQTT::Packet.new
|
19
|
+
expect(packet.inspect).to eq('#<MQTT::Packet>')
|
20
20
|
end
|
21
21
|
|
22
|
-
it "should
|
23
|
-
packet = MQTT::Packet.new
|
24
|
-
packet.
|
22
|
+
it "should have a type_id method to get the integer ID of the packet type" do
|
23
|
+
packet = MQTT::Packet::Pingreq.new
|
24
|
+
expect(packet.type_id).to eq(12)
|
25
25
|
end
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
+
it "should let you change attributes using the update_attributes method" do
|
29
|
+
packet = MQTT::Packet.new(:flags => [false, false, false, true])
|
30
|
+
packet.update_attributes(:flags => [false, false, true, true])
|
31
|
+
expect(packet.flags).to eq([false, false, true, true])
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "protected methods" do
|
35
|
+
let(:packet) { MQTT::Packet.new }
|
36
|
+
|
37
|
+
it "should provide a encode_bytes method to get some bytes as Integers" do
|
38
|
+
data = packet.send(:encode_bytes, 0x48, 0x65, 0x6c, 0x6c, 'o'.unpack('C1')[0])
|
39
|
+
expect(data).to eq('Hello')
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should provide a encode_bits method to encode an array of bits to a string" do
|
43
|
+
data = packet.send(:encode_bits, [false, true, true, false, true, false, true, false])
|
44
|
+
expect(data).to eq('V')
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should provide a add_short method to get a big-endian unsigned 16-bit integer" do
|
48
|
+
data = packet.send(:encode_short, 1024)
|
49
|
+
expect(data).to eq("\x04\x00")
|
50
|
+
expect(data.encoding.to_s).to eq("ASCII-8BIT")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should provide a add_string method to get a string preceeded by its length" do
|
54
|
+
data = packet.send(:encode_string, 'quack')
|
55
|
+
expect(data).to eq("\x00\x05quack")
|
56
|
+
expect(data.encoding.to_s).to eq("ASCII-8BIT")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should provide a shift_short method to get a 16-bit unsigned integer" do
|
60
|
+
buffer = "\x22\x8Bblahblah"
|
61
|
+
expect(packet.send(:shift_short,buffer)).to eq(8843)
|
62
|
+
expect(buffer).to eq('blahblah')
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should provide a shift_byte method to get one byte as integers" do
|
66
|
+
buffer = "\x01blahblah"
|
67
|
+
expect(packet.send(:shift_byte,buffer)).to eq(1)
|
68
|
+
expect(buffer).to eq('blahblah')
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should provide a shift_byte method to get one byte as integers" do
|
72
|
+
buffer = "Yblahblah"
|
73
|
+
expect(packet.send(:shift_bits, buffer)).to eq([true, false, false, true, true, false, true, false])
|
74
|
+
expect(buffer).to eq('blahblah')
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should provide a shift_string method to get a string preceeded by its length" do
|
78
|
+
buffer = "\x00\x05Hello World"
|
79
|
+
expect(packet.send(:shift_string,buffer)).to eq("Hello")
|
80
|
+
expect(buffer).to eq(' World')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "deprecated attributes" do
|
85
|
+
it "should still have a message_id method that is that same as id" do
|
28
86
|
packet = MQTT::Packet.new
|
29
|
-
packet.
|
87
|
+
packet.message_id = 1234
|
88
|
+
expect(packet.message_id).to eq(1234)
|
89
|
+
expect(packet.id).to eq(1234)
|
90
|
+
packet.id = 4321
|
91
|
+
expect(packet.message_id).to eq(4321)
|
92
|
+
expect(packet.id).to eq(4321)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe MQTT::Packet::Publish do
|
98
|
+
describe "when creating a packet" do
|
99
|
+
it "should allow you to set the packet QOS level as a hash parameter" do
|
100
|
+
packet = MQTT::Packet::Publish.new( :qos => 2 )
|
101
|
+
expect(packet.qos).to eq(2)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should allow you to set the packet retain flag as a hash parameter" do
|
105
|
+
packet = MQTT::Packet::Publish.new( :retain => true )
|
106
|
+
expect(packet.retain).to be_truthy
|
30
107
|
end
|
31
108
|
|
32
109
|
it "should throw an exception the QoS is greater than 2" do
|
33
|
-
|
34
|
-
packet = MQTT::Packet.new( :qos => 3 )
|
35
|
-
}.
|
110
|
+
expect {
|
111
|
+
packet = MQTT::Packet::Publish.new( :qos => 3 )
|
112
|
+
}.to raise_error(
|
36
113
|
'Invalid QoS value: 3'
|
37
114
|
)
|
38
115
|
end
|
39
116
|
|
40
117
|
it "should throw an exception the QoS is less than 0" do
|
41
|
-
|
42
|
-
packet = MQTT::Packet.new( :qos => -1 )
|
43
|
-
}.
|
118
|
+
expect {
|
119
|
+
packet = MQTT::Packet::Publish.new( :qos => -1 )
|
120
|
+
}.to raise_error(
|
44
121
|
'Invalid QoS value: -1'
|
45
122
|
)
|
46
123
|
end
|
47
124
|
end
|
48
125
|
|
49
|
-
describe "when setting packet
|
126
|
+
describe "when setting attributes on a packet" do
|
50
127
|
let(:packet) {
|
51
|
-
MQTT::Packet.new(
|
128
|
+
MQTT::Packet::Publish.new(
|
52
129
|
:duplicate => false,
|
53
130
|
:qos => 0,
|
54
131
|
:retain => false
|
55
132
|
)
|
56
133
|
}
|
57
134
|
|
58
|
-
it "should have a type_id method to get the integer ID of the packet type" do
|
59
|
-
packet = MQTT::Packet::Pingreq.new
|
60
|
-
packet.type_id.should == 12
|
61
|
-
end
|
62
|
-
|
63
135
|
it "should let you change the dup flag of a packet" do
|
64
136
|
packet.duplicate = true
|
65
|
-
packet.duplicate.
|
137
|
+
expect(packet.duplicate).to be_truthy
|
66
138
|
end
|
67
139
|
|
68
140
|
it "should let you change the dup flag of a packet using an integer" do
|
69
141
|
packet.duplicate = 1
|
70
|
-
packet.duplicate.
|
142
|
+
expect(packet.duplicate).to be_truthy
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should let you change the QoS value of a packet" do
|
146
|
+
packet.qos = 1
|
147
|
+
expect(packet.qos).to eq(1)
|
71
148
|
end
|
72
149
|
|
73
150
|
it "should let you change the retain flag of a packet" do
|
74
151
|
packet.retain = true
|
75
|
-
packet.retain.
|
152
|
+
expect(packet.retain).to be_truthy
|
76
153
|
end
|
77
154
|
|
78
155
|
it "should let you change the retain flag of a packet using an integer" do
|
79
156
|
packet.retain = 1
|
80
|
-
packet.retain.
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should let you attributes using the update_attributes method" do
|
85
|
-
packet = MQTT::Packet.new(:qos => 1)
|
86
|
-
packet.update_attributes(:qos => 2)
|
87
|
-
packet.qos.should == 2
|
88
|
-
end
|
89
|
-
|
90
|
-
describe "protected methods" do
|
91
|
-
let(:packet) { MQTT::Packet.new }
|
92
|
-
|
93
|
-
it "should provide a encode_bytes method to get some bytes as Integers" do
|
94
|
-
data = packet.send(:encode_bytes, 0x48, 0x65, 0x6c, 0x6c, 'o'.unpack('C1')[0])
|
95
|
-
data.should == 'Hello'
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should provide a add_short method to get a big-endian unsigned 16-bit integer" do
|
99
|
-
data = packet.send(:encode_short, 1024)
|
100
|
-
data.should == "\x04\x00"
|
101
|
-
data.encoding.to_s.should == "ASCII-8BIT"
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should provide a add_string method to get a string preceeded by its length" do
|
105
|
-
data = packet.send(:encode_string, 'quack')
|
106
|
-
data.should == "\x00\x05quack"
|
107
|
-
data.encoding.to_s.should == "ASCII-8BIT"
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should provide a shift_short method to get a 16-bit unsigned integer" do
|
111
|
-
buffer = "\x22\x8Bblahblah"
|
112
|
-
packet.send(:shift_short,buffer).should == 8843
|
113
|
-
buffer.should == 'blahblah'
|
114
|
-
end
|
115
|
-
|
116
|
-
it "should provide a shift_byte method to get one byte as integers" do
|
117
|
-
buffer = "\x01blahblah"
|
118
|
-
packet.send(:shift_byte,buffer).should == 1
|
119
|
-
buffer.should == 'blahblah'
|
120
|
-
end
|
121
|
-
|
122
|
-
it "should provide a shift_string method to get a string preceeded by its length" do
|
123
|
-
buffer = "\x00\x05Hello World"
|
124
|
-
packet.send(:shift_string,buffer).should == "Hello"
|
125
|
-
buffer.should == ' World'
|
157
|
+
expect(packet.retain).to be_truthy
|
126
158
|
end
|
127
159
|
end
|
128
|
-
end
|
129
160
|
|
130
|
-
describe MQTT::Packet::Publish do
|
131
161
|
describe "when serialising a packet" do
|
132
162
|
it "should output the correct bytes for a packet with default QOS and no flags" do
|
133
163
|
packet = MQTT::Packet::Publish.new( :topic => 'test', :payload => 'hello world' )
|
134
|
-
packet.to_s.
|
164
|
+
expect(packet.to_s).to eq("\x30\x11\x00\x04testhello world")
|
135
165
|
end
|
136
166
|
|
137
167
|
it "should output the correct bytes for a packet with QOS 1 and no flags" do
|
138
|
-
packet = MQTT::Packet::Publish.new( :
|
139
|
-
packet.to_s.
|
168
|
+
packet = MQTT::Packet::Publish.new( :id => 5, :qos => 1, :topic => 'a/b', :payload => 'hello world' )
|
169
|
+
expect(packet.to_s).to eq("\x32\x12\x00\x03a/b\x00\x05hello world")
|
140
170
|
end
|
141
171
|
|
142
172
|
it "should output the correct bytes for a packet with QOS 2 and retain flag set" do
|
143
|
-
packet = MQTT::Packet::Publish.new( :
|
144
|
-
packet.to_s.
|
173
|
+
packet = MQTT::Packet::Publish.new( :id => 5, :qos => 2, :retain => true, :topic => 'c/d', :payload => 'hello world' )
|
174
|
+
expect(packet.to_s).to eq("\x35\x12\x00\x03c/d\x00\x05hello world")
|
145
175
|
end
|
146
176
|
|
147
177
|
it "should output the correct bytes for a packet with QOS 2 and dup flag set" do
|
148
|
-
packet = MQTT::Packet::Publish.new( :
|
149
|
-
packet.to_s.
|
178
|
+
packet = MQTT::Packet::Publish.new( :id => 5, :qos => 2, :duplicate => true, :topic => 'c/d', :payload => 'hello world' )
|
179
|
+
expect(packet.to_s).to eq("\x3C\x12\x00\x03c/d\x00\x05hello world")
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should output the correct bytes for a packet with an empty payload" do
|
183
|
+
packet = MQTT::Packet::Publish.new( :topic => 'test' )
|
184
|
+
expect(packet.to_s).to eq("\x30\x06\x00\x04test")
|
150
185
|
end
|
151
186
|
|
152
187
|
it "should output a string as binary / 8-bit ASCII" do
|
153
188
|
packet = MQTT::Packet::Publish.new( :topic => 'test', :payload => 'hello world' )
|
154
|
-
packet.to_s.encoding.to_s.
|
189
|
+
expect(packet.to_s.encoding.to_s).to eq("ASCII-8BIT")
|
155
190
|
end
|
156
191
|
|
157
192
|
it "should support passing in non-strings to the topic and payload" do
|
158
193
|
packet = MQTT::Packet::Publish.new( :topic => :symbol, :payload => 1234 )
|
159
|
-
packet.to_s.
|
194
|
+
expect(packet.to_s).to eq("\x30\x0c\x00\x06symbol1234")
|
160
195
|
end
|
161
196
|
|
162
197
|
it "should throw an exception when there is no topic name" do
|
163
|
-
|
198
|
+
expect {
|
164
199
|
MQTT::Packet::Publish.new.to_s
|
165
|
-
}.
|
200
|
+
}.to raise_error(
|
166
201
|
'Invalid topic name when serialising packet'
|
167
202
|
)
|
168
203
|
end
|
169
204
|
|
170
205
|
it "should throw an exception when there is an empty topic name" do
|
171
|
-
|
206
|
+
expect {
|
172
207
|
MQTT::Packet::Publish.new( :topic => '' ).to_s
|
173
|
-
}.
|
208
|
+
}.to raise_error(
|
174
209
|
'Invalid topic name when serialising packet'
|
175
210
|
)
|
176
211
|
end
|
@@ -178,10 +213,10 @@ describe MQTT::Packet::Publish do
|
|
178
213
|
|
179
214
|
describe "when serialising an oversized packet" do
|
180
215
|
it "should throw an exception when body is bigger than 256MB" do
|
181
|
-
|
216
|
+
expect {
|
182
217
|
packet = MQTT::Packet::Publish.new( :topic => 'test', :payload => 'x'*268435455 )
|
183
218
|
packet.to_s
|
184
|
-
}.
|
219
|
+
}.to raise_error(
|
185
220
|
'Error serialising packet: body is more than 256MB'
|
186
221
|
)
|
187
222
|
end
|
@@ -191,29 +226,29 @@ describe MQTT::Packet::Publish do
|
|
191
226
|
let(:packet) { MQTT::Packet.parse( "\x30\x11\x00\x04testhello world" ) }
|
192
227
|
|
193
228
|
it "should correctly create the right type of packet object" do
|
194
|
-
packet.class.
|
229
|
+
expect(packet.class).to eq(MQTT::Packet::Publish)
|
195
230
|
end
|
196
231
|
|
197
232
|
it "should set the QOS level correctly" do
|
198
|
-
packet.qos.
|
233
|
+
expect(packet.qos).to eq(0)
|
199
234
|
end
|
200
235
|
|
201
236
|
it "should set the RETAIN flag correctly" do
|
202
|
-
packet.retain.
|
237
|
+
expect(packet.retain).to be_falsey
|
203
238
|
end
|
204
239
|
|
205
240
|
it "should set the DUP flag correctly" do
|
206
|
-
packet.duplicate.
|
241
|
+
expect(packet.duplicate).to be_falsey
|
207
242
|
end
|
208
243
|
|
209
244
|
it "should set the topic name correctly" do
|
210
|
-
packet.topic.
|
211
|
-
packet.topic.encoding.to_s.
|
245
|
+
expect(packet.topic).to eq('test')
|
246
|
+
expect(packet.topic.encoding.to_s).to eq('UTF-8')
|
212
247
|
end
|
213
248
|
|
214
249
|
it "should set the payload correctly" do
|
215
|
-
packet.payload.
|
216
|
-
packet.payload.encoding.to_s.
|
250
|
+
expect(packet.payload).to eq('hello world')
|
251
|
+
expect(packet.payload.encoding.to_s).to eq('ASCII-8BIT')
|
217
252
|
end
|
218
253
|
end
|
219
254
|
|
@@ -221,38 +256,66 @@ describe MQTT::Packet::Publish do
|
|
221
256
|
let(:packet) { MQTT::Packet.parse( "\x3D\x12\x00\x03c/d\x00\x05hello world" ) }
|
222
257
|
|
223
258
|
it "should correctly create the right type of packet object" do
|
224
|
-
packet.class.
|
259
|
+
expect(packet.class).to eq(MQTT::Packet::Publish)
|
225
260
|
end
|
226
261
|
|
227
262
|
it "should set the QOS level correctly" do
|
228
|
-
packet.qos.
|
263
|
+
expect(packet.qos).to eq(2)
|
229
264
|
end
|
230
265
|
|
231
266
|
it "should set the RETAIN flag correctly" do
|
232
|
-
packet.retain.
|
267
|
+
expect(packet.retain).to be_truthy
|
233
268
|
end
|
234
269
|
|
235
270
|
it "should set the DUP flag correctly" do
|
236
|
-
packet.duplicate.
|
271
|
+
expect(packet.duplicate).to be_truthy
|
237
272
|
end
|
238
273
|
|
239
274
|
it "should set the topic name correctly" do
|
240
|
-
packet.topic.
|
241
|
-
packet.topic.encoding.to_s.
|
275
|
+
expect(packet.topic).to eq('c/d')
|
276
|
+
expect(packet.topic.encoding.to_s).to eq('UTF-8')
|
242
277
|
end
|
243
278
|
|
244
279
|
it "should set the payload correctly" do
|
245
|
-
packet.payload.
|
246
|
-
packet.payload.encoding.to_s.
|
280
|
+
expect(packet.payload).to eq('hello world')
|
281
|
+
expect(packet.payload.encoding.to_s).to eq('ASCII-8BIT')
|
247
282
|
end
|
248
283
|
end
|
249
284
|
|
250
|
-
describe "when parsing a packet with
|
285
|
+
describe "when parsing a packet with an empty payload" do
|
286
|
+
let(:packet) { MQTT::Packet.parse( "\x30\x06\x00\x04test" ) }
|
287
|
+
|
288
|
+
it "should correctly create the right type of packet object" do
|
289
|
+
expect(packet.class).to eq(MQTT::Packet::Publish)
|
290
|
+
end
|
291
|
+
|
292
|
+
it "should set the topic name correctly" do
|
293
|
+
expect(packet.topic).to eq('test')
|
294
|
+
end
|
295
|
+
|
296
|
+
it "should set the payload correctly" do
|
297
|
+
expect(packet.payload).to be_empty
|
298
|
+
end
|
299
|
+
end
|
300
|
+
|
301
|
+
describe "when parsing a packet with a QoS value of 3" do
|
251
302
|
it "should throw an exception" do
|
252
|
-
|
303
|
+
expect {
|
253
304
|
packet = MQTT::Packet.parse( "\x36\x12\x00\x03a/b\x00\x05hello world" )
|
254
|
-
}.
|
255
|
-
|
305
|
+
}.to raise_error(
|
306
|
+
MQTT::ProtocolException,
|
307
|
+
'Invalid packet: QoS value of 3 is not allowed'
|
308
|
+
)
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
describe "when parsing a packet with QoS value of 0 and DUP set" do
|
313
|
+
it "should throw an exception" do
|
314
|
+
expect {
|
315
|
+
packet = MQTT::Packet.parse( "\x38\x10\x00\x03a/bhello world" )
|
316
|
+
}.to raise_error(
|
317
|
+
MQTT::ProtocolException,
|
318
|
+
'Invalid packet: DUP cannot be set for QoS 0'
|
256
319
|
)
|
257
320
|
end
|
258
321
|
end
|
@@ -266,15 +329,15 @@ describe MQTT::Packet::Publish do
|
|
266
329
|
}
|
267
330
|
|
268
331
|
it "should parse the packet type correctly" do
|
269
|
-
packet.class.
|
332
|
+
expect(packet.class).to eq(MQTT::Packet::Publish)
|
270
333
|
end
|
271
334
|
|
272
335
|
it "should get the topic name correctly" do
|
273
|
-
packet.topic.
|
336
|
+
expect(packet.topic).to eq('topic')
|
274
337
|
end
|
275
338
|
|
276
339
|
it "should get the body length correctly" do
|
277
|
-
packet.payload.bytesize.
|
340
|
+
expect(packet.payload.bytesize).to eq(314)
|
278
341
|
end
|
279
342
|
end
|
280
343
|
|
@@ -288,15 +351,15 @@ describe MQTT::Packet::Publish do
|
|
288
351
|
end
|
289
352
|
|
290
353
|
it "should parse the packet type correctly" do
|
291
|
-
packet.class.
|
354
|
+
expect(packet.class).to eq(MQTT::Packet::Publish)
|
292
355
|
end
|
293
356
|
|
294
357
|
it "should get the topic name correctly" do
|
295
|
-
packet.topic.
|
358
|
+
expect(packet.topic).to eq('topic')
|
296
359
|
end
|
297
360
|
|
298
361
|
it "should get the body length correctly" do
|
299
|
-
packet.payload.bytesize.
|
362
|
+
expect(packet.payload.bytesize).to eq(16384)
|
300
363
|
end
|
301
364
|
end
|
302
365
|
|
@@ -309,31 +372,31 @@ describe MQTT::Packet::Publish do
|
|
309
372
|
end
|
310
373
|
|
311
374
|
it "should have the correct topic byte length" do
|
312
|
-
packet.topic.bytesize.
|
375
|
+
expect(packet.topic.bytesize).to eq(8)
|
313
376
|
end
|
314
377
|
|
315
378
|
it "should have the correct topic string length", :unless => RUBY_VERSION =~ /^1\.8/ do
|
316
379
|
# Ruby 1.8 doesn't support UTF-8 properly
|
317
|
-
packet.topic.length.
|
380
|
+
expect(packet.topic.length).to eq(6)
|
318
381
|
end
|
319
382
|
|
320
383
|
it "should have the correct payload byte length" do
|
321
|
-
packet.payload.bytesize.
|
384
|
+
expect(packet.payload.bytesize).to eq(12)
|
322
385
|
end
|
323
386
|
|
324
387
|
it "should have the correct payload string length", :unless => RUBY_VERSION =~ /^1\.8/ do
|
325
388
|
# Ruby 1.8 doesn't support UTF-8 properly
|
326
|
-
packet.payload.length.
|
389
|
+
expect(packet.payload.length).to eq(10)
|
327
390
|
end
|
328
391
|
|
329
392
|
it "should encode to MQTT packet correctly" do
|
330
|
-
packet.to_s.
|
393
|
+
expect(packet.to_s).to eq("\x30\x16\x00\x08Test \xE2\x91\xA0Snowman: \xE2\x98\x83".force_encoding('BINARY'))
|
331
394
|
end
|
332
395
|
|
333
396
|
it "should parse the serialised packet" do
|
334
397
|
packet2 = MQTT::Packet.parse( packet.to_s )
|
335
|
-
packet2.topic.
|
336
|
-
packet2.payload.
|
398
|
+
expect(packet2.topic).to eq("Test ①".force_encoding('UTF-8'))
|
399
|
+
expect(packet2.payload).to eq("Snowman: ☃".force_encoding('BINARY'))
|
337
400
|
end
|
338
401
|
end
|
339
402
|
|
@@ -342,45 +405,50 @@ describe MQTT::Packet::Publish do
|
|
342
405
|
let(:packet) { MQTT::Packet.read(socket) }
|
343
406
|
|
344
407
|
it "should correctly create the right type of packet object" do
|
345
|
-
packet.class.
|
408
|
+
expect(packet.class).to eq(MQTT::Packet::Publish)
|
346
409
|
end
|
347
410
|
|
348
411
|
it "should set the body length is read correctly" do
|
349
|
-
packet.body_length.
|
412
|
+
expect(packet.body_length).to eq(17)
|
350
413
|
end
|
351
414
|
|
352
415
|
it "should set the QOS level correctly" do
|
353
|
-
packet.qos.
|
416
|
+
expect(packet.qos).to eq(0)
|
354
417
|
end
|
355
418
|
|
356
419
|
it "should set the RETAIN flag correctly" do
|
357
|
-
packet.retain.
|
420
|
+
expect(packet.retain).to be_falsey
|
358
421
|
end
|
359
422
|
|
360
423
|
it "should set the DUP flag correctly" do
|
361
|
-
packet.duplicate.
|
424
|
+
expect(packet.duplicate).to be_falsey
|
362
425
|
end
|
363
426
|
|
364
427
|
it "should set the topic name correctly" do
|
365
|
-
packet.topic.
|
366
|
-
packet.topic.encoding.to_s.
|
428
|
+
expect(packet.topic).to eq('test')
|
429
|
+
expect(packet.topic.encoding.to_s).to eq('UTF-8')
|
367
430
|
end
|
368
431
|
|
369
432
|
it "should set the payload correctly" do
|
370
|
-
packet.payload.
|
371
|
-
packet.payload.encoding.to_s.
|
433
|
+
expect(packet.payload).to eq('hello world')
|
434
|
+
expect(packet.payload.encoding.to_s).to eq('ASCII-8BIT')
|
372
435
|
end
|
373
436
|
end
|
374
437
|
|
375
438
|
describe "when calling the inspect method" do
|
376
439
|
it "should output the payload, if it is less than 16 bytes" do
|
377
440
|
packet = MQTT::Packet::Publish.new( :topic => "topic", :payload => "payload" )
|
378
|
-
packet.inspect.
|
441
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Publish: d0, q0, r0, m0, 'topic', 'payload'>")
|
379
442
|
end
|
380
443
|
|
381
444
|
it "should output the length of the payload, if it is more than 16 bytes" do
|
382
445
|
packet = MQTT::Packet::Publish.new( :topic => "topic", :payload => 'x'*32 )
|
383
|
-
packet.inspect.
|
446
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Publish: d0, q0, r0, m0, 'topic', ... (32 bytes)>")
|
447
|
+
end
|
448
|
+
|
449
|
+
it "should only output the length of a binary payload" do
|
450
|
+
packet = MQTT::Packet.parse("\x31\x12\x00\x04test\x8D\xF8\x09\x40\xC4\xE7\x4f\xF0\xFF\x30\xE0\xE7")
|
451
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Publish: d0, q0, r1, m0, 'test', ... (12 bytes)>")
|
384
452
|
end
|
385
453
|
end
|
386
454
|
end
|
@@ -389,7 +457,7 @@ describe MQTT::Packet::Connect do
|
|
389
457
|
describe "when serialising a packet" do
|
390
458
|
it "should output the correct bytes for a packet with no flags" do
|
391
459
|
packet = MQTT::Packet::Connect.new( :client_id => 'myclient' )
|
392
|
-
packet.to_s.
|
460
|
+
expect(packet.to_s).to eq("\020\026\x00\x06MQIsdp\x03\x02\x00\x0f\x00\x08myclient")
|
393
461
|
end
|
394
462
|
|
395
463
|
it "should output the correct bytes for a packet with clean session turned off" do
|
@@ -397,21 +465,53 @@ describe MQTT::Packet::Connect do
|
|
397
465
|
:client_id => 'myclient',
|
398
466
|
:clean_session => false
|
399
467
|
)
|
400
|
-
packet.to_s.
|
468
|
+
expect(packet.to_s).to eq("\020\026\x00\x06MQIsdp\x03\x00\x00\x0f\x00\x08myclient")
|
401
469
|
end
|
402
470
|
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
471
|
+
context "protocol version 3.1.0" do
|
472
|
+
it "should throw an exception when there is no client identifier" do
|
473
|
+
expect {
|
474
|
+
MQTT::Packet::Connect.new(:version => '3.1.0', :client_id => '').to_s
|
475
|
+
}.to raise_error(
|
476
|
+
'Client identifier too short while serialising packet'
|
477
|
+
)
|
478
|
+
end
|
479
|
+
|
480
|
+
it "should throw an exception when the client identifier is too long" do
|
481
|
+
expect {
|
482
|
+
client_id = '0EB8D2FE7C254715B4467C5B2ECAD100'
|
483
|
+
MQTT::Packet::Connect.new(:version => '3.1.0', :client_id => client_id).to_s
|
484
|
+
}.to raise_error(
|
485
|
+
'Client identifier too long when serialising packet'
|
486
|
+
)
|
487
|
+
end
|
488
|
+
end
|
489
|
+
|
490
|
+
context "protocol version 3.1.1" do
|
491
|
+
it "should allow no client identifier" do
|
492
|
+
packet = MQTT::Packet::Connect.new(
|
493
|
+
:version => '3.1.1',
|
494
|
+
:client_id => '',
|
495
|
+
:clean_session => true
|
496
|
+
)
|
497
|
+
expect(packet.to_s).to eq("\020\014\x00\x04MQTT\x04\x02\x00\x0f\x00\x00")
|
498
|
+
end
|
499
|
+
|
500
|
+
it "should allow a 32 character client identifier" do
|
501
|
+
client_id = '0EB8D2FE7C254715B4467C5B2ECAD100'
|
502
|
+
packet = MQTT::Packet::Connect.new(
|
503
|
+
:version => '3.1.1',
|
504
|
+
:client_id => client_id,
|
505
|
+
:clean_session => true
|
506
|
+
)
|
507
|
+
expect(packet.to_s).to eq("\x10,\x00\x04MQTT\x04\x02\x00\x0F\x00\x200EB8D2FE7C254715B4467C5B2ECAD100")
|
508
|
+
end
|
409
509
|
end
|
410
510
|
|
411
511
|
it "should throw an exception if the keep alive value is less than 0" do
|
412
|
-
|
512
|
+
expect {
|
413
513
|
MQTT::Packet::Connect.new(:client_id => 'test', :keep_alive => -2).to_s
|
414
|
-
}.
|
514
|
+
}.to raise_error(
|
415
515
|
'Invalid keep-alive value: cannot be less than 0'
|
416
516
|
)
|
417
517
|
end
|
@@ -425,12 +525,13 @@ describe MQTT::Packet::Connect do
|
|
425
525
|
:will_topic => 'topic',
|
426
526
|
:will_payload => 'hello'
|
427
527
|
)
|
428
|
-
packet.to_s.
|
528
|
+
expect(packet.to_s).to eq(
|
429
529
|
"\x10\x24"+
|
430
530
|
"\x00\x06MQIsdp"+
|
431
531
|
"\x03\x0e\x00\x0f"+
|
432
532
|
"\x00\x08myclient"+
|
433
533
|
"\x00\x05topic\x00\x05hello"
|
534
|
+
)
|
434
535
|
end
|
435
536
|
|
436
537
|
it "should output the correct bytes for a packet with a username and password" do
|
@@ -439,13 +540,14 @@ describe MQTT::Packet::Connect do
|
|
439
540
|
:username => 'username',
|
440
541
|
:password => 'password'
|
441
542
|
)
|
442
|
-
packet.to_s.
|
543
|
+
expect(packet.to_s).to eq(
|
443
544
|
"\x10\x2A"+
|
444
545
|
"\x00\x06MQIsdp"+
|
445
546
|
"\x03\xC2\x00\x0f"+
|
446
547
|
"\x00\x08myclient"+
|
447
548
|
"\x00\x08username"+
|
448
549
|
"\x00\x08password"
|
550
|
+
)
|
449
551
|
end
|
450
552
|
|
451
553
|
it "should output the correct bytes for a packet with everything" do
|
@@ -460,21 +562,40 @@ describe MQTT::Packet::Connect do
|
|
460
562
|
:username => 'user0123456789',
|
461
563
|
:password => 'pass0123456789'
|
462
564
|
)
|
463
|
-
packet.to_s.
|
565
|
+
expect(packet.to_s).to eq(
|
464
566
|
"\x10\x5F"+ # fixed header (2)
|
465
567
|
"\x00\x06MQIsdp"+ # protocol name (8)
|
466
|
-
"\x03\xf6"+ # protocol
|
568
|
+
"\x03\xf6"+ # protocol level + flags (2)
|
467
569
|
"\xff\xff"+ # keep alive (2)
|
468
570
|
"\x00\x1712345678901234567890123"+ # client identifier (25)
|
469
571
|
"\x00\x0Awill_topic"+ # will topic (12)
|
470
572
|
"\x00\x0Cwill_message"+ # will message (14)
|
471
573
|
"\x00\x0Euser0123456789"+ # username (16)
|
472
|
-
"\x00\x0Epass0123456789"
|
574
|
+
"\x00\x0Epass0123456789"
|
575
|
+
) # password (16)
|
576
|
+
end
|
577
|
+
|
578
|
+
context 'protocol version 3.1.1' do
|
579
|
+
it "should output the correct bytes for a packet with no flags" do
|
580
|
+
packet = MQTT::Packet::Connect.new( :version => '3.1.1', :client_id => 'myclient' )
|
581
|
+
expect(packet.to_s).to eq("\020\024\x00\x04MQTT\x04\x02\x00\x0f\x00\x08myclient")
|
582
|
+
end
|
583
|
+
end
|
584
|
+
|
585
|
+
context 'an invalid protocol version number' do
|
586
|
+
it "should throw a protocol exception" do
|
587
|
+
expect {
|
588
|
+
packet = MQTT::Packet::Connect.new( :version => 'x.x.x', :client_id => 'myclient' )
|
589
|
+
}.to raise_error(
|
590
|
+
ArgumentError,
|
591
|
+
"Unsupported protocol version: x.x.x"
|
592
|
+
)
|
593
|
+
end
|
473
594
|
end
|
474
595
|
|
475
596
|
end
|
476
597
|
|
477
|
-
describe "when parsing a simple Connect packet" do
|
598
|
+
describe "when parsing a simple 3.1.0 Connect packet" do
|
478
599
|
let(:packet) do
|
479
600
|
MQTT::Packet.parse(
|
480
601
|
"\x10\x16\x00\x06MQIsdp\x03\x00\x00\x0a\x00\x08myclient"
|
@@ -482,41 +603,95 @@ describe MQTT::Packet::Connect do
|
|
482
603
|
end
|
483
604
|
|
484
605
|
it "should correctly create the right type of packet object" do
|
485
|
-
packet.class.
|
606
|
+
expect(packet.class).to eq(MQTT::Packet::Connect)
|
607
|
+
end
|
608
|
+
|
609
|
+
it "should set the fixed header flags of the packet correctly" do
|
610
|
+
expect(packet.flags).to eq([false, false, false, false])
|
611
|
+
end
|
612
|
+
|
613
|
+
it "should set the Protocol Name of the packet correctly" do
|
614
|
+
expect(packet.protocol_name).to eq('MQIsdp')
|
615
|
+
expect(packet.protocol_name.encoding.to_s).to eq('UTF-8')
|
616
|
+
end
|
617
|
+
|
618
|
+
it "should set the Protocol Level of the packet correctly" do
|
619
|
+
expect(packet.protocol_level).to eq(3)
|
620
|
+
end
|
621
|
+
|
622
|
+
it "should set the Protocol version of the packet correctly" do
|
623
|
+
expect(packet.version).to eq('3.1.0')
|
624
|
+
end
|
625
|
+
|
626
|
+
it "should set the Client Identifier of the packet correctly" do
|
627
|
+
expect(packet.client_id).to eq('myclient')
|
628
|
+
expect(packet.client_id.encoding.to_s).to eq('UTF-8')
|
629
|
+
end
|
630
|
+
|
631
|
+
it "should set the Keep Alive timer of the packet correctly" do
|
632
|
+
expect(packet.keep_alive).to eq(10)
|
633
|
+
end
|
634
|
+
|
635
|
+
it "should set not have the clean session flag set" do
|
636
|
+
expect(packet.clean_session).to be_falsey
|
637
|
+
end
|
638
|
+
|
639
|
+
it "should set the the username field of the packet to nil" do
|
640
|
+
expect(packet.username).to be_nil
|
641
|
+
end
|
642
|
+
|
643
|
+
it "should set the the password field of the packet to nil" do
|
644
|
+
expect(packet.password).to be_nil
|
645
|
+
end
|
646
|
+
end
|
647
|
+
|
648
|
+
describe "when parsing a simple 3.1.1 Connect packet" do
|
649
|
+
let(:packet) do
|
650
|
+
MQTT::Packet.parse(
|
651
|
+
"\x10\x14\x00\x04MQTT\x04\x00\x00\x0a\x00\x08myclient"
|
652
|
+
)
|
653
|
+
end
|
654
|
+
|
655
|
+
it "should correctly create the right type of packet object" do
|
656
|
+
expect(packet.class).to eq(MQTT::Packet::Connect)
|
486
657
|
end
|
487
658
|
|
488
|
-
it "should set the
|
489
|
-
packet.
|
659
|
+
it "should set the fixed header flags of the packet correctly" do
|
660
|
+
expect(packet.flags).to eq([false, false, false, false])
|
490
661
|
end
|
491
662
|
|
492
663
|
it "should set the Protocol Name of the packet correctly" do
|
493
|
-
packet.protocol_name.
|
494
|
-
packet.protocol_name.encoding.to_s.
|
664
|
+
expect(packet.protocol_name).to eq('MQTT')
|
665
|
+
expect(packet.protocol_name.encoding.to_s).to eq('UTF-8')
|
495
666
|
end
|
496
667
|
|
497
|
-
it "should set the Protocol
|
498
|
-
packet.
|
668
|
+
it "should set the Protocol Level of the packet correctly" do
|
669
|
+
expect(packet.protocol_level).to eq(4)
|
670
|
+
end
|
671
|
+
|
672
|
+
it "should set the Protocol version of the packet correctly" do
|
673
|
+
expect(packet.version).to eq('3.1.1')
|
499
674
|
end
|
500
675
|
|
501
676
|
it "should set the Client Identifier of the packet correctly" do
|
502
|
-
packet.client_id.
|
503
|
-
packet.client_id.encoding.to_s.
|
677
|
+
expect(packet.client_id).to eq('myclient')
|
678
|
+
expect(packet.client_id.encoding.to_s).to eq('UTF-8')
|
504
679
|
end
|
505
680
|
|
506
681
|
it "should set the Keep Alive timer of the packet correctly" do
|
507
|
-
packet.keep_alive.
|
682
|
+
expect(packet.keep_alive).to eq(10)
|
508
683
|
end
|
509
684
|
|
510
685
|
it "should set not have the clean session flag set" do
|
511
|
-
packet.clean_session.
|
686
|
+
expect(packet.clean_session).to be_falsey
|
512
687
|
end
|
513
688
|
|
514
689
|
it "should set the the username field of the packet to nil" do
|
515
|
-
packet.username.
|
690
|
+
expect(packet.username).to be_nil
|
516
691
|
end
|
517
692
|
|
518
693
|
it "should set the the password field of the packet to nil" do
|
519
|
-
packet.password.
|
694
|
+
expect(packet.password).to be_nil
|
520
695
|
end
|
521
696
|
end
|
522
697
|
|
@@ -528,7 +703,7 @@ describe MQTT::Packet::Connect do
|
|
528
703
|
end
|
529
704
|
|
530
705
|
it "should set the clean session flag" do
|
531
|
-
packet.clean_session.
|
706
|
+
expect(packet.clean_session).to be_truthy
|
532
707
|
end
|
533
708
|
end
|
534
709
|
|
@@ -540,47 +715,51 @@ describe MQTT::Packet::Connect do
|
|
540
715
|
end
|
541
716
|
|
542
717
|
it "should correctly create the right type of packet object" do
|
543
|
-
packet.class.
|
718
|
+
expect(packet.class).to eq(MQTT::Packet::Connect)
|
544
719
|
end
|
545
720
|
|
546
|
-
it "should set the
|
547
|
-
packet.
|
721
|
+
it "should set the fixed header flags of the packet correctly" do
|
722
|
+
expect(packet.flags).to eq([false, false, false, false])
|
548
723
|
end
|
549
724
|
|
550
725
|
it "should set the Protocol Name of the packet correctly" do
|
551
|
-
packet.protocol_name.
|
552
|
-
packet.protocol_name.encoding.to_s.
|
726
|
+
expect(packet.protocol_name).to eq('MQIsdp')
|
727
|
+
expect(packet.protocol_name.encoding.to_s).to eq('UTF-8')
|
728
|
+
end
|
729
|
+
|
730
|
+
it "should set the Protocol Level of the packet correctly" do
|
731
|
+
expect(packet.protocol_level).to eq(3)
|
553
732
|
end
|
554
733
|
|
555
|
-
it "should set the Protocol
|
556
|
-
packet.
|
734
|
+
it "should set the Protocol version of the packet correctly" do
|
735
|
+
expect(packet.version).to eq('3.1.0')
|
557
736
|
end
|
558
737
|
|
559
738
|
it "should set the Client Identifier of the packet correctly" do
|
560
|
-
packet.client_id.
|
561
|
-
packet.client_id.encoding.to_s.
|
739
|
+
expect(packet.client_id).to eq('myclient')
|
740
|
+
expect(packet.client_id.encoding.to_s).to eq('UTF-8')
|
562
741
|
end
|
563
742
|
|
564
743
|
it "should set the clean session flag should be set" do
|
565
|
-
packet.clean_session.
|
744
|
+
expect(packet.clean_session).to be_truthy
|
566
745
|
end
|
567
746
|
|
568
747
|
it "should set the QOS of the Will should be 1" do
|
569
|
-
packet.will_qos.
|
748
|
+
expect(packet.will_qos).to eq(1)
|
570
749
|
end
|
571
750
|
|
572
751
|
it "should set the Will retain flag should be false" do
|
573
|
-
packet.will_retain.
|
752
|
+
expect(packet.will_retain).to be_falsey
|
574
753
|
end
|
575
754
|
|
576
755
|
it "should set the Will topic of the packet correctly" do
|
577
|
-
packet.will_topic.
|
578
|
-
packet.will_topic.encoding.to_s.
|
756
|
+
expect(packet.will_topic).to eq('topic')
|
757
|
+
expect(packet.will_topic.encoding.to_s).to eq('UTF-8')
|
579
758
|
end
|
580
759
|
|
581
760
|
it "should set the Will payload of the packet correctly" do
|
582
|
-
packet.will_payload.
|
583
|
-
packet.will_payload.encoding.to_s.
|
761
|
+
expect(packet.will_payload).to eq('hello')
|
762
|
+
expect(packet.will_payload.encoding.to_s).to eq('UTF-8')
|
584
763
|
end
|
585
764
|
end
|
586
765
|
|
@@ -597,39 +776,43 @@ describe MQTT::Packet::Connect do
|
|
597
776
|
end
|
598
777
|
|
599
778
|
it "should correctly create the right type of packet object" do
|
600
|
-
packet.class.
|
779
|
+
expect(packet.class).to eq(MQTT::Packet::Connect)
|
601
780
|
end
|
602
781
|
|
603
|
-
it "should set the
|
604
|
-
packet.
|
782
|
+
it "should set the fixed header flags of the packet correctly" do
|
783
|
+
expect(packet.flags).to eq([false, false, false, false])
|
605
784
|
end
|
606
785
|
|
607
786
|
it "should set the Protocol Name of the packet correctly" do
|
608
|
-
packet.protocol_name.
|
609
|
-
packet.protocol_name.encoding.to_s.
|
787
|
+
expect(packet.protocol_name).to eq('MQIsdp')
|
788
|
+
expect(packet.protocol_name.encoding.to_s).to eq('UTF-8')
|
610
789
|
end
|
611
790
|
|
612
|
-
it "should set the Protocol
|
613
|
-
packet.
|
791
|
+
it "should set the Protocol Level of the packet correctly" do
|
792
|
+
expect(packet.protocol_level).to eq(3)
|
793
|
+
end
|
794
|
+
|
795
|
+
it "should set the Protocol version of the packet correctly" do
|
796
|
+
expect(packet.version).to eq('3.1.0')
|
614
797
|
end
|
615
798
|
|
616
799
|
it "should set the Client Identifier of the packet correctly" do
|
617
|
-
packet.client_id.
|
618
|
-
packet.client_id.encoding.to_s.
|
800
|
+
expect(packet.client_id).to eq('myclient')
|
801
|
+
expect(packet.client_id.encoding.to_s).to eq('UTF-8')
|
619
802
|
end
|
620
803
|
|
621
804
|
it "should set the Keep Alive Timer of the packet correctly" do
|
622
|
-
packet.keep_alive.
|
805
|
+
expect(packet.keep_alive).to eq(10)
|
623
806
|
end
|
624
807
|
|
625
808
|
it "should set the Username of the packet correctly" do
|
626
|
-
packet.username.
|
627
|
-
packet.username.encoding.to_s.
|
809
|
+
expect(packet.username).to eq('username')
|
810
|
+
expect(packet.username.encoding.to_s).to eq('UTF-8')
|
628
811
|
end
|
629
812
|
|
630
813
|
it "should set the Username of the packet correctly" do
|
631
|
-
packet.password.
|
632
|
-
packet.password.encoding.to_s.
|
814
|
+
expect(packet.password).to eq('password')
|
815
|
+
expect(packet.password.encoding.to_s).to eq('UTF-8')
|
633
816
|
end
|
634
817
|
end
|
635
818
|
|
@@ -641,12 +824,12 @@ describe MQTT::Packet::Connect do
|
|
641
824
|
end
|
642
825
|
|
643
826
|
it "should set the Username of the packet correctly" do
|
644
|
-
packet.username.
|
645
|
-
packet.username.encoding.to_s.
|
827
|
+
expect(packet.username).to eq('username')
|
828
|
+
expect(packet.username.encoding.to_s).to eq('UTF-8')
|
646
829
|
end
|
647
830
|
|
648
831
|
it "should set the Username of the packet correctly" do
|
649
|
-
packet.password.
|
832
|
+
expect(packet.password).to be_nil
|
650
833
|
end
|
651
834
|
end
|
652
835
|
|
@@ -658,12 +841,12 @@ describe MQTT::Packet::Connect do
|
|
658
841
|
end
|
659
842
|
|
660
843
|
it "should set the Username of the packet correctly" do
|
661
|
-
packet.username.
|
844
|
+
expect(packet.username).to be_nil
|
662
845
|
end
|
663
846
|
|
664
847
|
it "should set the Username of the packet correctly" do
|
665
|
-
packet.password.
|
666
|
-
packet.password.encoding.to_s.
|
848
|
+
expect(packet.password).to eq('password')
|
849
|
+
expect(packet.password.encoding.to_s).to eq('UTF-8')
|
667
850
|
end
|
668
851
|
end
|
669
852
|
|
@@ -675,11 +858,11 @@ describe MQTT::Packet::Connect do
|
|
675
858
|
end
|
676
859
|
|
677
860
|
it "should set the Username of the packet correctly" do
|
678
|
-
packet.username.
|
861
|
+
expect(packet.username).to be_nil
|
679
862
|
end
|
680
863
|
|
681
864
|
it "should set the Username of the packet correctly" do
|
682
|
-
packet.password.
|
865
|
+
expect(packet.password).to be_nil
|
683
866
|
end
|
684
867
|
end
|
685
868
|
|
@@ -688,7 +871,7 @@ describe MQTT::Packet::Connect do
|
|
688
871
|
MQTT::Packet.parse(
|
689
872
|
"\x10\x5F"+ # fixed header (2)
|
690
873
|
"\x00\x06MQIsdp"+ # protocol name (8)
|
691
|
-
"\x03\xf6"+ # protocol
|
874
|
+
"\x03\xf6"+ # protocol level + flags (2)
|
692
875
|
"\xff\xff"+ # keep alive (2)
|
693
876
|
"\x00\x1712345678901234567890123"+ # client identifier (25)
|
694
877
|
"\x00\x0Awill_topic"+ # will topic (12)
|
@@ -699,82 +882,99 @@ describe MQTT::Packet::Connect do
|
|
699
882
|
end
|
700
883
|
|
701
884
|
it "should correctly create the right type of packet object" do
|
702
|
-
packet.class.
|
885
|
+
expect(packet.class).to eq(MQTT::Packet::Connect)
|
703
886
|
end
|
704
887
|
|
705
|
-
it "should set the
|
706
|
-
packet.
|
888
|
+
it "should set the fixed header flags of the packet correctly" do
|
889
|
+
expect(packet.flags).to eq([false, false, false, false])
|
707
890
|
end
|
708
891
|
|
709
892
|
it "should set the Protocol Name of the packet correctly" do
|
710
|
-
packet.protocol_name.
|
711
|
-
packet.protocol_name.encoding.to_s.
|
893
|
+
expect(packet.protocol_name).to eq('MQIsdp')
|
894
|
+
expect(packet.protocol_name.encoding.to_s).to eq('UTF-8')
|
895
|
+
end
|
896
|
+
|
897
|
+
it "should set the Protocol Level of the packet correctly" do
|
898
|
+
expect(packet.protocol_level).to eq(3)
|
712
899
|
end
|
713
900
|
|
714
|
-
it "should set the Protocol
|
715
|
-
packet.
|
901
|
+
it "should set the Protocol version of the packet correctly" do
|
902
|
+
expect(packet.version).to eq('3.1.0')
|
716
903
|
end
|
717
904
|
|
718
905
|
it "should set the Keep Alive Timer of the packet correctly" do
|
719
|
-
packet.keep_alive.
|
906
|
+
expect(packet.keep_alive).to eq(65535)
|
720
907
|
end
|
721
908
|
|
722
909
|
it "should set the Client Identifier of the packet correctly" do
|
723
|
-
packet.client_id.
|
724
|
-
packet.client_id.encoding.to_s.
|
910
|
+
expect(packet.client_id).to eq('12345678901234567890123')
|
911
|
+
expect(packet.client_id.encoding.to_s).to eq('UTF-8')
|
725
912
|
end
|
726
913
|
|
727
914
|
it "should set the Will QoS of the packet correctly" do
|
728
|
-
packet.will_qos.
|
915
|
+
expect(packet.will_qos).to eq(2)
|
729
916
|
end
|
730
917
|
|
731
918
|
it "should set the Will retain flag of the packet correctly" do
|
732
|
-
packet.will_retain.
|
919
|
+
expect(packet.will_retain).to be_truthy
|
733
920
|
end
|
734
921
|
|
735
922
|
it "should set the Will topic of the packet correctly" do
|
736
|
-
packet.will_topic.
|
737
|
-
packet.will_topic.encoding.to_s.
|
923
|
+
expect(packet.will_topic).to eq('will_topic')
|
924
|
+
expect(packet.will_topic.encoding.to_s).to eq('UTF-8')
|
738
925
|
end
|
739
926
|
|
740
927
|
it "should set the Will payload of the packet correctly" do
|
741
|
-
packet.will_payload.
|
742
|
-
packet.will_payload.encoding.to_s.
|
928
|
+
expect(packet.will_payload).to eq('will_message')
|
929
|
+
expect(packet.will_payload.encoding.to_s).to eq('UTF-8')
|
743
930
|
end
|
744
931
|
|
745
932
|
it "should set the Username of the packet correctly" do
|
746
|
-
packet.username.
|
747
|
-
packet.username.encoding.to_s.
|
933
|
+
expect(packet.username).to eq('user0123456789')
|
934
|
+
expect(packet.username.encoding.to_s).to eq('UTF-8')
|
748
935
|
end
|
749
936
|
|
750
937
|
it "should set the Username of the packet correctly" do
|
751
|
-
packet.password.
|
752
|
-
packet.password.encoding.to_s.
|
938
|
+
expect(packet.password).to eq('pass0123456789')
|
939
|
+
expect(packet.password.encoding.to_s).to eq('UTF-8')
|
753
940
|
end
|
754
941
|
end
|
755
942
|
|
756
943
|
describe "when parsing packet with an unknown protocol name" do
|
757
944
|
it "should throw a protocol exception" do
|
758
|
-
|
945
|
+
expect {
|
759
946
|
packet = MQTT::Packet.parse(
|
760
947
|
"\x10\x16\x00\x06FooBar\x03\x00\x00\x0a\x00\x08myclient"
|
761
948
|
)
|
762
|
-
}.
|
949
|
+
}.to raise_error(
|
763
950
|
MQTT::ProtocolException,
|
764
|
-
"Unsupported protocol
|
951
|
+
"Unsupported protocol: FooBar/3"
|
765
952
|
)
|
766
953
|
end
|
767
954
|
end
|
768
955
|
|
769
|
-
describe "when parsing packet with an unknown protocol
|
956
|
+
describe "when parsing packet with an unknown protocol level" do
|
770
957
|
it "should throw a protocol exception" do
|
771
|
-
|
958
|
+
expect {
|
772
959
|
packet = MQTT::Packet.parse(
|
773
960
|
"\x10\x16\x00\x06MQIsdp\x02\x00\x00\x0a\x00\x08myclient"
|
774
961
|
)
|
775
|
-
}.
|
962
|
+
}.to raise_error(
|
963
|
+
MQTT::ProtocolException,
|
964
|
+
"Unsupported protocol: MQIsdp/2"
|
965
|
+
)
|
966
|
+
end
|
967
|
+
end
|
968
|
+
|
969
|
+
describe "when parsing packet with invalid fixed header flags" do
|
970
|
+
it "should throw a protocol exception" do
|
971
|
+
expect {
|
972
|
+
MQTT::Packet.parse(
|
973
|
+
"\x13\x16\x00\x06MQIsdp\x03\x00\x00\x0a\x00\x08myclient"
|
974
|
+
)
|
975
|
+
}.to raise_error(
|
776
976
|
MQTT::ProtocolException,
|
777
|
-
"
|
977
|
+
"Invalid flags in CONNECT packet header"
|
778
978
|
)
|
779
979
|
end
|
780
980
|
end
|
@@ -782,7 +982,7 @@ describe MQTT::Packet::Connect do
|
|
782
982
|
describe "when calling the inspect method" do
|
783
983
|
it "should output correct string for the default options" do
|
784
984
|
packet = MQTT::Packet::Connect.new
|
785
|
-
packet.inspect.
|
985
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Connect: keep_alive=15, clean, client_id=''>")
|
786
986
|
end
|
787
987
|
|
788
988
|
it "should output correct string when parameters are given" do
|
@@ -792,16 +992,53 @@ describe MQTT::Packet::Connect do
|
|
792
992
|
:clean_session => false,
|
793
993
|
:username => 'foo'
|
794
994
|
)
|
795
|
-
packet.inspect.
|
995
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Connect: keep_alive=10, client_id='c123', username='foo'>")
|
996
|
+
end
|
997
|
+
end
|
998
|
+
|
999
|
+
describe "deprecated attributes" do
|
1000
|
+
it "should still have a protocol_version method that is that same as protocol_level" do
|
1001
|
+
packet = MQTT::Packet::Connect.new
|
1002
|
+
packet.protocol_version = 5
|
1003
|
+
expect(packet.protocol_version).to eq(5)
|
1004
|
+
expect(packet.protocol_level).to eq(5)
|
1005
|
+
packet.protocol_version = 4
|
1006
|
+
expect(packet.protocol_version).to eq(4)
|
1007
|
+
expect(packet.protocol_level).to eq(4)
|
796
1008
|
end
|
797
1009
|
end
|
798
1010
|
end
|
799
1011
|
|
800
1012
|
describe MQTT::Packet::Connack do
|
1013
|
+
|
1014
|
+
describe "when setting attributes on a packet" do
|
1015
|
+
let(:packet) { MQTT::Packet::Connack.new }
|
1016
|
+
|
1017
|
+
it "should let you change the session present flag of a packet" do
|
1018
|
+
packet.session_present = true
|
1019
|
+
expect(packet.session_present).to be_truthy
|
1020
|
+
end
|
1021
|
+
|
1022
|
+
it "should let you change the session present flag of a packet using an integer" do
|
1023
|
+
packet.session_present = 1
|
1024
|
+
expect(packet.session_present).to be_truthy
|
1025
|
+
end
|
1026
|
+
|
1027
|
+
it "should let you change the return code of a packet" do
|
1028
|
+
packet.return_code = 3
|
1029
|
+
expect(packet.return_code).to eq(3)
|
1030
|
+
end
|
1031
|
+
end
|
1032
|
+
|
801
1033
|
describe "when serialising a packet" do
|
802
|
-
it "should output the correct bytes for a sucessful connection acknowledgement packet" do
|
803
|
-
packet = MQTT::Packet::Connack.new( :return_code => 0x00 )
|
804
|
-
packet.to_s.
|
1034
|
+
it "should output the correct bytes for a sucessful connection acknowledgement packet without Session Present set" do
|
1035
|
+
packet = MQTT::Packet::Connack.new( :return_code => 0x00, :session_present => false )
|
1036
|
+
expect(packet.to_s).to eq("\x20\x02\x00\x00")
|
1037
|
+
end
|
1038
|
+
|
1039
|
+
it "should output the correct bytes for a sucessful connection acknowledgement packet with Session Present set" do
|
1040
|
+
packet = MQTT::Packet::Connack.new( :return_code => 0x00, :session_present => true )
|
1041
|
+
expect(packet.to_s).to eq("\x20\x02\x01\x00")
|
805
1042
|
end
|
806
1043
|
end
|
807
1044
|
|
@@ -811,19 +1048,49 @@ describe MQTT::Packet::Connack do
|
|
811
1048
|
end
|
812
1049
|
|
813
1050
|
it "should correctly create the right type of packet object" do
|
814
|
-
packet.class.
|
1051
|
+
expect(packet.class).to eq(MQTT::Packet::Connack)
|
1052
|
+
end
|
1053
|
+
|
1054
|
+
it "should set the fixed header flags of the packet correctly" do
|
1055
|
+
expect(packet.flags).to eq([false, false, false, false])
|
815
1056
|
end
|
816
1057
|
|
817
|
-
it "should set the
|
818
|
-
packet.
|
1058
|
+
it "should set the Session Pression flag of the packet correctly" do
|
1059
|
+
expect(packet.session_present).to eq(false)
|
819
1060
|
end
|
820
1061
|
|
821
1062
|
it "should set the return code of the packet correctly" do
|
822
|
-
packet.return_code.
|
1063
|
+
expect(packet.return_code).to eq(0x00)
|
823
1064
|
end
|
824
1065
|
|
825
1066
|
it "should set the return message of the packet correctly" do
|
826
|
-
packet.return_msg.
|
1067
|
+
expect(packet.return_msg).to match(/Connection Accepted/i)
|
1068
|
+
end
|
1069
|
+
end
|
1070
|
+
|
1071
|
+
describe "when parsing a successful Connection Accepted packet with Session Present set" do
|
1072
|
+
let(:packet) do
|
1073
|
+
MQTT::Packet.parse( "\x20\x02\x01\x00" )
|
1074
|
+
end
|
1075
|
+
|
1076
|
+
it "should correctly create the right type of packet object" do
|
1077
|
+
expect(packet.class).to eq(MQTT::Packet::Connack)
|
1078
|
+
end
|
1079
|
+
|
1080
|
+
it "should set the fixed header flags of the packet correctly" do
|
1081
|
+
expect(packet.flags).to eq([false, false, false, false])
|
1082
|
+
end
|
1083
|
+
|
1084
|
+
it "should set the Session Pression flag of the packet correctly" do
|
1085
|
+
expect(packet.session_present).to eq(true)
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
it "should set the return code of the packet correctly" do
|
1089
|
+
expect(packet.return_code).to eq(0x00)
|
1090
|
+
end
|
1091
|
+
|
1092
|
+
it "should set the return message of the packet correctly" do
|
1093
|
+
expect(packet.return_msg).to match(/Connection Accepted/i)
|
827
1094
|
end
|
828
1095
|
end
|
829
1096
|
|
@@ -833,15 +1100,15 @@ describe MQTT::Packet::Connack do
|
|
833
1100
|
end
|
834
1101
|
|
835
1102
|
it "should correctly create the right type of packet object" do
|
836
|
-
packet.class.
|
1103
|
+
expect(packet.class).to eq(MQTT::Packet::Connack)
|
837
1104
|
end
|
838
1105
|
|
839
1106
|
it "should set the return code of the packet correctly" do
|
840
|
-
packet.return_code.
|
1107
|
+
expect(packet.return_code).to eq(0x01)
|
841
1108
|
end
|
842
1109
|
|
843
1110
|
it "should set the return message of the packet correctly" do
|
844
|
-
packet.return_msg.
|
1111
|
+
expect(packet.return_msg).to match(/unacceptable protocol version/i)
|
845
1112
|
end
|
846
1113
|
end
|
847
1114
|
|
@@ -849,69 +1116,69 @@ describe MQTT::Packet::Connack do
|
|
849
1116
|
let(:packet) { MQTT::Packet.parse( "\x20\x02\x00\x02" ) }
|
850
1117
|
|
851
1118
|
it "should correctly create the right type of packet object" do
|
852
|
-
packet.class.
|
1119
|
+
expect(packet.class).to eq(MQTT::Packet::Connack)
|
853
1120
|
end
|
854
1121
|
|
855
1122
|
it "should set the return code of the packet correctly" do
|
856
|
-
packet.return_code.
|
1123
|
+
expect(packet.return_code).to eq(0x02)
|
857
1124
|
end
|
858
1125
|
|
859
1126
|
it "should set the return message of the packet correctly" do
|
860
|
-
packet.return_msg.
|
1127
|
+
expect(packet.return_msg).to match(/client identifier rejected/i)
|
861
1128
|
end
|
862
1129
|
end
|
863
1130
|
|
864
|
-
describe "when parsing a
|
1131
|
+
describe "when parsing a server unavailable packet" do
|
865
1132
|
let(:packet) do
|
866
1133
|
MQTT::Packet.parse( "\x20\x02\x00\x03" )
|
867
1134
|
end
|
868
1135
|
|
869
1136
|
it "should correctly create the right type of packet object" do
|
870
|
-
packet.class.
|
1137
|
+
expect(packet.class).to eq(MQTT::Packet::Connack)
|
871
1138
|
end
|
872
1139
|
|
873
1140
|
it "should set the return code of the packet correctly" do
|
874
|
-
packet.return_code.
|
1141
|
+
expect(packet.return_code).to eq(0x03)
|
875
1142
|
end
|
876
1143
|
|
877
1144
|
it "should set the return message of the packet correctly" do
|
878
|
-
packet.return_msg.
|
1145
|
+
expect(packet.return_msg).to match(/server unavailable/i)
|
879
1146
|
end
|
880
1147
|
end
|
881
1148
|
|
882
|
-
describe "when parsing a
|
1149
|
+
describe "when parsing a server unavailable packet" do
|
883
1150
|
let(:packet) do
|
884
1151
|
MQTT::Packet.parse( "\x20\x02\x00\x04" )
|
885
1152
|
end
|
886
1153
|
|
887
1154
|
it "should correctly create the right type of packet object" do
|
888
|
-
packet.class.
|
1155
|
+
expect(packet.class).to eq(MQTT::Packet::Connack)
|
889
1156
|
end
|
890
1157
|
|
891
1158
|
it "should set the return code of the packet correctly" do
|
892
|
-
packet.return_code.
|
1159
|
+
expect(packet.return_code).to eq(0x04)
|
893
1160
|
end
|
894
1161
|
|
895
1162
|
it "should set the return message of the packet correctly" do
|
896
|
-
packet.return_msg.
|
1163
|
+
expect(packet.return_msg).to match(/bad user name or password/i)
|
897
1164
|
end
|
898
1165
|
end
|
899
1166
|
|
900
|
-
describe "when parsing a
|
1167
|
+
describe "when parsing a server unavailable packet" do
|
901
1168
|
let(:packet) do
|
902
1169
|
MQTT::Packet.parse( "\x20\x02\x00\x05" )
|
903
1170
|
end
|
904
1171
|
|
905
1172
|
it "should correctly create the right type of packet object" do
|
906
|
-
packet.class.
|
1173
|
+
expect(packet.class).to eq(MQTT::Packet::Connack)
|
907
1174
|
end
|
908
1175
|
|
909
1176
|
it "should set the return code of the packet correctly" do
|
910
|
-
packet.return_code.
|
1177
|
+
expect(packet.return_code).to eq(0x05)
|
911
1178
|
end
|
912
1179
|
|
913
1180
|
it "should set the return message of the packet correctly" do
|
914
|
-
packet.return_msg.
|
1181
|
+
expect(packet.return_msg).to match(/not authorised/i)
|
915
1182
|
end
|
916
1183
|
end
|
917
1184
|
|
@@ -919,37 +1186,59 @@ describe MQTT::Packet::Connack do
|
|
919
1186
|
let(:packet) { MQTT::Packet.parse( "\x20\x02\x00\x10" ) }
|
920
1187
|
|
921
1188
|
it "should correctly create the right type of packet object" do
|
922
|
-
packet.class.
|
1189
|
+
expect(packet.class).to eq(MQTT::Packet::Connack)
|
923
1190
|
end
|
924
1191
|
|
925
1192
|
it "should set the return code of the packet correctly" do
|
926
|
-
packet.return_code.
|
1193
|
+
expect(packet.return_code).to eq(0x10)
|
927
1194
|
end
|
928
1195
|
|
929
1196
|
it "should set the return message of the packet correctly" do
|
930
|
-
packet.return_msg.
|
1197
|
+
expect(packet.return_msg).to match(/Connection refused: error code 16/i)
|
1198
|
+
end
|
1199
|
+
end
|
1200
|
+
|
1201
|
+
describe "when parsing packet with invalid Connack flags set" do
|
1202
|
+
it "should throw an exception" do
|
1203
|
+
expect {
|
1204
|
+
packet = MQTT::Packet.parse( "\x20\x02\xff\x05" )
|
1205
|
+
}.to raise_error(
|
1206
|
+
MQTT::ProtocolException,
|
1207
|
+
"Invalid flags in Connack variable header"
|
1208
|
+
)
|
931
1209
|
end
|
932
1210
|
end
|
933
1211
|
|
934
1212
|
describe "when parsing packet with extra bytes on the end" do
|
935
1213
|
it "should throw an exception" do
|
936
|
-
|
1214
|
+
expect {
|
937
1215
|
packet = MQTT::Packet.parse( "\x20\x03\x00\x00\x00" )
|
938
|
-
}.
|
1216
|
+
}.to raise_error(
|
939
1217
|
MQTT::ProtocolException,
|
940
1218
|
"Extra bytes at end of Connect Acknowledgment packet"
|
941
1219
|
)
|
942
1220
|
end
|
943
1221
|
end
|
944
1222
|
|
1223
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1224
|
+
it "should throw a protocol exception" do
|
1225
|
+
expect {
|
1226
|
+
MQTT::Packet.parse( "\x23\x02\x00\x00" )
|
1227
|
+
}.to raise_error(
|
1228
|
+
MQTT::ProtocolException,
|
1229
|
+
"Invalid flags in CONNACK packet header"
|
1230
|
+
)
|
1231
|
+
end
|
1232
|
+
end
|
1233
|
+
|
945
1234
|
describe "when calling the inspect method" do
|
946
1235
|
it "should output the right string when the return code is 0" do
|
947
1236
|
packet = MQTT::Packet::Connack.new( :return_code => 0x00 )
|
948
|
-
packet.inspect.
|
1237
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Connack: 0x00>")
|
949
1238
|
end
|
950
1239
|
it "should output the right string when the return code is 0x0F" do
|
951
1240
|
packet = MQTT::Packet::Connack.new( :return_code => 0x0F )
|
952
|
-
packet.inspect.
|
1241
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Connack: 0x0F>")
|
953
1242
|
end
|
954
1243
|
end
|
955
1244
|
end
|
@@ -957,8 +1246,8 @@ end
|
|
957
1246
|
describe MQTT::Packet::Puback do
|
958
1247
|
describe "when serialising a packet" do
|
959
1248
|
it "should output the correct bytes for a packet with no flags" do
|
960
|
-
packet = MQTT::Packet::Puback.new( :
|
961
|
-
packet.to_s.
|
1249
|
+
packet = MQTT::Packet::Puback.new( :id => 0x1234 )
|
1250
|
+
expect(packet.to_s).to eq("\x40\x02\x12\x34")
|
962
1251
|
end
|
963
1252
|
end
|
964
1253
|
|
@@ -966,36 +1255,47 @@ describe MQTT::Packet::Puback do
|
|
966
1255
|
let(:packet) { MQTT::Packet.parse( "\x40\x02\x12\x34" ) }
|
967
1256
|
|
968
1257
|
it "should correctly create the right type of packet object" do
|
969
|
-
packet.class.
|
1258
|
+
expect(packet.class).to eq(MQTT::Packet::Puback)
|
970
1259
|
end
|
971
1260
|
|
972
1261
|
it "should set the message id of the packet correctly" do
|
973
|
-
packet.
|
1262
|
+
expect(packet.id).to eq(0x1234)
|
974
1263
|
end
|
975
1264
|
end
|
976
1265
|
|
977
1266
|
describe "when parsing packet with extra bytes on the end" do
|
978
1267
|
it "should throw an exception" do
|
979
|
-
|
1268
|
+
expect {
|
980
1269
|
packet = MQTT::Packet.parse( "\x40\x03\x12\x34\x00" )
|
981
|
-
}.
|
1270
|
+
}.to raise_error(
|
982
1271
|
MQTT::ProtocolException,
|
983
1272
|
"Extra bytes at end of Publish Acknowledgment packet"
|
984
1273
|
)
|
985
1274
|
end
|
986
1275
|
end
|
987
1276
|
|
1277
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1278
|
+
it "should throw a protocol exception" do
|
1279
|
+
expect {
|
1280
|
+
MQTT::Packet.parse( "\x43\x02\x12\x34" )
|
1281
|
+
}.to raise_error(
|
1282
|
+
MQTT::ProtocolException,
|
1283
|
+
"Invalid flags in PUBACK packet header"
|
1284
|
+
)
|
1285
|
+
end
|
1286
|
+
end
|
1287
|
+
|
988
1288
|
it "should output the right string when calling inspect" do
|
989
|
-
packet = MQTT::Packet::Puback.new( :
|
990
|
-
packet.inspect.
|
1289
|
+
packet = MQTT::Packet::Puback.new( :id => 0x1234 )
|
1290
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Puback: 0x1234>")
|
991
1291
|
end
|
992
1292
|
end
|
993
1293
|
|
994
1294
|
describe MQTT::Packet::Pubrec do
|
995
1295
|
describe "when serialising a packet" do
|
996
1296
|
it "should output the correct bytes for a packet with no flags" do
|
997
|
-
packet = MQTT::Packet::Pubrec.new( :
|
998
|
-
packet.to_s.
|
1297
|
+
packet = MQTT::Packet::Pubrec.new( :id => 0x1234 )
|
1298
|
+
expect(packet.to_s).to eq("\x50\x02\x12\x34")
|
999
1299
|
end
|
1000
1300
|
end
|
1001
1301
|
|
@@ -1003,73 +1303,95 @@ describe MQTT::Packet::Pubrec do
|
|
1003
1303
|
let(:packet) { MQTT::Packet.parse( "\x50\x02\x12\x34" ) }
|
1004
1304
|
|
1005
1305
|
it "should correctly create the right type of packet object" do
|
1006
|
-
packet.class.
|
1306
|
+
expect(packet.class).to eq(MQTT::Packet::Pubrec)
|
1007
1307
|
end
|
1008
1308
|
|
1009
1309
|
it "should set the message id of the packet correctly" do
|
1010
|
-
packet.
|
1310
|
+
expect(packet.id).to eq(0x1234)
|
1011
1311
|
end
|
1012
1312
|
end
|
1013
1313
|
|
1014
1314
|
describe "when parsing packet with extra bytes on the end" do
|
1015
1315
|
it "should throw an exception" do
|
1016
|
-
|
1316
|
+
expect {
|
1017
1317
|
packet = MQTT::Packet.parse( "\x50\x03\x12\x34\x00" )
|
1018
|
-
}.
|
1318
|
+
}.to raise_error(
|
1019
1319
|
MQTT::ProtocolException,
|
1020
1320
|
"Extra bytes at end of Publish Received packet"
|
1021
1321
|
)
|
1022
1322
|
end
|
1023
1323
|
end
|
1024
1324
|
|
1325
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1326
|
+
it "should throw a protocol exception" do
|
1327
|
+
expect {
|
1328
|
+
MQTT::Packet.parse( "\x53\x02\x12\x34" )
|
1329
|
+
}.to raise_error(
|
1330
|
+
MQTT::ProtocolException,
|
1331
|
+
"Invalid flags in PUBREC packet header"
|
1332
|
+
)
|
1333
|
+
end
|
1334
|
+
end
|
1335
|
+
|
1025
1336
|
it "should output the right string when calling inspect" do
|
1026
|
-
packet = MQTT::Packet::Pubrec.new( :
|
1027
|
-
packet.inspect.
|
1337
|
+
packet = MQTT::Packet::Pubrec.new( :id => 0x1234 )
|
1338
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Pubrec: 0x1234>")
|
1028
1339
|
end
|
1029
1340
|
end
|
1030
1341
|
|
1031
1342
|
describe MQTT::Packet::Pubrel do
|
1032
1343
|
describe "when serialising a packet" do
|
1033
1344
|
it "should output the correct bytes for a packet with no flags" do
|
1034
|
-
packet = MQTT::Packet::Pubrel.new( :
|
1035
|
-
packet.to_s.
|
1345
|
+
packet = MQTT::Packet::Pubrel.new( :id => 0x1234 )
|
1346
|
+
expect(packet.to_s).to eq("\x62\x02\x12\x34")
|
1036
1347
|
end
|
1037
1348
|
end
|
1038
1349
|
|
1039
1350
|
describe "when parsing a packet" do
|
1040
|
-
let(:packet) { MQTT::Packet.parse( "\
|
1351
|
+
let(:packet) { MQTT::Packet.parse( "\x62\x02\x12\x34" ) }
|
1041
1352
|
|
1042
1353
|
it "should correctly create the right type of packet object" do
|
1043
|
-
packet.class.
|
1354
|
+
expect(packet.class).to eq(MQTT::Packet::Pubrel)
|
1044
1355
|
end
|
1045
1356
|
|
1046
1357
|
it "should set the message id of the packet correctly" do
|
1047
|
-
packet.
|
1358
|
+
expect(packet.id).to eq(0x1234)
|
1048
1359
|
end
|
1049
1360
|
end
|
1050
1361
|
|
1051
1362
|
describe "when parsing packet with extra bytes on the end" do
|
1052
1363
|
it "should throw an exception" do
|
1053
|
-
|
1054
|
-
packet = MQTT::Packet.parse( "\
|
1055
|
-
}.
|
1364
|
+
expect {
|
1365
|
+
packet = MQTT::Packet.parse( "\x62\x03\x12\x34\x00" )
|
1366
|
+
}.to raise_error(
|
1056
1367
|
MQTT::ProtocolException,
|
1057
1368
|
"Extra bytes at end of Publish Release packet"
|
1058
1369
|
)
|
1059
1370
|
end
|
1060
1371
|
end
|
1061
1372
|
|
1373
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1374
|
+
it "should throw a protocol exception" do
|
1375
|
+
expect {
|
1376
|
+
MQTT::Packet.parse( "\x60\x02\x12\x34" )
|
1377
|
+
}.to raise_error(
|
1378
|
+
MQTT::ProtocolException,
|
1379
|
+
"Invalid flags in PUBREL packet header"
|
1380
|
+
)
|
1381
|
+
end
|
1382
|
+
end
|
1383
|
+
|
1062
1384
|
it "should output the right string when calling inspect" do
|
1063
|
-
packet = MQTT::Packet::Pubrel.new( :
|
1064
|
-
packet.inspect.
|
1385
|
+
packet = MQTT::Packet::Pubrel.new( :id => 0x1234 )
|
1386
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Pubrel: 0x1234>")
|
1065
1387
|
end
|
1066
1388
|
end
|
1067
1389
|
|
1068
1390
|
describe MQTT::Packet::Pubcomp do
|
1069
1391
|
describe "when serialising a packet" do
|
1070
1392
|
it "should output the correct bytes for a packet with no flags" do
|
1071
|
-
packet = MQTT::Packet::Pubcomp.new( :
|
1072
|
-
packet.to_s.
|
1393
|
+
packet = MQTT::Packet::Pubcomp.new( :id => 0x1234 )
|
1394
|
+
expect(packet.to_s).to eq("\x70\x02\x12\x34")
|
1073
1395
|
end
|
1074
1396
|
end
|
1075
1397
|
|
@@ -1077,28 +1399,39 @@ describe MQTT::Packet::Pubcomp do
|
|
1077
1399
|
let(:packet) { MQTT::Packet.parse( "\x70\x02\x12\x34" ) }
|
1078
1400
|
|
1079
1401
|
it "should correctly create the right type of packet object" do
|
1080
|
-
packet.class.
|
1402
|
+
expect(packet.class).to eq(MQTT::Packet::Pubcomp)
|
1081
1403
|
end
|
1082
1404
|
|
1083
1405
|
it "should set the message id of the packet correctly" do
|
1084
|
-
packet.
|
1406
|
+
expect(packet.id).to eq(0x1234)
|
1085
1407
|
end
|
1086
1408
|
end
|
1087
1409
|
|
1088
1410
|
describe "when parsing packet with extra bytes on the end" do
|
1089
1411
|
it "should throw an exception" do
|
1090
|
-
|
1412
|
+
expect {
|
1091
1413
|
MQTT::Packet.parse( "\x70\x03\x12\x34\x00" )
|
1092
|
-
}.
|
1414
|
+
}.to raise_error(
|
1093
1415
|
MQTT::ProtocolException,
|
1094
1416
|
"Extra bytes at end of Publish Complete packet"
|
1095
1417
|
)
|
1096
1418
|
end
|
1097
1419
|
end
|
1098
1420
|
|
1421
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1422
|
+
it "should throw a protocol exception" do
|
1423
|
+
expect {
|
1424
|
+
MQTT::Packet.parse( "\x72\x02\x12\x34" )
|
1425
|
+
}.to raise_error(
|
1426
|
+
MQTT::ProtocolException,
|
1427
|
+
"Invalid flags in PUBCOMP packet header"
|
1428
|
+
)
|
1429
|
+
end
|
1430
|
+
end
|
1431
|
+
|
1099
1432
|
it "should output the right string when calling inspect" do
|
1100
|
-
packet = MQTT::Packet::Pubcomp.new( :
|
1101
|
-
packet.inspect.
|
1433
|
+
packet = MQTT::Packet::Pubcomp.new( :id => 0x1234 )
|
1434
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Pubcomp: 0x1234>")
|
1102
1435
|
end
|
1103
1436
|
end
|
1104
1437
|
|
@@ -1108,33 +1441,33 @@ describe MQTT::Packet::Subscribe do
|
|
1108
1441
|
|
1109
1442
|
it "should be able to set the topics from a String 'a/b'" do
|
1110
1443
|
packet.topics = 'a/b'
|
1111
|
-
packet.topics.
|
1444
|
+
expect(packet.topics).to eq([["a/b", 0]])
|
1112
1445
|
end
|
1113
1446
|
|
1114
1447
|
it "should be able to set the multiple topics from an array ['a/b', 'b/c']" do
|
1115
1448
|
packet.topics = ['a/b', 'b/c']
|
1116
|
-
packet.topics.
|
1449
|
+
expect(packet.topics).to eq([["a/b", 0], ['b/c', 0]])
|
1117
1450
|
end
|
1118
1451
|
|
1119
1452
|
it "should be able to set the topics from a Hash {'a/b' => 0, 'b/c' => 1}" do
|
1120
1453
|
packet.topics = {'a/b' => 0, 'b/c' => 1}
|
1121
|
-
packet.topics.
|
1454
|
+
expect(packet.topics).to eq([["a/b", 0], ["b/c", 1]])
|
1122
1455
|
end
|
1123
1456
|
|
1124
1457
|
it "should be able to set the topics from a single level array ['a/b', 0]" do
|
1125
1458
|
packet.topics = ['a/b', 0]
|
1126
|
-
packet.topics.
|
1459
|
+
expect(packet.topics).to eq([["a/b", 0]])
|
1127
1460
|
end
|
1128
1461
|
|
1129
1462
|
it "should be able to set the topics from a two level array [['a/b' => 0], ['b/c' => 1]]" do
|
1130
1463
|
packet.topics = [['a/b', 0], ['b/c', 1]]
|
1131
|
-
packet.topics.
|
1464
|
+
expect(packet.topics).to eq([['a/b', 0], ['b/c', 1]])
|
1132
1465
|
end
|
1133
1466
|
|
1134
1467
|
it "should throw an exception when setting topic with a non-string" do
|
1135
|
-
|
1468
|
+
expect {
|
1136
1469
|
packet.topics = 56
|
1137
|
-
}.
|
1470
|
+
}.to raise_error(
|
1138
1471
|
'Invalid topics input: 56'
|
1139
1472
|
)
|
1140
1473
|
end
|
@@ -1142,19 +1475,19 @@ describe MQTT::Packet::Subscribe do
|
|
1142
1475
|
|
1143
1476
|
describe "when serialising a packet" do
|
1144
1477
|
it "should output the correct bytes for a packet with a single topic" do
|
1145
|
-
packet = MQTT::Packet::Subscribe.new( :topics => 'a/b'
|
1146
|
-
packet.to_s.
|
1478
|
+
packet = MQTT::Packet::Subscribe.new( :id => 1, :topics => 'a/b' )
|
1479
|
+
expect(packet.to_s).to eq("\x82\x08\x00\x01\x00\x03a/b\x00")
|
1147
1480
|
end
|
1148
1481
|
|
1149
1482
|
it "should output the correct bytes for a packet with multiple topics" do
|
1150
|
-
packet = MQTT::Packet::Subscribe.new( :topics => [['a/b', 0], ['c/d', 1]]
|
1151
|
-
packet.to_s.
|
1483
|
+
packet = MQTT::Packet::Subscribe.new( :id => 6, :topics => [['a/b', 0], ['c/d', 1]] )
|
1484
|
+
expect(packet.to_s).to eq("\x82\x0e\000\x06\x00\x03a/b\x00\x00\x03c/d\x01")
|
1152
1485
|
end
|
1153
1486
|
|
1154
1487
|
it "should throw an exception when no topics are given" do
|
1155
|
-
|
1488
|
+
expect {
|
1156
1489
|
MQTT::Packet::Subscribe.new.to_s
|
1157
|
-
}.
|
1490
|
+
}.to raise_error(
|
1158
1491
|
'no topics given when serialising packet'
|
1159
1492
|
)
|
1160
1493
|
end
|
@@ -1164,19 +1497,19 @@ describe MQTT::Packet::Subscribe do
|
|
1164
1497
|
let(:packet) { MQTT::Packet.parse( "\x82\x08\x00\x01\x00\x03a/b\x00" ) }
|
1165
1498
|
|
1166
1499
|
it "should correctly create the right type of packet object" do
|
1167
|
-
packet.class.
|
1500
|
+
expect(packet.class).to eq(MQTT::Packet::Subscribe)
|
1168
1501
|
end
|
1169
1502
|
|
1170
|
-
it "should set the
|
1171
|
-
packet.
|
1503
|
+
it "should set the fixed header flags of the packet correctly" do
|
1504
|
+
expect(packet.flags).to eq([false, true, false, false])
|
1172
1505
|
end
|
1173
1506
|
|
1174
1507
|
it "should set the Message ID correctly" do
|
1175
|
-
packet.
|
1508
|
+
expect(packet.id).to eq(1)
|
1176
1509
|
end
|
1177
1510
|
|
1178
1511
|
it "should set the topic name correctly" do
|
1179
|
-
packet.topics.
|
1512
|
+
expect(packet.topics).to eq([['a/b',0]])
|
1180
1513
|
end
|
1181
1514
|
end
|
1182
1515
|
|
@@ -1184,31 +1517,42 @@ describe MQTT::Packet::Subscribe do
|
|
1184
1517
|
let(:packet) { MQTT::Packet.parse( "\x82\x0e\000\x06\x00\x03a/b\x00\x00\x03c/d\x01" ) }
|
1185
1518
|
|
1186
1519
|
it "should correctly create the right type of packet object" do
|
1187
|
-
packet.class.
|
1520
|
+
expect(packet.class).to eq(MQTT::Packet::Subscribe)
|
1188
1521
|
end
|
1189
1522
|
|
1190
|
-
it "should set the
|
1191
|
-
packet.
|
1523
|
+
it "should set the fixed header flags of the packet correctly" do
|
1524
|
+
expect(packet.flags).to eq([false, true, false, false])
|
1192
1525
|
end
|
1193
1526
|
|
1194
1527
|
it "should set the Message ID correctly" do
|
1195
|
-
packet.
|
1528
|
+
expect(packet.id).to eq(6)
|
1196
1529
|
end
|
1197
1530
|
|
1198
1531
|
it "should set the topic name correctly" do
|
1199
|
-
packet.topics.
|
1532
|
+
expect(packet.topics).to eq([['a/b',0],['c/d',1]])
|
1533
|
+
end
|
1534
|
+
end
|
1535
|
+
|
1536
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1537
|
+
it "should throw a protocol exception" do
|
1538
|
+
expect {
|
1539
|
+
MQTT::Packet.parse( "\x80\x08\x00\x01\x00\x03a/b\x00" )
|
1540
|
+
}.to raise_error(
|
1541
|
+
MQTT::ProtocolException,
|
1542
|
+
"Invalid flags in SUBSCRIBE packet header"
|
1543
|
+
)
|
1200
1544
|
end
|
1201
1545
|
end
|
1202
1546
|
|
1203
1547
|
describe "when calling the inspect method" do
|
1204
1548
|
it "should output correct string for a single topic" do
|
1205
1549
|
packet = MQTT::Packet::Subscribe.new(:topics => 'test')
|
1206
|
-
packet.inspect.
|
1550
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Subscribe: 0x00, 'test':0>")
|
1207
1551
|
end
|
1208
1552
|
|
1209
1553
|
it "should output correct string for multiple topics" do
|
1210
1554
|
packet = MQTT::Packet::Subscribe.new(:topics => {'a' => 1, 'b' => 0, 'c' => 2})
|
1211
|
-
packet.inspect.
|
1555
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Subscribe: 0x00, 'a':1, 'b':0, 'c':2>")
|
1212
1556
|
end
|
1213
1557
|
end
|
1214
1558
|
end
|
@@ -1216,28 +1560,28 @@ end
|
|
1216
1560
|
describe MQTT::Packet::Suback do
|
1217
1561
|
describe "when serialising a packet" do
|
1218
1562
|
it "should output the correct bytes for an acknowledgement to a single topic" do
|
1219
|
-
packet = MQTT::Packet::Suback.new( :
|
1220
|
-
packet.to_s.
|
1563
|
+
packet = MQTT::Packet::Suback.new( :id => 5, :return_codes => 0 )
|
1564
|
+
expect(packet.to_s).to eq("\x90\x03\x00\x05\x00")
|
1221
1565
|
end
|
1222
1566
|
|
1223
1567
|
it "should output the correct bytes for an acknowledgement to a two topics" do
|
1224
|
-
packet = MQTT::Packet::Suback.new( :
|
1225
|
-
packet.to_s.
|
1568
|
+
packet = MQTT::Packet::Suback.new( :id => 6 , :return_codes => [0,1] )
|
1569
|
+
expect(packet.to_s).to eq("\x90\x04\x00\x06\x00\x01")
|
1226
1570
|
end
|
1227
1571
|
|
1228
1572
|
it "should throw an exception when no granted QOSs are given" do
|
1229
|
-
|
1230
|
-
MQTT::Packet::Suback.new(:
|
1231
|
-
}.
|
1573
|
+
expect {
|
1574
|
+
MQTT::Packet::Suback.new( :id => 7 ).to_s
|
1575
|
+
}.to raise_error(
|
1232
1576
|
'no granted QOS given when serialising packet'
|
1233
1577
|
)
|
1234
1578
|
end
|
1235
1579
|
|
1236
1580
|
it "should throw an exception if the granted QOS is not an integer" do
|
1237
|
-
|
1238
|
-
MQTT::Packet::Suback.new(:
|
1239
|
-
}.
|
1240
|
-
'
|
1581
|
+
expect {
|
1582
|
+
MQTT::Packet::Suback.new( :id => 8, :return_codes => :foo ).to_s
|
1583
|
+
}.to raise_error(
|
1584
|
+
'return_codes should be an integer or an array of return codes'
|
1241
1585
|
)
|
1242
1586
|
end
|
1243
1587
|
end
|
@@ -1246,15 +1590,15 @@ describe MQTT::Packet::Suback do
|
|
1246
1590
|
let(:packet) { MQTT::Packet.parse( "\x90\x03\x12\x34\x00" ) }
|
1247
1591
|
|
1248
1592
|
it "should correctly create the right type of packet object" do
|
1249
|
-
packet.class.
|
1593
|
+
expect(packet.class).to eq(MQTT::Packet::Suback)
|
1250
1594
|
end
|
1251
1595
|
|
1252
1596
|
it "should set the message id of the packet correctly" do
|
1253
|
-
packet.
|
1597
|
+
expect(packet.id).to eq(0x1234)
|
1254
1598
|
end
|
1255
1599
|
|
1256
1600
|
it "should set the Granted QOS of the packet correctly" do
|
1257
|
-
packet.
|
1601
|
+
expect(packet.return_codes).to eq([0])
|
1258
1602
|
end
|
1259
1603
|
end
|
1260
1604
|
|
@@ -1262,27 +1606,50 @@ describe MQTT::Packet::Suback do
|
|
1262
1606
|
let(:packet) { MQTT::Packet.parse( "\x90\x04\x12\x34\x01\x01" ) }
|
1263
1607
|
|
1264
1608
|
it "should correctly create the right type of packet object" do
|
1265
|
-
packet.class.
|
1609
|
+
expect(packet.class).to eq(MQTT::Packet::Suback)
|
1266
1610
|
end
|
1267
1611
|
|
1268
1612
|
it "should set the message id of the packet correctly" do
|
1269
|
-
packet.
|
1613
|
+
expect(packet.id).to eq(0x1234)
|
1270
1614
|
end
|
1271
1615
|
|
1272
1616
|
it "should set the Granted QOS of the packet correctly" do
|
1273
|
-
packet.
|
1617
|
+
expect(packet.return_codes).to eq([1,1])
|
1618
|
+
end
|
1619
|
+
end
|
1620
|
+
|
1621
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1622
|
+
it "should throw a protocol exception" do
|
1623
|
+
expect {
|
1624
|
+
MQTT::Packet.parse( "\x92\x03\x12\x34\x00" )
|
1625
|
+
}.to raise_error(
|
1626
|
+
MQTT::ProtocolException,
|
1627
|
+
"Invalid flags in SUBACK packet header"
|
1628
|
+
)
|
1274
1629
|
end
|
1275
1630
|
end
|
1276
1631
|
|
1277
1632
|
describe "when calling the inspect method" do
|
1278
1633
|
it "should output correct string for a single granted qos" do
|
1279
|
-
packet = MQTT::Packet::Suback.new(:
|
1280
|
-
packet.inspect.
|
1634
|
+
packet = MQTT::Packet::Suback.new(:id => 0x1234, :return_codes => 0)
|
1635
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Suback: 0x1234, rc=0x00>")
|
1281
1636
|
end
|
1282
1637
|
|
1283
1638
|
it "should output correct string for multiple topics" do
|
1284
|
-
packet = MQTT::Packet::Suback.new(:
|
1285
|
-
packet.inspect.
|
1639
|
+
packet = MQTT::Packet::Suback.new(:id => 0x1235, :return_codes => [0,1,2])
|
1640
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Suback: 0x1235, rc=0x00,0x01,0x02>")
|
1641
|
+
end
|
1642
|
+
end
|
1643
|
+
|
1644
|
+
describe "deprecated attributes" do
|
1645
|
+
it "should still have a granted_qos method that is that same as return_codes" do
|
1646
|
+
packet = MQTT::Packet::Suback.new
|
1647
|
+
packet.granted_qos = [0,1,2]
|
1648
|
+
expect(packet.granted_qos).to eq([0,1,2])
|
1649
|
+
expect(packet.return_codes).to eq([0,1,2])
|
1650
|
+
packet.return_codes = [0,1,0]
|
1651
|
+
expect(packet.granted_qos).to eq([0,1,0])
|
1652
|
+
expect(packet.return_codes).to eq([0,1,0])
|
1286
1653
|
end
|
1287
1654
|
end
|
1288
1655
|
end
|
@@ -1290,19 +1657,19 @@ end
|
|
1290
1657
|
describe MQTT::Packet::Unsubscribe do
|
1291
1658
|
describe "when serialising a packet" do
|
1292
1659
|
it "should output the correct bytes for a packet with single topic" do
|
1293
|
-
packet = MQTT::Packet::Unsubscribe.new( :topics => 'a/b'
|
1294
|
-
packet.to_s.
|
1660
|
+
packet = MQTT::Packet::Unsubscribe.new( :id => 5, :topics => 'a/b' )
|
1661
|
+
expect(packet.to_s).to eq("\xa2\x07\x00\x05\x00\x03a/b")
|
1295
1662
|
end
|
1296
1663
|
|
1297
1664
|
it "should output the correct bytes for a packet with multiple topics" do
|
1298
|
-
packet = MQTT::Packet::Unsubscribe.new( :topics => ['a/b','c/d']
|
1299
|
-
packet.to_s.
|
1665
|
+
packet = MQTT::Packet::Unsubscribe.new( :id => 6, :topics => ['a/b','c/d'] )
|
1666
|
+
expect(packet.to_s).to eq("\xa2\x0c\000\006\000\003a/b\000\003c/d")
|
1300
1667
|
end
|
1301
1668
|
|
1302
1669
|
it "should throw an exception when no topics are given" do
|
1303
|
-
|
1670
|
+
expect {
|
1304
1671
|
MQTT::Packet::Unsubscribe.new.to_s
|
1305
|
-
}.
|
1672
|
+
}.to raise_error(
|
1306
1673
|
'no topics given when serialising packet'
|
1307
1674
|
)
|
1308
1675
|
end
|
@@ -1312,27 +1679,38 @@ describe MQTT::Packet::Unsubscribe do
|
|
1312
1679
|
let(:packet) { MQTT::Packet.parse( "\xa2\f\000\005\000\003a/b\000\003c/d" ) }
|
1313
1680
|
|
1314
1681
|
it "should correctly create the right type of packet object" do
|
1315
|
-
packet.class.
|
1682
|
+
expect(packet.class).to eq(MQTT::Packet::Unsubscribe)
|
1316
1683
|
end
|
1317
1684
|
|
1318
|
-
it "should set the
|
1319
|
-
packet.
|
1685
|
+
it "should set the fixed header flags of the packet correctly" do
|
1686
|
+
expect(packet.flags).to eq([false, true, false, false])
|
1320
1687
|
end
|
1321
1688
|
|
1322
1689
|
it "should set the topic name correctly" do
|
1323
|
-
packet.topics.
|
1690
|
+
expect(packet.topics).to eq(['a/b','c/d'])
|
1691
|
+
end
|
1692
|
+
end
|
1693
|
+
|
1694
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1695
|
+
it "should throw a protocol exception" do
|
1696
|
+
expect {
|
1697
|
+
MQTT::Packet.parse( "\xa0\x07\x00\x05\x00\x03a/b" )
|
1698
|
+
}.to raise_error(
|
1699
|
+
MQTT::ProtocolException,
|
1700
|
+
"Invalid flags in UNSUBSCRIBE packet header"
|
1701
|
+
)
|
1324
1702
|
end
|
1325
1703
|
end
|
1326
1704
|
|
1327
1705
|
describe "when calling the inspect method" do
|
1328
1706
|
it "should output correct string for a single topic" do
|
1329
1707
|
packet = MQTT::Packet::Unsubscribe.new(:topics => 'test')
|
1330
|
-
packet.inspect.
|
1708
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Unsubscribe: 0x00, 'test'>")
|
1331
1709
|
end
|
1332
1710
|
|
1333
1711
|
it "should output correct string for multiple topics" do
|
1334
|
-
packet = MQTT::Packet::Unsubscribe.new(:
|
1335
|
-
packet.inspect.
|
1712
|
+
packet = MQTT::Packet::Unsubscribe.new( :id => 42, :topics => ['a', 'b', 'c'] )
|
1713
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Unsubscribe: 0x2A, 'a', 'b', 'c'>")
|
1336
1714
|
end
|
1337
1715
|
end
|
1338
1716
|
end
|
@@ -1340,8 +1718,8 @@ end
|
|
1340
1718
|
describe MQTT::Packet::Unsuback do
|
1341
1719
|
describe "when serialising a packet" do
|
1342
1720
|
it "should output the correct bytes for a packet with no flags" do
|
1343
|
-
packet = MQTT::Packet::Unsuback.new( :
|
1344
|
-
packet.to_s.
|
1721
|
+
packet = MQTT::Packet::Unsuback.new( :id => 0x1234 )
|
1722
|
+
expect(packet.to_s).to eq("\xB0\x02\x12\x34")
|
1345
1723
|
end
|
1346
1724
|
end
|
1347
1725
|
|
@@ -1351,28 +1729,39 @@ describe MQTT::Packet::Unsuback do
|
|
1351
1729
|
end
|
1352
1730
|
|
1353
1731
|
it "should correctly create the right type of packet object" do
|
1354
|
-
packet.class.
|
1732
|
+
expect(packet.class).to eq(MQTT::Packet::Unsuback)
|
1355
1733
|
end
|
1356
1734
|
|
1357
1735
|
it "should set the message id of the packet correctly" do
|
1358
|
-
packet.
|
1736
|
+
expect(packet.id).to eq(0x1234)
|
1359
1737
|
end
|
1360
1738
|
end
|
1361
1739
|
|
1362
1740
|
describe "when parsing packet with extra bytes on the end" do
|
1363
1741
|
it "should throw an exception" do
|
1364
|
-
|
1742
|
+
expect {
|
1365
1743
|
packet = MQTT::Packet.parse( "\xB0\x03\x12\x34\x00" )
|
1366
|
-
}.
|
1744
|
+
}.to raise_error(
|
1367
1745
|
MQTT::ProtocolException,
|
1368
1746
|
"Extra bytes at end of Unsubscribe Acknowledgment packet"
|
1369
1747
|
)
|
1370
1748
|
end
|
1371
1749
|
end
|
1372
1750
|
|
1751
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1752
|
+
it "should throw a protocol exception" do
|
1753
|
+
expect {
|
1754
|
+
MQTT::Packet.parse( "\xB2\x02\x12\x34" )
|
1755
|
+
}.to raise_error(
|
1756
|
+
MQTT::ProtocolException,
|
1757
|
+
"Invalid flags in UNSUBACK packet header"
|
1758
|
+
)
|
1759
|
+
end
|
1760
|
+
end
|
1761
|
+
|
1373
1762
|
it "should output the right string when calling inspect" do
|
1374
|
-
packet = MQTT::Packet::Unsuback.new( :
|
1375
|
-
packet.inspect.
|
1763
|
+
packet = MQTT::Packet::Unsuback.new( :id => 0x1234 )
|
1764
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Unsuback: 0x1234>")
|
1376
1765
|
end
|
1377
1766
|
end
|
1378
1767
|
|
@@ -1380,28 +1769,39 @@ describe MQTT::Packet::Pingreq do
|
|
1380
1769
|
describe "when serialising a packet" do
|
1381
1770
|
it "should output the correct bytes for a packet with no flags" do
|
1382
1771
|
packet = MQTT::Packet::Pingreq.new
|
1383
|
-
packet.to_s.
|
1772
|
+
expect(packet.to_s).to eq("\xC0\x00")
|
1384
1773
|
end
|
1385
1774
|
end
|
1386
1775
|
|
1387
1776
|
describe "when parsing a packet" do
|
1388
1777
|
it "should correctly create the right type of packet object" do
|
1389
1778
|
packet = MQTT::Packet.parse( "\xC0\x00" )
|
1390
|
-
packet.class.
|
1779
|
+
expect(packet.class).to eq(MQTT::Packet::Pingreq)
|
1391
1780
|
end
|
1392
1781
|
|
1393
1782
|
it "should throw an exception if the packet has a payload" do
|
1394
|
-
|
1783
|
+
expect {
|
1395
1784
|
MQTT::Packet.parse( "\xC0\x05hello" )
|
1396
|
-
}.
|
1785
|
+
}.to raise_error(
|
1397
1786
|
'Extra bytes at end of Ping Request packet'
|
1398
1787
|
)
|
1399
1788
|
end
|
1400
1789
|
end
|
1401
1790
|
|
1791
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1792
|
+
it "should throw a protocol exception" do
|
1793
|
+
expect {
|
1794
|
+
MQTT::Packet.parse( "\xC2\x00" )
|
1795
|
+
}.to raise_error(
|
1796
|
+
MQTT::ProtocolException,
|
1797
|
+
"Invalid flags in PINGREQ packet header"
|
1798
|
+
)
|
1799
|
+
end
|
1800
|
+
end
|
1801
|
+
|
1402
1802
|
it "should output the right string when calling inspect" do
|
1403
1803
|
packet = MQTT::Packet::Pingreq.new
|
1404
|
-
packet.inspect.
|
1804
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Pingreq>")
|
1405
1805
|
end
|
1406
1806
|
end
|
1407
1807
|
|
@@ -1409,28 +1809,39 @@ describe MQTT::Packet::Pingresp do
|
|
1409
1809
|
describe "when serialising a packet" do
|
1410
1810
|
it "should output the correct bytes for a packet with no flags" do
|
1411
1811
|
packet = MQTT::Packet::Pingresp.new
|
1412
|
-
packet.to_s.
|
1812
|
+
expect(packet.to_s).to eq("\xD0\x00")
|
1413
1813
|
end
|
1414
1814
|
end
|
1415
1815
|
|
1416
1816
|
describe "when parsing a packet" do
|
1417
1817
|
it "should correctly create the right type of packet object" do
|
1418
1818
|
packet = MQTT::Packet.parse( "\xD0\x00" )
|
1419
|
-
packet.class.
|
1819
|
+
expect(packet.class).to eq(MQTT::Packet::Pingresp)
|
1420
1820
|
end
|
1421
1821
|
|
1422
1822
|
it "should throw an exception if the packet has a payload" do
|
1423
|
-
|
1823
|
+
expect {
|
1424
1824
|
MQTT::Packet.parse( "\xD0\x05hello" )
|
1425
|
-
}.
|
1825
|
+
}.to raise_error(
|
1426
1826
|
'Extra bytes at end of Ping Response packet'
|
1427
1827
|
)
|
1428
1828
|
end
|
1429
1829
|
end
|
1430
1830
|
|
1831
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1832
|
+
it "should throw a protocol exception" do
|
1833
|
+
expect {
|
1834
|
+
MQTT::Packet.parse( "\xD2\x00" )
|
1835
|
+
}.to raise_error(
|
1836
|
+
MQTT::ProtocolException,
|
1837
|
+
"Invalid flags in PINGRESP packet header"
|
1838
|
+
)
|
1839
|
+
end
|
1840
|
+
end
|
1841
|
+
|
1431
1842
|
it "should output the right string when calling inspect" do
|
1432
1843
|
packet = MQTT::Packet::Pingresp.new
|
1433
|
-
packet.inspect.
|
1844
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Pingresp>")
|
1434
1845
|
end
|
1435
1846
|
end
|
1436
1847
|
|
@@ -1439,28 +1850,39 @@ describe MQTT::Packet::Disconnect do
|
|
1439
1850
|
describe "when serialising a packet" do
|
1440
1851
|
it "should output the correct bytes for a packet with no flags" do
|
1441
1852
|
packet = MQTT::Packet::Disconnect.new
|
1442
|
-
packet.to_s.
|
1853
|
+
expect(packet.to_s).to eq("\xE0\x00")
|
1443
1854
|
end
|
1444
1855
|
end
|
1445
1856
|
|
1446
1857
|
describe "when parsing a packet" do
|
1447
1858
|
it "should correctly create the right type of packet object" do
|
1448
1859
|
packet = MQTT::Packet.parse( "\xE0\x00" )
|
1449
|
-
packet.class.
|
1860
|
+
expect(packet.class).to eq(MQTT::Packet::Disconnect)
|
1450
1861
|
end
|
1451
1862
|
|
1452
1863
|
it "should throw an exception if the packet has a payload" do
|
1453
|
-
|
1864
|
+
expect {
|
1454
1865
|
MQTT::Packet.parse( "\xE0\x05hello" )
|
1455
|
-
}.
|
1866
|
+
}.to raise_error(
|
1456
1867
|
'Extra bytes at end of Disconnect packet'
|
1457
1868
|
)
|
1458
1869
|
end
|
1459
1870
|
end
|
1460
1871
|
|
1872
|
+
describe "when parsing packet with invalid fixed header flags" do
|
1873
|
+
it "should throw a protocol exception" do
|
1874
|
+
expect {
|
1875
|
+
MQTT::Packet.parse( "\xE2\x00" )
|
1876
|
+
}.to raise_error(
|
1877
|
+
MQTT::ProtocolException,
|
1878
|
+
"Invalid flags in DISCONNECT packet header"
|
1879
|
+
)
|
1880
|
+
end
|
1881
|
+
end
|
1882
|
+
|
1461
1883
|
it "should output the right string when calling inspect" do
|
1462
1884
|
packet = MQTT::Packet::Disconnect.new
|
1463
|
-
packet.inspect.
|
1885
|
+
expect(packet.inspect).to eq("#<MQTT::Packet::Disconnect>")
|
1464
1886
|
end
|
1465
1887
|
end
|
1466
1888
|
|
@@ -1468,9 +1890,9 @@ end
|
|
1468
1890
|
describe "Serialising an invalid packet" do
|
1469
1891
|
context "that has a no type" do
|
1470
1892
|
it "should throw an exception" do
|
1471
|
-
|
1893
|
+
expect {
|
1472
1894
|
MQTT::Packet.new.to_s
|
1473
|
-
}.
|
1895
|
+
}.to raise_error(
|
1474
1896
|
RuntimeError,
|
1475
1897
|
"Invalid packet type: MQTT::Packet"
|
1476
1898
|
)
|
@@ -1481,10 +1903,10 @@ end
|
|
1481
1903
|
describe "Reading in an invalid packet from a socket" do
|
1482
1904
|
context "that has 0 length" do
|
1483
1905
|
it "should throw an exception" do
|
1484
|
-
|
1906
|
+
expect {
|
1485
1907
|
socket = StringIO.new
|
1486
1908
|
MQTT::Packet.read(socket)
|
1487
|
-
}.
|
1909
|
+
}.to raise_error(
|
1488
1910
|
MQTT::ProtocolException,
|
1489
1911
|
"Failed to read byte from socket"
|
1490
1912
|
)
|
@@ -1493,10 +1915,10 @@ describe "Reading in an invalid packet from a socket" do
|
|
1493
1915
|
|
1494
1916
|
context "that has an incomplete packet length header" do
|
1495
1917
|
it "should throw an exception" do
|
1496
|
-
|
1918
|
+
expect {
|
1497
1919
|
socket = StringIO.new("\x30\xFF")
|
1498
1920
|
MQTT::Packet.read(socket)
|
1499
|
-
}.
|
1921
|
+
}.to raise_error(
|
1500
1922
|
MQTT::ProtocolException,
|
1501
1923
|
"Failed to read byte from socket"
|
1502
1924
|
)
|
@@ -1505,10 +1927,10 @@ describe "Reading in an invalid packet from a socket" do
|
|
1505
1927
|
|
1506
1928
|
context "that has the maximum number of bytes in the length header" do
|
1507
1929
|
it "should throw an exception" do
|
1508
|
-
|
1930
|
+
expect {
|
1509
1931
|
socket = StringIO.new("\x30\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF")
|
1510
1932
|
MQTT::Packet.read(socket)
|
1511
|
-
}.
|
1933
|
+
}.to raise_error(
|
1512
1934
|
MQTT::ProtocolException,
|
1513
1935
|
"Failed to parse packet - input buffer (4) is not the same as the body length header (268435455)"
|
1514
1936
|
)
|
@@ -1519,9 +1941,9 @@ end
|
|
1519
1941
|
describe "Parsing an invalid packet" do
|
1520
1942
|
context "that has no length" do
|
1521
1943
|
it "should throw an exception" do
|
1522
|
-
|
1944
|
+
expect {
|
1523
1945
|
MQTT::Packet.parse( "" )
|
1524
|
-
}.
|
1946
|
+
}.to raise_error(
|
1525
1947
|
MQTT::ProtocolException,
|
1526
1948
|
"Invalid packet: less than 2 bytes long"
|
1527
1949
|
)
|
@@ -1530,9 +1952,9 @@ describe "Parsing an invalid packet" do
|
|
1530
1952
|
|
1531
1953
|
context "that has an invalid type identifier" do
|
1532
1954
|
it "should throw an exception" do
|
1533
|
-
|
1955
|
+
expect {
|
1534
1956
|
MQTT::Packet.parse( "\x00\x00" )
|
1535
|
-
}.
|
1957
|
+
}.to raise_error(
|
1536
1958
|
MQTT::ProtocolException,
|
1537
1959
|
"Invalid packet type identifier: 0"
|
1538
1960
|
)
|
@@ -1541,9 +1963,9 @@ describe "Parsing an invalid packet" do
|
|
1541
1963
|
|
1542
1964
|
context "that has an incomplete packet length header" do
|
1543
1965
|
it "should throw an exception" do
|
1544
|
-
|
1966
|
+
expect {
|
1545
1967
|
MQTT::Packet.parse( "\x30\xFF" )
|
1546
|
-
}.
|
1968
|
+
}.to raise_error(
|
1547
1969
|
MQTT::ProtocolException,
|
1548
1970
|
"The packet length header is incomplete"
|
1549
1971
|
)
|
@@ -1552,9 +1974,9 @@ describe "Parsing an invalid packet" do
|
|
1552
1974
|
|
1553
1975
|
context "that has too many bytes in the length field" do
|
1554
1976
|
it "should throw an exception" do
|
1555
|
-
|
1977
|
+
expect {
|
1556
1978
|
MQTT::Packet.parse( "\x30\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" )
|
1557
|
-
}.
|
1979
|
+
}.to raise_error(
|
1558
1980
|
MQTT::ProtocolException,
|
1559
1981
|
'Failed to parse packet - input buffer (4) is not the same as the body length header (268435455)'
|
1560
1982
|
)
|
@@ -1563,9 +1985,9 @@ describe "Parsing an invalid packet" do
|
|
1563
1985
|
|
1564
1986
|
context "that has a bigger buffer than expected" do
|
1565
1987
|
it "should throw an exception" do
|
1566
|
-
|
1988
|
+
expect {
|
1567
1989
|
MQTT::Packet.parse( "\x30\x11\x00\x04testhello big world" )
|
1568
|
-
}.
|
1990
|
+
}.to raise_error(
|
1569
1991
|
MQTT::ProtocolException,
|
1570
1992
|
"Failed to parse packet - input buffer (21) is not the same as the body length header (17)"
|
1571
1993
|
)
|
@@ -1574,9 +1996,9 @@ describe "Parsing an invalid packet" do
|
|
1574
1996
|
|
1575
1997
|
context "that has a smaller buffer than expected" do
|
1576
1998
|
it "should throw an exception" do
|
1577
|
-
|
1999
|
+
expect {
|
1578
2000
|
MQTT::Packet.parse( "\x30\x11\x00\x04testhello" )
|
1579
|
-
}.
|
2001
|
+
}.to raise_error(
|
1580
2002
|
MQTT::ProtocolException,
|
1581
2003
|
"Failed to parse packet - input buffer (11) is not the same as the body length header (17)"
|
1582
2004
|
)
|