git » chasquid » next » tree

[next] / internal / smtpsrv / 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
package smtpsrv

import (
	"crypto/tls"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/fs"
	"net"
	"net/smtp"
	"net/textproto"
	"os"
	"strings"
	"syscall"
	"testing"
	"time"

	"blitiri.com.ar/go/chasquid/internal/aliases"
	"blitiri.com.ar/go/chasquid/internal/auth"
	"blitiri.com.ar/go/chasquid/internal/domaininfo"
	"blitiri.com.ar/go/chasquid/internal/maillog"
	"blitiri.com.ar/go/chasquid/internal/testlib"
	"blitiri.com.ar/go/chasquid/internal/userdb"
)

// Flags.
var (
	externalSMTPAddr = flag.String("external_smtp_addr", "",
		"SMTP server address to test (defaults to use internal)")
	externalSubmissionAddr = flag.String("external_submission_addr", "",
		"submission server address to test (defaults to use internal)")
	externalSubmissionTLSAddr = flag.String("external_submission_tls_addr", "",
		"submission+TLS server address to test (defaults to use internal)")
)

var (
	// Server addresses. Will be filled in at init time.
	// We default to internal ones, but may get overridden via flags.
	smtpAddr          = ""
	submissionAddr    = ""
	submissionTLSAddr = ""

	// TLS configuration to use in the clients.
	// Will contain the generated server certificate as root CA.
	tlsConfig *tls.Config

	// Test couriers, so we can validate that emails got sent.
	localC  = testlib.NewTestCourier()
	remoteC = testlib.NewTestCourier()

	// Max data size, in MiB.
	maxDataSizeMiB = 5
)

//
// === Tests ===
//

func mustDial(tb testing.TB, mode SocketMode, startTLS bool) *smtp.Client {
	addr := ""
	switch mode {
	case ModeSMTP:
		addr = smtpAddr
	case ModeSubmission:
		addr = submissionAddr
	case ModeSubmissionTLS:
		addr = submissionTLSAddr
	}

	var err error
	var conn net.Conn
	if mode.TLS {
		conn, err = tls.Dial("tcp", addr, tlsConfig)
	} else {
		conn, err = net.Dial("tcp", addr)
	}
	if err != nil {
		tb.Fatalf("(net||tls).Dial: %v", err)
	}
	c, err := smtp.NewClient(conn, "127.0.0.1")
	if err != nil {
		tb.Fatalf("smtp.Dial: %v", err)
	}

	if err = c.Hello("test"); err != nil {
		tb.Fatalf("c.Hello: %v", err)
	}

	if startTLS {
		if ok, _ := c.Extension("STARTTLS"); !ok {
			tb.Fatalf("STARTTLS not advertised in EHLO")
		}

		if err = c.StartTLS(tlsConfig); err != nil {
			tb.Fatalf("StartTLS: %v", err)
		}
	}

	return c
}

func sendEmail(tb testing.TB, c *smtp.Client) {
	sendEmailWithAuth(tb, c, nil)
}

func sendEmailWithAuth(tb testing.TB, c *smtp.Client, auth smtp.Auth) {
	var err error
	from := "from@from"

	if auth != nil {
		if err = c.Auth(auth); err != nil {
			tb.Errorf("Auth: %v", err)
		}

		// If we authenticated, we must use the user as from, as the server
		// checks otherwise.
		from = "testuser@localhost"
	}

	if err = c.Mail(from); err != nil {
		tb.Errorf("Mail: %v", err)
	}

	if err = c.Rcpt("to@localhost"); err != nil {
		tb.Errorf("Rcpt: %v", err)
	}

	w, err := c.Data()
	if err != nil {
		tb.Fatalf("Data: %v", err)
	}

	msg := []byte("Subject: Hi!\n\n This is an email\n")
	if _, err = w.Write(msg); err != nil {
		tb.Errorf("Data write: %v", err)
	}

	localC.Expect(1)

	if err = w.Close(); err != nil {
		tb.Errorf("Data close: %v", err)
	}

	localC.Wait()
}

