-
Notifications
You must be signed in to change notification settings - Fork 219
/
server_test.go
1516 lines (1274 loc) · 39 KB
/
server_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 smtp_test
import (
"bufio"
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"log"
"net"
"strings"
"sync"
"testing"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
)
type message struct {
From string
To []string
RcptOpts []*smtp.RcptOptions
Data []byte
Opts *smtp.MailOptions
}
type backend struct {
authDisabled bool
messages []*message
anonmsgs []*message
implementLMTPData bool
lmtpStatus []struct {
addr string
err error
}
lmtpStatusSync chan struct{}
// Errors returned by Data method.
dataErrors chan error
// Error that will be returned by Data method.
dataErr error
// Read N bytes of message before returning dataErr.
dataErrOffset int64
panicOnMail bool
userErr error
}
func (be *backend) NewSession(_ *smtp.Conn) (smtp.Session, error) {
if be.implementLMTPData {
return &lmtpSession{&session{backend: be, anonymous: true}}, nil
}
return &session{backend: be, anonymous: true}, nil
}
type lmtpSession struct {
*session
}
type session struct {
backend *backend
anonymous bool
msg *message
}
var _ smtp.AuthSession = (*session)(nil)
func (s *session) AuthMechanisms() []string {
if s.backend.authDisabled {
return nil
}
return []string{sasl.Plain}
}
func (s *session) Auth(mech string) (sasl.Server, error) {
if s.backend.authDisabled {
return nil, smtp.ErrAuthUnsupported
}
return sasl.NewPlainServer(func(identity, username, password string) error {
if identity != "" && identity != username {
return errors.New("Invalid identity")
}
if username != "username" || password != "password" {
return errors.New("Invalid username or password")
}
s.anonymous = false
return nil
}), nil
}
func (s *session) Reset() {
s.msg = &message{}
}
func (s *session) Logout() error {
return nil
}
func (s *session) Mail(from string, opts *smtp.MailOptions) error {
if s.backend.userErr != nil {
return s.backend.userErr
}
if s.backend.panicOnMail {
panic("Everything is on fire!")
}
s.Reset()
s.msg.From = from
s.msg.Opts = opts
return nil
}
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
s.msg.To = append(s.msg.To, to)
s.msg.RcptOpts = append(s.msg.RcptOpts, opts)
return nil
}
func (s *session) Data(r io.Reader) error {
if s.backend.dataErr != nil {
if s.backend.dataErrOffset != 0 {
io.CopyN(ioutil.Discard, r, s.backend.dataErrOffset)
}
err := s.backend.dataErr
if s.backend.dataErrors != nil {
s.backend.dataErrors <- err
}
return err
}
if b, err := ioutil.ReadAll(r); err != nil {
if s.backend.dataErrors != nil {
s.backend.dataErrors <- err
}
return err
} else {
s.msg.Data = b
if s.anonymous {
s.backend.anonmsgs = append(s.backend.anonmsgs, s.msg)
} else {
s.backend.messages = append(s.backend.messages, s.msg)
}
if s.backend.dataErrors != nil {
s.backend.dataErrors <- nil
}
}
return nil
}
func (s *session) LMTPData(r io.Reader, collector smtp.StatusCollector) error {
if err := s.Data(r); err != nil {
return err
}
for _, val := range s.backend.lmtpStatus {
collector.SetStatus(val.addr, val.err)
if s.backend.lmtpStatusSync != nil {
s.backend.lmtpStatusSync <- struct{}{}
}
}
return nil
}
type failingListener struct {
c chan error
closed bool
mu sync.Mutex
}
func newFailingListener() *failingListener {
return &failingListener{c: make(chan error)}
}
func (l *failingListener) Send(err error) {
l.mu.Lock()
defer l.mu.Unlock()
if !l.closed {
l.c <- err
}
}
func (l *failingListener) Accept() (net.Conn, error) {
return nil, <-l.c
}
func (l *failingListener) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
if !l.closed {
close(l.c)
l.closed = true
}
return nil
}
func (l *failingListener) Addr() net.Addr {
return &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 12345,
}
}
type mockError struct {
msg string
temporary bool
}
func newMockError(msg string, temporary bool) *mockError {
return &mockError{
msg: msg,
temporary: temporary,
}
}
func (m *mockError) Error() string { return m.msg }
func (m *mockError) String() string { return m.msg }
func (m *mockError) Timeout() bool { return false }
func (m *mockError) Temporary() bool { return m.temporary }
type serverConfigureFunc func(*smtp.Server)
var (
authDisabled = func(s *smtp.Server) {
s.Backend.(*backend).authDisabled = true
}
)
func testServer(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
be = new(backend)
s = smtp.NewServer(be)
s.Domain = "localhost"
s.AllowInsecureAuth = true
for _, f := range fn {
f(s)
}
go s.Serve(l)
c, err = net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
scanner = bufio.NewScanner(c)
return
}
func testServerGreeted(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
be, s, c, scanner = testServer(t, fn...)
scanner.Scan()
if scanner.Text() != "220 localhost ESMTP Service Ready" {
t.Fatal("Invalid greeting:", scanner.Text())
}
return
}
func testServerEhlo(t *testing.T, fn ...serverConfigureFunc) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner, caps map[string]bool) {
be, s, c, scanner = testServerGreeted(t, fn...)
io.WriteString(c, "EHLO localhost\r\n")
scanner.Scan()
if scanner.Text() != "250-Hello localhost" {
t.Fatal("Invalid EHLO response:", scanner.Text())
}
expectedCaps := []string{"PIPELINING", "8BITMIME"}
caps = make(map[string]bool)
for scanner.Scan() {
s := scanner.Text()
if strings.HasPrefix(s, "250 ") {
caps[strings.TrimPrefix(s, "250 ")] = true
break
} else {
if !strings.HasPrefix(s, "250-") {
t.Fatal("Invalid capability response:", s)
}
caps[strings.TrimPrefix(s, "250-")] = true
}
}
for _, cap := range expectedCaps {
if !caps[cap] {
t.Fatal("Missing capability:", cap)
}
}
return
}
func TestServerAcceptErrorHandling(t *testing.T) {
errorLog := bytes.NewBuffer(nil)
be := new(backend)
s := smtp.NewServer(be)
s.Domain = "localhost"
s.AllowInsecureAuth = true
s.ErrorLog = log.New(errorLog, "", 0)
l := newFailingListener()
done := make(chan error, 1)
go func() {
done <- s.Serve(l)
l.Close()
}()
temporaryError := newMockError("temporary mock error", true)
l.Send(temporaryError)
permanentError := newMockError("permanent mock error", false)
l.Send(permanentError)
s.Close()
serveError := <-done
if serveError == nil {
t.Fatal("Serve had exited without an expected error")
} else if serveError != permanentError {
t.Fatal("Unexpected error:", serveError)
}
if !strings.Contains(errorLog.String(), temporaryError.String()) {
t.Fatal("Missing temporary error in log output:", errorLog.String())
}
}
func TestServer_helo(t *testing.T) {
_, s, c, scanner := testServerGreeted(t)
defer s.Close()
io.WriteString(c, "HELO localhost\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid HELO response:", scanner.Text())
}
}
func testServerAuthenticated(t *testing.T) (be *backend, s *smtp.Server, c net.Conn, scanner *bufio.Scanner) {
be, s, c, scanner, caps := testServerEhlo(t)
if _, ok := caps["AUTH PLAIN"]; !ok {
t.Fatal("AUTH PLAIN capability is missing when auth is enabled")
}
io.WriteString(c, "AUTH PLAIN\r\n")
scanner.Scan()
if scanner.Text() != "334 " {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
io.WriteString(c, "AHVzZXJuYW1lAHBhc3N3b3Jk\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "235 ") {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
return
}
func TestServerAuthTwice(t *testing.T) {
_, _, c, scanner, caps := testServerEhlo(t)
if _, ok := caps["AUTH PLAIN"]; !ok {
t.Fatal("AUTH PLAIN capability is missing when auth is enabled")
}
io.WriteString(c, "AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "235 ") {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
io.WriteString(c, "AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "503 ") {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
io.WriteString(c, "RSET\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
io.WriteString(c, "AUTH PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "503 ") {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
}
func TestServerCancelSASL(t *testing.T) {
_, _, c, scanner, caps := testServerEhlo(t)
if _, ok := caps["AUTH PLAIN"]; !ok {
t.Fatal("AUTH PLAIN capability is missing when auth is enabled")
}
io.WriteString(c, "AUTH PLAIN\r\n")
scanner.Scan()
if scanner.Text() != "334 " {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
io.WriteString(c, "*\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "501 ") {
t.Fatal("Invalid AUTH response:", scanner.Text())
}
}
func TestServerEmptyFrom1(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServerEmptyFrom2(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServerPanicRecover(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
s.Backend.(*backend).panicOnMail = true
// Don't log panic in tests to not confuse people who run 'go test'.
s.ErrorLog = log.New(ioutil.Discard, "", 0)
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "421 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServerSMTPUTF8(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
s.EnableSMTPUTF8 = true
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> SMTPUTF8\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServerSMTPUTF8_Disabled(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> SMTPUTF8\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServer8BITMIME(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> BODY=8bitMIME\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServer_BODYInvalidValue(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> BODY=RABIIT\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServerUnknownArg(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> RABIIT\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServerBadSize(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> SIZE=rabbit\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServerTooBig(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
s.MaxMessageBytes = 4294967294
io.WriteString(c, "MAIL FROM:<[email protected]> SIZE=4294967295\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
}
func TestServerEmptyTo(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
io.WriteString(c, "RCPT TO:\r\n")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RCPT response:", scanner.Text())
}
}
func TestServer(t *testing.T) {
be, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RCPT response:", scanner.Text())
}
io.WriteString(c, "DATA\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "354 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
io.WriteString(c, "From: [email protected]\r\n")
io.WriteString(c, "\r\n")
io.WriteString(c, "Hey\r <3\r\n")
io.WriteString(c, "..this dot is fine\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
if len(be.messages) != 1 || len(be.anonmsgs) != 0 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
msg := be.messages[0]
if msg.From != "[email protected]" {
t.Fatal("Invalid mail sender:", msg.From)
}
if len(msg.To) != 1 || msg.To[0] != "[email protected]" {
t.Fatal("Invalid mail recipients:", msg.To)
}
if string(msg.Data) != "From: [email protected]\r\n\r\nHey\r <3\r\n.this dot is fine\r\n" {
t.Fatal("Invalid mail data:", string(msg.Data))
}
}
func TestServer_LFDotLF(t *testing.T) {
be, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RCPT response:", scanner.Text())
}
io.WriteString(c, "DATA\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "354 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
io.WriteString(c, "From: [email protected]\r\n")
io.WriteString(c, "\r\n")
io.WriteString(c, "hey\r\n")
io.WriteString(c, "\n.\n")
io.WriteString(c, "this is going to break your server\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
if len(be.messages) != 1 || len(be.anonmsgs) != 0 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
msg := be.messages[0]
if string(msg.Data) != "From: [email protected]\r\n\r\nhey\r\n\n.\nthis is going to break your server\r\n" {
t.Fatal("Invalid mail data:", string(msg.Data))
}
}
func TestServer_EmptyMessage(t *testing.T) {
be, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RCPT response:", scanner.Text())
}
io.WriteString(c, "DATA\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "354 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
io.WriteString(c, "\r\n\r\n.\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
if len(be.messages) != 1 || len(be.anonmsgs) != 0 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
msg := be.messages[0]
if string(msg.Data) != "\r\n\r\n" {
t.Fatal("Invalid mail data:", string(msg.Data), msg.Data)
}
}
func TestServer_authDisabled(t *testing.T) {
_, s, c, scanner, caps := testServerEhlo(t, authDisabled)
defer s.Close()
defer c.Close()
if _, ok := caps["AUTH PLAIN"]; ok {
t.Fatal("AUTH PLAIN capability is present when auth is disabled")
}
io.WriteString(c, "AUTH PLAIN\r\n")
scanner.Scan()
if scanner.Text() != "502 5.7.0 Authentication not supported" {
t.Fatal("Invalid AUTH response with auth disabled:", scanner.Text())
}
}
func TestServer_otherCommands(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
io.WriteString(c, "HELP\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "502 ") {
t.Fatal("Invalid HELP response:", scanner.Text())
}
io.WriteString(c, "VRFY\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "252 ") {
t.Fatal("Invalid VRFY response:", scanner.Text())
}
io.WriteString(c, "NOOP\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid NOOP response:", scanner.Text())
}
io.WriteString(c, "RSET\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RSET response:", scanner.Text())
}
io.WriteString(c, "QUIT\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "221 ") {
t.Fatal("Invalid QUIT response:", scanner.Text())
}
}
func TestServer_tooManyInvalidCommands(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
// Let's assume XXXX is a non-existing command
for i := 0; i < 4; i++ {
io.WriteString(c, "XXXX\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid invalid command response:", scanner.Text())
}
}
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid invalid command response:", scanner.Text())
}
}
func TestServer_tooLongMessage(t *testing.T) {
be, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
s.MaxMessageBytes = 50
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
io.WriteString(c, "This is a very long message.\r\n")
io.WriteString(c, "Much longer than you can possibly imagine.\r\n")
io.WriteString(c, "And much longer than the server's MaxMessageBytes.\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "552 ") {
t.Fatal("Invalid DATA response, expected an error but got:", scanner.Text())
}
if len(be.messages) != 0 || len(be.anonmsgs) != 0 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
}
// See https://www.postfix.org/smtp-smuggling.html
func TestServer_smtpSmuggling(t *testing.T) {
cases := []struct {
name string
lines []string
expected string
}{
{
name: "<CR><LF>.<LF>",
lines: []string{
"This is a message with an SMTP smuggling dot:\r\n",
".\n",
"Final dot comes after.\r\n",
".\r\n",
},
expected: "This is a message with an SMTP smuggling dot:\r\n\nFinal dot comes after.\r\n",
},
{
name: "<LF>.<CR><LF>",
lines: []string{
"This is a message with an SMTP smuggling dot:\n", // not a line on its own
".\r\n",
"Final dot comes after.\r\n",
".\r\n",
},
expected: "This is a message with an SMTP smuggling dot:\n.\r\nFinal dot comes after.\r\n",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
be, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
for _, line := range tc.lines {
io.WriteString(c, line)
}
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response, expected an error but got:", scanner.Text())
}
if len(be.messages) != 1 {
t.Fatal("Invalid number of sent messages:", len(be.messages))
}
msg := be.messages[0]
if string(msg.Data) != tc.expected {
t.Fatalf("Invalid mail data: %q", string(msg.Data))
}
})
}
}
func TestServer_tooLongLine(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
io.WriteString(c, "MAIL FROM:<[email protected]> "+strings.Repeat("A", 2000))
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid response, expected an error but got:", scanner.Text())
}
}
func TestServer_anonymousUserError(t *testing.T) {
be, s, c, scanner, _ := testServerEhlo(t)
defer s.Close()
defer c.Close()
be.userErr = smtp.ErrAuthRequired
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if scanner.Text() != "502 5.7.0 Please authenticate first" {
t.Fatal("Backend refused anonymous mail but client was permitted:", scanner.Text())
}
}
func TestServer_anonymousUserOK(t *testing.T) {
be, s, c, scanner, _ := testServerEhlo(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM: [email protected]\r\n")
scanner.Scan()
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
io.WriteString(c, "Hey <3\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
if len(be.messages) != 0 || len(be.anonmsgs) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
}
func TestServer_authParam(t *testing.T) {
be, s, c, scanner, _ := testServerEhlo(t)
defer s.Close()
defer c.Close()
// Invalid HEXCHAR
io.WriteString(c, "MAIL FROM: [email protected] AUTH=<hey+A>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
// Invalid HEXCHAR
io.WriteString(c, "MAIL FROM: [email protected] AUTH=<he+YYa>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "500 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
// https://tools.ietf.org/html/rfc4954#section-4
// >servers that advertise support for this
// >extension MUST support the AUTH parameter to the MAIL FROM
// >command even when the client has not authenticated itself to the
// >server.
io.WriteString(c, "MAIL FROM: [email protected] [email protected]\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
// Go on as usual.
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
io.WriteString(c, "DATA\r\n")
scanner.Scan()
io.WriteString(c, "Hey <3\r\n")
io.WriteString(c, ".\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid DATA response:", scanner.Text())
}
if len(be.messages) != 0 || len(be.anonmsgs) != 1 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
if val := be.anonmsgs[0].Opts.Auth; val == nil || *val != "[email protected]" {
t.Fatal("Invalid Auth value:", val)
}
}
func TestServer_Chunking(t *testing.T) {
be, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()
io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}
io.WriteString(c, "RCPT TO:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid RCPT response:", scanner.Text())
}
io.WriteString(c, "BDAT 8\r\n")
io.WriteString(c, "Hey <3\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid BDAT response:", scanner.Text())
}
io.WriteString(c, "BDAT 8 LAST\r\n")
io.WriteString(c, "Hey :3\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "250 ") {
t.Fatal("Invalid BDAT response:", scanner.Text())
}
if len(be.messages) != 1 || len(be.anonmsgs) != 0 {
t.Fatal("Invalid number of sent messages:", be.messages, be.anonmsgs)
}
msg := be.messages[0]
if msg.From != "[email protected]" {