forked from mochi-mqtt/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpackets.go
3853 lines (3794 loc) · 91.6 KB
/
tpackets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2022 mochi-co
// SPDX-FileContributor: mochi-co
package packets
// TPacketCase contains data for cross-checking the encoding and decoding
// of packets and expected scenarios.
type TPacketCase struct {
RawBytes []byte // the bytes that make the packet
ActualBytes []byte // the actual byte array that is created in the event of a byte mutation
Group string // a group that should run the test, blank for all
Desc string // a description of the test
FailFirst error // expected fail result to be run immediately after the method is called
Packet *Packet // the packet that is Expected
ActualPacket *Packet // the actual packet after mutations
Expect error // generic Expected fail result to be checked
Isolate bool // isolate can be used to isolate a test
Primary bool // primary is a test that should be run using readPackets
Case byte // the identifying byte of the case
}
// TPacketCases is a slice of TPacketCase.
type TPacketCases []TPacketCase
// Get returns a case matching a given T byte.
func (f TPacketCases) Get(b byte) TPacketCase {
for _, v := range f {
if v.Case == b {
return v
}
}
return TPacketCase{}
}
const (
TConnectMqtt31 byte = iota
TConnectMqtt311
TConnectMqtt5
TConnectMqtt5LWT
TConnectClean
TConnectCleanLWT
TConnectUserPass
TConnectUserPassLWT
TConnectMalProtocolName
TConnectMalProtocolVersion
TConnectMalFlags
TConnectMalKeepalive
TConnectMalClientID
TConnectMalWillTopic
TConnectMalWillFlag
TConnectMalUsername
TConnectMalPassword
TConnectMalFixedHeader
TConnectMalReservedBit
TConnectMalProperties
TConnectMalWillProperties
TConnectInvalidProtocolName
TConnectInvalidProtocolVersion
TConnectInvalidProtocolVersion2
TConnectInvalidReservedBit
TConnectInvalidClientIDTooLong
TConnectInvalidPasswordNoUsername
TConnectInvalidFlagNoUsername
TConnectInvalidFlagNoPassword
TConnectInvalidUsernameNoFlag
TConnectInvalidPasswordNoFlag
TConnectInvalidUsernameTooLong
TConnectInvalidPasswordTooLong
TConnectInvalidWillFlagNoPayload
TConnectInvalidWillFlagQosOutOfRange
TConnectInvalidWillSurplusRetain
TConnectZeroByteUsername
TConnectSpecInvalidUTF8D800
TConnectSpecInvalidUTF8DFFF
TConnectSpecInvalidUTF80000
TConnectSpecInvalidUTF8NoSkip
TConnackAcceptedNoSession
TConnackAcceptedSessionExists
TConnackAcceptedMqtt5
TConnackAcceptedAdjustedExpiryInterval
TConnackMinMqtt5
TConnackMinCleanMqtt5
TConnackInvalidMinMqtt5
TConnackBadProtocolVersion
TConnackProtocolViolationNoSession
TConnackBadClientID
TConnackServerUnavailable
TConnackBadUsernamePassword
TConnackBadUsernamePasswordNoSession
TConnackMqtt5BadUsernamePasswordNoSession
TConnackNotAuthorised
TConnackMalSessionPresent
TConnackMalReturnCode
TConnackMalProperties
TConnackDropProperties
TConnackDropPropertiesPartial
TPublishNoPayload
TPublishBasic
TPublishBasicTopicAliasOnly
TPublishBasicMqtt5
TPublishMqtt5
TPublishQos1
TPublishQos1NoPayload
TPublishQos1Dup
TPublishQos2
TPublishQos2Mqtt5
TPublishQos2Upgraded
TPublishSubscriberIdentifier
TPublishRetain
TPublishRetainMqtt5
TPublishDup
TPublishMalTopicName
TPublishMalPacketID
TPublishMalProperties
TPublishCopyBasic
TPublishSpecQos0NoPacketID
TPublishSpecQosMustPacketID
TPublishDropOversize
TPublishInvalidQos0NoPacketID
TPublishInvalidQosMustPacketID
TPublishInvalidSurplusSubID
TPublishInvalidSurplusWildcard
TPublishInvalidSurplusWildcard2
TPublishInvalidNoTopic
TPublishInvalidTopicAlias
TPublishInvalidExcessTopicAlias
TPublishSpecDenySysTopic
TPuback
TPubackMqtt5
TPubackMalPacketID
TPubackMalProperties
TPubrec
TPubrecMqtt5
TPubrecMqtt5IDInUse
TPubrecMalPacketID
TPubrecMalProperties
TPubrecMalReasonCode
TPubrecInvalidReason
TPubrel
TPubrelMqtt5
TPubrelMqtt5AckNoPacket
TPubrelMalPacketID
TPubrelMalProperties
TPubrelInvalidReason
TPubcomp
TPubcompMqtt5
TPubcompMqtt5AckNoPacket
TPubcompMalPacketID
TPubcompMalProperties
TPubcompInvalidReason
TSubscribe
TSubscribeMany
TSubscribeMqtt5
TSubscribeRetainHandling1
TSubscribeRetainHandling2
TSubscribeRetainAsPublished
TSubscribeMalPacketID
TSubscribeMalTopic
TSubscribeMalQos
TSubscribeMalQosRange
TSubscribeMalProperties
TSubscribeInvalidQosMustPacketID
TSubscribeSpecQosMustPacketID
TSubscribeInvalidNoFilters
TSubscribeInvalidSharedNoLocal
TSubscribeInvalidFilter
TSubscribeInvalidIdentifierOversize
TSuback
TSubackMany
TSubackDeny
TSubackUnspecifiedError
TSubackUnspecifiedErrorMqtt5
TSubackMqtt5
TSubackPacketIDInUse
TSubackInvalidFilter
TSubackInvalidSharedNoLocal
TSubackMalPacketID
TSubackMalProperties
TUnsubscribe
TUnsubscribeMany
TUnsubscribeMqtt5
TUnsubscribeDropProperties
TUnsubscribeMalPacketID
TUnsubscribeMalTopicName
TUnsubscribeMalProperties
TUnsubscribeInvalidQosMustPacketID
TUnsubscribeSpecQosMustPacketID
TUnsubscribeInvalidNoFilters
TUnsuback
TUnsubackMany
TUnsubackMqtt5
TUnsubackPacketIDInUse
TUnsubackMalPacketID
TUnsubackMalProperties
TPingreq
TPingresp
TDisconnect
TDisconnectTakeover
TDisconnectMqtt5
TDisconnectNormalMqtt5
TDisconnectSecondConnect
TDisconnectReceiveMaximum
TDisconnectDropProperties
TDisconnectShuttingDown
TDisconnectMalProperties
TDisconnectMalReasonCode
TDisconnectZeroNonZeroExpiry
TAuth
TAuthMalReasonCode
TAuthMalProperties
TAuthInvalidReason
TAuthInvalidReason2
)
// TPacketData contains individual encoding and decoding scenarios for each packet type.
var TPacketData = map[byte]TPacketCases{
Connect: {
{
Case: TConnectMqtt31,
Desc: "mqtt v3.1",
Primary: true,
RawBytes: []byte{
Connect << 4, 17, // Fixed header
0, 6, // Protocol Name - MSB+LSB
'M', 'Q', 'I', 's', 'd', 'p', // Protocol Name
3, // Protocol Version
0, // Packet Flags
0, 30, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
},
Packet: &Packet{
FixedHeader: FixedHeader{
Type: Connect,
Remaining: 17,
},
ProtocolVersion: 3,
Connect: ConnectParams{
ProtocolName: []byte("MQIsdp"),
Clean: false,
Keepalive: 30,
ClientIdentifier: "zen",
},
},
},
{
Case: TConnectMqtt311,
Desc: "mqtt v3.1.1",
Primary: true,
RawBytes: []byte{
Connect << 4, 15, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
0, // Packet Flags
0, 60, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
},
Packet: &Packet{
FixedHeader: FixedHeader{
Type: Connect,
Remaining: 15,
},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Clean: false,
Keepalive: 60,
ClientIdentifier: "zen",
},
},
},
{
Case: TConnectMqtt5,
Desc: "mqtt v5",
Primary: true,
RawBytes: []byte{
Connect << 4, 87, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
5, // Protocol Version
0, // Packet Flags
0, 30, // Keepalive
// Properties
71, // length
17, 0, 0, 0, 120, // Session Expiry Interval (17)
21, 0, 5, 'S', 'H', 'A', '-', '1', // Authentication Method (21)
22, 0, 9, 'a', 'u', 't', 'h', '-', 'd', 'a', 't', 'a', // Authentication Data (22)
23, 1, // Request Problem Info (23)
25, 1, // Request Response Info (25)
33, 1, 244, // Receive Maximum (33)
34, 3, 231, // Topic Alias Maximum (34)
38, // User Properties (38)
0, 5, 'h', 'e', 'l', 'l', 'o',
0, 6, 228, 184, 150, 231, 149, 140,
38, // User Properties (38)
0, 4, 'k', 'e', 'y', '2',
0, 6, 'v', 'a', 'l', 'u', 'e', '2',
39, 0, 0, 125, 0, // Maximum Packet Size (39)
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
},
Packet: &Packet{
FixedHeader: FixedHeader{
Type: Connect,
Remaining: 87,
},
ProtocolVersion: 5,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Clean: false,
Keepalive: 30,
ClientIdentifier: "zen",
},
Properties: Properties{
SessionExpiryInterval: uint32(120),
SessionExpiryIntervalFlag: true,
AuthenticationMethod: "SHA-1",
AuthenticationData: []byte("auth-data"),
RequestProblemInfo: byte(1),
RequestProblemInfoFlag: true,
RequestResponseInfo: byte(1),
ReceiveMaximum: uint16(500),
TopicAliasMaximum: uint16(999),
User: []UserProperty{
{
Key: "hello",
Val: "世界",
},
{
Key: "key2",
Val: "value2",
},
},
MaximumPacketSize: uint32(32000),
},
},
},
{
Case: TConnectClean,
Desc: "mqtt 3.1.1, clean session",
RawBytes: []byte{
Connect << 4, 15, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
2, // Packet Flags
0, 45, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
},
Packet: &Packet{
FixedHeader: FixedHeader{
Type: Connect,
Remaining: 15,
},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Clean: true,
Keepalive: 45,
ClientIdentifier: "zen",
},
},
},
{
Case: TConnectMqtt5LWT,
Desc: "mqtt 5 clean session, lwt",
RawBytes: []byte{
Connect << 4, 47, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
5, // Protocol Version
14, // Packet Flags
0, 30, // Keepalive
// Properties
10, // length
17, 0, 0, 0, 120, // Session Expiry Interval (17)
39, 0, 0, 125, 0, // Maximum Packet Size (39)
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
5, // will properties length
24, 0, 0, 2, 88, // will delay interval (24)
0, 3, // Will Topic - MSB+LSB
'l', 'w', 't',
0, 8, // Will Message MSB+LSB
'n', 'o', 't', 'a', 'g', 'a', 'i', 'n',
},
Packet: &Packet{
ProtocolVersion: 5,
FixedHeader: FixedHeader{
Type: Connect,
Remaining: 42,
},
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Clean: true,
Keepalive: 30,
ClientIdentifier: "zen",
WillFlag: true,
WillTopic: "lwt",
WillPayload: []byte("notagain"),
WillQos: 1,
WillProperties: Properties{
WillDelayInterval: uint32(600),
},
},
Properties: Properties{
SessionExpiryInterval: uint32(120),
SessionExpiryIntervalFlag: true,
MaximumPacketSize: uint32(32000),
},
},
},
{
Case: TConnectUserPass,
Desc: "mqtt 3.1.1, username, password",
RawBytes: []byte{
Connect << 4, 28, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
0 | 1<<6 | 1<<7, // Packet Flags
0, 20, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
0, 5, // Username MSB+LSB
'm', 'o', 'c', 'h', 'i',
0, 4, // Password MSB+LSB
',', '.', '/', ';',
},
Packet: &Packet{
FixedHeader: FixedHeader{
Type: Connect,
Remaining: 28,
},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Clean: false,
Keepalive: 20,
ClientIdentifier: "zen",
UsernameFlag: true,
PasswordFlag: true,
Username: []byte("mochi"),
Password: []byte(",./;"),
},
},
},
{
Case: TConnectUserPassLWT,
Desc: "mqtt 3.1.1, username, password, lwt",
Primary: true,
RawBytes: []byte{
Connect << 4, 44, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
206, // Packet Flags
0, 120, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
0, 3, // Will Topic - MSB+LSB
'l', 'w', 't',
0, 9, // Will Message MSB+LSB
'n', 'o', 't', ' ', 'a', 'g', 'a', 'i', 'n',
0, 5, // Username MSB+LSB
'm', 'o', 'c', 'h', 'i',
0, 4, // Password MSB+LSB
',', '.', '/', ';',
},
Packet: &Packet{
FixedHeader: FixedHeader{
Type: Connect,
Remaining: 44,
},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Clean: true,
Keepalive: 120,
ClientIdentifier: "zen",
UsernameFlag: true,
PasswordFlag: true,
Username: []byte("mochi"),
Password: []byte(",./;"),
WillFlag: true,
WillTopic: "lwt",
WillPayload: []byte("not again"),
WillQos: 1,
},
},
},
{
Case: TConnectZeroByteUsername,
Desc: "username flag but 0 byte username",
Group: "decode",
RawBytes: []byte{
Connect << 4, 23, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
5, // Protocol Version
130, // Packet Flags
0, 30, // Keepalive
5, // length
17, 0, 0, 0, 120, // Session Expiry Interval (17)
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
0, 0, // Username MSB+LSB
},
Packet: &Packet{
FixedHeader: FixedHeader{
Type: Connect,
Remaining: 23,
},
ProtocolVersion: 5,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Clean: true,
Keepalive: 30,
ClientIdentifier: "zen",
Username: []byte{},
UsernameFlag: true,
},
Properties: Properties{
SessionExpiryInterval: uint32(120),
SessionExpiryIntervalFlag: true,
},
},
},
// Fail States
{
Case: TConnectMalProtocolName,
Desc: "malformed protocol name",
Group: "decode",
FailFirst: ErrMalformedProtocolName,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 7, // Protocol Name - MSB+LSB
'M', 'Q', 'I', 's', 'd', // Protocol Name
},
},
{
Case: TConnectMalProtocolVersion,
Desc: "malformed protocol version",
Group: "decode",
FailFirst: ErrMalformedProtocolVersion,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
},
},
{
Case: TConnectMalFlags,
Desc: "malformed flags",
Group: "decode",
FailFirst: ErrMalformedFlags,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
},
},
{
Case: TConnectMalKeepalive,
Desc: "malformed keepalive",
Group: "decode",
FailFirst: ErrMalformedKeepalive,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
0, // Flags
},
},
{
Case: TConnectMalClientID,
Desc: "malformed client id",
Group: "decode",
FailFirst: ErrClientIdentifierNotValid,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
0, // Flags
0, 20, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', // Client ID "zen"
},
},
{
Case: TConnectMalWillTopic,
Desc: "malformed will topic",
Group: "decode",
FailFirst: ErrMalformedWillTopic,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
14, // Flags
0, 20, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
0, 6, // Will Topic - MSB+LSB
'l',
},
},
{
Case: TConnectMalWillFlag,
Desc: "malformed will flag",
Group: "decode",
FailFirst: ErrMalformedWillPayload,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
14, // Flags
0, 20, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
0, 3, // Will Topic - MSB+LSB
'l', 'w', 't',
0, 9, // Will Message MSB+LSB
'n', 'o', 't', ' ', 'a',
},
},
{
Case: TConnectMalUsername,
Desc: "malformed username",
Group: "decode",
FailFirst: ErrMalformedUsername,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
206, // Flags
0, 20, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
0, 3, // Will Topic - MSB+LSB
'l', 'w', 't',
0, 9, // Will Message MSB+LSB
'n', 'o', 't', ' ', 'a', 'g', 'a', 'i', 'n',
0, 5, // Username MSB+LSB
'm', 'o', 'c',
},
},
{
Case: TConnectInvalidFlagNoUsername,
Desc: "username flag with no username bytes",
Group: "decode",
FailFirst: ErrProtocolViolationFlagNoUsername,
RawBytes: []byte{
Connect << 4, 17, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
5, // Protocol Version
130, // Flags
0, 20, // Keepalive
0,
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
},
},
{
Case: TConnectMalPassword,
Desc: "malformed password",
Group: "decode",
FailFirst: ErrMalformedPassword,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
206, // Flags
0, 20, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
0, 3, // Will Topic - MSB+LSB
'l', 'w', 't',
0, 9, // Will Message MSB+LSB
'n', 'o', 't', ' ', 'a', 'g', 'a', 'i', 'n',
0, 5, // Username MSB+LSB
'm', 'o', 'c', 'h', 'i',
0, 4, // Password MSB+LSB
',', '.',
},
},
{
Case: TConnectMalFixedHeader,
Desc: "malformed fixedheader oversize",
Group: "decode",
FailFirst: ErrMalformedProtocolName, // packet test doesn't test fixedheader oversize
RawBytes: []byte{
Connect << 4, 255, 255, 255, 255, 255, // Fixed header
},
},
{
Case: TConnectMalReservedBit,
Desc: "reserved bit not 0",
Group: "nodecode",
FailFirst: ErrProtocolViolation,
RawBytes: []byte{
Connect << 4, 15, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
1, // Packet Flags
0, 45, // Keepalive
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n',
},
},
{
Case: TConnectMalProperties,
Desc: "malformed properties",
Group: "decode",
FailFirst: ErrMalformedProperties,
RawBytes: []byte{
Connect << 4, 47, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
5, // Protocol Version
14, // Packet Flags
0, 30, // Keepalive
10,
},
Packet: &Packet{
ProtocolVersion: 5,
},
},
{
Case: TConnectMalWillProperties,
Desc: "malformed will properties",
Group: "decode",
FailFirst: ErrMalformedWillProperties,
RawBytes: []byte{
Connect << 4, 47, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
5, // Protocol Version
14, // Packet Flags
0, 30, // Keepalive
10, // length
17, 0, 0, 0, 120, // Session Expiry Interval (17)
39, 0, 0, 125, 0, // Maximum Packet Size (39)
0, 3, // Client ID - MSB+LSB
'z', 'e', 'n', // Client ID "zen"
5, // will properties length
},
Packet: &Packet{
ProtocolVersion: 5,
},
},
// Validation Tests
{
Case: TConnectInvalidProtocolName,
Desc: "invalid protocol name",
Group: "validate",
Expect: ErrProtocolViolationProtocolName,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
Connect: ConnectParams{
ProtocolName: []byte("stuff"),
},
},
},
{
Case: TConnectInvalidProtocolVersion,
Desc: "invalid protocol version",
Group: "validate",
Expect: ErrProtocolViolationProtocolVersion,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 2,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
},
},
},
{
Case: TConnectInvalidProtocolVersion2,
Desc: "invalid protocol version",
Group: "validate",
Expect: ErrProtocolViolationProtocolVersion,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 2,
Connect: ConnectParams{
ProtocolName: []byte("MQIsdp"),
},
},
},
{
Case: TConnectInvalidReservedBit,
Desc: "reserved bit not 0",
Group: "validate",
Expect: ErrProtocolViolationReservedBit,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
},
ReservedBit: 1,
},
},
{
Case: TConnectInvalidClientIDTooLong,
Desc: "client id too long",
Group: "validate",
Expect: ErrClientIdentifierNotValid,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
ClientIdentifier: func() string {
return string(make([]byte, 65536))
}(),
},
},
},
{
Case: TConnectInvalidUsernameNoFlag,
Desc: "has username but no flag",
Group: "validate",
Expect: ErrProtocolViolationUsernameNoFlag,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Username: []byte("username"),
},
},
},
{
Case: TConnectInvalidFlagNoPassword,
Desc: "has password flag but no password",
Group: "validate",
Expect: ErrProtocolViolationFlagNoPassword,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
PasswordFlag: true,
},
},
},
{
Case: TConnectInvalidPasswordNoFlag,
Desc: "has password flag but no password",
Group: "validate",
Expect: ErrProtocolViolationPasswordNoFlag,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
Password: []byte("password"),
},
},
},
{
Case: TConnectInvalidUsernameTooLong,
Desc: "username too long",
Group: "validate",
Expect: ErrProtocolViolationUsernameTooLong,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
UsernameFlag: true,
Username: func() []byte {
return make([]byte, 65536)
}(),
},
},
},
{
Case: TConnectInvalidPasswordTooLong,
Desc: "password too long",
Group: "validate",
Expect: ErrProtocolViolationPasswordTooLong,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
UsernameFlag: true,
Username: []byte{},
PasswordFlag: true,
Password: func() []byte {
return make([]byte, 65536)
}(),
},
},
},
{
Case: TConnectInvalidWillFlagNoPayload,
Desc: "will flag no payload",
Group: "validate",
Expect: ErrProtocolViolationWillFlagNoPayload,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
WillFlag: true,
},
},
},
{
Case: TConnectInvalidWillFlagQosOutOfRange,
Desc: "will flag no payload",
Group: "validate",
Expect: ErrProtocolViolationQosOutOfRange,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
WillFlag: true,
WillTopic: "a/b/c",
WillPayload: []byte{'b'},
WillQos: 4,
},
},
},
{
Case: TConnectInvalidWillSurplusRetain,
Desc: "no will flag surplus retain",
Group: "validate",
Expect: ErrProtocolViolationWillFlagSurplusRetain,
Packet: &Packet{
FixedHeader: FixedHeader{Type: Connect},
ProtocolVersion: 4,
Connect: ConnectParams{
ProtocolName: []byte("MQTT"),
WillRetain: true,
},
},
},
// Spec Tests
{
Case: TConnectSpecInvalidUTF8D800,
Desc: "invalid utf8 string (a) - code point U+D800",
Group: "decode",
FailFirst: ErrClientIdentifierNotValid,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
0, // Flags
0, 20, // Keepalive
0, 4, // Client ID - MSB+LSB
'e', 0xed, 0xa0, 0x80, // Client id bearing U+D800
},
},
{
Case: TConnectSpecInvalidUTF8DFFF,
Desc: "invalid utf8 string (b) - code point U+DFFF",
Group: "decode",
FailFirst: ErrClientIdentifierNotValid,
RawBytes: []byte{
Connect << 4, 0, // Fixed header
0, 4, // Protocol Name - MSB+LSB
'M', 'Q', 'T', 'T', // Protocol Name
4, // Protocol Version
0, // Flags
0, 20, // Keepalive
0, 4, // Client ID - MSB+LSB
'e', 0xed, 0xa3, 0xbf, // Client id bearing U+D8FF
},
},
{