// Checks err is a textproto.Error with the given code and message.
func expectTPErr(tb testing.TB, err error, code int, msg string) {
	tb.Helper()
	if err == nil {
		tb.Fatalf("Expected error %d %q, got nil", code, msg)
	}
	netErr, ok := err.(*textproto.Error)
	if !ok {
		tb.Fatalf("Expected textproto.Error(%d %q), got %T: %v",
			code, msg, err, err)
	}
	if netErr.Code != code || netErr.Msg != msg {
		tb.Fatalf("Expected textproto.Error(%d %q), got %d %q",
			code, msg, netErr.Code, netErr.Msg)
	}
}

func TestSimple(t *testing.T) {
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()
	sendEmail(t, c)
}

func TestSimpleTLS(t *testing.T) {
	c := mustDial(t, ModeSMTP, true)
	defer c.Close()
	sendEmail(t, c)
}

func TestManyEmails(t *testing.T) {
	c := mustDial(t, ModeSMTP, true)
	defer c.Close()
	sendEmail(t, c)
	sendEmail(t, c)
	sendEmail(t, c)
}

func TestAuth(t *testing.T) {
	c := mustDial(t, ModeSubmission, true)
	defer c.Close()

	auth := smtp.PlainAuth("", "testuser@localhost", "testpasswd", "127.0.0.1")
	sendEmailWithAuth(t, c, auth)
}

func TestSubmissionWithoutAuth(t *testing.T) {
	c := mustDial(t, ModeSubmission, true)
	defer c.Close()

	if err := c.Mail("from@from"); err == nil {
		t.Errorf("Mail not failed as expected")
	}
}

func TestAuthOnTLS(t *testing.T) {
	c := mustDial(t, ModeSubmissionTLS, false)
	defer c.Close()

	auth := smtp.PlainAuth("", "testuser@localhost", "testpasswd", "127.0.0.1")
	sendEmailWithAuth(t, c, auth)
}

func TestAuthOnSMTP(t *testing.T) {
	c := mustDial(t, ModeSMTP, true)
	defer c.Close()

	auth := smtp.PlainAuth("", "testuser@localhost", "testpasswd", "127.0.0.1")

	// At least for now, we allow AUTH over the SMTP port to avoid unnecessary
	// complexity, so we expect it to work.
	sendEmailWithAuth(t, c, auth)
}

func TestBrokenAuth(t *testing.T) {
	c := mustDial(t, ModeSubmission, true)
	defer c.Close()

	auth := smtp.PlainAuth("", "user@broken", "passwd", "127.0.0.1")
	err := c.Auth(auth)
	expectTPErr(t, err, 454, "4.7.0 Temporary authentication failure")
}

func TestWrongMailParsing(t *testing.T) {
	addrs := []string{"from", "a b c", "a @ b", "<x>", "<x y>", "><",
		"a@b/c", "a@[1.2.3.4]"}
	for _, addr := range addrs {
		c := mustDial(t, ModeSMTP, false)

		if err := c.Mail(addr); err == nil {
			t.Errorf("Mail not failed as expected with %q", addr)
		}

		if err := c.Mail("from@plain"); err != nil {
			t.Errorf("Mail: %v", err)
		}

		for _, addr := range addrs {
			if err := c.Rcpt(addr); err == nil {
				t.Errorf("Rcpt not failed as expected with %q", addr)
			}
		}

		c.Close()
	}
}

func TestMalformedDomain(t *testing.T) {
	// Unauthenticated MAIL FROM.
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()

	err := c.Mail("a@b/c")
	expectTPErr(t, err, 501, "5.1.8 Malformed sender domain")

	// Authenticated RCPT TO. We need to authenticate, otherwise it may get
	// rejected as relaying.
	c = mustDial(t, ModeSubmission, true)
	defer c.Close()

	auth := smtp.PlainAuth("", "testuser@localhost", "testpasswd", "127.0.0.1")
	if err := c.Auth(auth); err != nil {
		t.Fatalf("Auth: %v", err)
	}

	if err := c.Mail("testuser@localhost"); err != nil {
		t.Fatalf("Mail: %v", err)
	}

	err = c.Rcpt("a@b/c")
	expectTPErr(t, err, 501, "5.1.2 Malformed destination domain")
}

