• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# MQTT-SN layer unit tests
2# Copyright (C) 2019 Martine Lenders <m.lenders@fu-berlin.de>
3#
4# This program is published under GPLv2 license
5#
6# Type the following command to start the test
7# $ test/run_tests -P "load_contrib('mqttsn')" -t test/contrib/mqttsn.uts
8
9+ Syntax check
10= Import the MQTT-SN layer
11from scapy.contrib.mqttsn import *
12
13+ MQTT-SN protocol test
14
15= MQTTSN + MQTTSNAdvertise, packet instantiation and len field adjust
16p = MQTTSN() / MQTTSNAdvertise(gw_id=142, duration=54403)
17assert p.len is None
18assert p.type == ADVERTISE
19assert p.gw_id == 142
20assert p.duration == 54403
21b = bytes(p)
22p = MQTTSN(b)
23assert p.len == 5
24assert p.type == ADVERTISE
25assert p.gw_id == 142
26assert p.duration == 54403
27
28= MQTTSNAdvertise, packet dissection
29b = b"\x05\x00\x98\x2b\x9a"
30p = MQTTSN(b)
31assert p.len == 5
32assert p.type == ADVERTISE
33assert p.gw_id == 0x98
34assert p.duration == 0x2b9a
35
36= MQTTSNSearchGW, packet instantiation
37p = MQTTSN() / MQTTSNSearchGW(radius=175)
38assert p.len is None
39assert p.type == SEARCHGW
40assert p.radius == 175
41
42= MQTTSNSearchGW, packet dissection
43b = b"\x03\x01\xcc"
44p = MQTTSN(b)
45assert p.len == 3
46assert p.type == SEARCHGW
47assert p.radius == 0xcc
48
49= MQTTSNGwInfo, packet instantiation
50p = MQTTSN() / MQTTSNGwInfo(gw_id=135, gw_addr="test\0test")
51assert p.len is None
52assert p.type == GWINFO
53assert p.gw_id == 135
54assert p.gw_addr == b"test\x00test"
55
56= MQTTSN + MQTTSNGwInfo, packet instantiation and len field adjust
57p = MQTTSN(len=7) / MQTTSNGwInfo(gw_id=7, gw_addr="test") / "xyz"
58assert p.len == 7
59assert p.type == GWINFO
60assert p.gw_id == 7
61assert p.gw_addr == b"test"
62assert p.load == b"xyz"
63b = bytes(p)
64p = MQTTSN(b)
65assert p.len == 7
66assert p.type == GWINFO
67assert p.gw_id == 7
68assert p.gw_addr == b"test"
69assert p.load == b"xyz"
70
71= MQTTSNGwInfo, packet dissection
72b = b"\x07\x02\x14testing"
73p = MQTTSN(b)
74assert p.len == 7
75assert p.type == GWINFO
76assert p.gw_id == 0x14
77assert p.gw_addr == b"test"
78assert p.load == b"ing"
79
80= MQTTSNGwInfo, packet dissection - invalid length
81b = b"\x01\x00\x01\x02\x14test"
82p = MQTTSN(b)
83print(type(p), repr(p))
84assert p.len == 1
85assert p.type == GWINFO
86assert p.gw_id == 0x14
87assert p.gw_addr == b""
88
89= MQTTSNConnect, packet instantiation
90p = MQTTSN() / MQTTSNConnect(duration=40666, client_id="test")
91assert p.len is None
92assert p.type == CONNECT
93assert p.dup == 0
94assert p.qos == QOS_0
95assert p.retain == 0
96assert p.will == 0
97assert p.cleansess == 0
98assert p.tid_type == TID_NORMAL
99assert p.prot_id == 1
100assert p.duration == 40666
101assert p.client_id == b"test"
102
103= MQTTSNConnect, packet dissection
104b = b"\x0a\x04\x04\x1a\x77\x5btesting"
105p = MQTTSN(b)
106assert p.len == 10
107assert p.type == CONNECT
108assert p.dup == 0
109assert p.qos == QOS_0
110assert p.retain == 0
111assert p.will == 0
112assert p.cleansess == 1
113assert p.tid_type == TID_NORMAL
114assert p.prot_id == 0x1a
115assert p.duration == 0x775b
116assert p.client_id == b"test"
117assert p.load == b"ing"
118
119= MQTTSNConnack, packet instantiation
120p = MQTTSN() / MQTTSNConnack()
121assert p.len is None
122assert p.type == CONNACK
123assert p.return_code == ACCEPTED
124
125= MQTTSNConnack, packet dissection
126b = b"\x03\x05\x02"
127p = MQTTSN(b)
128assert p.len == 3
129assert p.type == CONNACK
130assert p.return_code == REJ_TID
131
132= MQTTSNWillTopicReq, packet instantiation
133p = MQTTSN() / MQTTSNWillTopicReq()
134assert p.len is None
135assert p.type == WILLTOPICREQ
136
137= MQTTSNWillTopicReq, packet dissection
138b = b"\x02\x06"
139p = MQTTSN(b)
140assert p.len == 2
141assert p.type == WILLTOPICREQ
142
143= MQTTSNWillTopic, packet instantiation
144p = MQTTSN() / MQTTSNWillTopic(will_topic="/test")
145assert p.len is None
146assert p.type == WILLTOPIC
147assert p.dup == 0
148assert p.qos == QOS_0
149assert p.retain == 0
150assert p.will == 0
151assert p.cleansess == 0
152assert p.tid_type == TID_NORMAL
153assert p.will_topic == b"/test"
154
155= MQTTSNWillTopic, packet dissection
156b = b"\x08\x07\x00/testing"
157p = MQTTSN(b)
158assert p.len == 8
159assert p.type == WILLTOPIC
160assert p.dup == 0
161assert p.qos == QOS_0
162assert p.retain == 0
163assert p.will == 0
164assert p.cleansess == 0
165assert p.tid_type == TID_NORMAL
166assert p.will_topic == b"/test"
167
168= MQTTSNWillMsgReq, packet instantiation
169p = MQTTSN() / MQTTSNWillMsgReq()
170assert p.len is None
171assert p.type == WILLMSGREQ
172
173= MQTTSNWillMsgReq, packet dissection
174b = b"\x02\x08"
175p = MQTTSN(b)
176assert p.len == 2
177assert p.type == WILLMSGREQ
178
179= MQTTSNWillMsg, packet instantiation
180p = MQTTSN() / MQTTSNWillMsg(will_msg="test")
181assert p.len is None
182assert p.type == WILLMSG
183assert p.will_msg == b"test"
184
185= MQTTSNWillMsg, packet dissection
186b = b"\x06\x09testing"
187p = MQTTSN(b)
188assert p.len == 6
189assert p.type == WILLMSG
190assert p.will_msg == b"test"
191assert p.load == b"ing"
192
193= MQTTSNRegister, packet instantiation
194p = MQTTSN() / MQTTSNRegister(mid=30533, topic_name="/test")
195assert p.len is None
196assert p.type == REGISTER
197assert p.tid == 0
198assert p.mid == 30533
199assert p.topic_name == b"/test"
200
201= MQTTSNRegister, packet dissection
202b = b"\x0b\x0a\0\0\x48\x8a/testing"
203p = MQTTSN(b)
204assert p.len == 11
205assert p.type == REGISTER
206assert p.tid == 0
207assert p.mid == 0x488a
208assert p.topic_name == b"/test"
209assert p.load == b"ing"
210
211= MQTTSNRegack, packet instantiation
212p = MQTTSN() / MQTTSNRegack(tid=61547, mid=8593, return_code=REJ_NOTSUP)
213assert p.len is None
214assert p.type == REGACK
215assert p.tid == 61547
216assert p.mid == 8593
217assert p.return_code == REJ_NOTSUP
218
219= MQTTSNRegack, packet dissection
220b = b"\x08\x0b\xc5\xe8\x31\x87\x01"
221p = MQTTSN(b)
222assert p.len == 8
223assert p.type == REGACK
224assert p.tid == 0xc5e8
225assert p.mid == 0x3187
226assert p.return_code == REJ_CONJ
227
228= MQTTSNPublish, packet instantiation
229p = MQTTSN() / MQTTSNPublish(qos=QOS_1, tid=52032, mid=35252,
230                             data="Hello world!")
231assert p.len is None
232assert p.type == PUBLISH
233assert p.dup == 0
234assert p.qos == QOS_1
235assert p.retain == 0
236assert p.will == 0
237assert p.cleansess == 0
238assert p.tid_type == TID_NORMAL
239assert p.tid == 52032
240assert p.mid == 35252
241assert p.data == b"Hello world!"
242
243= MQTTSNPublish, packet instantiation - long data
244p = MQTTSN() / MQTTSNPublish(qos=QOS_NEG1, tid=62839, mid=36181,
245                             data=726 * "X")
246assert p.len is None
247assert p.type == PUBLISH
248assert p.dup == 0
249assert p.qos == QOS_NEG1
250assert p.retain == 0
251assert p.will == 0
252assert p.cleansess == 0
253assert p.tid_type == TID_NORMAL
254assert p.tid == 62839
255assert p.mid == 36181
256assert p.data == 726 * b"X"
257# Check if length field was constructed correctly
258b = bytes(p)
259assert b[:3] == b'\x01\x02\xdf'
260p = MQTTSN(b)
261assert p.len == 735
262assert p.data == 726 * b"X"
263
264= MQTTSNPublish, packet dissection
265b = b"\x0b\x0c\x40\x19\x7f\x6a\x26testing"
266p = MQTTSN(b)
267assert p.len == 11
268assert p.type == PUBLISH
269assert p.dup == 0
270assert p.qos == QOS_2
271assert p.retain == 0
272assert p.will == 0
273assert p.cleansess == 0
274assert p.tid_type == TID_NORMAL
275assert p.tid == 0x197f
276assert p.mid == 0x6a26
277assert p.data == b"test"
278assert p.load == b"ing"
279
280= MQTTSNPublish, packet dissection - long data
281b = b"\x01\x04\x64\x0c" + b"\x00\xb1\x39\xd7\x4a" + (1115 * b"X")
282p = MQTTSN(b)
283assert p.len == 0x0464 == (4 + 5 + 1115)
284assert p.type == PUBLISH
285assert p.dup == 0
286assert p.qos == QOS_0
287assert p.retain == 0
288assert p.will == 0
289assert p.cleansess == 0
290assert p.tid_type == TID_NORMAL
291assert p.tid == 0xb139
292assert p.mid == 0xd74a
293assert p.data == 1115 * b"X"
294
295= MQTTSNPuback, packet instantiation
296p = MQTTSN() / MQTTSNPuback(tid=27610, mid=30284, return_code=ACCEPTED)
297assert p.len is None
298assert p.type == PUBACK
299assert p.tid == 27610
300assert p.mid == 30284
301assert p.return_code == ACCEPTED
302
303= MQTTSNPuback, packet dissection
304b = b"\x08\x0d\x03\xda\x73\x9a\x02"
305p = MQTTSN(b)
306assert p.len == 8
307assert p.type == PUBACK
308assert p.tid == 0x03da
309assert p.mid == 0x739a
310assert p.return_code == REJ_TID
311
312= MQTTSNPubcomp, packet instantiation
313p = MQTTSN() / MQTTSNPubcomp(mid=36193)
314assert p.len is None
315assert p.type == PUBCOMP
316assert p.mid == 36193
317
318= MQTTSNPubcomp, packet dissection
319b = b"\x04\x0e\x26\xa2"
320p = MQTTSN(b)
321assert p.len == 4
322assert p.type == PUBCOMP
323assert p.mid == 0x26a2
324
325= MQTTSNPubrec, packet instantiation
326p = MQTTSN() / MQTTSNPubrec(mid=44837)
327assert p.len is None
328assert p.type == PUBREC
329assert p.mid == 44837
330
331= MQTTSNPubrec, packet dissection
332b = b"\x04\x0f\x36\xc4"
333p = MQTTSN(b)
334assert p.len == 4
335assert p.type == PUBREC
336assert p.mid == 0x36c4
337
338= MQTTSNPubrel, packet instantiation
339p = MQTTSN() / MQTTSNPubrel(mid=42834)
340assert p.len is None
341assert p.type == PUBREL
342assert p.mid == 42834
343
344= MQTTSNPubrel, packet dissection
345b = b"\x04\x10\x94\x0f"
346p = MQTTSN(b)
347assert p.len == 4
348assert p.type == PUBREL
349assert p.mid == 0x940f
350
351= MQTTSNSubscribe, packet instantiation - topic name
352p = MQTTSN() / MQTTSNSubscribe(mid=63780, topic_name="/test")
353assert p.len is None
354assert p.type == SUBSCRIBE
355assert p.dup == 0
356assert p.qos == QOS_0
357assert p.retain == 0
358assert p.will == 0
359assert p.cleansess == 0
360assert p.tid_type == TID_NORMAL
361assert p.topic_name == b"/test"
362assert p.short_topic is None
363assert p.tid is None
364
365= MQTTSNSubscribe, packet instantiation - predefined topic ID
366p = MQTTSN() / MQTTSNSubscribe(mid=63780, tid_type=TID_PREDEF,
367                               tid=1187)
368assert p.len is None
369assert p.type == SUBSCRIBE
370assert p.dup == 0
371assert p.qos == QOS_0
372assert p.retain == 0
373assert p.will == 0
374assert p.cleansess == 0
375assert p.tid_type == TID_PREDEF
376assert p.topic_name is None
377assert p.short_topic is None
378assert p.tid == 1187
379
380= MQTTSNSubscribe, packet instantiation - short topic
381p = MQTTSN() / MQTTSNSubscribe(mid=63780, tid_type=TID_SHORT, short_topic="fx")
382assert p.len is None
383assert p.type == SUBSCRIBE
384assert p.dup == 0
385assert p.qos == QOS_0
386assert p.retain == 0
387assert p.will == 0
388assert p.cleansess == 0
389assert p.tid_type == TID_SHORT
390assert p.topic_name is None
391assert p.short_topic == b"fx"
392assert p.tid is None
393
394= MQTTSNSubscribe, packet dissection - topic name
395b = b"\x07\x12\x00\x66\x8a/t"
396p = MQTTSN(b)
397assert p.len == 7
398assert p.type == SUBSCRIBE
399assert p.dup == 0
400assert p.qos == QOS_0
401assert p.retain == 0
402assert p.will == 0
403assert p.cleansess == 0
404assert p.tid_type == TID_NORMAL
405assert p.mid == 0x668a
406assert p.topic_name == b"/t"
407assert p.short_topic is None
408assert p.tid is None
409
410= MQTTSNSubscribe, packet dissection - short topic
411b = b"\x07\x12\x01\x66\x8a/t"
412p = MQTTSN(b)
413assert p.len == 7
414assert p.type == SUBSCRIBE
415assert p.dup == 0
416assert p.qos == QOS_0
417assert p.retain == 0
418assert p.will == 0
419assert p.cleansess == 0
420assert p.tid_type == TID_PREDEF
421assert p.mid == 0x668a
422assert p.topic_name is None
423assert p.short_topic is None
424assert p.tid == (ord("/") << 8 | ord("t")) == 12148
425
426= MQTTSNSubscribe, packet dissection - predefined topic ID
427b = b"\x07\x12\x02\x66\x8a/t"
428p = MQTTSN(b)
429assert p.len == 7
430assert p.type == SUBSCRIBE
431assert p.dup == 0
432assert p.qos == QOS_0
433assert p.retain == 0
434assert p.will == 0
435assert p.cleansess == 0
436assert p.tid_type == TID_SHORT
437assert p.mid == 0x668a
438assert p.topic_name is None
439assert p.short_topic == b"/t"
440assert p.tid is None
441
442= MQTTSNSuback, packet instantiation
443p = MQTTSN() / MQTTSNSuback(qos=QOS_0, tid=5496, mid=63108,
444                             return_code=REJ_TID)
445assert p.len is None
446assert p.type == SUBACK
447assert p.dup == 0
448assert p.qos == QOS_0
449assert p.retain == 0
450assert p.will == 0
451assert p.cleansess == 0
452assert p.tid_type == TID_NORMAL
453assert p.tid == 5496
454assert p.mid == 63108
455assert p.return_code == REJ_TID
456
457= MQTTSNSuback, packet dissection
458b = b"\x08\x13\xa4\x93\x0b\x02\xc6\x00"
459p = MQTTSN(b)
460assert p.len == 8
461assert p.type == SUBACK
462assert p.dup == 1
463assert p.qos == QOS_1
464assert p.retain == 0
465assert p.will == 0
466assert p.cleansess == 1
467assert p.tid_type == TID_NORMAL
468assert p.tid == 0x930b
469assert p.mid == 0x02c6
470assert p.return_code == ACCEPTED
471
472= MQTTSNUnsubscribe, packet instantiation - topic name
473p = MQTTSN() / MQTTSNUnsubscribe(mid=63780, topic_name="/test")
474assert p.len is None
475assert p.type == UNSUBSCRIBE
476assert p.dup == 0
477assert p.qos == QOS_0
478assert p.retain == 0
479assert p.will == 0
480assert p.cleansess == 0
481assert p.tid_type == TID_NORMAL
482assert p.topic_name == b"/test"
483assert p.short_topic is None
484assert p.tid is None
485
486= MQTTSNUnsubscribe, packet instantiation - predefined topic ID
487p = MQTTSN() / MQTTSNUnsubscribe(mid=63780, tid_type=TID_PREDEF,
488                                 tid=1187)
489assert p.len is None
490assert p.type == UNSUBSCRIBE
491assert p.dup == 0
492assert p.qos == QOS_0
493assert p.retain == 0
494assert p.will == 0
495assert p.cleansess == 0
496assert p.tid_type == TID_PREDEF
497assert p.topic_name is None
498assert p.short_topic is None
499assert p.tid == 1187
500
501= MQTTSNUnsubscribe, packet instantiation - short topic
502p = MQTTSN() / MQTTSNUnsubscribe(mid=63780, tid_type=TID_SHORT,
503                                 short_topic="fx")
504assert p.len is None
505assert p.type == UNSUBSCRIBE
506assert p.dup == 0
507assert p.qos == QOS_0
508assert p.retain == 0
509assert p.will == 0
510assert p.cleansess == 0
511assert p.tid_type == TID_SHORT
512assert p.topic_name is None
513assert p.short_topic == b"fx"
514assert p.tid is None
515
516= MQTTSNUnsubscribe, packet dissection - topic name
517b = b"\x07\x14\x00\x66\x8a/t"
518p = MQTTSN(b)
519assert p.len == 7
520assert p.type == UNSUBSCRIBE
521assert p.dup == 0
522assert p.qos == QOS_0
523assert p.retain == 0
524assert p.will == 0
525assert p.cleansess == 0
526assert p.tid_type == TID_NORMAL
527assert p.mid == 0x668a
528assert p.topic_name == b"/t"
529assert p.short_topic is None
530assert p.tid is None
531
532= MQTTSNUnsubscribe, packet dissection - short topic
533b = b"\x07\x14\x01\x66\x8a/t"
534p = MQTTSN(b)
535assert p.len == 7
536assert p.type == UNSUBSCRIBE
537assert p.dup == 0
538assert p.qos == QOS_0
539assert p.retain == 0
540assert p.will == 0
541assert p.cleansess == 0
542assert p.tid_type == TID_PREDEF
543assert p.mid == 0x668a
544assert p.topic_name is None
545assert p.short_topic is None
546assert p.tid == (ord("/") << 8 | ord("t")) == 12148
547
548= MQTTSNUnsubscribe, packet dissection - predefined topic ID
549b = b"\x07\x14\x02\x66\x8a/t"
550p = MQTTSN(b)
551assert p.len == 7
552assert p.type == UNSUBSCRIBE
553assert p.dup == 0
554assert p.qos == QOS_0
555assert p.retain == 0
556assert p.will == 0
557assert p.cleansess == 0
558assert p.tid_type == TID_SHORT
559assert p.mid == 0x668a
560assert p.topic_name is None
561assert p.short_topic == b"/t"
562assert p.tid is None
563
564= MQTTSNUnsuback, packet instantiation
565p = MQTTSN() / MQTTSNUnsuback(mid=44541)
566assert p.len is None
567assert p.type == UNSUBACK
568assert p.mid == 44541
569
570= MQTTSNUnsuback, packet dissection
571b = b"\x08\x15\xcb\x3d"
572p = MQTTSN(b)
573assert p.len == 8
574assert p.type == UNSUBACK
575assert p.mid == 0xcb3d
576
577= MQTTSNPingReq, packet instantiation - no client ID
578p = MQTTSN() / MQTTSNPingReq()
579assert p.len is None
580assert p.type == PINGREQ
581assert p.client_id == b""
582
583= MQTTSNPingReq, packet instantiation - with client ID
584p = MQTTSN() / MQTTSNPingReq(client_id="test")
585assert p.len is None
586assert p.type == PINGREQ
587assert p.client_id == b"test"
588
589= MQTTSNPingReq, packet dissection
590b = b"\x07\x16hello"
591p = MQTTSN(b)
592assert p.len == 7
593assert p.type == PINGREQ
594assert p.client_id == b"hello"
595
596= MQTTSNPingResp, packet instantiation
597p = MQTTSN() / MQTTSNPingResp()
598assert p.len is None
599assert p.type == PINGRESP
600
601= MQTTSNPingResp, packet dissection
602b = b"\x02\x17"
603p = MQTTSN(b)
604assert p.len == 2
605assert p.type == PINGRESP
606
607= MQTTSNDisconnect, packet instantiation and len field adjust - w/o duration
608p = MQTTSN() / MQTTSNDisconnect()
609assert p.len is None
610assert p.type == DISCONNECT
611assert p.duration is None
612b = bytes(p)
613p = MQTTSN(b)
614assert p.len == 2
615assert p.type == DISCONNECT
616
617= MQTTSNDisconnect, packet instantiation and len field adjust - w duration
618p = MQTTSN() / MQTTSNDisconnect(duration=19567)
619assert p.len is None
620assert p.type == DISCONNECT
621assert p.duration == 19567
622b = bytes(p)
623p = MQTTSN(b)
624assert p.len == 4
625assert p.type == DISCONNECT
626assert p.duration == 19567
627
628= MQTTSNDisconnect, packet dissection - w/o duration
629b = b"\x02\x18"
630p = MQTTSN(b)
631assert p.len == 2
632assert p.type == DISCONNECT
633
634= MQTTSNDisconnect, packet dissection - w duration
635b = b"\x04\x18\x03\x12"
636p = MQTTSN(b)
637assert p.len == 4
638assert p.type == DISCONNECT
639assert p.duration == 0x0312
640
641= MQTTSNWillTopicUpd, packet instantiation
642p = MQTTSN() / MQTTSNWillTopicUpd(will_topic="/test")
643assert p.len is None
644assert p.type == WILLTOPICUPD
645assert p.dup == 0
646assert p.qos == QOS_0
647assert p.retain == 0
648assert p.will == 0
649assert p.cleansess == 0
650assert p.tid_type == TID_NORMAL
651assert p.will_topic == b"/test"
652
653= MQTTSNWillTopicUpd, packet dissection
654b = b"\x08\x1a\x00/testing"
655p = MQTTSN(b)
656assert p.len == 8
657assert p.type == WILLTOPICUPD
658assert p.dup == 0
659assert p.qos == QOS_0
660assert p.retain == 0
661assert p.will == 0
662assert p.cleansess == 0
663assert p.tid_type == TID_NORMAL
664assert p.will_topic == b"/test"
665
666= MQTTSNWillTopicResp, packet instantiation
667p = MQTTSN() / MQTTSNWillTopicResp()
668assert p.len is None
669assert p.type == WILLTOPICRESP
670assert p.return_code == ACCEPTED
671
672= MQTTSNWillTopicResp, packet dissection
673b = b"\x03\x1b\x02"
674p = MQTTSN(b)
675assert p.len == 3
676assert p.type == WILLTOPICRESP
677assert p.return_code == REJ_TID
678
679= MQTTSNWillMsgUpd, packet instantiation
680p = MQTTSN() / MQTTSNWillMsgUpd(will_msg="test")
681assert p.len is None
682assert p.type == WILLMSGUPD
683assert p.will_msg == b"test"
684
685= MQTTSNWillMsgUpd, packet dissection
686b = b"\x06\x1ctesting"
687p = MQTTSN(b)
688assert p.len == 6
689assert p.type == WILLMSGUPD
690assert p.will_msg == b"test"
691assert p.load == b"ing"
692
693= MQTTSNWillMsgResp, packet instantiation
694p = MQTTSN() / MQTTSNWillMsgResp()
695assert p.len is None
696assert p.type == WILLMSGRESP
697assert p.return_code == ACCEPTED
698
699= MQTTSNWillMsgResp, packet dissection
700b = b"\x03\x1d\x02"
701p = MQTTSN(b)
702assert p.len == 3
703assert p.type == WILLMSGRESP
704assert p.return_code == REJ_TID
705
706= MQTTSNEncaps, packet instantiation
707p = MQTTSN() / MQTTSNEncaps(radius=1, w_node_id="test") / MQTTSN() / \
708    MQTTSNConnack()
709assert p.len is None
710assert p.type == ENCAPS_MSG
711assert p.radius == 1
712assert p.w_node_id == b"test"
713assert p.payload.payload.len is None
714assert p.payload.payload.type == CONNACK
715assert p.payload.payload.return_code == ACCEPTED
716b = bytes(p)
717p = MQTTSN(b)
718assert p.len == 7
719assert p.type == ENCAPS_MSG
720assert p.radius == 1
721assert p.w_node_id == b"test"
722assert p.return_code == ACCEPTED
723assert p.payload.payload.len == 3
724assert p.payload.payload.type == CONNACK
725assert p.payload.payload.return_code == ACCEPTED
726
727= MQTTSNEncaps, packet dissection
728b = b"\x07\xfe\x02test\x03\x05\x00"
729p = MQTTSN(b)
730assert p.len == 7
731assert p.type == ENCAPS_MSG
732assert p.radius == 2
733assert p.w_node_id == b"test"
734assert p.payload.payload.len == 3
735assert p.payload.payload.type == CONNACK
736assert p.payload.payload.return_code == ACCEPTED
737
738= MQTTSNEncaps, packet dissection -- long payload
739b = b"\x07\xfe\x02test" + b"\x01\x04\x64\x0c" + b"\x00\xb1\x39\xd7\x4a" + \
740        (1115 * b"X")
741p = MQTTSN(b)
742assert p.len == 7
743assert p.type == ENCAPS_MSG
744assert p.radius == 2
745assert p.w_node_id == b"test"
746assert p.payload.payload.len == 4 + 5 + 1115 == 0x0464
747assert p.payload.payload.type == PUBLISH
748assert p.payload.payload.dup == 0
749assert p.payload.payload.qos == QOS_0
750assert p.payload.payload.retain == 0
751assert p.payload.payload.will == 0
752assert p.payload.payload.cleansess == 0
753assert p.payload.payload.tid_type == TID_NORMAL
754assert p.payload.payload.tid == 0xb139
755assert p.payload.payload.mid == 0xd74a
756assert p.payload.payload.data == 1115 * b"X"
757
758= MQTTSN without payload
759p = MQTTSN()
760assert bytes(p) == b"\x02\x00"
761
762= MQTTSN without payload -- invalid lengths
763p = MQTTSN(len=1)
764try:
765    bytes(p)        # expect Scapy_Exception
766    assert false
767except Scapy_Exception:
768    pass
769
770p = MQTTSN(len=0x10000)
771try:
772    bytes(p)        # expect Scapy_Exception
773    assert false
774except Scapy_Exception:
775    pass
776
777b = '\x01'
778try:
779    p = MQTTSN(b)   # expect Scapy_Exception
780    assert false
781except Scapy_Exception:
782    pass
783
784b = '\x01\x02'
785try:
786    p = MQTTSN(b)   # expect Scapy_Exception
787    assert false
788except Scapy_Exception:
789    pass
790
791
792= MQTT-SN RandVariableFieldLen
793assert type(MQTTSN().fieldtype["len"].randval()) == RandVariableFieldLen
794assert type(MQTTSN().fieldtype["len"].randval() + 0) == int
795
796= Disect full IPv6 packages
797~ dport == 1883 (0x75b)
798b = b"\x60\x00\x00\x00\x00\x2c\x11\x40\x20\x01\x0d\xb8\x00\x00\x00\x00" \
799    b"\x17\x11\x6b\x10\x65\xf7\x5f\x0a\x20\x01\x0d\xb8\x00\x00\x00\x00" \
800    b"\x17\x11\x6b\x10\x65\xfd\xbe\x06\xc0\x00\x07\x5b\x00\x2c\x40\x7e" \
801    b"\x0b\x0a\0\0\x48\x8a/testing"
802p = IPv6(b)
803assert MQTTSNRegister in p
804
805~ sport == 1883 (0x75b)
806b = b"\x60\x00\x00\x00\x00\x0f\x11\x40\x20\x01\x0d\xb8\x00\x00\x00\x00" \
807    b"\x17\x11\x6b\x10\x65\xfd\xbe\x06\x20\x01\x0d\xb8\x00\x00\x00\x00" \
808    b"\x17\x11\x6b\x10\x65\xf7\x5f\x0a\x07\x5b\xc0\x00\x00\x0f\x62\x7c" \
809    b"\x07\x0d\x00\x01\x86\x2f\x00"
810p = IPv6(b)
811assert MQTTSNPuback in p
812
813= UDP packet instantiation
814b = bytes(UDP() / MQTTSN() / MQTTSNConnack())
815p = UDP(b)
816assert MQTTSNConnack in p
817assert p.sport == 1883
818assert p.dport == 1883
819