From beff2579c27524657c54cb90c794dc8b20f63b02 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:42:19 +0100 Subject: [PATCH] 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 --- internal/outbound/outbound.go | 9 ++- internal/outbound/outbound_test.go | 114 +++++++++++++++++++++++++++++ internal/store/archive_test.go | 24 ++++++ 3 files changed, 146 insertions(+), 1 deletion(-) diff --git a/internal/outbound/outbound.go b/internal/outbound/outbound.go index 95a0ef8..09ebcaa 100644 --- a/internal/outbound/outbound.go +++ b/internal/outbound/outbound.go @@ -279,7 +279,14 @@ type PhaseError struct { Err error } -func (e *PhaseError) Error() string { return e.Err.Error() } +// Error names the command, so the admin can tell a refused recipient from a refused sender. +func (e *PhaseError) Error() string { + label := map[string]string{"MAIL": "MAIL FROM", "RCPT": "RCPT TO", "DATA": "DATA"}[e.Phase] + if label == "" { + label = e.Phase + } + return label + ": " + e.Err.Error() +} func (e *PhaseError) Unwrap() error { return e.Err } // AtRcpt marks err as the relay's answer to RCPT TO (SMTPRelay does this itself; it is exported diff --git a/internal/outbound/outbound_test.go b/internal/outbound/outbound_test.go index a873fee..203dad5 100644 --- a/internal/outbound/outbound_test.go +++ b/internal/outbound/outbound_test.go @@ -220,6 +220,17 @@ func TestRelayRejectionsAreClassifiedByTheCommandTheyAnswer(t *testing.T) { 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) + } } } @@ -391,3 +402,106 @@ func TestSingleBatchErrorsAreNotDecoratedWithBatchNumbers(t *testing.T) { 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)) + } + } +} diff --git a/internal/store/archive_test.go b/internal/store/archive_test.go index e44b17f..e21e913 100644 --- a/internal/store/archive_test.go +++ b/internal/store/archive_test.go @@ -445,3 +445,27 @@ func TestThreadRootsFromTheNewestEndOfAChain(t *testing.T) { t.Fatalf("roots: %v", forward) } } + +func TestThreadListingsAreOldestFirst(t *testing.T) { + s := newTestStore(t) + l, _ := s.CreateList("dev", "example.org", "Dev") + late := msg("", "Late reply", "b") + late.Date = time.Unix(1_700_000_900, 0) + late.InReplyTo = "" + early := msg("", "Early reply", "b") + early.Date = time.Unix(1_700_000_100, 0) + early.InReplyTo = "" + root := msg("", "Root", "b") + root.Date = time.Unix(1_700_000_000, 0) + rootID, _, _ := s.ArchiveMessage(l.ID, root) + s.ArchiveMessage(l.ID, late) // inserted before the earlier reply + s.ArchiveMessage(l.ID, early) + th, _ := s.ThreadMessages(l.ID, rootID) + got := []string{} + for _, m := range th { + got = append(got, m.Subject) + } + if strings.Join(got, "|") != "Root|Early reply|Late reply" { + t.Fatalf("a thread reads in the order it happened, not in the order it was stored: %v", got) + } +}