func TestUnsafeAddress(t *testing.T) {
	// Unauthenticated MAIL FROM.
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()

	err := c.Mail(`"a@b"@from`)
	expectTPErr(t, err, 501, "5.1.7 Sender address malformed")

	err = c.Mail("a\u009bb@from")
	expectTPErr(t, err, 501, "5.1.7 Sender address malformed")

	// Characters that need quoting are fine.
	if err := c.Mail(`"a;b"@from`); err != nil {
		t.Errorf("Mail: %v", err)
	}

	// Authenticated RCPT TO. We need to authenticate, otherwise it may get
	// rejected as relaying.
	c = mustDial(t, ModeSubmission, true)
	defer c.Close()

	auth := smtp.PlainAuth("", "testuser@localhost", "testpasswd", "127.0.0.1")
	if err := c.Auth(auth); err != nil {
		t.Fatalf("Auth: %v", err)
	}

	if err := c.Mail("testuser@localhost"); err != nil {
		t.Fatalf("Mail: %v", err)
	}

	err = c.Rcpt(`"a@b"@remote`)
	expectTPErr(t, err, 501, "5.1.3 Malformed destination address")

	err = c.Rcpt("a\u009bb@remote")
	expectTPErr(t, err, 501, "5.1.3 Malformed destination address")
}

func TestNullMailFrom(t *testing.T) {
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()

	addrs := []string{"<>", "  <>", "<> OPTION"}
	for _, addr := range addrs {
		simpleCmd(t, c, fmt.Sprintf("MAIL FROM:%s", addr), 250)
	}
}

func TestRcptBeforeMail(t *testing.T) {
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()

	if err := c.Rcpt("to@to"); err == nil {
		t.Errorf("Rcpt not failed as expected")
	}
}

func TestRcptOption(t *testing.T) {
	c := mustDial(t, ModeSMTP, true)
	defer c.Close()

	if err := c.Mail("from@localhost"); err != nil {
		t.Fatalf("Mail: %v", err)
	}

	params := []string{
		"<to@localhost>", "  <to@localhost>", "<to@localhost> OPTION"}
	for _, p := range params {
		simpleCmd(t, c, fmt.Sprintf("RCPT TO:%s", p), 250)
	}
}

func TestRelayForbidden(t *testing.T) {
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()

	if err := c.Mail("from@somewhere"); err != nil {
		t.Errorf("Mail: %v", err)
	}

	if err := c.Rcpt("to@somewhere"); err == nil {
		t.Errorf("Accepted relay email")
	}
}

func TestTooManyRecipients(t *testing.T) {
	c := mustDial(t, ModeSubmission, true)
	defer c.Close()

	auth := smtp.PlainAuth("", "testuser@localhost", "testpasswd", "127.0.0.1")
	if err := c.Auth(auth); err != nil {
		t.Fatalf("Auth: %v", err)
	}

	if err := c.Mail("testuser@localhost"); err != nil {
		t.Fatalf("Mail: %v", err)
	}

	for i := range 101 {
		if err := c.Rcpt(fmt.Sprintf("to%d@somewhere", i)); err != nil {
			t.Fatalf("Rcpt: %v", err)
		}
	}

	err := c.Rcpt("to102@somewhere")
	expectTPErr(t, err, 452, "4.5.3 Too many recipients")
}

func TestRcptBrokenExists(t *testing.T) {
	c := mustDial(t, ModeSMTP, true)
	defer c.Close()

	if err := c.Mail("from@localhost"); err != nil {
		t.Fatalf("Mail: %v", err)
	}

	err := c.Rcpt("to@broken")
	expectTPErr(t, err, 451, "4.4.3 Temporary error checking address")
}

func TestRcptUserDoesNotExist(t *testing.T) {
	c := mustDial(t, ModeSMTP, true)
	defer c.Close()

	if err := c.Mail("from@localhost"); err != nil {
		t.Fatalf("Mail: %v", err)
	}

	err := c.Rcpt("doesnotexist@localhost")
	expectTPErr(t, err, 550,
		"5.1.1 Destination address is unknown (user does not exist)")
}

var str1MiB string

