parsimony/internal/router/bounce_test.go
tgiachi cd31d919a9 test(router): pin the edges of DSN parsing
- message/global-delivery-status (RFC 6533) counts like delivery-status.
- Only the first delivery-status part is read.
- A malformed delivery-status part does not count.
- Drop the redundant empty-boundary check: the multipart reader already
  fails on it.
2026-11-10 11:52:25 +01:00

152 lines
5.3 KiB
Go

package router
import (
"fmt"
"strings"
"testing"
)
// dsn builds a delivery status notification as an MTA sends it (RFC 3464): a multipart/report with
// a human part, a message/delivery-status part and the returned headers.
func dsn(action, status string) []byte {
return []byte(strings.ReplaceAll(fmt.Sprintf(`From: MAILER-DAEMON@mx.example.com
To: dev+bounce@example.org
Subject: Undelivered Mail Returned to Sender
MIME-Version: 1.0
Content-Type: multipart/report; report-type=delivery-status;
boundary="BOUNDARY"
--BOUNDARY
Content-Type: text/plain; charset=us-ascii
I'm sorry to have to inform you that your message could not be delivered.
--BOUNDARY
Content-Type: message/delivery-status
Reporting-MTA: dns; mx.example.com
Arrival-Date: Mon, 9 Nov 2026 10:00:00 +0100
Final-Recipient: rfc822; alice@example.com
Original-Recipient: rfc822;alice@example.com
Action: %s
Status: %s
Diagnostic-Code: smtp; 550 something
--BOUNDARY
Content-Type: text/rfc822-headers
From: bob@example.com
Subject: hello
--BOUNDARY--
`, action, status), "\n", "\r\n"))
}
func bounceCount(t *testing.T, e *env) int {
t.Helper()
m, err := e.s.MemberByID(e.alice)
if err != nil {
t.Fatal(err)
}
return m.BounceCount
}
func TestOnlyFailedRecipientUnknownDSNsCount(t *testing.T) {
cases := []struct {
action, status string
counts bool
}{
{"failed", "5.1.1", true},
{"failed", "5.1.2", true},
{"failed", "5.1.3", true},
{"failed", "5.1.6", true},
{"failed", "5.1.10", true},
{"FAILED (permanent)", "5.1.1 (bad destination mailbox address)", true},
{"failed", "5.7.1", false}, // spam or policy rejection of the content
{"failed", "5.2.2", false}, // mailbox full
{"failed", "5.1.7", false}, // bad sender address
{"failed", "5.1.11", false}, // not in the list (prefix of 5.1.1x must not match)
{"failed", "4.1.1", false},
{"delayed", "5.1.1", false},
{"delayed", "4.4.1", false},
{"delivered", "2.0.0", false},
}
for _, c := range cases {
e := newEnv(t)
handled, err := e.command("", e.r.bounceAddress(e.list, e.alice), dsn(c.action, c.status))
if !handled || err != nil {
t.Fatalf("%s %s: %v %v", c.action, c.status, handled, err)
}
if got := bounceCount(t, e) == 1; got != c.counts {
t.Errorf("%s %s: counted=%v, want %v", c.action, c.status, got, c.counts)
}
}
}
func TestStatusLinesOutsideADeliveryStatusPartDoNotCount(t *testing.T) {
quoted := "From: carol@example.com\r\nSubject: Re: your bounce\r\nContent-Type: text/plain\r\n\r\n" +
"> Action: failed\r\n> Status: 5.1.1\r\nAction: failed\r\nStatus: 5.1.1\r\n"
// A report whose status sits in the human part, not in message/delivery-status.
human := strings.Replace(string(dsn("delayed", "4.4.1")), "I'm sorry", "Action: failed\r\nStatus: 5.1.1\r\n\r\nI'm sorry", 1)
// The returned original message quotes a DSN of its own.
returned := strings.Replace(string(dsn("delayed", "4.4.1")), "Subject: hello", "Subject: hello\r\nAction: failed\r\nStatus: 5.1.1", 1)
for name, msg := range map[string]string{
"plain text reply": quoted,
"status in human part": human,
"status in returned": returned,
"not mime at all": "Action: failed\r\nStatus: 5.1.1\r\n",
"broken multipart": "Content-Type: multipart/report; boundary=x\r\n\r\nno parts here\r\nStatus: 5.1.1\r\n",
"empty": "",
} {
e := newEnv(t)
if _, err := e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg)); err != nil {
t.Fatalf("%s: %v", name, err)
}
if n := bounceCount(t, e); n != 0 {
t.Errorf("%s: counted %d bounces", name, n)
}
}
}
func TestAnyFailedRecipientBlockCounts(t *testing.T) {
// The first recipient block is a delay, the second a hard failure: the report counts once.
msg := strings.Replace(string(dsn("failed", "5.1.1")), "Final-Recipient:",
"Final-Recipient: rfc822; alice@example.com\r\nAction: delayed\r\nStatus: 4.4.1\r\n\r\nFinal-Recipient:", 1)
e := newEnv(t)
e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg))
if n := bounceCount(t, e); n != 1 {
t.Fatalf("counted %d", n)
}
}
func TestGlobalDeliveryStatusCounts(t *testing.T) {
// RFC 6533: the internationalized report has the same fields.
msg := strings.Replace(string(dsn("failed", "5.1.1")), "message/delivery-status", "message/global-delivery-status", 1)
e := newEnv(t)
e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg))
if n := bounceCount(t, e); n != 1 {
t.Fatalf("counted %d", n)
}
}
func TestOnlyTheFirstDeliveryStatusPartIsRead(t *testing.T) {
// A report has one delivery-status part; another one after it is not the MTA speaking.
extra := "--BOUNDARY\r\nContent-Type: message/delivery-status\r\n\r\nReporting-MTA: dns; x\r\n\r\n" +
"Final-Recipient: rfc822; alice@example.com\r\nAction: failed\r\nStatus: 5.1.1\r\n\r\n--BOUNDARY--"
msg := strings.Replace(string(dsn("delayed", "4.4.1")), "--BOUNDARY--", extra, 1)
e := newEnv(t)
e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg))
if n := bounceCount(t, e); n != 0 {
t.Fatalf("counted %d", n)
}
}
func TestAMalformedDeliveryStatusDoesNotCount(t *testing.T) {
msg := strings.Replace(string(dsn("failed", "5.1.1")), "Arrival-Date:", "this line is not a field\r\nArrival-Date:", 1)
e := newEnv(t)
e.command("", e.r.bounceAddress(e.list, e.alice), []byte(msg))
if n := bounceCount(t, e); n != 0 {
t.Fatalf("counted %d", n)
}
}