parsimony/internal/outbound/outbound_test.go
tgiachi beff2579c2 test: pin the SMTP command named in a relay error, SMTPUTF8 and thread order
- Name the refused command in the error (MAIL FROM, RCPT TO, DATA) so the queue tells a refused sender from a refused recipient
- Test a refusal of the DATA command itself, SMTPUTF8 for non-ASCII addresses and oldest-first threads
2026-11-10 11:42:19 +01:00

507 lines
16 KiB
Go

package outbound
import (
"context"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"testing"
"time"
"github.com/emersion/go-smtp"
)
type captured struct {
from string
to []string
data string
}
type capture struct {
mu sync.Mutex
msgs []captured
}
func (c *capture) NewSession(*smtp.Conn) (smtp.Session, error) { return &session{c: c}, nil }
type session struct {
c *capture
from string
to []string
}
func (s *session) Reset() { s.from, s.to = "", nil }
func (s *session) Logout() error { return nil }
func (s *session) Mail(from string, _ *smtp.MailOptions) error {
s.from = from
return nil
}
func (s *session) Rcpt(to string, _ *smtp.RcptOptions) error {
s.to = append(s.to, to)
return nil
}
func (s *session) Data(r io.Reader) error {
b, _ := io.ReadAll(r)
s.c.mu.Lock()
s.c.msgs = append(s.c.msgs, captured{s.from, s.to, string(b)})
s.c.mu.Unlock()
return nil
}
func startServer(t *testing.T) (string, *capture) {
t.Helper()
cap := &capture{}
srv := smtp.NewServer(cap)
srv.AllowInsecureAuth = true
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
go srv.Serve(l)
t.Cleanup(func() { srv.Close() })
return l.Addr().String(), cap
}
func TestSMTPRelaySend(t *testing.T) {
addr, cap := startServer(t)
r := &SMTPRelay{Addr: addr}
err := r.Send(context.Background(), "bounce@example.org", []string{"a@x.com", "b@x.com"}, []byte("Subject: hi\r\n\r\nbody\r\n"))
if err != nil {
t.Fatalf("send: %v", err)
}
if len(cap.msgs) != 1 {
t.Fatalf("want 1 transaction, got %d", len(cap.msgs))
}
m := cap.msgs[0]
if m.from != "bounce@example.org" || len(m.to) != 2 {
t.Fatalf("unexpected envelope: %+v", m)
}
if m.data != "Subject: hi\r\n\r\nbody\r\n" {
t.Fatalf("data = %q", m.data)
}
}
func TestSMTPRelayBatches(t *testing.T) {
addr, cap := startServer(t)
r := &SMTPRelay{Addr: addr, BatchSize: 50}
var to []string
for i := 0; i < 120; i++ {
to = append(to, fmt.Sprintf("u%d@x.com", i))
}
if err := r.Send(context.Background(), "b@example.org", to, []byte("Subject: x\r\n\r\n.\r\n")); err != nil {
t.Fatal(err)
}
if len(cap.msgs) != 3 {
t.Fatalf("want 3 transactions, got %d", len(cap.msgs))
}
if len(cap.msgs[0].to) != 50 || len(cap.msgs[2].to) != 20 {
t.Fatalf("batch sizes: %d %d", len(cap.msgs[0].to), len(cap.msgs[2].to))
}
}
func TestSMTPRelayUnreachable(t *testing.T) {
r := &SMTPRelay{Addr: "127.0.0.1:1"}
if err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("x")); err == nil {
t.Fatal("want error")
}
}
func TestFakeRecords(t *testing.T) {
f := &Fake{}
f.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("m"))
if len(f.Sent) != 1 || f.Sent[0].From != "b@x.org" || string(f.Sent[0].Data) != "m" {
t.Fatalf("fake: %+v", f.Sent)
}
}
func TestSMTPRelayDefaultBatchSize(t *testing.T) {
addr, cap := startServer(t)
r := &SMTPRelay{Addr: addr} // BatchSize left at zero
var to []string
for i := 0; i < 120; i++ {
to = append(to, fmt.Sprintf("u%d@x.com", i))
}
if err := r.Send(context.Background(), "b@example.org", to, []byte("Subject: x\r\n\r\n.\r\n")); err != nil {
t.Fatal(err)
}
if len(cap.msgs) != 3 {
t.Fatalf("default batch size must be %d, got %d transactions", DefaultBatchSize, len(cap.msgs))
}
}
func TestClassifyErrors(t *testing.T) {
smtpErr := func(code int, enh smtp.EnhancedCode) error {
return &smtp.SMTPError{Code: code, EnhancedCode: enh, Message: "x"}
}
cases := []struct {
name string
err error
perm, mailbox bool
}{
{"nil", nil, false, false},
{"plain", errors.New("connection refused"), false, false},
{"451", smtpErr(451, smtp.EnhancedCode{4, 3, 0}), false, false},
{"421", smtpErr(421, smtp.NoEnhancedCode), false, false},
{"451 at RCPT", AtRcpt(smtpErr(451, smtp.EnhancedCode{4, 1, 1})), false, false},
{"554 relay denied", smtpErr(554, smtp.EnhancedCode{5, 7, 1}), true, false},
{"550 no enhanced code", smtpErr(550, smtp.NoEnhancedCode), true, false},
{"explicit permanent", &PermanentError{Err: errors.New("no")}, true, false},
{"explicit bad mailbox", &PermanentError{Err: errors.New("no"), BadMailbox: true}, true, true},
// The same enhanced code means different things depending on the command it answers.
{"5.1.1 without a command", smtpErr(550, smtp.EnhancedCode{5, 1, 1}), true, false},
{"5.1.1 at RCPT", AtRcpt(smtpErr(550, smtp.EnhancedCode{5, 1, 1})), true, true},
{"5.1.2 at RCPT", AtRcpt(smtpErr(550, smtp.EnhancedCode{5, 1, 2})), true, true},
{"5.1.3 at RCPT", AtRcpt(smtpErr(553, smtp.EnhancedCode{5, 1, 3})), true, true},
{"5.1.6 at RCPT", AtRcpt(smtpErr(550, smtp.EnhancedCode{5, 1, 6})), true, true},
{"5.1.10 at RCPT", AtRcpt(smtpErr(550, smtp.EnhancedCode{5, 1, 10})), true, true},
{"wrapped, at RCPT", AtRcpt(fmt.Errorf("batch 0-1: %w", smtpErr(550, smtp.EnhancedCode{5, 1, 1}))), true, true},
{"5.1.0 at RCPT", AtRcpt(smtpErr(550, smtp.EnhancedCode{5, 1, 0})), true, false},
{"5.1.4 at RCPT", AtRcpt(smtpErr(550, smtp.EnhancedCode{5, 1, 4})), true, false},
{"5.1.7 at RCPT (bad sender)", AtRcpt(smtpErr(553, smtp.EnhancedCode{5, 1, 7})), true, false},
{"5.1.8 at RCPT (bad sender)", AtRcpt(smtpErr(553, smtp.EnhancedCode{5, 1, 8})), true, false},
{"5.2.2 at RCPT (mailbox full)", AtRcpt(smtpErr(552, smtp.EnhancedCode{5, 2, 2})), true, false},
{"5.7.1 at RCPT (policy)", AtRcpt(smtpErr(554, smtp.EnhancedCode{5, 7, 1})), true, false},
}
for _, c := range cases {
if got := IsPermanent(c.err); got != c.perm {
t.Errorf("%s: IsPermanent = %v", c.name, got)
}
if got := IsBadMailbox(c.err); got != c.mailbox {
t.Errorf("%s: IsBadMailbox = %v", c.name, got)
}
}
}
// rejecting refuses one stage of the SMTP transaction.
type rejecting struct{ mail, rcpt, data error }
func (r rejecting) NewSession(*smtp.Conn) (smtp.Session, error) { return &rejectSession{r}, nil }
type rejectSession struct{ r rejecting }
func (s *rejectSession) Reset() {}
func (s *rejectSession) Logout() error { return nil }
func (s *rejectSession) Mail(string, *smtp.MailOptions) error { return s.r.mail }
func (s *rejectSession) Rcpt(string, *smtp.RcptOptions) error { return s.r.rcpt }
func (s *rejectSession) Data(io.Reader) error { return s.r.data }
func se(code int, enh smtp.EnhancedCode, msg string) error {
return &smtp.SMTPError{Code: code, EnhancedCode: enh, Message: msg}
}
func TestRelayRejectionsAreClassifiedByTheCommandTheyAnswer(t *testing.T) {
for name, tc := range map[string]struct {
r rejecting
perm, mailbox bool
}{
"unknown user at RCPT": {rejecting{rcpt: se(550, smtp.EnhancedCode{5, 1, 1}, "no such user")}, true, true},
"relay denied at RCPT": {rejecting{rcpt: se(554, smtp.EnhancedCode{5, 7, 1}, "relay access denied")}, true, false},
"try later at RCPT": {rejecting{rcpt: se(451, smtp.EnhancedCode{4, 3, 0}, "try later")}, false, false},
"bad sender 5.1.8 at MAIL": {rejecting{mail: se(550, smtp.EnhancedCode{5, 1, 8}, "bad outbound sender")}, true, false},
"sender domain 5.1.0 at MAIL": {rejecting{mail: se(550, smtp.EnhancedCode{5, 1, 0}, "sender domain not found")}, true, false},
"unlisted sender 5.1.7 at MAIL": {rejecting{mail: se(553, smtp.EnhancedCode{5, 1, 7}, "sender address rejected")}, true, false},
"even 5.1.1 at MAIL is the sender": {rejecting{mail: se(550, smtp.EnhancedCode{5, 1, 1}, "sender unknown")}, true, false},
"5.1.1 after DATA is not a mailbox": {rejecting{data: se(550, smtp.EnhancedCode{5, 1, 1}, "content refused")}, true, false},
"policy refusal after DATA": {rejecting{data: se(554, smtp.EnhancedCode{5, 7, 1}, "spam")}, true, false},
"temporary after DATA": {rejecting{data: se(451, smtp.EnhancedCode{4, 3, 0}, "try later")}, false, false},
} {
srv := smtp.NewServer(tc.r)
ln, _ := net.Listen("tcp", "127.0.0.1:0")
go srv.Serve(ln)
r := &SMTPRelay{Addr: ln.Addr().String()}
err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("Subject: x\r\n\r\nx\r\n"))
srv.Close()
if err == nil {
t.Fatalf("%s: want an error", name)
}
if IsPermanent(err) != tc.perm || IsBadMailbox(err) != tc.mailbox {
t.Errorf("%s: permanent=%v mailbox=%v for %v", name, IsPermanent(err), IsBadMailbox(err), err)
}
// The error says which command was refused, for the admin reading the queue.
stage := map[bool]string{true: "RCPT TO: ", false: ""}[tc.r.rcpt != nil]
if tc.r.mail != nil {
stage = "MAIL FROM: "
}
if tc.r.data != nil {
stage = "DATA: "
}
if !strings.HasPrefix(err.Error(), stage) {
t.Errorf("%s: error %q should start with %q", name, err.Error(), stage)
}
}
}
// quitDropper is a bare SMTP server that accepts the message and then hangs up on QUIT.
type quitDropper struct {
mu sync.Mutex
accepted int
}
func startQuitDropper(t *testing.T, reply string) (*quitDropper, string) {
t.Helper()
q := &quitDropper{}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() {
defer c.Close()
c.Write([]byte("220 test ESMTP\r\n"))
buf := make([]byte, 0, 4096)
one := make([]byte, 1)
inData := false
line := func() (string, bool) {
buf = buf[:0]
for {
if _, err := c.Read(one); err != nil {
return "", false
}
buf = append(buf, one[0])
if len(buf) >= 2 && buf[len(buf)-2] == '\r' && buf[len(buf)-1] == '\n' {
return string(buf[:len(buf)-2]), true
}
}
}
for {
l, ok := line()
if !ok {
return
}
switch {
case inData:
if l == "." {
inData = false
q.mu.Lock()
q.accepted++
q.mu.Unlock()
c.Write([]byte("250 2.0.0 queued\r\n"))
}
case strings.HasPrefix(l, "EHLO"), strings.HasPrefix(l, "HELO"):
c.Write([]byte("250 test\r\n"))
case strings.HasPrefix(l, "MAIL"), strings.HasPrefix(l, "RCPT"):
c.Write([]byte("250 2.1.0 ok\r\n"))
case l == "DATA":
inData = true
c.Write([]byte("354 go ahead\r\n"))
case l == "QUIT":
if reply != "" {
c.Write([]byte(reply))
}
return // hang up
}
}
}()
}
}()
return q, ln.Addr().String()
}
func TestAFailedQuitAfterTheMessageWasAcceptedIsNotAFailure(t *testing.T) {
// The relay said 250 to the message: retrying because QUIT misbehaved would deliver it twice.
for name, reply := range map[string]string{"hang up without a reply": "", "421 instead of 221": "421 4.4.2 closing early\r\n"} {
q, addr := startQuitDropper(t, reply)
err := (&SMTPRelay{Addr: addr}).Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("Subject: x\r\n\r\nx\r\n"))
if err != nil {
t.Errorf("%s: Send must succeed, the message was accepted: %v", name, err)
}
q.mu.Lock()
if q.accepted != 1 {
t.Errorf("%s: accepted %d times", name, q.accepted)
}
q.mu.Unlock()
}
}
// silent accepts connections and never says a word.
func silent(t *testing.T) string {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
var conns []net.Conn
var mu sync.Mutex
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
mu.Lock()
conns = append(conns, c)
mu.Unlock()
}
}()
t.Cleanup(func() {
ln.Close()
mu.Lock()
for _, c := range conns {
c.Close()
}
mu.Unlock()
})
return ln.Addr().String()
}
func TestSendStopsWhenTheContextIsCancelled(t *testing.T) {
r := &SMTPRelay{Addr: silent(t)}
ctx, cancel := context.WithCancel(context.Background())
errc := make(chan error, 1)
go func() { errc <- r.Send(ctx, "b@x.org", []string{"a@x.com"}, []byte("x")) }()
time.Sleep(100 * time.Millisecond)
cancel()
select {
case err := <-errc:
if !errors.Is(err, context.Canceled) {
t.Fatalf("want context.Canceled, got %v", err)
}
case <-time.After(2 * time.Second):
t.Fatal("Send ignored the cancelled context while talking to a silent relay")
}
}
func TestSendHonoursTheContextDeadline(t *testing.T) {
r := &SMTPRelay{Addr: silent(t)}
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)
defer cancel()
start := time.Now()
err := r.Send(ctx, "b@x.org", []string{"a@x.com"}, []byte("x"))
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("want context.DeadlineExceeded, got %v", err)
}
if took := time.Since(start); took > 2*time.Second {
t.Fatalf("took %v", took)
}
if IsPermanent(err) {
t.Fatalf("a timeout is temporary: %v", err)
}
}
func TestSingleBatchErrorsAreNotDecoratedWithBatchNumbers(t *testing.T) {
r := &SMTPRelay{Addr: "127.0.0.1:1"}
err := r.Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("x"))
if err == nil || strings.HasPrefix(err.Error(), "batch") {
t.Fatalf("one recipient, one batch: the error should be the plain cause, got %v", err)
}
many := make([]string, 120)
for i := range many {
many[i] = fmt.Sprintf("u%d@x.com", i)
}
err = (&SMTPRelay{Addr: "127.0.0.1:1", BatchSize: 50}).Send(context.Background(), "b@x.org", many, []byte("x"))
if err == nil || !strings.HasPrefix(err.Error(), "batch 0-50:") {
t.Fatalf("with several batches the failing range is useful: %v", err)
}
}
func TestNonASCIIAddressesAskForSMTPUTF8(t *testing.T) {
var gotUTF8 []bool
var mu sync.Mutex
backend := smtp.BackendFunc(func(*smtp.Conn) (smtp.Session, error) {
return &utf8Session{record: func(v bool) { mu.Lock(); gotUTF8 = append(gotUTF8, v); mu.Unlock() }}, nil
})
srv := smtp.NewServer(backend)
srv.EnableSMTPUTF8 = true
ln, _ := net.Listen("tcp", "127.0.0.1:0")
go srv.Serve(ln)
defer srv.Close()
r := &SMTPRelay{Addr: ln.Addr().String()}
if err := r.Send(context.Background(), "bounce@example.org", []string{"jörg@example.com"}, []byte("Subject: x\r\n\r\nx\r\n")); err != nil {
t.Fatal(err)
}
if err := r.Send(context.Background(), "bounce@example.org", []string{"plain@example.com"}, []byte("Subject: x\r\n\r\nx\r\n")); err != nil {
t.Fatal(err)
}
mu.Lock()
defer mu.Unlock()
if len(gotUTF8) != 2 || !gotUTF8[0] || gotUTF8[1] {
t.Fatalf("SMTPUTF8 only for the message that needs it: %v", gotUTF8)
}
}
type utf8Session struct{ record func(bool) }
func (s *utf8Session) Reset() {}
func (s *utf8Session) Logout() error { return nil }
func (s *utf8Session) Mail(_ string, o *smtp.MailOptions) error {
s.record(o != nil && o.UTF8)
return nil
}
func (s *utf8Session) Rcpt(string, *smtp.RcptOptions) error { return nil }
func (s *utf8Session) Data(r io.Reader) error { io.Copy(io.Discard, r); return nil }
// refuseData is a bare SMTP server that accepts MAIL and RCPT but answers the DATA command itself with reply.
func refuseData(t *testing.T, reply string) string {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() {
defer c.Close()
c.Write([]byte("220 test ESMTP\r\n"))
one := make([]byte, 1)
var buf []byte
for {
if _, err := c.Read(one); err != nil {
return
}
buf = append(buf, one[0])
if len(buf) < 2 || buf[len(buf)-2] != '\r' || buf[len(buf)-1] != '\n' {
continue
}
l := string(buf[:len(buf)-2])
buf = buf[:0]
switch {
case strings.HasPrefix(l, "EHLO"), strings.HasPrefix(l, "HELO"):
c.Write([]byte("250 test\r\n"))
case strings.HasPrefix(l, "MAIL"), strings.HasPrefix(l, "RCPT"):
c.Write([]byte("250 2.1.0 ok\r\n"))
case l == "DATA":
c.Write([]byte(reply))
case l == "QUIT":
c.Write([]byte("221 bye\r\n"))
return
}
}
}()
}
}()
return ln.Addr().String()
}
func TestARefusalOfTheDATACommandItselfIsNotAMissingMailbox(t *testing.T) {
// 5.1.1 here would be absurd, but a hostile or broken relay must not be able to strike members with it.
for name, tc := range map[string]struct {
reply string
perm bool
}{
"554 no valid recipients": {"554 5.5.1 no valid recipients\r\n", true},
"5.1.1 at DATA": {"550 5.1.1 whatever\r\n", true},
"451 try later": {"451 4.3.0 try later\r\n", false},
} {
err := (&SMTPRelay{Addr: refuseData(t, tc.reply)}).Send(context.Background(), "b@x.org", []string{"a@x.com"}, []byte("Subject: x\r\n\r\nx\r\n"))
if err == nil || !strings.HasPrefix(err.Error(), "DATA: ") {
t.Fatalf("%s: the error says DATA was refused: %v", name, err)
}
if IsBadMailbox(err) || IsPermanent(err) != tc.perm {
t.Errorf("%s: permanent=%v mailbox=%v", name, IsPermanent(err), IsBadMailbox(err))
}
}
}