func sendLargeEmail(tb testing.TB, c *smtp.Client, sizeMiB int) error {
	tb.Helper()
	if err := c.Mail("from@from"); err != nil {
		tb.Fatalf("Mail: %v", err)
	}
	if err := c.Rcpt("to@localhost"); err != nil {
		tb.Fatalf("Rcpt: %v", err)
	}

	w, err := c.Data()
	if err != nil {
		tb.Fatalf("Data: %v", err)
	}

	if _, err := w.Write([]byte("Subject: I ate too much\n\n")); err != nil {
		tb.Fatalf("Data write: %v", err)
	}

	// Write the 1 MiB string sizeMiB times.
	for range sizeMiB {
		if _, err := w.Write([]byte(str1MiB)); err != nil {
			tb.Fatalf("Data write: %v", err)
		}
	}

	return w.Close()
}

func TestTooMuchData(t *testing.T) {
	c := mustDial(t, ModeSMTP, true)
	defer c.Close()

	localC.Expect(1)
	err := sendLargeEmail(t, c, maxDataSizeMiB-1)
	if err != nil {
		t.Errorf("Error sending large but ok email: %v", err)
	}
	localC.Wait()

	// Repeat the test - we want to check that the limit applies to each
	// message, not the entire connection.
	localC.Expect(1)
	err = sendLargeEmail(t, c, maxDataSizeMiB-1)
	if err != nil {
		t.Errorf("Error sending large but ok email: %v", err)
	}
	localC.Wait()

	err = sendLargeEmail(t, c, maxDataSizeMiB+1)
	expectTPErr(t, err, 552, "5.3.4 Message too big")

	// Repeat the test once again, the limit should not prevent connection
	// from continuing.
	localC.Expect(1)
	err = sendLargeEmail(t, c, maxDataSizeMiB-1)
	if err != nil {
		t.Errorf("Error sending large but ok email: %v", err)
	}
	localC.Wait()
}

func simpleCmd(t *testing.T, c *smtp.Client, cmd string, expected int) string {
	t.Helper()
	if err := c.Text.PrintfLine("%s", cmd); err != nil {
		t.Fatalf("Failed to write %s: %v", cmd, err)
	}

	_, msg, err := c.Text.ReadResponse(expected)
	if err != nil {
		t.Errorf("Incorrect %s response: %v", cmd, err)
	}
	return msg
}

func TestSimpleCommands(t *testing.T) {
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()
	simpleCmd(t, c, "HELP", 214)
	simpleCmd(t, c, "NOOP", 250)
	simpleCmd(t, c, "VRFY", 502)
	simpleCmd(t, c, "EXPN", 502)
}

func TestLongLines(t *testing.T) {
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()

	// Send a not-too-long line.
	simpleCmd(t, c, fmt.Sprintf("%1000s", "x"), 500)

	// Send a very long line, expect an error.
	msg := simpleCmd(t, c, fmt.Sprintf("%1001s", "x"), 554)
	if msg != "error reading command: line too long" {
		t.Errorf("Expected 'line too long', got %v", msg)
	}
}

func TestReset(t *testing.T) {
	c := mustDial(t, ModeSMTP, false)
	defer c.Close()

	if err := c.Mail("from@plain"); err != nil {
		t.Fatalf("MAIL FROM: %v", err)
	}

	if err := c.Reset(); err != nil {
		t.Errorf("RSET: %v", err)
	}

	if err := c.Mail("from@plain"); err != nil {
		t.Errorf("MAIL after RSET: %v", err)
	}
}

func TestRepeatedStartTLS(t *testing.T) {
	c, err := smtp.Dial(smtpAddr)
	if err != nil {
		t.Fatalf("smtp.Dial: %v", err)
	}

	if err = c.StartTLS(tlsConfig); err != nil {
		t.Fatalf("StartTLS: %v", err)
	}

	if err = c.StartTLS(tlsConfig); err == nil {
		t.Errorf("Second STARTTLS did not fail as expected")
	}
}

// Test that STARTTLS fails on a TLS connection.
func TestStartTLSOnTLS(t *testing.T) {
	c := mustDial(t, ModeSubmissionTLS, false)
	defer c.Close()

	if err := c.StartTLS(tlsConfig); err == nil {
		t.Errorf("STARTTLS did not fail as expected")
	}
}

