-
Notifications
You must be signed in to change notification settings - Fork 4
/
request_test.go
1012 lines (907 loc) · 30 KB
/
request_test.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
package mqtt_test
import (
"context"
"encoding/hex"
"errors"
"io"
"net"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/pascaldekloe/mqtt"
"github.com/pascaldekloe/mqtt/mqtttest"
)
func TestPing(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, conn, "c000") // PINGREQ
sendPacketHex(t, conn, "d000") // PINGRESP
})
err := client.Ping(testTimeout)
if err != nil {
t.Errorf("got error %q [%T]", err, err)
}
<-brokerMockDone
}
func TestPingReqTimeout(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
var buf [1]byte
switch _, err := io.ReadFull(conn, buf[:]); {
case err != nil:
t.Error("broker read error:", err)
case buf[0] != 0xC0:
t.Errorf("want PINGREQ head 0xC0, got %#x", buf[0])
}
// leave partial read
})
err := client.Ping(testTimeout)
var e net.Error
if !errors.As(err, &e) || !e.Timeout() {
t.Errorf("got error %q [%T], want a Timeout net.Error", err, err)
}
<-brokerMockDone
}
func TestSubscribeMultiple(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, conn, hex.EncodeToString([]byte{
0x82, 19,
0x60, 0x00, // packet identifier
0, 5, 'u', '/', 'n', 'o', 'i',
2, // max QOS
0, 6, 'u', '/', 's', 'h', 'i', 'n',
2, // max QOS
}))
sendPacketHex(t, conn, "900460000102") // SUBACK
})
err := client.Subscribe(testTimeout, "u/noi", "u/shin")
if err != nil {
t.Errorf("got error %q [%T]", err, err)
}
<-brokerMockDone
}
func TestSubscribeReqTimeout(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
var buf [1]byte
switch _, err := io.ReadFull(conn, buf[:]); {
case err != nil:
t.Error("broker read error:", err)
case buf[0] != 0x82:
t.Errorf("want SUBSCRIBE head 0x82, got %#x", buf[0])
}
// leave partial read
})
err := client.Subscribe(testTimeout, "x")
var e net.Error
if !errors.As(err, &e) || !e.Timeout() {
t.Errorf("got error %q [%T], want a Timeout net.Error", err, err)
}
<-brokerMockDone
}
func TestUnsubscribeMultiple(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, conn, hex.EncodeToString([]byte{
0xa2, 17,
0x40, 0x00, // packet identifier
0, 5, 'u', '/', 'n', 'o', 'i',
0, 6, 'u', '/', 's', 'h', 'i', 'n',
}))
sendPacketHex(t, conn, "b0024000") // UNSUBACK
})
err := client.Unsubscribe(testTimeout, "u/noi", "u/shin")
if err != nil {
t.Errorf("got error %q [%T]", err, err)
}
<-brokerMockDone
}
func TestUnsubscribeReqTimeout(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
var buf [1]byte
switch _, err := io.ReadFull(conn, buf[:]); {
case err != nil:
t.Error("broker read error:", err)
case buf[0] != 0xa2:
t.Errorf("want UNSUBSCRIBE head 0xa2, got %#x", buf[0])
}
// leave partial read
})
err := client.Unsubscribe(testTimeout, "x")
var e net.Error
if !errors.As(err, &e) || !e.Timeout() {
t.Errorf("got error %q [%T], want a Timeout net.Error", err, err)
}
<-brokerMockDone
}
func TestPublish(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, conn, hex.EncodeToString([]byte{
0x30, 12,
0, 5, 'g', 'r', 'e', 'e', 't',
'h', 'e', 'l', 'l', 'o'}))
})
err := client.Publish(testTimeout, []byte("hello"), "greet")
if err != nil {
t.Errorf("got error %q [%T]", err, err)
}
<-brokerMockDone
}
func TestPublishReqTimeout(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
testRoutine(t, func() {
var buf [1]byte
switch _, err := io.ReadFull(conn, buf[:]); {
case err != nil:
t.Error("broker read error:", err)
case buf[0] != 0x30:
t.Errorf("want PUBLISH head 0x30, got %#x", buf[0])
}
// leave partial read
})
err := client.Publish(testTimeout, []byte{'x'}, "y")
var e net.Error
if !errors.As(err, &e) || !e.Timeout() {
t.Errorf("got error %q [%T], want a Timeout net.Error", err, err)
}
}
func TestPublishAtLeastOnce(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, conn, hex.EncodeToString([]byte{
0x32, 14,
0, 5, 'g', 'r', 'e', 'e', 't',
0x80, 0x00, // packet identifier
'h', 'e', 'l', 'l', 'o'}))
sendPacketHex(t, conn, "40028000") // PUBACK
})
exchange, err := client.PublishAtLeastOnce([]byte("hello"), "greet")
if err != nil {
t.Errorf("got error %q [%T]", err, err)
}
verifyExchange(t, testTimeout, exchange)
<-brokerMockDone
}
func TestPublishAtLeastOnceReqTimeout(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
var buf [1]byte
switch _, err := io.ReadFull(conn, buf[:]); {
case err != nil:
t.Error("broker read error:", err)
case buf[0] != 0x32:
t.Errorf("want PUBLISH head 0x32, got %#x", buf[0])
}
// leave partial read
})
select {
case <-client.Online():
break
case <-testTimeout:
t.Fatal("test timeout while awaiting client Online")
}
exchange, err := client.PublishAtLeastOnce([]byte{'x'}, "y")
if err != nil {
t.Fatalf("got error %q [%T]", err, err)
}
verifyExchangeTimeout(t, testTimeout, exchange)
<-brokerMockDone
}
// A Client must resend each PUBLISH which is pending PUBACK when the connection
// is reset (for whater reasons).
func TestPublishAtLeastOnceResend(t *testing.T) {
client, conns, testTimeout := newClientPipeN(t, 2, mqtttest.Transfer{Err: io.EOF})
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, conns[0], hex.EncodeToString([]byte{
0x32, 6,
0, 1, 'y',
0x80, 0x00, // packet identifier
'x'}))
if err := conns[0].Close(); err != nil {
t.Error("broker got error on first connection close:", err)
}
wantPacketHex(t, conns[1], "100c00044d515454040000000000") // CONNECT
sendPacketHex(t, conns[1], "20020000") // CONNACK
wantPacketHex(t, conns[1], hex.EncodeToString([]byte{
0x3a, 6, // with duplicate [DUP] flag
0, 1, 'y',
0x80, 0x00, // packet identifier
'x'}))
sendPacketHex(t, conns[1], "40028000") // PUBACK after all
})
exchange, err := client.PublishAtLeastOnce([]byte{'x'}, "y")
if err != nil {
t.Errorf("got error %q [%T]", err, err)
}
verifyExchange(t, testTimeout, exchange)
<-brokerMockDone
}
// A Client must send pending PUBLISH once reconnected and continue.
func TestPublishAtLeastOnceWhileDown(t *testing.T) {
t.Parallel()
clientConn0, brokerConn0 := net.Pipe()
clientConn1, brokerConn1 := net.Pipe()
// test expiry
testTimeout := make(chan struct{})
expire := time.AfterFunc(2*time.Second, func() {
t.Error("test timed out")
close(testTimeout)
clientConn0.Close()
clientConn1.Close()
})
defer expire.Stop()
client, err := mqtt.VolatileSession("test-client", &mqtt.Config{
PauseTimeout: time.Second / 4,
AtLeastOnceMax: 3,
Dialer: newDialerMock(t, 50*time.Millisecond, clientConn0, clientConn1),
})
if err != nil {
t.Fatal("VolatileSession error:", err)
}
// fail first connection with timeout
brokerConn0.SetDeadline(time.Now().Add(20 * time.Millisecond))
message, topic, err := client.ReadSlices()
var ne net.Error
if err == nil || !errors.As(err, &ne) || !ne.Timeout() {
t.Fatalf("got message %q, topic %q, error %q, want first connection to fail on a timeout error",
message, topic, err)
}
// client is down
// publish should enqueue
exchange, err := client.PublishAtLeastOnce([]byte("x"), "y")
if err != nil {
t.Fatal("PublishAtLeastOnce error:", err)
}
select {
case err, ok := <-exchange:
if !ok {
t.Fatal("exchange completed without connection")
}
if !errors.Is(err, mqtt.ErrDown) {
t.Fatalf("exchange got error %q, want a mqtt.ErrDown", err)
}
case <-testTimeout:
t.Fatal("test timeout while awaiting publish exchange without connection")
}
// schedule second publish for after reconnect
secondPublishDone := testRoutine(t, func() {
time.Sleep(time.Second / 2)
exchange, err := client.PublishAtLeastOnce([]byte("a"), "b")
if err != nil {
t.Fatal("PublishAtLeastOnce error:", err)
}
select {
case err, ok := <-exchange:
if ok {
t.Fatalf("second exchange got error %q", err)
}
case <-testTimeout:
t.Fatal("test timeout while awaiting second publish exchange")
}
})
// mock broker reconnect, delayed receive and receive continue
testRoutine(t, func() {
// CONNECT
wantPacketHex(t, brokerConn1, "101700044d51545404000000000b746573742d636c69656e74")
sendPacketHex(t, brokerConn1, "20020000") // CONNACK
wantPacketHex(t, brokerConn1, "3206000179800078") // PUBLISH (enqueued)
sendPacketHex(t, brokerConn1, "40028000") // PUBACK
wantPacketHex(t, brokerConn1, "3206000162800161") // PUBLISH (second)
sendPacketHex(t, brokerConn1, "40028001") // PUBACK
brokerConn1.Close() // causes EOF next
})
message, topic, err = client.ReadSlices()
if err == nil || !errors.Is(err, io.EOF) {
t.Fatalf("got message %q, topic %q, error %q, want second connection to EOF",
message, topic, err)
}
// first publish should recover and complete
select {
case err, ok := <-exchange:
if ok {
t.Errorf("first exchange got error %q, want channel close", err)
}
case <-testTimeout:
t.Fatal("test timeout while awaiting first publish exchange completion")
}
select {
case <-secondPublishDone:
break // OK
case <-testTimeout:
t.Fatal("test timeout while awaiting second publish routine")
}
}
// TestPublishAtLeastOnceRestart sends three messages as QOS 1 PUBLISH. The
// broker simulation will do all of the following:
//
// 1. Receive the first message.
// 2. Receive the second message.
// 3. Acknowledge the first mesage.
// 4. Partially receive the third message.
//
// Then the session is continued with a new client. It must automatically send
// the second and the third message again.
func TestPublishAtLeastOnceRestart(t *testing.T) {
t.Parallel()
dir := t.TempDir() // persistence location
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
testTimeout := ctx.Done()
const publish1 = "3206000178800031" // '1' (0x31) @ 'x' (0x78)
const publish2 = "3206000178800132" // '2' (0x32) @ 'x' (0x78)
const publish3 = "3206000178800233" // '3' (0x33) @ 'x' (0x78)
const publish2Dupe = "3a06000178800132" // with duplicate [DUP] flag
const publish3Dupe = "3a06000178800233" // with duplicate [DUP] flag
clientConn, brokerConn := net.Pipe()
client, err := mqtt.InitSession("test-client", mqtt.FileSystem(dir), &mqtt.Config{
PauseTimeout: time.Second / 4,
AtLeastOnceMax: 3,
Dialer: newDialerMock(t, 0, clientConn),
})
if err != nil {
t.Fatal("InitSession error:", err)
}
verifyClient(t, client)
brokerConn.SetDeadline(time.Now().Add(800 * time.Millisecond))
wantPacketHex(t, brokerConn, "101700044d51545404000000000b746573742d636c69656e74")
sendPacketHex(t, brokerConn, "20020000") // CONNACK
<-client.Online()
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, brokerConn, publish1)
wantPacketHex(t, brokerConn, publish2)
sendPacketHex(t, brokerConn, "40028000") // PUBACK № 1
var buf [1]byte
switch _, err := io.ReadFull(brokerConn, buf[:]); {
case err != nil:
t.Error("broker read error:", err)
case buf[0] != 0x32:
t.Errorf("want PUBLISH head 0x32, got %#x", buf[0])
}
// leave № 3 partial read
err := client.Close()
if err != nil {
t.Error("Close error:", err)
}
})
exchange1, err := client.PublishAtLeastOnce([]byte{'1'}, "x")
if err != nil {
t.Errorf("publish № 1 got error %q [%T]", err, err)
}
exchange2, err := client.PublishAtLeastOnce([]byte{'2'}, "x")
if err != nil {
t.Errorf("publish № 2 got error %q [%T]", err, err)
}
exchange3, err := client.PublishAtLeastOnce([]byte{'3'}, "x")
if err != nil {
t.Errorf("publish № 3 got error %q [%T]", err, err)
}
verifyExchange(t, testTimeout, exchange1)
verifyExchangeError(t, testTimeout, exchange2, mqtt.ErrClosed)
verifyExchangeError(t, testTimeout, exchange3, io.ErrClosedPipe)
<-brokerMockDone
// verify persistence; seals compatibility
publish2File := filepath.Join(dir, "08001") // named after it's packet ID
publish3File := filepath.Join(dir, "08002")
if bytes, err := os.ReadFile(publish2File); err != nil {
t.Error("publish № 2 file:", err)
} else {
got := hex.EncodeToString(bytes)
// packet + sequence number + checksum:
const want = publish2 + "0300000000000000" + "c0dcafa6"
if got != want {
t.Errorf("publish № 2 file contains 0x%s, want 0x%s", got, want)
}
}
if bytes, err := os.ReadFile(publish3File); err != nil {
t.Error("publish № 3 file:", err)
} else {
got := hex.EncodeToString(bytes)
// packet + sequence number + checksum:
const want = publish3 + "04000000000000000" + "5a75959"
if got != want {
t.Errorf("publish № 3 file contains 0x%s, want 0x%s", got, want)
}
}
if t.Failed() {
return
}
t.Log("continue with another Client")
clientConn, brokerConn = net.Pipe()
client, warn, err := mqtt.AdoptSession(mqtt.FileSystem(dir), &mqtt.Config{
PauseTimeout: time.Second / 4,
AtLeastOnceMax: 3,
Dialer: newDialerMock(t, 0, clientConn),
})
if err != nil {
t.Fatal("AdoptSession error:", err)
}
for _, err := range warn {
t.Error("AdoptSession warning:", err)
}
verifyClient(t, client)
brokerConn.SetDeadline(time.Now().Add(800 * time.Millisecond))
wantPacketHex(t, brokerConn, "101700044d51545404000000000b746573742d636c69656e74")
sendPacketHex(t, brokerConn, "20020000") // CONNACK
wantPacketHex(t, brokerConn, publish2Dupe)
wantPacketHex(t, brokerConn, publish3Dupe)
sendPacketHex(t, brokerConn, "40028001") // PUBACK № 2
sendPacketHex(t, brokerConn, "40028002") // PUBACK № 3
// await PUBACK appliance
time.Sleep(100 * time.Millisecond)
if _, err := os.Stat(publish2File); err == nil {
t.Error("publish № 2 file exits after PUBACK", err)
} else if !os.IsNotExist(err) {
t.Error("publish № 2 file error:", err)
}
if _, err := os.Stat(publish3File); err == nil {
t.Error("publish № 3 file exits after PUBACK", err)
} else if !os.IsNotExist(err) {
t.Error("publish № 3 file error:", err)
}
}
func TestPublishExactlyOnce(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, conn, hex.EncodeToString([]byte{
0x34, 14,
0, 5, 'g', 'r', 'e', 'e', 't',
0xc0, 0x00, // packet identifier
'h', 'e', 'l', 'l', 'o'}))
sendPacketHex(t, conn, "5002c000") // PUBREC
wantPacketHex(t, conn, "6202c000") // PUBREL
sendPacketHex(t, conn, "7002c000") // PUBCOMP
})
exchange, err := client.PublishExactlyOnce([]byte("hello"), "greet")
if err != nil {
t.Errorf("got error %q [%T]", err, err)
}
verifyExchange(t, testTimeout, exchange)
<-brokerMockDone
}
func TestPublishExactlyOnceReqTimeout(t *testing.T) {
client, conn, testTimeout := newClientPipe(t)
brokerMockDone := testRoutine(t, func() {
var buf [1]byte
switch _, err := io.ReadFull(conn, buf[:]); {
case err != nil:
t.Error("broker read error:", err)
case buf[0] != 0x34:
t.Errorf("want PUBLISH head 0x34, got %#x", buf[0])
}
// leave partial read
})
select {
case <-client.Online():
break
case <-testTimeout:
t.Fatal("test timeout while awaiting Online")
}
exchange, err := client.PublishExactlyOnce([]byte{'x'}, "y")
if err != nil {
t.Fatalf("Publish error %q [%T]", err, err)
}
verifyExchangeTimeout(t, testTimeout, exchange)
<-brokerMockDone
}
// TestPublishExactlyOnceRestart sends five messages as QOS 2 PUBLISH. The
// broker simulation will do all of the following:
//
// - Complete PUBLISH № 1.
// - Halt at PUBCOMP № 2 (not send).
// - Halt at PUBREL № 3 (not receive).
// - Halt at PUBREC № 4 (not send)
// - Halt at PUBLISH № 5 (not receive).
//
// … it does so with the following steps.
//
// - Receive PUBLISH № 1.
// - Receive PUBLISH № 2.
// - Send PUBREC № 1.
// - Send PUBREC № 2.
// - Receive PUBREL № 1.
// - Receive PUBREL № 2.
// - Send PUBCOMP № 1.
// - <little sleep>
// - Receive PUBLISH № 3.
// - Receive PUBLISH № 4.
// - Send PUBREC № 3.
//
// Then the session is continued with a new client. It must automatically
// PUBLISH message № 4 and 5 again, and it must PUBREL message № 2 and 3.
func TestPublishExactlyOnceRestart(t *testing.T) {
t.Parallel()
dir := t.TempDir() // persistence location
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
testTimeout := ctx.Done()
const publish1 = "3406000178c00031" // '1' (0x31) @ 'x' (0x78)
const publish2 = "3406000178c00132" // '2' (0x32) @ 'x' (0x78)
const publish3 = "3406000178c00233" // '3' (0x33) @ 'x' (0x78)
const publish4 = "3406000178c00334" // '4' (0x34) @ 'x' (0x78)
const publish5 = "3406000178c00435" // '5' (0x35) @ 'x' (0x78)
const publish4Dupe = "3c06000178c00334" // with duplicate [DUP] flag
const publish5Dupe = "3c06000178c00435" // with duplicate [DUP] flag
clientConn, brokerConn := net.Pipe()
client, err := mqtt.InitSession("test-client", mqtt.FileSystem(dir), &mqtt.Config{
PauseTimeout: time.Second / 4,
ExactlyOnceMax: 5,
Dialer: newDialerMock(t, 0, clientConn),
})
if err != nil {
t.Fatal("InitSession error:", err)
}
verifyClient(t, client, mqtttest.Transfer{Err: io.ErrClosedPipe})
brokerConn.SetDeadline(time.Now().Add(time.Second))
wantPacketHex(t, brokerConn, "101700044d51545404000000000b746573742d636c69656e74")
sendPacketHex(t, brokerConn, "20020000") // CONNACK
brokerMockDone := testRoutine(t, func() {
wantPacketHex(t, brokerConn, publish1)
wantPacketHex(t, brokerConn, publish2)
sendPacketHex(t, brokerConn, "5002c000") // PUBREC № 1
wantPacketHex(t, brokerConn, "6202c000") // PUBREL № 1
sendPacketHex(t, brokerConn, "5002c001") // PUBREC № 2
wantPacketHex(t, brokerConn, "6202c001") // PUBREL № 2
sendPacketHex(t, brokerConn, "7002c000") // PUBCOMP № 1
wantPacketHex(t, brokerConn, publish3)
wantPacketHex(t, brokerConn, publish4)
sendPacketHex(t, brokerConn, "5002c002") // PUBREC № 3
// PUBLISH № 5 before client close
time.Sleep(200 * time.Millisecond)
err := client.Close()
if err != nil {
t.Error("Close error:", err)
}
})
<-client.Online()
exchange1, err := client.PublishExactlyOnce([]byte{'1'}, "x")
if err != nil {
t.Errorf("publish № 1 got error %q [%T]", err, err)
}
exchange2, err := client.PublishExactlyOnce([]byte{'2'}, "x")
if err != nil {
t.Errorf("publish № 2 got error %q [%T]", err, err)
}
time.Sleep(50 * time.Millisecond)
exchange3, err := client.PublishExactlyOnce([]byte{'3'}, "x")
if err != nil {
t.Errorf("publish № 3 got error %q [%T]", err, err)
}
time.Sleep(50 * time.Millisecond)
exchange4, err := client.PublishExactlyOnce([]byte{'4'}, "x")
if err != nil {
t.Errorf("publish № 4 got error %q [%T]", err, err)
}
time.Sleep(50 * time.Millisecond)
exchange5, err := client.PublishExactlyOnce([]byte{'5'}, "x")
if err != nil {
t.Errorf("publish № 5 got error %q [%T]", err, err)
}
<-brokerMockDone
verifyExchange(t, testTimeout, exchange1)
verifyExchangeError(t, testTimeout, exchange2, mqtt.ErrClosed)
verifyExchangeError(t, testTimeout, exchange3, mqtt.ErrClosed)
verifyExchangeError(t, testTimeout, exchange4, mqtt.ErrClosed)
verifyExchangeError(t, testTimeout, exchange5, mqtt.ErrClosed)
// verify persistence; seals compatibility
publish1File := filepath.Join(dir, "0c000") // named after it's packet ID
publish2File := filepath.Join(dir, "0c001")
publish3File := filepath.Join(dir, "0c002")
publish4File := filepath.Join(dir, "0c003")
publish5File := filepath.Join(dir, "0c004")
if _, err := os.Stat(publish1File); err == nil {
t.Error("publish № 1 file still exits after PUBCOMP", err)
} else if !os.IsNotExist(err) {
t.Error("publish № 1 file error:", err)
}
if bytes, err := os.ReadFile(publish2File); err != nil {
t.Error("publish № 2 file:", err)
} else {
got := hex.EncodeToString(bytes)
// packet + sequence number + checksum:
const want = "6202c001" + "0500000000000000" + "74d798bf" // PUBREL № 2
if got != want {
t.Errorf("publish № 2 file contains 0x%s, want 0x%s", got, want)
}
}
if bytes, err := os.ReadFile(publish3File); err != nil {
t.Error("publish № 3 file:", err)
} else {
got := hex.EncodeToString(bytes)
// packet + sequence number + checksum:
const want = "6202c002" + "0800000000000000" + "6bb2f52f" // PUBREL № 3
if got != want {
t.Errorf("publish № 3 file contains 0x%s, want 0x%s", got, want)
}
}
if bytes, err := os.ReadFile(publish4File); err != nil {
t.Error("publish № 4 file:", err)
} else {
got := hex.EncodeToString(bytes)
// packet + sequence number + checksum:
const want = publish4 + "0700000000000000" + "2d4cbd7c"
if got != want {
t.Errorf("publish № 4 file contains 0x%s, want 0x%s", got, want)
}
}
if bytes, err := os.ReadFile(publish5File); err != nil {
t.Error("publish № 5 file:", err)
} else {
got := hex.EncodeToString(bytes)
// packet + sequence number + checksum:
const want = publish5 + "0900000000000000" + "1d6d8b0e"
if got != want {
t.Errorf("publish № 5 file contains 0x%s, want 0x%s", got, want)
}
}
if t.Failed() {
return
}
t.Log("continue with another Client")
clientConn, brokerConn = net.Pipe()
client, warn, err := mqtt.AdoptSession(mqtt.FileSystem(dir), &mqtt.Config{
PauseTimeout: time.Second / 4,
ExactlyOnceMax: 4,
Dialer: newDialerMock(t, 0, clientConn),
})
for _, err := range warn {
t.Error("AdoptSession warning:", err)
}
if err != nil {
t.Fatal("AdoptSession error:", err)
}
verifyClient(t, client)
brokerConn.SetDeadline(time.Now().Add(time.Second))
wantPacketHex(t, brokerConn, "101700044d51545404000000000b746573742d636c69656e74")
sendPacketHex(t, brokerConn, "20020000") // CONNACK
wantPacketHex(t, brokerConn, "6202c001") // PUBREL № 2
wantPacketHex(t, brokerConn, "6202c002") // PUBREL № 3
wantPacketHex(t, brokerConn, publish4Dupe)
wantPacketHex(t, brokerConn, publish5Dupe)
go func() {
sendPacketHex(t, brokerConn, "5002c003") // PUBREC № 4
sendPacketHex(t, brokerConn, "5002c004") // PUBREC № 5
}()
wantPacketHex(t, brokerConn, "6202c003") // PUBREL № 4
wantPacketHex(t, brokerConn, "6202c004") // PUBREL № 5
sendPacketHex(t, brokerConn, "7002c001") // PUBCOMP № 2
sendPacketHex(t, brokerConn, "7002c002") // PUBCOMP № 3
sendPacketHex(t, brokerConn, "7002c003") // PUBCOMP № 4
sendPacketHex(t, brokerConn, "7002c004") // PUBCOMP № 5
// await PUBCOMP appliance
time.Sleep(100 * time.Millisecond)
if _, err := os.Stat(publish2File); err == nil {
t.Error("publish № 2 file still exits after PUBCOMP", err)
} else if !os.IsNotExist(err) {
t.Error("publish № 2 file error:", err)
}
if _, err := os.Stat(publish3File); err == nil {
t.Error("publish № 3 file still exits after PUBCOMP", err)
} else if !os.IsNotExist(err) {
t.Error("publish № 3 file error:", err)
}
if _, err := os.Stat(publish4File); err == nil {
t.Error("publish № 4 file still exits after PUBCOMP", err)
} else if !os.IsNotExist(err) {
t.Error("publish № 4 file error:", err)
}
if _, err := os.Stat(publish5File); err == nil {
t.Error("publish № 5 file still exits after PUBCOMP", err)
} else if !os.IsNotExist(err) {
t.Error("publish № 5 file error:", err)
}
}
// Brokers may resend a PUBREL even after receiving PUBCOMP (in case the serice
// crashed for example).
func TestPUBRELRetry(t *testing.T) {
_, conn, _ := newClientPipe(t)
sendPacketHex(t, conn, "62021234") // PUBREL
wantPacketHex(t, conn, "70021234") // PUBCOMP
}
func TestAbandon(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
client, conn, _ := newClientPipe(t)
pingDone := testRoutine(t, func() {
err := client.Ping(ctx.Done())
if !errors.Is(err, mqtt.ErrAbandoned) {
t.Errorf("ping got error %q [%T], want an mqtt.ErrAbandoned", err, err)
}
})
wantPacketHex(t, conn, "c000") // PINGREQ
subscribeDone := testRoutine(t, func() {
err := client.Subscribe(ctx.Done(), "x")
if !errors.Is(err, mqtt.ErrAbandoned) {
t.Errorf("subscribe got error %q [%T], want an mqtt.ErrAbandoned", err, err)
}
})
wantPacketHex(t, conn, "8206600000017802") // SUBSCRIBE
unsubscribeDone := testRoutine(t, func() {
err := client.Unsubscribe(ctx.Done(), "x")
if !errors.Is(err, mqtt.ErrAbandoned) {
t.Errorf("unsubscribe got error %q [%T], want an mqtt.ErrAbandoned", err, err)
}
})
wantPacketHex(t, conn, "a2054001000178") // UNSUBSCRIBE
cancel()
<-pingDone
<-subscribeDone
<-unsubscribeDone
}
func TestBreak(t *testing.T) {
client, conn, testTimeout := newClientPipe(t, mqtttest.Transfer{Err: io.EOF})
pingDone := testRoutine(t, func() {
err := client.Ping(testTimeout)
if !errors.Is(err, mqtt.ErrBreak) {
t.Errorf("ping got error %q [%T], want an mqtt.ErrBreak", err, err)
}
})
wantPacketHex(t, conn, "c000") // PINGREQ
subscribeDone := testRoutine(t, func() {
err := client.Subscribe(testTimeout, "x")
if !errors.Is(err, mqtt.ErrBreak) {
t.Errorf("subscribe got error %q [%T], want an mqtt.ErrBreak", err, err)
}
})
wantPacketHex(t, conn, "8206600000017802") // SUBSCRIBE
unsubscribeDone := testRoutine(t, func() {
err := client.Unsubscribe(testTimeout, "x")
if !errors.Is(err, mqtt.ErrBreak) {
t.Errorf("unsubscribe got error %q [%T], want an mqtt.ErrBreak", err, err)
}
})
wantPacketHex(t, conn, "a2054001000178") // UNSUBSCRIBE
if err := conn.Close(); err != nil {
t.Error("broker mock got error on pipe close:", err)
}
<-pingDone
<-subscribeDone
<-unsubscribeDone
}
func TestDeny(t *testing.T) {
// no invocation to the client allowed
client, _, testTimeout := newClientPipe(t)
// UTF-8 validation
err := client.PublishRetained(testTimeout, nil, "topic with \xED\xA0\x80 not allowed")
if !mqtt.IsDeny(err) {
t.Errorf("publish with U+D800 in topic got error %q [%T], want an mqtt.IsDeny", err, err)
}
_, err = client.PublishAtLeastOnceRetained(nil, "topic with \xED\xA0\x81 not allowed")
if !mqtt.IsDeny(err) {
t.Errorf("publish with U+D801 in topic got error %q [%T], want an mqtt.IsDeny", err, err)
}
_, err = client.PublishExactlyOnceRetained(nil, "topic with \xED\xBF\xBF not allowed")
if !mqtt.IsDeny(err) {
t.Errorf("publish with U+DFFF in topic got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.SubscribeLimitAtMostOnce(nil, "null char \x00 not allowed")
if !mqtt.IsDeny(err) {
t.Errorf("subscribe with null character got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.SubscribeLimitAtLeastOnce(nil, "char \x80 breaks UTF-8")
if !mqtt.IsDeny(err) {
t.Errorf("subscribe with broken UTF-8 got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.Subscribe(testTimeout)
if !mqtt.IsDeny(err) {
t.Errorf("subscribe with nothing got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.Unsubscribe(testTimeout)
if !mqtt.IsDeny(err) {
t.Errorf("unsubscribe with nothing got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.Subscribe(testTimeout, "")
if !mqtt.IsDeny(err) {
t.Errorf("subscribe with zero topic got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.Unsubscribe(testTimeout, "")
if !mqtt.IsDeny(err) {
t.Errorf("unsubscribe with zero topic got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.Publish(testTimeout, nil, "")
if !mqtt.IsDeny(err) {
t.Errorf("publish with zero topic got error %q [%T], want an mqtt.IsDeny", err, err)
}
// size limits
tooBig := strings.Repeat("A", 1<<16)
err = client.Unsubscribe(testTimeout, tooBig)
if !mqtt.IsDeny(err) {
t.Errorf("unsubscribe with 64 KiB filter got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.Publish(testTimeout, make([]byte, 256*1024*1024), "")
if !mqtt.IsDeny(err) {
t.Errorf("publish with 256 MiB got error %q [%T], want an mqtt.IsDeny", err, err)
}
filtersTooBig := make([]string, 256*1024)
KiB := strings.Repeat("A", 1024)
for i := range filtersTooBig {
filtersTooBig[i] = KiB
}
err = client.Subscribe(testTimeout, filtersTooBig...)
if !mqtt.IsDeny(err) {
t.Errorf("subscribe with 256 MiB topic filters got error %q [%T], want an mqtt.IsDeny", err, err)
}
err = client.Unsubscribe(testTimeout, filtersTooBig...)
if !mqtt.IsDeny(err) {
t.Errorf("unsubscribe with 256 MiB topic filters got error %q [%T], want an mqtt.IsDeny", err, err)
}
}
func verifyExchange(t *testing.T, testTimeout <-chan struct{}, exchange <-chan error) {
t.Helper()
var errDownN int
for {
select {
case <-testTimeout:
t.Fatal("test timeout while awaiting exchange")
case err, ok := <-exchange:
if !ok {
return
}
if !errors.Is(err, mqtt.ErrDown) {
t.Errorf("exchange error %q [%T]", err, err)
return
}
errDownN++
if errDownN > 1 {
t.Fatal("exchange ErrDown duplicate")
}
t.Log("exchange ErrDown permitted")
}
}
}
func verifyExchangeTimeout(t *testing.T, testTimeout <-chan struct{}, exchange <-chan error) {
t.Helper()
var errDownN int
for {
select {
case <-testTimeout:
t.Fatal("test timeout while awaiting exchange timeout error")
case err, ok := <-exchange:
if !ok {
t.Errorf("exchange complete, want timeout error")
return
}
if !errors.Is(err, mqtt.ErrDown) {
var e net.Error
if !errors.As(err, &e) || !e.Timeout() {
t.Errorf("exchange error %q [%T], want a timeout error", err, err)
}
return
}
errDownN++
if errDownN > 1 {
t.Fatal("exchange ErrDown duplicate")
}
t.Log("exchange ErrDown permitted")
}
}
}
func verifyExchangeError(t *testing.T, testTimeout <-chan struct{}, exchange <-chan error, want error) {
t.Helper()
var errDownN int
for {
select {
case <-testTimeout:
t.Fatal("test timeout while awaiting exchange error")
case err, ok := <-exchange:
if !ok {
t.Errorf("exchange complete, want error %q", want)
return
}
if !errors.Is(err, mqtt.ErrDown) {
if !errors.Is(err, want) {
t.Errorf("exchange error %q [%T], want ErrDown", err, err)