-
Notifications
You must be signed in to change notification settings - Fork 366
/
server_test.go
1150 lines (1026 loc) · 33.4 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 guerrilla
import (
"os"
"testing"
"bufio"
"net/textproto"
"strings"
"sync"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"github.com/flashmob/go-guerrilla/backends"
"github.com/flashmob/go-guerrilla/log"
"github.com/flashmob/go-guerrilla/mail"
"github.com/flashmob/go-guerrilla/mocks"
)
// getMockServerConfig gets a mock ServerConfig struct used for creating a new server
func getMockServerConfig() *ServerConfig {
sc := &ServerConfig{
IsEnabled: true, // not tested here
Hostname: "saggydimes.test.com",
MaxSize: 1024, // smtp message max size
TLS: ServerTLSConfig{
PrivateKeyFile: "./tests/mail.guerrillamail.com.key.pem",
PublicKeyFile: "./tests/mail.guerrillamail.com.cert.pem",
StartTLSOn: true,
AlwaysOn: false,
},
Timeout: 5,
ListenInterface: "127.0.0.1:2529",
MaxClients: 30, // not tested here
LogFile: "./tests/testlog",
}
return sc
}
// getMockServerConn gets a new server using sc. Server will be using a mocked TCP connection
// using the dummy backend
// RCP TO command only allows test.com host
func getMockServerConn(sc *ServerConfig, t *testing.T) (*mocks.Conn, *server) {
var logOpenError error
var mainlog log.Logger
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
backend, err := backends.New(
backends.BackendConfig{"log_received_mails": true, "save_workers_size": 1},
mainlog)
if err != nil {
t.Error("new dummy backend failed because:", err)
}
server, err := newServer(sc, backend, mainlog)
if err != nil {
//t.Error("new server failed because:", err)
} else {
server.setAllowedHosts([]string{"test.com"})
}
conn := mocks.NewConn()
return conn, server
}
// test the RootCAs tls config setting
var rootCAPK = `-----BEGIN CERTIFICATE-----
MIIDqjCCApKgAwIBAgIJALh2TrsBR5MiMA0GCSqGSIb3DQEBCwUAMGkxCzAJBgNV
BAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzEhMB8G
A1UECgwYSW50ZXJuZXQgV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lkZ2l0cyBQdHkgTHRkMRIwEAYDVQQDDAlsb2NhbGhv
c3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDlkuYwF0qRXJWY3RWE
NIUa+Aqnbh6Mc8K/7B+5/e+VT1zAp8dHe96OV5kBVFyJ2xx8DQmy5v8aWyvRfB5x
qC2m9Mj4WnWKTqwgI7P5Vbf3nypoCLxONtQyNwMcl5ItyOvvbkIfm4i+FHNH51ph
olCEBwvYwS3wz1YgXoCO0NkQf1Hqgzy3kWQ7RqYiaWeKlhc0EWCGbORnN8ZddFAv
DYD67qR4tyMKuf2HYFqh8YiNB9hy7/d8fhwgVHjP+ygYpqZ6qcJTPt1aZt/i0Aj7
xBHRVdcEqGIcOb3y09J4f33iELnZS6WuMRVwzlSjHmFrNLY8rTqhJzVi4Vk42p54
aqPrAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAIQmlo8iCpyYggkbpfDmThBPHfy1
cZcCi/tRFoFe1ccwn2ezLMIKmW38ZebiroawwqrZgU6AP+dMxVKLMjpyLPSrpFKa
3o/LbVF7qMfH8/y2q8t7javd6rxoENH9uxLyHhauzI1iWy0whoDWBNiZrPBTBCjq
jDGZARZqGyrPeXi+RNe1cMvZCxAFy7gqEtWFLWWrp0gYNPvxkHhhQBrUcF+8T/Nf
9G4hKZSN/KAgC0CNBVuNrdyNc3l8H66BfwwL5X0+pesBYZM+MEfmBZOo+p7OWx2r
ug8tR8eSL1vGleONtFRBUVG7NbtjhBf9FhvPZcSRR10od/vWHku9E01i4xg=
-----END CERTIFICATE-----`
func truncateIfExists(filename string) error {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
return os.Truncate(filename, 0)
}
return nil
}
func deleteIfExists(filename string) error {
if _, err := os.Stat(filename); !os.IsNotExist(err) {
return os.Remove(filename)
}
return nil
}
func cleanTestArtifacts(t *testing.T) {
if err := deleteIfExists("rootca.test.pem"); err != nil {
t.Error(err)
}
if err := deleteIfExists("client.test.key"); err != nil {
t.Error(err)
}
if err := deleteIfExists("client.test.pem"); err != nil {
t.Error(err)
}
if err := deleteIfExists("./tests/mail.guerrillamail.com.key.pem"); err != nil {
t.Error(err)
}
if err := deleteIfExists("./tests/mail.guerrillamail.com.cert.pem"); err != nil {
t.Error(err)
}
if err := deleteIfExists("./tests/different-go-guerrilla.pid"); err != nil {
t.Error(err)
}
if err := deleteIfExists("./tests/go-guerrilla.pid"); err != nil {
t.Error(err)
}
if err := deleteIfExists("./tests/go-guerrilla2.pid"); err != nil {
t.Error(err)
}
if err := deleteIfExists("./tests/pidfile.pid"); err != nil {
t.Error(err)
}
if err := deleteIfExists("./tests/pidfile2.pid"); err != nil {
t.Error(err)
}
if err := truncateIfExists("./tests/testlog"); err != nil {
t.Error(err)
}
if err := truncateIfExists("./tests/testlog2"); err != nil {
t.Error(err)
}
}
func TestTLSConfig(t *testing.T) {
defer cleanTestArtifacts(t)
if err := ioutil.WriteFile("rootca.test.pem", []byte(rootCAPK), 0644); err != nil {
t.Fatal("couldn't create rootca.test.pem file.", err)
return
}
if err := ioutil.WriteFile("client.test.key", []byte(clientPrvKey), 0644); err != nil {
t.Fatal("couldn't create client.test.key file.", err)
return
}
if err := ioutil.WriteFile("client.test.pem", []byte(clientPubKey), 0644); err != nil {
t.Fatal("couldn't create client.test.pem file.", err)
return
}
s := server{}
s.setConfig(&ServerConfig{
TLS: ServerTLSConfig{
StartTLSOn: true,
PrivateKeyFile: "client.test.key",
PublicKeyFile: "client.test.pem",
RootCAs: "rootca.test.pem",
ClientAuthType: "NoClientCert",
Curves: []string{"P521", "P384"},
Ciphers: []string{"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA"},
Protocols: []string{"tls1.0", "tls1.2"},
},
})
if err := s.configureTLS(); err != nil {
t.Error(err)
}
c := s.tlsConfigStore.Load().(*tls.Config)
if len(c.CurvePreferences) != 2 {
t.Error("c.CurvePreferences should have two elements")
} else if c.CurvePreferences[0] != tls.CurveP521 && c.CurvePreferences[1] != tls.CurveP384 {
t.Error("c.CurvePreferences curves not setup")
}
if !strings.Contains(string(c.RootCAs.Subjects()[0]), "Mountain View") {
t.Error("c.RootCAs not correctly set")
}
if c.ClientAuth != tls.NoClientCert {
t.Error("c.ClientAuth should be tls.NoClientCert")
}
if len(c.CipherSuites) != 2 {
t.Error("c.CipherSuites length should be 2")
}
if c.CipherSuites[0] != tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 && c.CipherSuites[1] != tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA {
t.Error("c.CipherSuites not correctly set ")
}
if c.MinVersion != tls.VersionTLS10 {
t.Error("c.MinVersion should be tls.VersionTLS10")
}
if c.MaxVersion != tls.VersionTLS12 {
t.Error("c.MinVersion should be tls.VersionTLS10")
}
if c.PreferServerCipherSuites != false {
t.Error("PreferServerCipherSuites should be false")
}
}
func TestHandleClient(t *testing.T) {
var mainlog log.Logger
var logOpenError error
defer cleanTestArtifacts(t)
sc := getMockServerConfig()
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
// call the serve.handleClient() func in a goroutine.
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
// fmt.Println(line)
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
if err := w.PrintfLine("HELO test.test.com"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
//fmt.Println(line)
if err := w.PrintfLine("QUIT"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
//fmt.Println("line is:", line)
expected := "221 2.0.0 Bye"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
wg.Wait() // wait for handleClient to exit
}
func TestGithubIssue197(t *testing.T) {
var mainlog log.Logger
var logOpenError error
defer cleanTestArtifacts(t)
sc := getMockServerConfig()
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
server.backend().Start()
// we assume that 1.1.1.1 is a domain (ip-literal syntax is incorrect)
// [2001:DB8::FF00:42:8329] is an address literal
server.setAllowedHosts([]string{"1.1.1.1", "[2001:DB8::FF00:42:8329]"})
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
// fmt.Println(line)
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
if err := w.PrintfLine("HELO test.test.com"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// Case 1
if err := w.PrintfLine("rcpt to: <hi@[1.1.1.1]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if client.parser.IP == nil {
t.Error("[1.1.1.1] not parsed as address-liteal")
}
// case 2, should be parsed as domain
if err := w.PrintfLine("rcpt to: <[email protected]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if client.parser.IP != nil {
t.Error("1.1.1.1 should not be parsed as an IP (syntax requires IP addresses to be in braces, eg <hi@[1.1.1.1]>")
}
// case 3
// prefix ipv6 is case insensitive
if err := w.PrintfLine("rcpt to: <hi@[ipv6:2001:DB8::FF00:42:8329]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if client.parser.IP == nil {
t.Error("[ipv6:2001:DB8::FF00:42:8329] should be parsed as an address-literal, it wasnt")
}
// case 4
if err := w.PrintfLine("rcpt to: <hi@[IPv6:2001:0db8:0000:0000:0000:ff00:0042:8329]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if client.parser.Domain != "2001:DB8::FF00:42:8329" && client.parser.IP == nil {
t.Error("[IPv6:2001:0db8:0000:0000:0000:ff00:0042:8329] is same as 2001:DB8::FF00:42:8329, lol")
}
if err := w.PrintfLine("QUIT"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
//fmt.Println("line is:", line)
expected := "221 2.0.0 Bye"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
wg.Wait() // wait for handleClient to exit
}
var githubIssue198data string
var customBackend = func() backends.Decorator {
return func(p backends.Processor) backends.Processor {
return backends.ProcessWith(
func(e *mail.Envelope, task backends.SelectTask) (backends.Result, error) {
if task == backends.TaskSaveMail {
githubIssue198data = e.DeliveryHeader + e.Data.String()
}
return p.Process(e, task)
})
}
}
// TestGithubIssue198 is an interesting test because it shows how to do an integration test for
// a backend using a custom backend.
func TestGithubIssue198(t *testing.T) {
var mainlog log.Logger
var logOpenError error
defer cleanTestArtifacts(t)
sc := getMockServerConfig()
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
backends.Svc.AddProcessor("custom", customBackend)
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
be, err := backends.New(map[string]interface{}{
"save_process": "HeadersParser|Header|custom", "primary_mail_host": "example.com"},
mainlog)
if err != nil {
t.Error(err)
return
}
server.setBackend(be)
if err := server.backend().Start(); err != nil {
t.Error(err)
return
}
server.setAllowedHosts([]string{"1.1.1.1", "[2001:DB8::FF00:42:8329]"})
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
client.RemoteIP = "127.0.0.1"
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
// Test with HELO greeting
line = sendMessage("HELO", true, w, t, line, r, err, client)
if !strings.Contains(githubIssue198data, " SMTPS ") {
t.Error("'with SMTPS' not present")
}
if !strings.Contains(githubIssue198data, "from 127.0.0.1") {
t.Error("'from 127.0.0.1' not present")
}
/////////////////////
if err := w.PrintfLine("RSET"); err != nil {
t.Error(err)
}
// Test with EHLO
line, _ = r.ReadLine()
line = sendMessage("EHLO", true, w, t, line, r, err, client)
if !strings.Contains(githubIssue198data, " ESMTPS ") {
t.Error("'with ESMTPS' not present")
}
/////////////////////
if err := w.PrintfLine("RSET"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// Test with EHLO & no TLS
line = sendMessage("EHLO", false, w, t, line, r, err, client)
/////////////////////
if !strings.Contains(githubIssue198data, " ESMTP ") {
t.Error("'with ESTMP' not present")
}
if err := w.PrintfLine("QUIT"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
expected := "221 2.0.0 Bye"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
wg.Wait() // wait for handleClient to exit
}
func sendMessage(greet string, TLS bool, w *textproto.Writer, t *testing.T, line string, r *textproto.Reader, err error, client *client) string {
if err := w.PrintfLine(greet + " test.test.com"); err != nil {
t.Error(err)
}
for {
line, _ = r.ReadLine()
if strings.Index(line, "250 ") == 0 {
break
}
if strings.Index(line, "250") != 0 {
t.Error(err)
}
}
if err := w.PrintfLine("MAIL FROM: [email protected]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if err := w.PrintfLine("RCPT TO: <hi@[ipv6:2001:DB8::FF00:42:8329]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
client.Hashes = append(client.Hashes, "abcdef1526777763")
client.TLS = TLS
if err := w.PrintfLine("DATA"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if err := w.PrintfLine("Subject: Test subject\r\n\r\nHello Sir,\nThis is a test.\r\n."); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
return line
}
func TestGithubIssue199(t *testing.T) {
var mainlog log.Logger
var logOpenError error
defer cleanTestArtifacts(t)
sc := getMockServerConfig()
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
server.backend().Start()
server.setAllowedHosts([]string{"grr.la", "fake.com", "[1.1.1.1]", "[2001:db8::8a2e:370:7334]", "saggydimes.test.com"})
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
// fmt.Println(line)
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
if err := w.PrintfLine("HELO test"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// case 1
if err := w.PrintfLine(
"MAIL FROM: <\" yo-- man wazz'''up? surprise surprise, this is [email protected] \"@example.com>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// [SPACE][SPACE]yo--[SPACE]man[SPACE]wazz'''up?[SPACE]surprise[SPACE]surprise,[SPACE]this[SPACE]is[SPACE][email protected][SPACE]
if client.parser.LocalPart != " yo-- man wazz'''up? surprise surprise, this is [email protected] " {
t.Error("expecting local part: [ yo-- man wazz'''up? surprise surprise, this is [email protected] ], got client.parser.LocalPart")
}
if !client.parser.LocalPartQuotes {
t.Error("was expecting client.parser.LocalPartQuotes true, got false")
}
// from should just as above but without angle brackets <>
if from := client.MailFrom.String(); from != "\" yo-- man wazz'''up? surprise surprise, this is [email protected] \"@example.com" {
t.Error("mail from was:", from)
}
if line != "250 2.1.0 OK" {
t.Error("line did not have: 250 2.1.0 OK, got", line)
}
if err := w.PrintfLine("RSET"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// case 2, address literal mailboxes
if err := w.PrintfLine("MAIL FROM: <hi@[1.1.1.1]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// stringer should be aware its an ip and return the host part in angle brackets
if from := client.MailFrom.String(); from != "hi@[1.1.1.1]" {
t.Error("mail from was:", from)
}
if err := w.PrintfLine("RSET"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// case 3
if err := w.PrintfLine("MAIL FROM: <hi@[IPv6:2001:0db8:0000:0000:0000:8a2e:0370:7334]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// stringer should be aware its an ip and return the host part in angle brackets, and ipv6 should be normalized
if from := client.MailFrom.String(); from != "hi@[2001:db8::8a2e:370:7334]" {
t.Error("mail from was:", from)
}
if err := w.PrintfLine("RSET"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// case 4
// rcpt to: <hi@[IPv6:2001:0db8:0000:0000:0000:ff00:0042:8329]>
if err := w.PrintfLine("MAIL FROM: <>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if err := w.PrintfLine("RCPT TO: <Postmaster>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// stringer should return an empty string
if from := client.MailFrom.String(); from != "" {
t.Error("mail from was:", from)
}
// note here the saggydimes.test.com was added because no host was specified in the RCPT TO command
if rcpt := client.RcptTo[0].String(); rcpt != "[email protected]" {
t.Error("mail from was:", rcpt)
}
// additional cases
/*
user part:
" al\ph\a "@grr.la should be " alpha "@grr.la.
"alpha"@grr.la should be [email protected].
"alp\h\a"@grr.la should be [email protected].
*/
if err := w.PrintfLine("RSET"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if err := w.PrintfLine("RCPT TO: <\" al\\ph\\a \"@grr.la>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if client.RcptTo[0].User != " alpha " {
t.Error(client.RcptTo[0].User)
}
// the unnecessary \\ should be removed
if rcpt := client.RcptTo[0].String(); rcpt != "\" alpha \"@grr.la" {
t.Error(rcpt)
}
if err := w.PrintfLine("RSET"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if err := w.PrintfLine("RCPT TO: <\"alpha\"@grr.la>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// we don't need to quote, so stringer should return without the quotes
if rcpt := client.RcptTo[0].String(); rcpt != "[email protected]" {
t.Error(rcpt)
}
if err := w.PrintfLine("RSET"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if err := w.PrintfLine("RCPT TO: <\"a\\l\\pha\"@grr.la>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// we don't need to quote, so stringer should return without the quotes
if rcpt := client.RcptTo[0].String(); rcpt != "[email protected]" {
t.Error(rcpt)
}
if err := w.PrintfLine("QUIT"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
wg.Wait() // wait for handleClient to exit
}
func TestGithubIssue200(t *testing.T) {
var mainlog log.Logger
var logOpenError error
defer cleanTestArtifacts(t)
sc := getMockServerConfig()
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
server.backend().Start()
server.setAllowedHosts([]string{"1.1.1.1", "[2001:DB8::FF00:42:8329]"})
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
// fmt.Println(line)
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
if err := w.PrintfLine("HELO test\"><script>alert('hi')</script>test.com"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if line != "550 5.5.2 Syntax error" {
t.Error("line expected to be: 550 5.5.2 Syntax error, got", line)
}
if err := w.PrintfLine("HELO test.com"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if !strings.Contains(line, "250") {
t.Error("line did not have 250 code, got", line)
}
if err := w.PrintfLine("QUIT"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
//fmt.Println("line is:", line)
expected := "221 2.0.0 Bye"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
wg.Wait() // wait for handleClient to exit
}
func TestGithubIssue201(t *testing.T) {
var mainlog log.Logger
var logOpenError error
defer cleanTestArtifacts(t)
sc := getMockServerConfig()
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
server.backend().Start()
// note that saggydimes.test.com is the hostname of the server, it comes form the config
// it will be used for rcpt to:<postmaster> which does not specify a host
server.setAllowedHosts([]string{"a.com", "saggydimes.test.com"})
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
// fmt.Println(line)
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
if err := w.PrintfLine("HELO test"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
// case 1
if err := w.PrintfLine("RCPT TO: <[email protected]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if line != "250 2.1.5 OK" {
t.Error("line did not have: 250 2.1.5 OK, got", line)
}
// case 2
if err := w.PrintfLine("RCPT TO: <[email protected]>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if line != "454 4.1.1 Error: Relay access denied: not-a.com" {
t.Error("line is not:454 4.1.1 Error: Relay access denied: not-a.com, got", line)
}
// case 3 (no host specified)
if err := w.PrintfLine("RCPT TO: <poSTmAsteR>"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if line != "250 2.1.5 OK" {
t.Error("line is not:[250 2.1.5 OK], got", line)
}
// case 4
if err := w.PrintfLine("RCPT TO: <\"po\\ST\\mAs\\t\\eR\">"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if line != "250 2.1.5 OK" {
t.Error("line is not:[250 2.1.5 OK], got", line)
}
// the local part should be just "postmaster" (normalized)
if client.parser.LocalPart != "postmaster" {
t.Error("client.parser.LocalPart was not postmaster, got:", client.parser.LocalPart)
}
if client.parser.LocalPartQuotes {
t.Error("client.parser.LocalPartQuotes was true, expecting false")
}
if err := w.PrintfLine("QUIT"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
//fmt.Println("line is:", line)
expected := "221 2.0.0 Bye"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
wg.Wait() // wait for handleClient to exit
}
func TestXClient(t *testing.T) {
var mainlog log.Logger
var logOpenError error
defer cleanTestArtifacts(t)
sc := getMockServerConfig()
sc.XClientOn = true
mainlog, logOpenError = log.GetLogger(sc.LogFile, "debug")
if logOpenError != nil {
mainlog.WithError(logOpenError).Errorf("Failed creating a logger for mock conn [%s]", sc.ListenInterface)
}
conn, server := getMockServerConn(sc, t)
// call the serve.handleClient() func in a goroutine.
client := NewClient(conn.Server, 1, mainlog, mail.NewPool(5))
var wg sync.WaitGroup
wg.Add(1)
go func() {
server.handleClient(client)
wg.Done()
}()
// Wait for the greeting from the server
r := textproto.NewReader(bufio.NewReader(conn.Client))
line, _ := r.ReadLine()
// fmt.Println(line)
w := textproto.NewWriter(bufio.NewWriter(conn.Client))
if err := w.PrintfLine("HELO test.test.com"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
//fmt.Println(line)
if err := w.PrintfLine("XCLIENT ADDR=212.96.64.216 NAME=[UNAVAILABLE]"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
if client.RemoteIP != "212.96.64.216" {
t.Error("client.RemoteIP should be 212.96.64.216, but got:", client.RemoteIP)
}
expected := "250 2.1.0 OK"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
// try malformed input
if err := w.PrintfLine("XCLIENT c"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
expected = "250 2.1.0 OK"
if strings.Index(line, expected) != 0 {
t.Error("expected", expected, "but got:", line)
}
if err := w.PrintfLine("QUIT"); err != nil {
t.Error(err)
}
line, _ = r.ReadLine()
wg.Wait() // wait for handleClient to exit
}
// The backend gateway should time out after 1 second because it sleeps for 2 sec.
// The transaction should wait until finished, and then test to see if we can do
// a second transaction
func TestGatewayTimeout(t *testing.T) {
defer cleanTestArtifacts(t)
bcfg := backends.BackendConfig{
"save_workers_size": 1,
"save_process": "HeadersParser|Debugger",
"log_received_mails": true,
"primary_mail_host": "example.com",
"gw_save_timeout": "1s",
"gw_val_rcpt_timeout": "1s",
"sleep_seconds": 2,
}
cfg := &AppConfig{
LogFile: log.OutputOff.String(),
AllowedHosts: []string{"grr.la"},
}
cfg.BackendConfig = bcfg
d := Daemon{Config: cfg}
err := d.Start()
if err != nil {
t.Error("server didn't start")
} else {
conn, err := net.Dial("tcp", "127.0.0.1:2525")
if err != nil {
return
}
in := bufio.NewReader(conn)
str, err := in.ReadString('\n')
if err != nil {
t.Error(err)
}
if _, err := fmt.Fprint(conn, "HELO host\r\n"); err != nil {
t.Error(err)
}
str, err = in.ReadString('\n')
// perform 2 transactions
// both should panic.
for i := 0; i < 2; i++ {
if _, err := fmt.Fprint(conn, "MAIL FROM:<[email protected]>r\r\n"); err != nil {
t.Error(err)
}
if str, err = in.ReadString('\n'); err != nil {
t.Error(err)
}
if _, err := fmt.Fprint(conn, "RCPT TO:<[email protected]>\r\n"); err != nil {
t.Error(err)
}
if str, err = in.ReadString('\n'); err != nil {
t.Error(err)
}
if _, err := fmt.Fprint(conn, "DATA\r\n"); err != nil {
t.Error(err)
}
if str, err = in.ReadString('\n'); err != nil {
t.Error(err)
}
if _, err := fmt.Fprint(conn, "Subject: Test subject\r\n"); err != nil {
t.Error(err)
}
if _, err := fmt.Fprint(conn, "\r\n"); err != nil {
t.Error(err)
}
if _, err := fmt.Fprint(conn, "A an email body\r\n"); err != nil {
t.Error(err)
}
if _, err := fmt.Fprint(conn, ".\r\n"); err != nil {
t.Error(err)
}
str, err = in.ReadString('\n')
expect := "transaction timeout"
if err != nil {
t.Error(err)
} else if !strings.Contains(str, expect) {
t.Error("Expected the reply to have'", expect, "'but got", str)
}
}
_ = str
d.Shutdown()
}
}
// The processor will panic and gateway should recover from it
func TestGatewayPanic(t *testing.T) {
defer cleanTestArtifacts(t)