// The server name requested via SNI must only be used as our hostname if one
// of our certificates is valid for it.
func TestServerNameFromSNI(t *testing.T) {
	cases := []struct {
		sni  string
		want string
	}{
		{"localhost", "localhost"},
		{"other.example.com", "localhost"},
		{"localhost; dkim=pass header.d=example.com", "localhost"},
	}

	for _, c := range cases {
		cfg := tlsConfig.Clone()
		cfg.ServerName = c.sni
		cfg.InsecureSkipVerify = true

		// STARTTLS, and then EHLO, whose reply starts with our hostname.
		sc, err := smtp.Dial(smtpAddr)
		if err != nil {
			t.Fatalf("smtp.Dial: %v", err)
		}
		if err := sc.StartTLS(cfg); err != nil {
			t.Fatalf("%q: StartTLS: %v", c.sni, err)
		}
		msg := simpleCmd(t, sc, "EHLO test", 250)
		if !strings.HasPrefix(msg, c.want+" ") {
			t.Errorf("%q: STARTTLS: EHLO reply %q, expected hostname %q",
				c.sni, msg, c.want)
		}
		sc.Close()

		// TLS-wrapped, where the greeting contains our hostname.
		conn, err := tls.Dial("tcp", submissionTLSAddr, cfg)
		if err != nil {
			t.Fatalf("%q: tls.Dial: %v", c.sni, err)
		}
		_, msg, err = textproto.NewConn(conn).ReadResponse(220)
		if err != nil {
			t.Fatalf("%q: reading greeting: %v", c.sni, err)
		}
		if !strings.HasPrefix(msg, c.want+" ") {
			t.Errorf("%q: TLS: greeting %q, expected hostname %q",
				c.sni, msg, c.want)
		}
		conn.Close()
	}
}

func TestAddDKIMSigner(t *testing.T) {
	s := NewServer()
	err := s.AddDKIMSigner("example.com", "selector", "keyfile-does-not-exist")
	if !errors.Is(err, fs.ErrNotExist) {
		t.Errorf("AddDKIMSigner: expected not exist, got %v", err)
	}

	tmpDir := t.ArtifactDir()

	// Invalid PEM file.
	kf1 := tmpDir + "/key1-bad_pem.pem"
	testlib.Rewrite(t, kf1, "not a valid PEM file")
	err = s.AddDKIMSigner("example.com", "selector", kf1)
	if !errors.Is(err, errDecodingPEMBlock) {
		t.Errorf("AddDKIMSigner: expected %v, got %v",
			errDecodingPEMBlock, err)
	}

	// Unsupported block type.
	kf2 := tmpDir + "/key2.pem"
	testlib.Rewrite(t, kf2,
		"-----BEGIN TEST KEY-----\n-----END TEST KEY-----")
	err = s.AddDKIMSigner("example.com", "selector", kf2)
	if !errors.Is(err, errUnsupportedBlockType) {
		t.Errorf("AddDKIMSigner: expected %v, got %v",
			errUnsupportedBlockType, err)
	}

	// x509 error: this is an ed448 key, which is not supported.
	kf3 := tmpDir + "/key3.pem"
	testlib.Rewrite(t, kf3, `-----BEGIN PRIVATE KEY-----
MEcCAQAwBQYDK2VxBDsEOSBHT9DNG6/FNBnRGrLay+jIrK8WrViiVMz9AoXqYSb6
ghwTZSd3E0X8oIFTgs9ch3pxJM1KDrs4NA==
-----END PRIVATE KEY-----`)
	err = s.AddDKIMSigner("example.com", "selector", kf3)
	if !strings.Contains(err.Error(),
		"x509: PKCS#8 wrapping contained private key with unknown algorithm") {
		t.Errorf("AddDKIMSigner: expected x509 error, got %q", err.Error())
	}

	// Unsupported key type: X25519.
	kf4 := tmpDir + "/key4.pem"
	testlib.Rewrite(t, kf4, `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VuBCIEIKBUDwEDc5cCv/yEvnA93yk0gXyiTZe7Qip8QU3rJuZC
-----END PRIVATE KEY-----`)
	err = s.AddDKIMSigner("example.com", "selector", kf4)
	if !errors.Is(err, errUnsupportedKeyType) {
		t.Errorf("AddDKIMSigner: expected %v, got %v",
			errUnsupportedKeyType, err)
	}

	// Successful.
	kf5 := tmpDir + "/key5.pem"
	testlib.Rewrite(t, kf5, `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEID6bjSoiW6g6NJA67RNl0SZ7zpylVOq9w/VGAXF5whnS
-----END PRIVATE KEY-----`)
	err = s.AddDKIMSigner("example.com", "selector", kf5)
	if err != nil {
		t.Errorf("AddDKIMSigner: %v", err)
	}
}

