-
Notifications
You must be signed in to change notification settings - Fork 38
/
smtpd_test.go
1548 lines (1181 loc) · 35.7 KB
/
smtpd_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 smtpd_test
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"log"
"net"
"net/smtp"
"net/textproto"
"os"
"strings"
"testing"
"time"
"github.com/chrj/smtpd"
)
var localhostCert = []byte(`-----BEGIN CERTIFICATE-----
MIIFkzCCA3ugAwIBAgIUQvhoyGmvPHq8q6BHrygu4dPp0CkwDQYJKoZIhvcNAQEL
BQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDESMBAGA1UEAwwJbG9jYWxob3N0MB4X
DTIwMDUyMTE2MzI1NVoXDTMwMDUxOTE2MzI1NVowWTELMAkGA1UEBhMCQVUxEzAR
BgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5
IEx0ZDESMBAGA1UEAwwJbG9jYWxob3N0MIICIjANBgkqhkiG9w0BAQEFAAOCAg8A
MIICCgKCAgEAk773plyfK4u2uIIZ6H7vEnTb5qJT6R/KCY9yniRvCFV+jCrISAs9
0pgU+/P8iePnZRGbRCGGt1B+1/JAVLIYFZuawILHNs4yWKAwh0uNpR1Pec8v7vpq
NpdUzXKQKIqFynSkcLA8c2DOZwuhwVc8rZw50yY3r4i4Vxf0AARGXapnBfy6WerR
/6xT7y/OcK8+8aOirDQ9P6WlvZ0ynZKi5q2o1eEVypT2us9r+HsCYosKEEAnjzjJ
wP5rvredxUqb7OupIkgA4Nq80+4tqGGQfWetmoi3zXRhKpijKjgxBOYEqSUWm9ws
/aC91Iy5RawyTB0W064z75OgfuI5GwFUbyLD0YVN4DLSAI79GUfvc8NeLEXpQvYq
+f8P+O1Hbv2AQ28IdbyQrNefB+/WgjeTvXLploNlUihVhpmLpptqnauw/DY5Ix51
w60lHIZ6esNOmMQB+/z/IY5gpmuo66yH8aSCPSYBFxQebB7NMqYGOS9nXx62/Bn1
OUVXtdtrhfbbdQW6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proto.Conn, expectedCode int, format string, args ...interface{}) error {
id, err := c.Cmd(format, args...)
if err != nil {
return err
}
c.StartResponse(id)
_, _, err = c.ReadResponse(expectedCode)
c.EndResponse(id)
return err
}
func runserver(t *testing.T, server *smtpd.Server) (addr string, closer func()) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Listen failed: %v", err)
}
go func() {
server.Serve(ln)
}()
done := make(chan bool)
go func() {
<-done
ln.Close()
}()
return ln.Addr().String(), func() {
done <- true
}
}
func runsslserver(t *testing.T, server *smtpd.Server) (addr string, closer func()) {
cert, err := tls.X509KeyPair(localhostCert, localhostKey)
if err != nil {
t.Fatalf("Cert load failed: %v", err)
}
server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
return runserver(t, server)
}
func TestSMTP(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Hello("localhost"); err != nil {
t.Fatalf("HELO failed: %v", err)
}
if supported, _ := c.Extension("AUTH"); supported {
t.Fatal("AUTH supported before TLS")
}
if supported, _ := c.Extension("8BITMIME"); !supported {
t.Fatal("8BITMIME not supported")
}
if supported, _ := c.Extension("STARTTLS"); supported {
t.Fatal("STARTTLS supported")
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("Mail failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("Rcpt failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("Rcpt2 failed: %v", err)
}
wc, err := c.Data()
if err != nil {
t.Fatalf("Data failed: %v", err)
}
_, err = fmt.Fprintf(wc, "This is the email body")
if err != nil {
t.Fatalf("Data body failed: %v", err)
}
err = wc.Close()
if err != nil {
t.Fatalf("Data close failed: %v", err)
}
if err := c.Reset(); err != nil {
t.Fatalf("Reset failed: %v", err)
}
if err := c.Verify("[email protected]"); err == nil {
t.Fatal("Unexpected support for VRFY")
}
if err := cmd(c.Text, 250, "NOOP"); err != nil {
t.Fatalf("NOOP failed: %v", err)
}
if err := c.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
}
func TestListenAndServe(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{})
closer()
server := &smtpd.Server{
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
}
go func() {
server.ListenAndServe(addr)
}()
time.Sleep(100 * time.Millisecond)
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
}
func TestSTARTTLS(t *testing.T) {
addr, closer := runsslserver(t, &smtpd.Server{
Authenticator: func(peer smtpd.Peer, username, password string) error { return nil },
ForceTLS: true,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if supported, _ := c.Extension("AUTH"); supported {
t.Fatal("AUTH supported before TLS")
}
if err := c.Mail("[email protected]"); err == nil {
t.Fatal("Mail workded before TLS with ForceTLS")
}
if err := cmd(c.Text, 220, "STARTTLS"); err != nil {
t.Fatalf("STARTTLS failed: %v", err)
}
if err := cmd(c.Text, 250, "foobar"); err == nil {
t.Fatal("STARTTLS didn't fail with invalid handshake")
}
if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Fatalf("STARTTLS failed: %v", err)
}
if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err == nil {
t.Fatal("STARTTLS worked twice")
}
if supported, _ := c.Extension("AUTH"); !supported {
t.Fatal("AUTH not supported after TLS")
}
if _, mechs := c.Extension("AUTH"); !strings.Contains(mechs, "PLAIN") {
t.Fatal("PLAIN AUTH not supported after TLS")
}
if _, mechs := c.Extension("AUTH"); !strings.Contains(mechs, "LOGIN") {
t.Fatal("LOGIN AUTH not supported after TLS")
}
if err := c.Auth(smtp.PlainAuth("foo", "foo", "bar", "127.0.0.1")); err != nil {
t.Fatalf("Auth failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("Mail failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("Rcpt failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("Rcpt2 failed: %v", err)
}
wc, err := c.Data()
if err != nil {
t.Fatalf("Data failed: %v", err)
}
_, err = fmt.Fprintf(wc, "This is the email body")
if err != nil {
t.Fatalf("Data body failed: %v", err)
}
err = wc.Close()
if err != nil {
t.Fatalf("Data close failed: %v", err)
}
if err := c.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
}
func TestAuthRejection(t *testing.T) {
addr, closer := runsslserver(t, &smtpd.Server{
Authenticator: func(peer smtpd.Peer, username, password string) error {
return smtpd.Error{Code: 550, Message: "Denied"}
},
ForceTLS: true,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Fatalf("STARTTLS failed: %v", err)
}
if err := c.Auth(smtp.PlainAuth("foo", "foo", "bar", "127.0.0.1")); err == nil {
t.Fatal("Auth worked despite rejection")
}
}
func TestAuthNotSupported(t *testing.T) {
addr, closer := runsslserver(t, &smtpd.Server{
ForceTLS: true,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Fatalf("STARTTLS failed: %v", err)
}
if err := c.Auth(smtp.PlainAuth("foo", "foo", "bar", "127.0.0.1")); err == nil {
t.Fatal("Auth worked despite no authenticator")
}
}
func TestAuthBypass(t *testing.T) {
addr, closer := runsslserver(t, &smtpd.Server{
Authenticator: func(peer smtpd.Peer, username, password string) error {
return smtpd.Error{Code: 550, Message: "Denied"}
},
ForceTLS: true,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Fatalf("STARTTLS failed: %v", err)
}
if err := c.Mail("[email protected]"); err == nil {
t.Fatal("Unexpected MAIL success")
}
}
func TestConnectionCheck(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ConnectionChecker: func(peer smtpd.Peer) error {
return smtpd.Error{Code: 552, Message: "Denied"}
},
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
if _, err := smtp.Dial(addr); err == nil {
t.Fatal("Dial succeeded despite ConnectionCheck")
}
}
func TestConnectionCheckSimpleError(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ConnectionChecker: func(peer smtpd.Peer) error {
return errors.New("Denied")
},
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
if _, err := smtp.Dial(addr); err == nil {
t.Fatal("Dial succeeded despite ConnectionCheck")
}
}
func TestHELOCheck(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
HeloChecker: func(peer smtpd.Peer, name string) error {
if name != "foobar.local" {
t.Fatal("Wrong HELO name")
}
return smtpd.Error{Code: 552, Message: "Denied"}
},
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Hello("foobar.local"); err == nil {
t.Fatal("Unexpected HELO success")
}
}
func TestSenderCheck(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
SenderChecker: func(peer smtpd.Peer, addr string) error {
return smtpd.Error{Code: 552, Message: "Denied"}
},
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err == nil {
t.Fatal("Unexpected MAIL success")
}
}
func TestRecipientCheck(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
RecipientChecker: func(peer smtpd.Peer, addr string) error {
return smtpd.Error{Code: 552, Message: "Denied"}
},
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("Mail failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err == nil {
t.Fatal("Unexpected RCPT success")
}
}
func TestMaxMessageSize(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
MaxMessageSize: 5,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("RCPT failed: %v", err)
}
wc, err := c.Data()
if err != nil {
t.Fatalf("Data failed: %v", err)
}
_, err = fmt.Fprintf(wc, "This is the email body")
if err != nil {
t.Fatalf("Data body failed: %v", err)
}
err = wc.Close()
if err == nil {
t.Fatal("Allowed message larger than 5 bytes to pass.")
}
if err := c.Quit(); err != nil {
t.Fatalf("QUIT failed: %v", err)
}
}
func TestHandler(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
Handler: func(peer smtpd.Peer, env smtpd.Envelope) error {
if env.Sender != "[email protected]" {
t.Fatalf("Unknown sender: %v", env.Sender)
}
if len(env.Recipients) != 1 {
t.Fatalf("Too many recipients: %d", len(env.Recipients))
}
if env.Recipients[0] != "[email protected]" {
t.Fatalf("Unknown recipient: %v", env.Recipients[0])
}
if string(env.Data) != "This is the email body\n" {
t.Fatalf("Wrong message body: %v", string(env.Data))
}
return nil
},
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("RCPT failed: %v", err)
}
wc, err := c.Data()
if err != nil {
t.Fatalf("Data failed: %v", err)
}
_, err = fmt.Fprintf(wc, "This is the email body")
if err != nil {
t.Fatalf("Data body failed: %v", err)
}
err = wc.Close()
if err != nil {
t.Fatalf("Data close failed: %v", err)
}
if err := c.Quit(); err != nil {
t.Fatalf("QUIT failed: %v", err)
}
}
func TestRejectHandler(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
Handler: func(peer smtpd.Peer, env smtpd.Envelope) error {
return smtpd.Error{Code: 550, Message: "Rejected"}
},
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("RCPT failed: %v", err)
}
wc, err := c.Data()
if err != nil {
t.Fatalf("Data failed: %v", err)
}
_, err = fmt.Fprintf(wc, "This is the email body")
if err != nil {
t.Fatalf("Data body failed: %v", err)
}
err = wc.Close()
if err == nil {
t.Fatal("Unexpected accept of data")
}
if err := c.Quit(); err != nil {
t.Fatalf("QUIT failed: %v", err)
}
}
func TestMaxConnections(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
MaxConnections: 1,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c1, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
_, err = smtp.Dial(addr)
if err == nil {
t.Fatal("Dial succeeded despite MaxConnections = 1")
}
c1.Close()
}
func TestNoMaxConnections(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
MaxConnections: -1,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c1, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
c1.Close()
}
func TestMaxRecipients(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
MaxRecipients: 1,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("RCPT failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err == nil {
t.Fatal("RCPT succeeded despite MaxRecipients = 1")
}
if err := c.Quit(); err != nil {
t.Fatalf("QUIT failed: %v", err)
}
}
func TestInvalidHelo(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Hello(""); err == nil {
t.Fatal("Unexpected HELO success")
}
}
func TestInvalidSender(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("invalid@@example.org"); err == nil {
t.Fatal("Unexpected MAIL success")
}
}
func TestInvalidRecipient(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("Mail failed: %v", err)
}
if err := c.Rcpt("invalid@@example.org"); err == nil {
t.Fatal("Unexpected RCPT success")
}
}
func TestRCPTbeforeMAIL(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err == nil {
t.Fatal("Unexpected RCPT success")
}
}
func TestDATAbeforeRCPT(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
if _, err := c.Data(); err == nil {
t.Fatal("Data accepted despite no recipients")
}
if err := c.Quit(); err != nil {
t.Fatalf("QUIT failed: %v", err)
}
}
func TestInterruptedDATA(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
Handler: func(peer smtpd.Peer, env smtpd.Envelope) error {
t.Fatal("Accepted DATA despite disconnection")
return nil
},
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("RCPT failed: %v", err)
}
wc, err := c.Data()
if err != nil {
t.Fatalf("Data failed: %v", err)
}
_, err = fmt.Fprintf(wc, "This is the email body")
if err != nil {
t.Fatalf("Data body failed: %v", err)
}
c.Close()
}
func TestTimeoutClose(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
MaxConnections: 1,
ReadTimeout: time.Second,
WriteTimeout: time.Second,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c1, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
time.Sleep(time.Second * 2)
c2, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c1.Mail("[email protected]"); err == nil {
t.Fatal("MAIL succeeded despite being timed out.")
}
if err := c2.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
if err := c2.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
c2.Close()
}
func TestTLSTimeout(t *testing.T) {
addr, closer := runsslserver(t, &smtpd.Server{
ReadTimeout: time.Second * 2,
WriteTimeout: time.Second * 2,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Fatalf("STARTTLS failed: %v", err)
}
time.Sleep(time.Second)
if err := c.Mail("[email protected]"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}
time.Sleep(time.Second)
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("RCPT failed: %v", err)
}
time.Sleep(time.Second)
if err := c.Rcpt("[email protected]"); err != nil {
t.Fatalf("RCPT failed: %v", err)
}
time.Sleep(time.Second)
if err := c.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
}
func TestLongLine(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
if err := c.Mail(fmt.Sprintf("%[email protected]", strings.Repeat("x", 65*1024))); err == nil {
t.Fatalf("MAIL failed: %v", err)
}
if err := c.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
}
func TestXCLIENT(t *testing.T) {
addr, closer := runserver(t, &smtpd.Server{
EnableXCLIENT: true,
SenderChecker: func(peer smtpd.Peer, addr string) error {