mqtt 0.3.1 → 0.4.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.
@@ -0,0 +1,1721 @@
1
+ # encoding: BINARY
2
+ # Encoding is set to binary, so that the binary packets aren't validated as UTF-8
3
+
4
+ $:.unshift(File.dirname(__FILE__))
5
+
6
+ require 'spec_helper'
7
+
8
+ describe MQTT::SN::Packet do
9
+
10
+ describe "when creating a new packet" do
11
+ it "should allow you to set the packet dup flag as a hash parameter" do
12
+ packet = MQTT::SN::Packet.new(:duplicate => true)
13
+ expect(packet.duplicate).to be_truthy
14
+ end
15
+
16
+ it "should allow you to set the packet QoS level as a hash parameter" do
17
+ packet = MQTT::SN::Packet.new(:qos => 2)
18
+ expect(packet.qos).to eq(2)
19
+ end
20
+
21
+ it "should allow you to set the packet retain flag as a hash parameter" do
22
+ packet = MQTT::SN::Packet.new(:retain => true)
23
+ expect(packet.retain).to be_truthy
24
+ end
25
+ end
26
+
27
+ describe "getting the type id on a un-subclassed packet" do
28
+ it "should raise an exception" do
29
+ expect {
30
+ MQTT::SN::Packet.new.type_id
31
+ }.to raise_error(
32
+ RuntimeError,
33
+ "Invalid packet type: MQTT::SN::Packet"
34
+ )
35
+ end
36
+ end
37
+
38
+ describe "Parsing a packet that does not match the packet length" do
39
+ it "should raise an exception" do
40
+ expect {
41
+ packet = MQTT::SN::Packet.parse("\x02\x1834567")
42
+ }.to raise_error(
43
+ MQTT::SN::ProtocolException,
44
+ "Length of packet is not the same as the length header"
45
+ )
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+
52
+ describe MQTT::SN::Packet::Advertise do
53
+ it "should have the right type id" do
54
+ packet = MQTT::SN::Packet::Advertise.new
55
+ expect(packet.type_id).to eq(0x00)
56
+ end
57
+
58
+ describe "when serialising a packet" do
59
+ it "should output the correct bytes" do
60
+ packet = MQTT::SN::Packet::Advertise.new(:gateway_id => 5, :duration => 30)
61
+ expect(packet.to_s).to eq("\x05\x00\x05\x00\x1E")
62
+ end
63
+ end
64
+
65
+ describe "when parsing a ADVERTISE packet" do
66
+ let(:packet) { MQTT::SN::Packet.parse("\x05\x00\x05\x00\x3C") }
67
+
68
+ it "should correctly create the right type of packet object" do
69
+ expect(packet.class).to eq(MQTT::SN::Packet::Advertise)
70
+ end
71
+
72
+ it "should set the gateway id of the packet correctly" do
73
+ expect(packet.gateway_id).to eq(5)
74
+ end
75
+
76
+ it "should set the duration of the packet correctly" do
77
+ expect(packet.duration).to eq(60)
78
+ end
79
+ end
80
+ end
81
+
82
+
83
+ describe MQTT::SN::Packet::Searchgw do
84
+ it "should have the right type id" do
85
+ packet = MQTT::SN::Packet::Searchgw.new
86
+ expect(packet.type_id).to eq(0x01)
87
+ end
88
+
89
+ describe "when serialising a packet" do
90
+ it "should output the correct bytes" do
91
+ packet = MQTT::SN::Packet::Searchgw.new(:radius => 2)
92
+ expect(packet.to_s).to eq("\x03\x01\x02")
93
+ end
94
+ end
95
+
96
+ describe "when parsing a ADVERTISE packet" do
97
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x01\x03") }
98
+
99
+ it "should correctly create the right type of packet object" do
100
+ expect(packet.class).to eq(MQTT::SN::Packet::Searchgw)
101
+ end
102
+
103
+ it "should set the duration of the packet correctly" do
104
+ expect(packet.radius).to eq(3)
105
+ end
106
+ end
107
+ end
108
+
109
+
110
+ describe MQTT::SN::Packet::Gwinfo do
111
+ it "should have the right type id" do
112
+ packet = MQTT::SN::Packet::Gwinfo.new
113
+ expect(packet.type_id).to eq(0x02)
114
+ end
115
+
116
+ describe "when serialising a packet" do
117
+ it "should output the correct bytes when there is no gateway address" do
118
+ packet = MQTT::SN::Packet::Gwinfo.new(:gateway_id => 6)
119
+ expect(packet.to_s).to eq("\x03\x02\x06")
120
+ end
121
+
122
+ it "should output the correct bytes with a gateway address" do
123
+ packet = MQTT::SN::Packet::Gwinfo.new(:gateway_id => 6, :gateway_address => 'ADDR')
124
+ expect(packet.to_s).to eq("\x07\x02\x06ADDR")
125
+ end
126
+ end
127
+
128
+ describe "when parsing a GWINFO packet with no gateway address" do
129
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x02\x06") }
130
+
131
+ it "should correctly create the right type of packet object" do
132
+ expect(packet.class).to eq(MQTT::SN::Packet::Gwinfo)
133
+ end
134
+
135
+ it "should set the Gateway ID of the packet correctly" do
136
+ expect(packet.gateway_id).to eq(6)
137
+ end
138
+
139
+ it "should set the Gateway ID of the packet correctly" do
140
+ expect(packet.gateway_address).to be_nil
141
+ end
142
+ end
143
+
144
+ describe "when parsing a GWINFO packet with a gateway address" do
145
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x02\x06ADDR") }
146
+
147
+ it "should correctly create the right type of packet object" do
148
+ expect(packet.class).to eq(MQTT::SN::Packet::Gwinfo)
149
+ end
150
+
151
+ it "should set the Gateway ID of the packet correctly" do
152
+ expect(packet.gateway_id).to eq(6)
153
+ end
154
+
155
+ it "should set the Gateway ID of the packet correctly" do
156
+ expect(packet.gateway_address).to eq('ADDR')
157
+ end
158
+ end
159
+ end
160
+
161
+
162
+ describe MQTT::SN::Packet::Connect do
163
+ it "should have the right type id" do
164
+ packet = MQTT::SN::Packet::Connect.new
165
+ expect(packet.type_id).to eq(0x04)
166
+ end
167
+
168
+ describe "when serialising a packet" do
169
+ it "should output the correct bytes for a packet with no flags" do
170
+ packet = MQTT::SN::Packet::Connect.new(
171
+ :client_id => 'mqtt-sn-client-pub'
172
+ )
173
+ expect(packet.to_s).to eq("\x18\x04\x04\x01\x00\x0fmqtt-sn-client-pub")
174
+ end
175
+
176
+ it "should output the correct bytes for a packet with clean session turned off" do
177
+ packet = MQTT::SN::Packet::Connect.new(
178
+ :client_id => 'myclient',
179
+ :clean_session => false
180
+ )
181
+ expect(packet.to_s).to eq("\016\004\000\001\000\017myclient")
182
+ end
183
+
184
+ it "should raise an exception when there is no client identifier" do
185
+ expect {
186
+ MQTT::SN::Packet::Connect.new.to_s
187
+ }.to raise_error(
188
+ 'Invalid client identifier when serialising packet'
189
+ )
190
+ end
191
+
192
+ it "should output the correct bytes for a packet with a will request" do
193
+ packet = MQTT::SN::Packet::Connect.new(
194
+ :client_id => 'myclient',
195
+ :request_will => true,
196
+ :clean_session => true
197
+ )
198
+ expect(packet.to_s).to eq("\016\004\014\001\000\017myclient")
199
+ end
200
+
201
+ it "should output the correct bytes for with a custom keep alive" do
202
+ packet = MQTT::SN::Packet::Connect.new(
203
+ :client_id => 'myclient',
204
+ :request_will => true,
205
+ :clean_session => true,
206
+ :keep_alive => 30
207
+ )
208
+ expect(packet.to_s).to eq("\016\004\014\001\000\036myclient")
209
+ end
210
+ end
211
+
212
+ describe "when parsing a simple Connect packet" do
213
+ let(:packet) { MQTT::SN::Packet.parse("\x18\x04\x04\x01\x00\x00mqtt-sn-client-pub") }
214
+
215
+ it "should correctly create the right type of packet object" do
216
+ expect(packet.class).to eq(MQTT::SN::Packet::Connect)
217
+ end
218
+
219
+ it "should not have the request will flag set" do
220
+ expect(packet.request_will).to be_falsy
221
+ end
222
+
223
+ it "shoul have the clean session flag set" do
224
+ expect(packet.clean_session).to be_truthy
225
+ end
226
+
227
+ it "should set the Keep Alive timer of the packet correctly" do
228
+ expect(packet.keep_alive).to eq(0)
229
+ end
230
+
231
+ it "should set the Client Identifier of the packet correctly" do
232
+ expect(packet.client_id).to eq('mqtt-sn-client-pub')
233
+ end
234
+ end
235
+
236
+ describe "when parsing a Connect packet with the clean session flag set" do
237
+ let(:packet) { MQTT::SN::Packet.parse("\016\004\004\001\000\017myclient") }
238
+
239
+ it "should set the clean session flag" do
240
+ expect(packet.clean_session).to be_truthy
241
+ end
242
+ end
243
+
244
+ describe "when parsing a Connect packet with the will request flag set" do
245
+ let(:packet) { MQTT::SN::Packet.parse("\016\004\014\001\000\017myclient") }
246
+
247
+ it "should correctly create the right type of packet object" do
248
+ expect(packet.class).to eq(MQTT::SN::Packet::Connect)
249
+ end
250
+ it "should set the Client Identifier of the packet correctly" do
251
+ expect(packet.client_id).to eq('myclient')
252
+ end
253
+
254
+ it "should set the clean session flag should be set" do
255
+ expect(packet.clean_session).to be_truthy
256
+ end
257
+
258
+ it "should set the Will retain flag should be false" do
259
+ expect(packet.request_will).to be_truthy
260
+ end
261
+ end
262
+
263
+ context "that has an invalid type identifier" do
264
+ it "should raise an exception" do
265
+ expect {
266
+ MQTT::SN::Packet.parse("\x02\xFF")
267
+ }.to raise_error(
268
+ MQTT::SN::ProtocolException,
269
+ "Invalid packet type identifier: 255"
270
+ )
271
+ end
272
+ end
273
+
274
+ describe "when parsing a Connect packet an unsupport protocol ID" do
275
+ it "should raise an exception" do
276
+ expect {
277
+ packet = MQTT::SN::Packet.parse(
278
+ "\016\004\014\005\000\017myclient"
279
+ )
280
+ }.to raise_error(
281
+ MQTT::SN::ProtocolException,
282
+ "Unsupported protocol ID number: 5"
283
+ )
284
+ end
285
+ end
286
+ end
287
+
288
+ describe MQTT::SN::Packet::Connack do
289
+ it "should have the right type id" do
290
+ packet = MQTT::SN::Packet::Connack.new
291
+ expect(packet.type_id).to eq(0x05)
292
+ end
293
+
294
+ describe "when serialising a packet" do
295
+ it "should output the correct bytes for a sucessful connection acknowledgement packet" do
296
+ packet = MQTT::SN::Packet::Connack.new(:return_code => 0x00)
297
+ expect(packet.to_s).to eq("\x03\x05\x00")
298
+ end
299
+
300
+ it "should raise an exception if the return code isn't an Integer" do
301
+ packet = MQTT::SN::Packet::Connack.new(:return_code => true)
302
+ expect { packet.to_s }.to raise_error("return_code must be an Integer")
303
+ end
304
+ end
305
+
306
+ describe "when parsing a successful Connection Accepted packet" do
307
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x05\x00") }
308
+
309
+ it "should correctly create the right type of packet object" do
310
+ expect(packet.class).to eq(MQTT::SN::Packet::Connack)
311
+ end
312
+
313
+ it "should set the return code of the packet correctly" do
314
+ expect(packet.return_code).to eq(0x00)
315
+ end
316
+
317
+ it "should set the return message of the packet correctly" do
318
+ expect(packet.return_msg).to match(/accepted/i)
319
+ end
320
+ end
321
+
322
+ describe "when parsing a congestion packet" do
323
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x05\x01") }
324
+
325
+ it "should correctly create the right type of packet object" do
326
+ expect(packet.class).to eq(MQTT::SN::Packet::Connack)
327
+ end
328
+
329
+ it "should set the return code of the packet correctly" do
330
+ expect(packet.return_code).to eq(0x01)
331
+ end
332
+
333
+ it "should set the return message of the packet correctly" do
334
+ expect(packet.return_msg).to match(/rejected: congestion/i)
335
+ end
336
+ end
337
+
338
+ describe "when parsing a invalid topic ID packet" do
339
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x05\x02") }
340
+
341
+ it "should correctly create the right type of packet object" do
342
+ expect(packet.class).to eq(MQTT::SN::Packet::Connack)
343
+ end
344
+
345
+ it "should set the return code of the packet correctly" do
346
+ expect(packet.return_code).to eq(0x02)
347
+ end
348
+
349
+ it "should set the return message of the packet correctly" do
350
+ expect(packet.return_msg).to match(/rejected: invalid topic ID/i)
351
+ end
352
+ end
353
+
354
+ describe "when parsing a 'not supported' packet" do
355
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x05\x03") }
356
+
357
+ it "should correctly create the right type of packet object" do
358
+ expect(packet.class).to eq(MQTT::SN::Packet::Connack)
359
+ end
360
+
361
+ it "should set the return code of the packet correctly" do
362
+ expect(packet.return_code).to eq(0x03)
363
+ end
364
+
365
+ it "should set the return message of the packet correctly" do
366
+ expect(packet.return_msg).to match(/not supported/i)
367
+ end
368
+ end
369
+
370
+ describe "when parsing an unknown connection refused packet" do
371
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x05\x10") }
372
+
373
+ it "should correctly create the right type of packet object" do
374
+ expect(packet.class).to eq(MQTT::SN::Packet::Connack)
375
+ end
376
+
377
+ it "should set the return code of the packet correctly" do
378
+ expect(packet.return_code).to eq(0x10)
379
+ end
380
+
381
+ it "should set the return message of the packet correctly" do
382
+ expect(packet.return_msg).to match(/rejected/i)
383
+ end
384
+ end
385
+ end
386
+
387
+ describe MQTT::SN::Packet::Willtopicreq do
388
+ it "should have the right type id" do
389
+ packet = MQTT::SN::Packet::Willtopicreq.new
390
+ expect(packet.type_id).to eq(0x06)
391
+ end
392
+
393
+ describe "when serialising a packet" do
394
+ it "should output the correct bytes" do
395
+ packet = MQTT::SN::Packet::Willtopicreq.new
396
+ expect(packet.to_s).to eq("\x02\x06")
397
+ end
398
+ end
399
+
400
+ describe "when parsing a Willtopicreq packet" do
401
+ let(:packet) { MQTT::SN::Packet.parse("\x02\x06") }
402
+
403
+ it "should correctly create the right type of packet object" do
404
+ expect(packet.class).to eq(MQTT::SN::Packet::Willtopicreq)
405
+ end
406
+ end
407
+ end
408
+
409
+ describe MQTT::SN::Packet::Willtopic do
410
+ it "should have the right type id" do
411
+ packet = MQTT::SN::Packet::Willtopic.new
412
+ expect(packet.type_id).to eq(0x07)
413
+ end
414
+
415
+ describe "when serialising a packet" do
416
+ it "should output the correct bytes for a Willtopic packet" do
417
+ packet = MQTT::SN::Packet::Willtopic.new(:topic_name => 'test', :qos => 0)
418
+ expect(packet.to_s).to eq("\x07\x07\x00test")
419
+ end
420
+
421
+ it "should output the correct bytes for a Willtopic packet with QoS 1" do
422
+ packet = MQTT::SN::Packet::Willtopic.new(:topic_name => 'test', :qos => 1)
423
+ expect(packet.to_s).to eq("\x07\x07\x20test")
424
+ end
425
+
426
+ it "should output the correct bytes for a Willtopic packet with no topic name" do
427
+ packet = MQTT::SN::Packet::Willtopic.new(:topic_name => nil)
428
+ expect(packet.to_s).to eq("\x02\x07")
429
+ end
430
+
431
+ it "should output the correct bytes for a Willtopic packet with an empty topic name" do
432
+ packet = MQTT::SN::Packet::Willtopic.new(:topic_name => '')
433
+ expect(packet.to_s).to eq("\x02\x07")
434
+ end
435
+ end
436
+
437
+ describe "when parsing a Willtopic packet" do
438
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x07\x40test") }
439
+
440
+ it "should correctly create the right type of packet object" do
441
+ expect(packet.class).to eq(MQTT::SN::Packet::Willtopic)
442
+ end
443
+
444
+ it "should set the topic name of the packet correctly" do
445
+ expect(packet.topic_name).to eq('test')
446
+ end
447
+
448
+ it "should set the QoS value of the packet correctly" do
449
+ expect(packet.qos).to eq(2)
450
+ end
451
+
452
+ it "should set the retain flag of the packet correctly" do
453
+ expect(packet.retain).to be_falsy
454
+ end
455
+ end
456
+
457
+ describe "when parsing a Willtopic packet with no topic name" do
458
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x07\x00") }
459
+
460
+ it "should correctly create the right type of packet object" do
461
+ expect(packet.class).to eq(MQTT::SN::Packet::Willtopic)
462
+ end
463
+
464
+ it "should set the topic name of the packet correctly" do
465
+ expect(packet.topic_name).to be_nil
466
+ end
467
+ end
468
+ end
469
+
470
+ describe MQTT::SN::Packet::Willmsgreq do
471
+ it "should have the right type id" do
472
+ packet = MQTT::SN::Packet::Willmsgreq.new
473
+ expect(packet.type_id).to eq(0x08)
474
+ end
475
+
476
+ describe "when serialising a packet" do
477
+ it "should output the correct bytes" do
478
+ packet = MQTT::SN::Packet::Willmsgreq.new
479
+ expect(packet.to_s).to eq("\x02\x08")
480
+ end
481
+ end
482
+
483
+ describe "when parsing a Willmsgreq packet" do
484
+ let(:packet) { MQTT::SN::Packet.parse("\x02\x08") }
485
+
486
+ it "should correctly create the right type of packet object" do
487
+ expect(packet.class).to eq(MQTT::SN::Packet::Willmsgreq)
488
+ end
489
+ end
490
+ end
491
+
492
+ describe MQTT::SN::Packet::Willmsg do
493
+ it "should have the right type id" do
494
+ packet = MQTT::SN::Packet::Willmsg.new
495
+ expect(packet.type_id).to eq(0x09)
496
+ end
497
+
498
+ describe "when serialising a packet" do
499
+ it "should output the correct bytes for a Willmsg packet" do
500
+ packet = MQTT::SN::Packet::Willmsg.new(:data => 'msg')
501
+ expect(packet.to_s).to eq("\x05\x09msg")
502
+ end
503
+ end
504
+
505
+ describe "when parsing a Willmsg packet" do
506
+ let(:packet) { MQTT::SN::Packet.parse("\x0D\x09willmessage") }
507
+
508
+ it "should correctly create the right type of packet object" do
509
+ expect(packet.class).to eq(MQTT::SN::Packet::Willmsg)
510
+ end
511
+
512
+ it "should set the topic id of the packet correctly" do
513
+ expect(packet.data).to eq('willmessage')
514
+ end
515
+ end
516
+ end
517
+
518
+ describe MQTT::SN::Packet::Register do
519
+ it "should have the right type id" do
520
+ packet = MQTT::SN::Packet::Register.new
521
+ expect(packet.type_id).to eq(0x0A)
522
+ end
523
+
524
+ describe "when serialising a packet" do
525
+ it "should output the correct bytes" do
526
+ packet = MQTT::SN::Packet::Register.new(
527
+ :id => 0x01,
528
+ :topic_id => 0x01,
529
+ :topic_name => 'test'
530
+ )
531
+ expect(packet.to_s).to eq("\x0A\x0A\x00\x01\x00\x01test")
532
+ end
533
+
534
+ it "should raise an exception if the Packet Id isn't an Integer" do
535
+ packet = MQTT::SN::Packet::Register.new(:id => "0x45")
536
+ expect { packet.to_s }.to raise_error("id must be an Integer")
537
+ end
538
+
539
+ it "should raise an exception if the Topic Id isn't an Integer" do
540
+ packet = MQTT::SN::Packet::Register.new(:topic_id => "0x45")
541
+ expect { packet.to_s }.to raise_error("topic_id must be an Integer")
542
+ end
543
+ end
544
+
545
+ describe "when parsing a Register packet" do
546
+ let(:packet) { MQTT::SN::Packet.parse("\x0A\x0A\x00\x01\x00\x01test") }
547
+
548
+ it "should correctly create the right type of packet object" do
549
+ expect(packet.class).to eq(MQTT::SN::Packet::Register)
550
+ end
551
+
552
+ it "should set the topic id type of the packet correctly" do
553
+ expect(packet.topic_id_type).to eq(:normal)
554
+ end
555
+
556
+ it "should set the topic id of the packet correctly" do
557
+ expect(packet.topic_id).to eq(0x01)
558
+ end
559
+
560
+ it "should set the message id of the packet correctly" do
561
+ expect(packet.id).to eq(0x01)
562
+ end
563
+
564
+ it "should set the topic name of the packet correctly" do
565
+ expect(packet.topic_name).to eq('test')
566
+ end
567
+ end
568
+ end
569
+
570
+
571
+ describe MQTT::SN::Packet::Regack do
572
+ it "should have the right type id" do
573
+ packet = MQTT::SN::Packet::Regack.new
574
+ expect(packet.type_id).to eq(0x0B)
575
+ end
576
+
577
+ describe "when serialising a packet" do
578
+ it "should output the correct bytes" do
579
+ packet = MQTT::SN::Packet::Regack.new(
580
+ :id => 0x02,
581
+ :topic_id => 0x01,
582
+ :return_code => 0x03
583
+ )
584
+ expect(packet.to_s).to eq("\x07\x0B\x00\x01\x00\x02\x03")
585
+ end
586
+
587
+ it "should raise an exception if the Packet Id isn't an Integer" do
588
+ packet = MQTT::SN::Packet::Regack.new(:id => "0x45")
589
+ expect { packet.to_s }.to raise_error("id must be an Integer")
590
+ end
591
+
592
+ it "should raise an exception if the Topic Id isn't an Integer" do
593
+ packet = MQTT::SN::Packet::Regack.new(:topic_id => "0x45")
594
+ expect { packet.to_s }.to raise_error("topic_id must be an Integer")
595
+ end
596
+ end
597
+
598
+ describe "when parsing a REGACK packet" do
599
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x0B\x00\x01\x00\x02\x03") }
600
+
601
+ it "should correctly create the right type of packet object" do
602
+ expect(packet.class).to eq(MQTT::SN::Packet::Regack)
603
+ end
604
+
605
+ it "should set the topic id type of the packet correctly" do
606
+ expect(packet.topic_id_type).to eq(:normal)
607
+ end
608
+
609
+ it "should set the topic id of the packet correctly" do
610
+ expect(packet.topic_id).to eq(0x01)
611
+ end
612
+
613
+ it "should set the message id of the packet correctly" do
614
+ expect(packet.id).to eq(0x02)
615
+ end
616
+
617
+ it "should set the topic name of the packet correctly" do
618
+ expect(packet.return_code).to eq(0x03)
619
+ end
620
+ end
621
+ end
622
+
623
+
624
+ describe MQTT::SN::Packet::Publish do
625
+ it "should have the right type id" do
626
+ packet = MQTT::SN::Packet::Publish.new
627
+ expect(packet.type_id).to eq(0x0C)
628
+ end
629
+
630
+ describe "when serialising a packet with a normal topic id type" do
631
+ it "should output the correct bytes for a publish packet" do
632
+ packet = MQTT::SN::Packet::Publish.new(
633
+ :topic_id => 0x01,
634
+ :topic_id_type => :normal,
635
+ :data => "Hello World"
636
+ )
637
+ expect(packet.to_s).to eq("\x12\x0C\x00\x00\x01\x00\x00Hello World")
638
+ end
639
+
640
+ it "should raise an exception if the Packet Id isn't an Integer" do
641
+ packet = MQTT::SN::Packet::Publish.new(:id => "0x45")
642
+ expect { packet.to_s }.to raise_error("id must be an Integer")
643
+ end
644
+ end
645
+
646
+ describe "when serialising a packet with a short topic id type" do
647
+ it "should output the correct bytes for a publish packet of QoS -1" do
648
+ packet = MQTT::SN::Packet::Publish.new(
649
+ :qos => -1,
650
+ :topic_id => 'tt',
651
+ :topic_id_type => :short,
652
+ :data => "Hello World"
653
+ )
654
+ expect(packet.to_s).to eq("\x12\x0C\x62tt\x00\x00Hello World")
655
+ end
656
+
657
+ it "should output the correct bytes for a publish packet of QoS 0" do
658
+ packet = MQTT::SN::Packet::Publish.new(
659
+ :qos => 0,
660
+ :topic_id => 'tt',
661
+ :topic_id_type => :short,
662
+ :data => "Hello World"
663
+ )
664
+ expect(packet.to_s).to eq("\x12\x0C\x02tt\x00\x00Hello World")
665
+ end
666
+
667
+ it "should output the correct bytes for a publish packet of QoS 1" do
668
+ packet = MQTT::SN::Packet::Publish.new(
669
+ :qos => 1,
670
+ :topic_id => 'tt',
671
+ :topic_id_type => :short,
672
+ :data => "Hello World"
673
+ )
674
+ expect(packet.to_s).to eq("\x12\x0C\x22tt\x00\x00Hello World")
675
+ end
676
+
677
+ it "should output the correct bytes for a publish packet of QoS 2" do
678
+ packet = MQTT::SN::Packet::Publish.new(
679
+ :qos => 2,
680
+ :topic_id => 'tt',
681
+ :topic_id_type => :short,
682
+ :data => "Hello World"
683
+ )
684
+ expect(packet.to_s).to eq("\x12\x0C\x42tt\x00\x00Hello World")
685
+ end
686
+ end
687
+
688
+ describe "when serialising a packet with a pre-defined topic id type" do
689
+ it "should output the correct bytes for a publish packet" do
690
+ packet = MQTT::SN::Packet::Publish.new(
691
+ :topic_id => 0x00EE,
692
+ :topic_id_type => :predefined,
693
+ :data => "Hello World"
694
+ )
695
+ expect(packet.to_s).to eq("\x12\x0C\x01\x00\xEE\x00\x00Hello World")
696
+ end
697
+ end
698
+
699
+ describe "when serialising packet larger than 256 bytes" do
700
+ let(:packet) {
701
+ MQTT::SN::Packet::Publish.new(
702
+ :topic_id => 0x10,
703
+ :topic_id_type => :normal,
704
+ :data => "Hello World" * 100
705
+ )
706
+ }
707
+
708
+ it "should have the first three bytes set to 0x01, 0x04, 0x55" do
709
+ expect(packet.to_s.unpack('CCC')).to eq([0x01,0x04,0x55])
710
+ end
711
+
712
+ it "should have a total length of 0x0455 (1109) bytes" do
713
+ expect(packet.to_s.length).to eq(0x0455)
714
+ end
715
+ end
716
+
717
+ describe "when serialising an excessively large packet" do
718
+ it "should raise an exception" do
719
+ expect {
720
+ MQTT::SN::Packet::Publish.new(
721
+ :topic_id => 0x01,
722
+ :topic_id_type => :normal,
723
+ :data => "Hello World" * 6553
724
+ ).to_s
725
+ }.to raise_error(
726
+ RuntimeError,
727
+ "MQTT-SN Packet is too big, maximum packet body size is 65531"
728
+ )
729
+ end
730
+ end
731
+
732
+ describe "when parsing a Publish packet with a normal topic id" do
733
+ let(:packet) { MQTT::SN::Packet.parse("\x12\x0C\x00\x00\x01\x00\x00Hello World") }
734
+
735
+ it "should correctly create the right type of packet object" do
736
+ expect(packet.class).to eq(MQTT::SN::Packet::Publish)
737
+ end
738
+
739
+ it "should set the QoS value of the packet correctly" do
740
+ expect(packet.qos).to be === 0
741
+ end
742
+
743
+ it "should set the duplicate flag of the packet correctly" do
744
+ expect(packet.duplicate).to be === false
745
+ end
746
+
747
+ it "should set the retain flag of the packet correctly" do
748
+ expect(packet.retain).to be === false
749
+ end
750
+
751
+ it "should set the topic id of the packet correctly" do
752
+ expect(packet.topic_id_type).to be === :normal
753
+ end
754
+
755
+ it "should set the topic id of the packet correctly" do
756
+ expect(packet.topic_id).to be === 0x01
757
+ end
758
+
759
+ it "should set the message id of the packet correctly" do
760
+ expect(packet.id).to be === 0x0000
761
+ end
762
+
763
+ it "should set the topic name of the packet correctly" do
764
+ expect(packet.data).to eq("Hello World")
765
+ end
766
+ end
767
+
768
+ describe "when parsing a Publish packet with a short topic id" do
769
+ let(:packet) { MQTT::SN::Packet.parse("\x12\x0C\x02tt\x00\x00Hello World") }
770
+
771
+ it "should correctly create the right type of packet object" do
772
+ expect(packet.class).to eq(MQTT::SN::Packet::Publish)
773
+ end
774
+
775
+ it "should set the QoS value of the packet correctly" do
776
+ expect(packet.qos).to be === 0
777
+ end
778
+
779
+ it "should set the duplicate flag of the packet correctly" do
780
+ expect(packet.duplicate).to be === false
781
+ end
782
+
783
+ it "should set the retain flag of the packet correctly" do
784
+ expect(packet.retain).to be === false
785
+ end
786
+
787
+ it "should set the topic id type of the packet correctly" do
788
+ expect(packet.topic_id_type).to be === :short
789
+ end
790
+
791
+ it "should set the topic id of the packet correctly" do
792
+ expect(packet.topic_id).to be === 'tt'
793
+ end
794
+
795
+ it "should set the message id of the packet correctly" do
796
+ expect(packet.id).to be === 0x0000
797
+ end
798
+
799
+ it "should set the topic name of the packet correctly" do
800
+ expect(packet.data).to eq("Hello World")
801
+ end
802
+ end
803
+
804
+ describe "when parsing a Publish packet with a short topic id and QoS -1" do
805
+ let(:packet) { MQTT::SN::Packet.parse("\x12\x0C\x62tt\x00\x00Hello World") }
806
+
807
+ it "should correctly create the right type of packet object" do
808
+ expect(packet.class).to eq(MQTT::SN::Packet::Publish)
809
+ end
810
+
811
+ it "should set the QoS value of the packet correctly" do
812
+ expect(packet.qos).to be === -1
813
+ end
814
+
815
+ it "should set the duplicate flag of the packet correctly" do
816
+ expect(packet.duplicate).to be === false
817
+ end
818
+
819
+ it "should set the retain flag of the packet correctly" do
820
+ expect(packet.retain).to be === false
821
+ end
822
+
823
+ it "should set the topic id type of the packet correctly" do
824
+ expect(packet.topic_id_type).to be === :short
825
+ end
826
+
827
+ it "should set the topic id of the packet correctly" do
828
+ expect(packet.topic_id).to be === 'tt'
829
+ end
830
+
831
+ it "should set the message id of the packet correctly" do
832
+ expect(packet.id).to be === 0x0000
833
+ end
834
+
835
+ it "should set the topic name of the packet correctly" do
836
+ expect(packet.data).to eq("Hello World")
837
+ end
838
+ end
839
+
840
+ describe "when parsing a Publish packet with a predefined topic id type" do
841
+ let(:packet) { MQTT::SN::Packet.parse("\x12\x0C\x01\x00\xEE\x00\x00Hello World") }
842
+
843
+ it "should correctly create the right type of packet object" do
844
+ expect(packet.class).to eq(MQTT::SN::Packet::Publish)
845
+ end
846
+
847
+ it "should set the topic id type of the packet correctly" do
848
+ expect(packet.topic_id_type).to eql(:predefined)
849
+ end
850
+
851
+ it "should set the topic id of the packet correctly" do
852
+ expect(packet.topic_id).to eq(0xEE)
853
+ end
854
+ end
855
+
856
+ describe "when parsing a Publish packet with a invalid topic id type" do
857
+ let(:packet) { MQTT::SN::Packet.parse("\x12\x0C\x03\x00\x10\x55\xCCHello World") }
858
+
859
+ it "should correctly create the right type of packet object" do
860
+ expect(packet.class).to eq(MQTT::SN::Packet::Publish)
861
+ end
862
+
863
+ it "should set the QoS value of the packet correctly" do
864
+ expect(packet.qos).to be === 0
865
+ end
866
+
867
+ it "should set the duplicate flag of the packet correctly" do
868
+ expect(packet.duplicate).to be === false
869
+ end
870
+
871
+ it "should set the retain flag of the packet correctly" do
872
+ expect(packet.retain).to be === false
873
+ end
874
+
875
+ it "should set the topic id type of the packet correctly" do
876
+ expect(packet.topic_id_type).to be_nil
877
+ end
878
+
879
+ it "should set the topic id of the packet correctly" do
880
+ expect(packet.topic_id).to eq(0x10)
881
+ end
882
+
883
+ it "should set the message id of the packet correctly" do
884
+ expect(packet.id).to be === 0x55CC
885
+ end
886
+
887
+ it "should set the topic name of the packet correctly" do
888
+ expect(packet.data).to eq("Hello World")
889
+ end
890
+ end
891
+
892
+ describe "when parsing a Publish packet longer than 256 bytes" do
893
+ let(:packet) { MQTT::SN::Packet.parse("\x01\x04\x55\x0C\x62tt\x00\x00" + ("Hello World" * 100)) }
894
+
895
+ it "should correctly create the right type of packet object" do
896
+ expect(packet.class).to eq(MQTT::SN::Packet::Publish)
897
+ end
898
+
899
+ it "should set the QoS value of the packet correctly" do
900
+ expect(packet.qos).to be === -1
901
+ end
902
+
903
+ it "should set the duplicate flag of the packet correctly" do
904
+ expect(packet.duplicate).to be === false
905
+ end
906
+
907
+ it "should set the retain flag of the packet correctly" do
908
+ expect(packet.retain).to be === false
909
+ end
910
+
911
+ it "should set the topic id type of the packet correctly" do
912
+ expect(packet.topic_id_type).to be === :short
913
+ end
914
+
915
+ it "should set the topic id of the packet correctly" do
916
+ expect(packet.topic_id).to be === 'tt'
917
+ end
918
+
919
+ it "should set the message id of the packet correctly" do
920
+ expect(packet.id).to be === 0x0000
921
+ end
922
+
923
+ it "should set the topic name of the packet correctly" do
924
+ expect(packet.data).to eq("Hello World" * 100)
925
+ end
926
+ end
927
+ end
928
+
929
+ describe MQTT::SN::Packet::Puback do
930
+ it "should have the right type id" do
931
+ packet = MQTT::SN::Packet::Puback.new
932
+ expect(packet.type_id).to eq(0x0D)
933
+ end
934
+
935
+ describe "when serialising a packet" do
936
+ it "should output the correct bytes" do
937
+ packet = MQTT::SN::Packet::Puback.new(:id => 0x02, :topic_id => 0x03, :return_code => 0x01)
938
+ expect(packet.to_s).to eq("\x07\x0D\x00\x03\x00\x02\x01")
939
+ end
940
+
941
+ it "should raise an exception if the Packet Id isn't an Integer" do
942
+ packet = MQTT::SN::Packet::Puback.new(:id => "0x45")
943
+ expect { packet.to_s }.to raise_error("id must be an Integer")
944
+ end
945
+
946
+ it "should raise an exception if the Topic Id isn't an Integer" do
947
+ packet = MQTT::SN::Packet::Puback.new(:topic_id => "0x45")
948
+ expect { packet.to_s }.to raise_error("topic_id must be an Integer")
949
+ end
950
+ end
951
+
952
+ describe "when parsing a PUBACK packet" do
953
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x0D\x00\x01\x00\x02\x03") }
954
+
955
+ it "should correctly create the right type of packet object" do
956
+ expect(packet.class).to eq(MQTT::SN::Packet::Puback)
957
+ end
958
+
959
+ it "should set the topic id of the packet correctly" do
960
+ expect(packet.topic_id).to eq(0x01)
961
+ end
962
+
963
+ it "should set the message id of the packet correctly" do
964
+ expect(packet.id).to eq(0x02)
965
+ end
966
+
967
+ it "should set the return code of the packet correctly" do
968
+ expect(packet.return_code).to eq(0x03)
969
+ end
970
+ end
971
+ end
972
+
973
+ describe MQTT::SN::Packet::Pubcomp do
974
+ it "should have the right type id" do
975
+ packet = MQTT::SN::Packet::Pubcomp.new
976
+ expect(packet.type_id).to eq(0x0E)
977
+ end
978
+
979
+ describe "when serialising a packet" do
980
+ it "should output the correct bytes" do
981
+ packet = MQTT::SN::Packet::Pubcomp.new(:id => 0x02)
982
+ expect(packet.to_s).to eq("\x04\x0E\x00\x02")
983
+ end
984
+
985
+ it "should raise an exception if the Packet Id isn't an Integer" do
986
+ packet = MQTT::SN::Packet::Pubcomp.new(:id => "0x45")
987
+ expect { packet.to_s }.to raise_error("id must be an Integer")
988
+ end
989
+ end
990
+
991
+ describe "when parsing a PUBCOMP packet" do
992
+ let(:packet) { MQTT::SN::Packet.parse("\x04\x0E\x00\x02") }
993
+
994
+ it "should correctly create the right type of packet object" do
995
+ expect(packet.class).to eq(MQTT::SN::Packet::Pubcomp)
996
+ end
997
+
998
+ it "should set the message id of the packet correctly" do
999
+ expect(packet.id).to eq(0x02)
1000
+ end
1001
+ end
1002
+ end
1003
+
1004
+ describe MQTT::SN::Packet::Pubrec do
1005
+ it "should have the right type id" do
1006
+ packet = MQTT::SN::Packet::Pubrec.new
1007
+ expect(packet.type_id).to eq(0x0F)
1008
+ end
1009
+
1010
+ describe "when serialising a packet" do
1011
+ it "should output the correct bytes" do
1012
+ packet = MQTT::SN::Packet::Pubrec.new(:id => 0x02)
1013
+ expect(packet.to_s).to eq("\x04\x0F\x00\x02")
1014
+ end
1015
+
1016
+ it "should raise an exception if the Packet Id isn't an Integer" do
1017
+ packet = MQTT::SN::Packet::Pubrec.new(:id => "0x45")
1018
+ expect { packet.to_s }.to raise_error("id must be an Integer")
1019
+ end
1020
+ end
1021
+
1022
+ describe "when parsing a PUBREC packet" do
1023
+ let(:packet) { MQTT::SN::Packet.parse("\x04\x0F\x00\x02") }
1024
+
1025
+ it "should correctly create the right type of packet object" do
1026
+ expect(packet.class).to eq(MQTT::SN::Packet::Pubrec)
1027
+ end
1028
+
1029
+ it "should set the message id of the packet correctly" do
1030
+ expect(packet.id).to eq(0x02)
1031
+ end
1032
+ end
1033
+ end
1034
+
1035
+
1036
+ describe MQTT::SN::Packet::Pubrel do
1037
+ it "should have the right type id" do
1038
+ packet = MQTT::SN::Packet::Pubrel.new
1039
+ expect(packet.type_id).to eq(0x10)
1040
+ end
1041
+
1042
+ describe "when serialising a packet" do
1043
+ it "should output the correct bytes" do
1044
+ packet = MQTT::SN::Packet::Pubrel.new(:id => 0x02)
1045
+ expect(packet.to_s).to eq("\x04\x10\x00\x02")
1046
+ end
1047
+
1048
+ it "should raise an exception if the Packet Id isn't an Integer" do
1049
+ packet = MQTT::SN::Packet::Pubrel.new(:id => "0x45")
1050
+ expect { packet.to_s }.to raise_error("id must be an Integer")
1051
+ end
1052
+ end
1053
+
1054
+ describe "when parsing a PUBREL packet" do
1055
+ let(:packet) { MQTT::SN::Packet.parse("\x04\x10\x00\x02") }
1056
+
1057
+ it "should correctly create the right type of packet object" do
1058
+ expect(packet.class).to eq(MQTT::SN::Packet::Pubrel)
1059
+ end
1060
+
1061
+ it "should set the message id of the packet correctly" do
1062
+ expect(packet.id).to eq(0x02)
1063
+ end
1064
+ end
1065
+ end
1066
+
1067
+
1068
+ describe MQTT::SN::Packet::Subscribe do
1069
+ it "should have the right type id" do
1070
+ packet = MQTT::SN::Packet::Subscribe.new
1071
+ expect(packet.type_id).to eq(0x12)
1072
+ end
1073
+
1074
+ describe "when serialising a packet" do
1075
+ it "should output the correct bytes for a Subscribe packet with a normal topic name" do
1076
+ packet = MQTT::SN::Packet::Subscribe.new(
1077
+ :duplicate => false,
1078
+ :qos => 0,
1079
+ :id => 0x02,
1080
+ :topic_name => 'test'
1081
+ )
1082
+ expect(packet.to_s).to eq("\x09\x12\x00\x00\x02test")
1083
+ end
1084
+
1085
+ it "should output the correct bytes for a Subscribe packet with a short topic name" do
1086
+ packet = MQTT::SN::Packet::Subscribe.new(
1087
+ :duplicate => false,
1088
+ :qos => 0,
1089
+ :id => 0x04,
1090
+ :topic_id_type => :short,
1091
+ :topic_name => 'TT'
1092
+ )
1093
+ expect(packet.to_s).to eq("\x07\x12\x02\x00\x04TT")
1094
+ end
1095
+
1096
+ it "should output the correct bytes for a Subscribe packet with a short topic id" do
1097
+ packet = MQTT::SN::Packet::Subscribe.new(
1098
+ :duplicate => false,
1099
+ :qos => 0,
1100
+ :id => 0x04,
1101
+ :topic_id_type => :short,
1102
+ :topic_id => 'TT'
1103
+ )
1104
+ expect(packet.to_s).to eq("\x07\x12\x02\x00\x04TT")
1105
+ end
1106
+
1107
+ it "should output the correct bytes for a Subscribe packet with a predefined topic id" do
1108
+ packet = MQTT::SN::Packet::Subscribe.new(
1109
+ :duplicate => false,
1110
+ :qos => 0,
1111
+ :id => 0x05,
1112
+ :topic_id_type => :predefined,
1113
+ :topic_id => 16
1114
+ )
1115
+ expect(packet.to_s).to eq("\x07\x12\x01\x00\x05\x00\x10")
1116
+ end
1117
+
1118
+ it "should raise an exception if the Packet Id isn't an Integer" do
1119
+ packet = MQTT::SN::Packet::Subscribe.new(:id => "0x45")
1120
+ expect { packet.to_s }.to raise_error("id must be an Integer")
1121
+ end
1122
+ end
1123
+
1124
+ describe "when parsing a Subscribe packet with a normal topic id type" do
1125
+ let(:packet) { MQTT::SN::Packet.parse("\x09\x12\x00\x00\x03test") }
1126
+
1127
+ it "should correctly create the right type of packet object" do
1128
+ expect(packet.class).to eq(MQTT::SN::Packet::Subscribe)
1129
+ end
1130
+
1131
+ it "should set the message id of the packet correctly" do
1132
+ expect(packet.id).to eq(0x03)
1133
+ end
1134
+
1135
+ it "should set the QoS value of the packet correctly" do
1136
+ expect(packet.qos).to eq(0)
1137
+ end
1138
+
1139
+ it "should set the duplicate flag of the packet correctly" do
1140
+ expect(packet.duplicate).to eq(false)
1141
+ end
1142
+
1143
+ it "should set the topic id type of the packet correctly" do
1144
+ expect(packet.topic_id_type).to eq(:normal)
1145
+ end
1146
+
1147
+ it "should set the topic name of the packet correctly" do
1148
+ expect(packet.topic_name).to eq('test')
1149
+ end
1150
+ end
1151
+
1152
+ describe "when parsing a Subscribe packet with a short topic id type" do
1153
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x12\x02\x00\x04TT") }
1154
+
1155
+ it "should correctly create the right type of packet object" do
1156
+ expect(packet.class).to eq(MQTT::SN::Packet::Subscribe)
1157
+ end
1158
+
1159
+ it "should set the message id of the packet correctly" do
1160
+ expect(packet.id).to eq(0x04)
1161
+ end
1162
+
1163
+ it "should set the QoS value of the packet correctly" do
1164
+ expect(packet.qos).to eq(0)
1165
+ end
1166
+
1167
+ it "should set the duplicate flag of the packet correctly" do
1168
+ expect(packet.duplicate).to eq(false)
1169
+ end
1170
+
1171
+ it "should set the topic id type of the packet correctly" do
1172
+ expect(packet.topic_id_type).to eq(:short)
1173
+ end
1174
+
1175
+ it "should set the topic id of the packet correctly" do
1176
+ expect(packet.topic_id).to eq('TT')
1177
+ end
1178
+
1179
+ it "should set the topic name of the packet correctly" do
1180
+ expect(packet.topic_name).to eq('TT')
1181
+ end
1182
+ end
1183
+
1184
+ describe "when parsing a Subscribe packet with a predefined topic id type" do
1185
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x12\x01\x00\x05\x00\x10") }
1186
+
1187
+ it "should correctly create the right type of packet object" do
1188
+ expect(packet.class).to eq(MQTT::SN::Packet::Subscribe)
1189
+ end
1190
+
1191
+ it "should set the message id of the packet correctly" do
1192
+ expect(packet.id).to eq(0x05)
1193
+ end
1194
+
1195
+ it "should set the QoS value of the packet correctly" do
1196
+ expect(packet.qos).to eq(0)
1197
+ end
1198
+
1199
+ it "should set the duplicate flag of the packet correctly" do
1200
+ expect(packet.duplicate).to eq(false)
1201
+ end
1202
+
1203
+ it "should set the topic id type of the packet correctly" do
1204
+ expect(packet.topic_id_type).to eq(:predefined)
1205
+ end
1206
+
1207
+ it "should set the topic id of the packet correctly" do
1208
+ expect(packet.topic_id).to eq(16)
1209
+ end
1210
+
1211
+ it "should set the topic name of the packet to nil" do
1212
+ expect(packet.topic_name).to be_nil
1213
+ end
1214
+ end
1215
+ end
1216
+
1217
+
1218
+ describe MQTT::SN::Packet::Suback do
1219
+ it "should have the right type id" do
1220
+ packet = MQTT::SN::Packet::Suback.new
1221
+ expect(packet.type_id).to eq(0x13)
1222
+ end
1223
+
1224
+ describe "when serialising a packet" do
1225
+ it "should output the correct bytes for a normal topic id" do
1226
+ packet = MQTT::SN::Packet::Suback.new(
1227
+ :id => 0x02,
1228
+ :qos => 0,
1229
+ :topic_id => 0x01,
1230
+ :return_code => 0x03
1231
+ )
1232
+ expect(packet.to_s).to eq("\x08\x13\x00\x00\x01\x00\x02\x03")
1233
+ end
1234
+
1235
+ it "should output the correct bytes for a short topic id" do
1236
+ packet = MQTT::SN::Packet::Suback.new(
1237
+ :id => 0x03,
1238
+ :qos => 0,
1239
+ :topic_id => 'tt',
1240
+ :topic_id_type => :short,
1241
+ :return_code => 0x03
1242
+ )
1243
+ expect(packet.to_s).to eq("\x08\x13\x02tt\x00\x03\x03")
1244
+ end
1245
+
1246
+ it "should output the correct bytes for a packet with no topic id" do
1247
+ packet = MQTT::SN::Packet::Suback.new(
1248
+ :id => 0x02,
1249
+ :return_code => 0x02
1250
+ )
1251
+ expect(packet.to_s).to eq("\x08\x13\x00\x00\x00\x00\x02\x02")
1252
+ end
1253
+
1254
+ it "should raise an exception if the Packet Id isn't an Integer" do
1255
+ packet = MQTT::SN::Packet::Suback.new(:id => "0x45")
1256
+ expect { packet.to_s }.to raise_error("id must be an Integer")
1257
+ end
1258
+
1259
+ it "should raise an exception if the Topic Id isn't an Integer" do
1260
+ packet = MQTT::SN::Packet::Suback.new(:topic_id => "0x45", :topic_id_type => :normal)
1261
+ expect { packet.to_s }.to raise_error("topic_id must be an Integer for type normal")
1262
+ end
1263
+
1264
+ it "should raise an exception if the Topic Id isn't a String" do
1265
+ packet = MQTT::SN::Packet::Suback.new(:topic_id => 10, :topic_id_type => :short)
1266
+ expect { packet.to_s }.to raise_error("topic_id must be an String for type short")
1267
+ end
1268
+ end
1269
+
1270
+ describe "when parsing a SUBACK packet" do
1271
+ let(:packet) { MQTT::SN::Packet.parse("\x08\x13\x00\x00\x01\x00\x02\x03") }
1272
+
1273
+ it "should correctly create the right type of packet object" do
1274
+ expect(packet.class).to eq(MQTT::SN::Packet::Suback)
1275
+ end
1276
+
1277
+ it "should set the topic id of the packet correctly" do
1278
+ expect(packet.qos).to eq(0)
1279
+ end
1280
+
1281
+ it "should set the topic id type of the packet correctly" do
1282
+ expect(packet.topic_id_type).to eq(:normal)
1283
+ end
1284
+
1285
+ it "should set the topic id of the packet correctly" do
1286
+ expect(packet.topic_id).to eq(0x01)
1287
+ end
1288
+
1289
+ it "should set the message id of the packet correctly" do
1290
+ expect(packet.id).to eq(0x02)
1291
+ end
1292
+
1293
+ it "should set the topic name of the packet correctly" do
1294
+ expect(packet.return_code).to eq(0x03)
1295
+ end
1296
+ end
1297
+ end
1298
+
1299
+
1300
+ describe MQTT::SN::Packet::Unsubscribe do
1301
+ it "should have the right type id" do
1302
+ packet = MQTT::SN::Packet::Unsubscribe.new
1303
+ expect(packet.type_id).to eq(0x14)
1304
+ end
1305
+
1306
+ describe "when serialising a packet" do
1307
+ it "should output the correct bytes for a Unsubscribe packet with a normal topic name" do
1308
+ packet = MQTT::SN::Packet::Unsubscribe.new(
1309
+ :id => 0x02,
1310
+ :duplicate => false,
1311
+ :qos => 0,
1312
+ :topic_name => 'test'
1313
+ )
1314
+ expect(packet.to_s).to eq("\x09\x14\x00\x00\x02test")
1315
+ end
1316
+
1317
+ it "should output the correct bytes for a Unsubscribe packet with a short topic name" do
1318
+ packet = MQTT::SN::Packet::Unsubscribe.new(
1319
+ :duplicate => false,
1320
+ :qos => 0,
1321
+ :id => 0x04,
1322
+ :topic_id_type => :short,
1323
+ :topic_name => 'TT'
1324
+ )
1325
+ expect(packet.to_s).to eq("\x07\x14\x02\x00\x04TT")
1326
+ end
1327
+
1328
+ it "should output the correct bytes for a Unsubscribe packet with a short topic id" do
1329
+ packet = MQTT::SN::Packet::Unsubscribe.new(
1330
+ :duplicate => false,
1331
+ :qos => 0,
1332
+ :id => 0x04,
1333
+ :topic_id_type => :short,
1334
+ :topic_id => 'TT'
1335
+ )
1336
+ expect(packet.to_s).to eq("\x07\x14\x02\x00\x04TT")
1337
+ end
1338
+
1339
+ it "should output the correct bytes for a Unsubscribe packet with a predefined topic id" do
1340
+ packet = MQTT::SN::Packet::Unsubscribe.new(
1341
+ :duplicate => false,
1342
+ :qos => 0,
1343
+ :id => 0x05,
1344
+ :topic_id_type => :predefined,
1345
+ :topic_id => 16
1346
+ )
1347
+ expect(packet.to_s).to eq("\x07\x14\x01\x00\x05\x00\x10")
1348
+ end
1349
+
1350
+ it "should raise an exception if the Packet Id isn't an Integer" do
1351
+ packet = MQTT::SN::Packet::Unsubscribe.new(:id => "0x45")
1352
+ expect { packet.to_s }.to raise_error("id must be an Integer")
1353
+ end
1354
+ end
1355
+
1356
+ describe "when parsing a Unsubscribe packet with a normal topic id type" do
1357
+ let(:packet) { MQTT::SN::Packet.parse("\x09\x14\x00\x00\x03test") }
1358
+
1359
+ it "should correctly create the right type of packet object" do
1360
+ expect(packet.class).to eq(MQTT::SN::Packet::Unsubscribe)
1361
+ end
1362
+
1363
+ it "should set the message id of the packet correctly" do
1364
+ expect(packet.id).to eq(0x03)
1365
+ end
1366
+
1367
+ it "should set the QoS value of the packet correctly" do
1368
+ expect(packet.qos).to eq(0)
1369
+ end
1370
+
1371
+ it "should set the duplicate flag of the packet correctly" do
1372
+ expect(packet.duplicate).to eq(false)
1373
+ end
1374
+
1375
+ it "should set the topic name of the packet correctly" do
1376
+ expect(packet.topic_name).to eq('test')
1377
+ end
1378
+ end
1379
+
1380
+ describe "when parsing a Subscribe packet with a short topic id type" do
1381
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x14\x02\x00\x04TT") }
1382
+
1383
+ it "should correctly create the right type of packet object" do
1384
+ expect(packet.class).to eq(MQTT::SN::Packet::Unsubscribe)
1385
+ end
1386
+
1387
+ it "should set the message id of the packet correctly" do
1388
+ expect(packet.id).to eq(0x04)
1389
+ end
1390
+
1391
+ it "should set the QoS value of the packet correctly" do
1392
+ expect(packet.qos).to eq(0)
1393
+ end
1394
+
1395
+ it "should set the duplicate flag of the packet correctly" do
1396
+ expect(packet.duplicate).to eq(false)
1397
+ end
1398
+
1399
+ it "should set the topic id type of the packet correctly" do
1400
+ expect(packet.topic_id_type).to eq(:short)
1401
+ end
1402
+
1403
+ it "should set the topic id of the packet correctly" do
1404
+ expect(packet.topic_id).to eq('TT')
1405
+ end
1406
+
1407
+ it "should set the topic name of the packet correctly" do
1408
+ expect(packet.topic_name).to eq('TT')
1409
+ end
1410
+ end
1411
+
1412
+ describe "when parsing a Subscribe packet with a predefined topic id type" do
1413
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x14\x01\x00\x05\x00\x10") }
1414
+
1415
+ it "should correctly create the right type of packet object" do
1416
+ expect(packet.class).to eq(MQTT::SN::Packet::Unsubscribe)
1417
+ end
1418
+
1419
+ it "should set the message id of the packet correctly" do
1420
+ expect(packet.id).to eq(0x05)
1421
+ end
1422
+
1423
+ it "should set the QoS value of the packet correctly" do
1424
+ expect(packet.qos).to eq(0)
1425
+ end
1426
+
1427
+ it "should set the duplicate flag of the packet correctly" do
1428
+ expect(packet.duplicate).to eq(false)
1429
+ end
1430
+
1431
+ it "should set the topic id type of the packet correctly" do
1432
+ expect(packet.topic_id_type).to eq(:predefined)
1433
+ end
1434
+
1435
+ it "should set the topic id of the packet correctly" do
1436
+ expect(packet.topic_id).to eq(16)
1437
+ end
1438
+
1439
+ it "should set the topic name of the packet to nil" do
1440
+ expect(packet.topic_name).to be_nil
1441
+ end
1442
+ end
1443
+ end
1444
+
1445
+
1446
+ describe MQTT::SN::Packet::Unsuback do
1447
+ it "should have the right type id" do
1448
+ packet = MQTT::SN::Packet::Unsuback.new
1449
+ expect(packet.type_id).to eq(0x15)
1450
+ end
1451
+
1452
+ describe "when serialising a packet" do
1453
+ it "should output the correct bytes" do
1454
+ packet = MQTT::SN::Packet::Unsuback.new(:id => 0x02)
1455
+ expect(packet.to_s).to eq("\x04\x15\x00\x02")
1456
+ end
1457
+
1458
+ it "should raise an exception if the Packet Id isn't an Integer" do
1459
+ packet = MQTT::SN::Packet::Unsuback.new(:id => "0x45")
1460
+ expect { packet.to_s }.to raise_error("id must be an Integer")
1461
+ end
1462
+ end
1463
+
1464
+ describe "when parsing a SUBACK packet" do
1465
+ let(:packet) { MQTT::SN::Packet.parse("\x04\x15\x00\x02") }
1466
+
1467
+ it "should correctly create the right type of packet object" do
1468
+ expect(packet.class).to eq(MQTT::SN::Packet::Unsuback)
1469
+ end
1470
+
1471
+ it "should set the message id of the packet correctly" do
1472
+ expect(packet.id).to eq(0x02)
1473
+ end
1474
+ end
1475
+ end
1476
+
1477
+
1478
+ describe MQTT::SN::Packet::Pingreq do
1479
+ it "should have the right type id" do
1480
+ packet = MQTT::SN::Packet::Pingreq.new
1481
+ expect(packet.type_id).to eq(0x16)
1482
+ end
1483
+
1484
+ describe "when serialising a packet" do
1485
+ it "should output the correct bytes for a pingreq packet" do
1486
+ packet = MQTT::SN::Packet::Pingreq.new
1487
+ expect(packet.to_s).to eq("\x02\x16")
1488
+ end
1489
+ end
1490
+
1491
+ describe "when parsing a Pingreq packet" do
1492
+ let(:packet) { MQTT::SN::Packet.parse("\x02\x16") }
1493
+
1494
+ it "should correctly create the right type of packet object" do
1495
+ expect(packet.class).to eq(MQTT::SN::Packet::Pingreq)
1496
+ end
1497
+ end
1498
+ end
1499
+
1500
+
1501
+ describe MQTT::SN::Packet::Pingresp do
1502
+ it "should have the right type id" do
1503
+ packet = MQTT::SN::Packet::Pingresp.new
1504
+ expect(packet.type_id).to eq(0x17)
1505
+ end
1506
+
1507
+ describe "when serialising a packet" do
1508
+ it "should output the correct bytes for a pingresp packet" do
1509
+ packet = MQTT::SN::Packet::Pingresp.new
1510
+ expect(packet.to_s).to eq("\x02\x17")
1511
+ end
1512
+ end
1513
+
1514
+ describe "when parsing a Pingresp packet" do
1515
+ let(:packet) { MQTT::SN::Packet.parse("\x02\x17") }
1516
+
1517
+ it "should correctly create the right type of packet object" do
1518
+ expect(packet.class).to eq(MQTT::SN::Packet::Pingresp)
1519
+ end
1520
+ end
1521
+ end
1522
+
1523
+
1524
+ describe MQTT::SN::Packet::Disconnect do
1525
+ it "should have the right type id" do
1526
+ packet = MQTT::SN::Packet::Disconnect.new
1527
+ expect(packet.type_id).to eq(0x18)
1528
+ end
1529
+
1530
+ describe "when serialising a packet" do
1531
+ it "should output the correct bytes for a disconnect packet" do
1532
+ packet = MQTT::SN::Packet::Disconnect.new
1533
+ expect(packet.to_s).to eq("\x02\x18")
1534
+ end
1535
+
1536
+ it "should output the correct bytes for a disconnect packet with a duration" do
1537
+ packet = MQTT::SN::Packet::Disconnect.new(:duration => 10)
1538
+ expect(packet.to_s).to eq("\x04\x18\x00\x0A")
1539
+ end
1540
+ end
1541
+
1542
+ describe "when parsing a Disconnect packet" do
1543
+ let(:packet) { MQTT::SN::Packet.parse("\x02\x18") }
1544
+
1545
+ it "should correctly create the right type of packet object" do
1546
+ expect(packet.class).to eq(MQTT::SN::Packet::Disconnect)
1547
+ end
1548
+
1549
+ it "should have the duration field set to nil" do
1550
+ expect(packet.duration).to be_nil
1551
+ end
1552
+ end
1553
+
1554
+ describe "when parsing a Disconnect packet with duration field" do
1555
+ let(:packet) { MQTT::SN::Packet.parse("\x04\x18\x00\x0A") }
1556
+
1557
+ it "should correctly create the right type of packet object" do
1558
+ expect(packet.class).to eq(MQTT::SN::Packet::Disconnect)
1559
+ end
1560
+
1561
+ it "should have the duration field set to 10" do
1562
+ expect(packet.duration).to eq(10)
1563
+ end
1564
+ end
1565
+ end
1566
+
1567
+
1568
+ describe MQTT::SN::Packet::Willtopicupd do
1569
+ it "should have the right type id" do
1570
+ packet = MQTT::SN::Packet::Willtopicupd.new
1571
+ expect(packet.type_id).to eq(0x1A)
1572
+ end
1573
+
1574
+ describe "when serialising a packet" do
1575
+ it "should output the correct bytes for a Willtopicupd packet" do
1576
+ packet = MQTT::SN::Packet::Willtopicupd.new(:topic_name => 'test', :qos => 0)
1577
+ expect(packet.to_s).to eq("\x07\x1A\x00test")
1578
+ end
1579
+
1580
+ it "should output the correct bytes for a Willtopic packet with QoS 1" do
1581
+ packet = MQTT::SN::Packet::Willtopicupd.new(:topic_name => 'test', :qos => 1)
1582
+ expect(packet.to_s).to eq("\x07\x1A\x20test")
1583
+ end
1584
+
1585
+ it "should output the correct bytes for a Willtopic packet with no topic name" do
1586
+ packet = MQTT::SN::Packet::Willtopicupd.new(:topic_name => nil)
1587
+ expect(packet.to_s).to eq("\x02\x1A")
1588
+ end
1589
+
1590
+ it "should output the correct bytes for a Willtopic packet with an empty topic name" do
1591
+ packet = MQTT::SN::Packet::Willtopicupd.new(:topic_name => '')
1592
+ expect(packet.to_s).to eq("\x02\x1A")
1593
+ end
1594
+ end
1595
+
1596
+ describe "when parsing a Willtopicupd packet" do
1597
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x1A\x40test") }
1598
+
1599
+ it "should correctly create the right type of packet object" do
1600
+ expect(packet.class).to eq(MQTT::SN::Packet::Willtopicupd)
1601
+ end
1602
+
1603
+ it "should set the topic name of the packet correctly" do
1604
+ expect(packet.topic_name).to eq('test')
1605
+ end
1606
+
1607
+ it "should set the QoS value of the packet correctly" do
1608
+ expect(packet.qos).to eq(2)
1609
+ end
1610
+
1611
+ it "should set the retain flag of the packet correctly" do
1612
+ expect(packet.retain).to be_falsy
1613
+ end
1614
+ end
1615
+
1616
+ describe "when parsing a Willtopicupd packet with no topic name" do
1617
+ let(:packet) { MQTT::SN::Packet.parse("\x02\x1A") }
1618
+
1619
+ it "should correctly create the right type of packet object" do
1620
+ expect(packet.class).to eq(MQTT::SN::Packet::Willtopicupd)
1621
+ end
1622
+
1623
+ it "should set the topic name of the packet correctly" do
1624
+ expect(packet.topic_name).to be_nil
1625
+ end
1626
+ end
1627
+ end
1628
+
1629
+ describe MQTT::SN::Packet::Willtopicresp do
1630
+ it "should have the right type id" do
1631
+ packet = MQTT::SN::Packet::Willtopicresp.new
1632
+ expect(packet.type_id).to eq(0x1B)
1633
+ end
1634
+
1635
+ describe "when serialising a packet" do
1636
+ it "should output the correct bytes" do
1637
+ packet = MQTT::SN::Packet::Willtopicresp.new(
1638
+ :return_code => 0x03
1639
+ )
1640
+ expect(packet.to_s).to eq("\x03\x1B\x03")
1641
+ end
1642
+
1643
+ it "should raise an exception if the return code isn't an Integer" do
1644
+ packet = MQTT::SN::Packet::Willtopicresp.new(:return_code => true)
1645
+ expect { packet.to_s }.to raise_error("return_code must be an Integer")
1646
+ end
1647
+ end
1648
+
1649
+ describe "when parsing a WILLTOPICRESP packet" do
1650
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x1B\x04") }
1651
+
1652
+ it "should correctly create the right type of packet object" do
1653
+ expect(packet.class).to eq(MQTT::SN::Packet::Willtopicresp)
1654
+ end
1655
+
1656
+ it "should set the return code of the packet correctly" do
1657
+ expect(packet.return_code).to eq(0x04)
1658
+ end
1659
+ end
1660
+ end
1661
+
1662
+
1663
+ describe MQTT::SN::Packet::Willmsgupd do
1664
+ it "should have the right type id" do
1665
+ packet = MQTT::SN::Packet::Willmsgupd.new
1666
+ expect(packet.type_id).to eq(0x1C)
1667
+ end
1668
+
1669
+ describe "when serialising a packet" do
1670
+ it "should output the correct bytes for a Willmsgupd packet" do
1671
+ packet = MQTT::SN::Packet::Willmsgupd.new(:data => 'test1')
1672
+ expect(packet.to_s).to eq("\x07\x1Ctest1")
1673
+ end
1674
+ end
1675
+
1676
+ describe "when parsing a Willmsgupd packet" do
1677
+ let(:packet) { MQTT::SN::Packet.parse("\x07\x1Ctest2") }
1678
+
1679
+ it "should correctly create the right type of packet object" do
1680
+ expect(packet.class).to eq(MQTT::SN::Packet::Willmsgupd)
1681
+ end
1682
+
1683
+ it "should set the topic name of the packet correctly" do
1684
+ expect(packet.data).to eq('test2')
1685
+ end
1686
+ end
1687
+ end
1688
+
1689
+
1690
+ describe MQTT::SN::Packet::Willmsgresp do
1691
+ it "should have the right type id" do
1692
+ packet = MQTT::SN::Packet::Willmsgresp.new
1693
+ expect(packet.type_id).to eq(0x1D)
1694
+ end
1695
+
1696
+ describe "when serialising a packet" do
1697
+ it "should output the correct bytes" do
1698
+ packet = MQTT::SN::Packet::Willmsgresp.new(
1699
+ :return_code => 0x03
1700
+ )
1701
+ expect(packet.to_s).to eq("\x03\x1D\x03")
1702
+ end
1703
+
1704
+ it "should raise an exception if the return code isn't an Integer" do
1705
+ packet = MQTT::SN::Packet::Willmsgresp.new(:return_code => true)
1706
+ expect { packet.to_s }.to raise_error("return_code must be an Integer")
1707
+ end
1708
+ end
1709
+
1710
+ describe "when parsing a WILLMSGRESP packet" do
1711
+ let(:packet) { MQTT::SN::Packet.parse("\x03\x1D\x04") }
1712
+
1713
+ it "should correctly create the right type of packet object" do
1714
+ expect(packet.class).to eq(MQTT::SN::Packet::Willmsgresp)
1715
+ end
1716
+
1717
+ it "should set the return code of the packet correctly" do
1718
+ expect(packet.return_code).to eq(0x04)
1719
+ end
1720
+ end
1721
+ end