// chanListener is a net.Listener that returns the results sent to it from
// Accept, and net.ErrClosed once the channel is closed.
type chanListener chan acceptResult

type acceptResult struct {
	conn net.Conn
	err  error
}

func (l chanListener) Accept() (net.Conn, error) {
	r, ok := <-l
	if !ok {
		return nil, net.ErrClosed
	}
	return r.conn, r.err
}

func (l chanListener) Close() error   { return nil }
func (l chanListener) Addr() net.Addr { return &net.TCPAddr{} }

// serveInBackground runs s.serve(l) in a goroutine, and returns a channel
// that is closed once it returns.
func serveInBackground(s *Server, l net.Listener) chan struct{} {
	done := make(chan struct{})
	go func() {
		s.serve(l, ModeSMTP)
		close(done)
	}()
	return done
}

func waitForServe(t *testing.T, done chan struct{}) {
	t.Helper()
	select {
	case <-done:
	case <-time.After(5 * time.Second):
		t.Fatalf("serve did not return after net.ErrClosed")
	}
}

// newPipeConn sends a new connection to the listener, and returns the client
// side of it.
func newPipeConn(l chanListener) *textproto.Conn {
	srvConn, cliConn := net.Pipe()
	l <- acceptResult{conn: srvConn}
	return textproto.NewConn(cliConn)
}

func expectGreeting(t *testing.T, c *textproto.Conn, hostname string) {
	t.Helper()
	greeting, err := c.ReadLine()
	if err != nil {
		t.Fatalf("error reading greeting: %v", err)
	}
	if want := "220 " + hostname + " ESMTP chasquid"; greeting != want {
		t.Errorf("got greeting %q, expected %q", greeting, want)
	}
}

func TestAcceptErrors(t *testing.T) {
	s := NewServer()
	s.Hostname = "acceptsrv"
	prevErrors := acceptErrors.Value()

	emfile := &net.OpError{Op: "accept", Net: "tcp",
		Err: os.NewSyscallError("accept4", syscall.EMFILE)}
	srvConn, cliConn := net.Pipe()
	defer cliConn.Close()

	l := make(chanListener, 4)
	l <- acceptResult{err: emfile}
	l <- acceptResult{err: emfile}
	l <- acceptResult{conn: srvConn}
	l <- acceptResult{err: emfile}
	close(l)

	// serve should retry on errors, and return once the listener reports
	// net.ErrClosed.
	done := serveInBackground(s, l)

	// The connection we got in between the errors must have been handled.
	expectGreeting(t, textproto.NewConn(cliConn), "acceptsrv")

	waitForServe(t, done)

	if n := acceptErrors.Value() - prevErrors; n != 3 {
		t.Errorf("acceptErrors increased by %d, expected 3", n)
	}
}

func TestConnectionLimit(t *testing.T) {
	s := NewServer()
	s.Hostname = "limitsrv"
	s.maxConns = 2
	prevOverLimit := connectionsOverLimit.Value()

	l := make(chanListener)
	done := serveInBackground(s, l)

	// The first two connections are within the limit.
	c1 := newPipeConn(l)
	expectGreeting(t, c1, "limitsrv")
	c2 := newPipeConn(l)
	defer c2.Close()
	expectGreeting(t, c2, "limitsrv")

	// The third one is over the limit, and gets closed without a greeting.
	c3 := newPipeConn(l)
	if line, err := c3.ReadLine(); err != io.EOF {
		t.Errorf("over the limit: got %q, %v; expected EOF", line, err)
	}
	if n := connectionsOverLimit.Value() - prevOverLimit; n != 1 {
		t.Errorf("connectionsOverLimit increased by %d, expected 1", n)
	}

	// Once a connection finishes, we can accept new ones again.
	c1.Close()
	deadline := time.Now().Add(5 * time.Second)
	for s.activeConns.Load() != 1 {
		if time.Now().After(deadline) {
			t.Fatalf("activeConns = %d, expected 1", s.activeConns.Load())
		}
		time.Sleep(time.Millisecond)
	}

	c4 := newPipeConn(l)
	defer c4.Close()
	expectGreeting(t, c4, "limitsrv")

	close(l)
	waitForServe(t, done)
}

//
// === Benchmarks ===
//

func BenchmarkManyEmails(b *testing.B) {
	c := mustDial(b, ModeSMTP, false)
	defer c.Close()

	for b.Loop() {
		sendEmail(b, c)
	}
}

func BenchmarkManyEmailsParallel(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		c := mustDial(b, ModeSMTP, false)
		defer c.Close()

		for pb.Next() {
			sendEmail(b, c)
		}
	})
}

//
// === Test environment ===
//

type brokenAuthBE struct{}

func (b brokenAuthBE) Authenticate(user, password string) (bool, error) {
	return false, fmt.Errorf("failed to auth")
}

func (b brokenAuthBE) Exists(user string) (bool, error) {
	return false, fmt.Errorf("failed to check if user exists")
}

func (b brokenAuthBE) Reload() error {
	return fmt.Errorf("failed to reload")
}

// realMain is the real main function, which returns the value to pass to
// os.Exit(). We have to do this so we can use defer.
func realMain(m *testing.M) int {
	flag.Parse()

	// Create a 1MiB string, which the large message tests use.
	str1MiB = strings.Repeat("a", 1024*1024)

	// Set up the mail log to stdout, which is captured by the test runner,
	// so we have better debugging information on failures.
	maillog.Default = maillog.New(os.Stdout)

	if *externalSMTPAddr != "" {
		smtpAddr = *externalSMTPAddr
		submissionAddr = *externalSubmissionAddr
		submissionTLSAddr = *externalSubmissionTLSAddr
		tlsConfig = &tls.Config{
			InsecureSkipVerify: true,
		}
	} else {
		// Generate certificates in a temporary directory.
		tmpDir, err := os.MkdirTemp("", "chasquid_test:")
		if err != nil {
			fmt.Printf("Failed to create temp dir: %v\n", tmpDir)
			return 1
		}
		defer os.RemoveAll(tmpDir)

		tlsConfig, err = testlib.GenerateCert(tmpDir)
		if err != nil {
			fmt.Printf("Failed to generate cert for testing: %v\n", err)
			return 1
		}

		s := NewServer()
		s.Hostname = "localhost"
		s.MaxDataSize = int64(maxDataSizeMiB) * 1024 * 1024
		s.AddCerts(tmpDir+"/cert.pem", tmpDir+"/key.pem")

		// Open the listeners ourselves, so the ports are already bound
		// (and we know their addresses) by the time the tests run.
		for _, l := range []struct {
			addr *string
			mode SocketMode
		}{
			{&smtpAddr, ModeSMTP},
			{&submissionAddr, ModeSubmission},
			{&submissionTLSAddr, ModeSubmissionTLS},
		} {
			ln, err := net.Listen("tcp", "localhost:0")
			if err != nil {
				fmt.Printf("Failed to listen: %v\n", err)
				return 1
			}
			*l.addr = ln.Addr().String()
			s.AddListeners([]net.Listener{ln}, l.mode)
		}

		s.InitQueue(tmpDir+"/queue", localC, remoteC)

		dinfo, err := domaininfo.New(tmpDir + "/domaininfo")
		if err != nil {
			fmt.Printf("Error initializing domaininfo: %v", err)
			return 1
		}
		s.SetDomainInfo(dinfo)

		udb := userdb.New("/dev/null")
		udb.AddUser("testuser", "testpasswd")
		s.aliasesR.AddAliasForTesting(
			"to@localhost", "testuser@localhost", nil, aliases.EMAIL)
		s.authr.Register("localhost", auth.WrapNoErrorBackend(udb))
		s.AddDomain("localhost")

		s.AddDomain("broken")
		s.authr.Register("broken", &brokenAuthBE{})

		// Disable SPF lookups, to avoid leaking DNS queries.
		disableSPFForTesting = true

		// Disable reloading.
		reloadEvery = nil

		go s.ListenAndServe()
	}

	return m.Run()
}

func TestMain(m *testing.M) {
	os.Exit(realMain(m))
}