test: close gaps found by mutation checks

- Cover the default relay batch size
- Cover partial and total relay failure in Router.Send
- Cover stripping of spoofed List-* headers without a base URL
- Cover the end of the confirmation rate-limit window
This commit is contained in:
tgiachi 2026-11-09 15:09:23 +01:00
commit d6a0f6e5a9
3 changed files with 76 additions and 0 deletions

View file

@ -113,3 +113,18 @@ func TestFakeRecords(t *testing.T) {
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))
}
}

View file

@ -314,3 +314,58 @@ func TestSystemBounceAddressDiscarded(t *testing.T) {
t.Fatalf("%v %v", handled, err)
}
}
// flaky fails for the listed addresses.
type flaky struct {
bad map[string]bool
sent []string
}
func (f *flaky) Send(_ context.Context, _ string, to []string, _ []byte) error {
if f.bad[to[0]] {
return errors.New("rejected")
}
f.sent = append(f.sent, to[0])
return nil
}
func TestSendSkipsFailedRecipients(t *testing.T) {
e := newEnv(t)
res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(sample))
f := &flaky{bad: map[string]bool{"alice@example.com": true}}
if err := e.r.Send(e.ctx, f, res); err != nil {
t.Fatalf("partial failure must not fail the delivery: %v", err)
}
if len(f.sent) != 1 || f.sent[0] != "bob@example.com" {
t.Fatalf("sent: %v", f.sent)
}
}
func TestSendFailsWhenNobodyIsReached(t *testing.T) {
e := newEnv(t)
res, _ := e.r.Route("alice@example.com", "dev@example.org", []byte(sample))
f := &flaky{bad: map[string]bool{"alice@example.com": true, "bob@example.com": true}}
if err := e.r.Send(e.ctx, f, res); err == nil {
t.Fatal("want an error so the MTA retries")
}
}
func TestIncomingListHeadersAreStripped(t *testing.T) {
r := setup(t) // no BaseURL: One-Click must not survive from the outside
msg := "From: Alice <alice@example.com>\r\nList-Unsubscribe-Post: List-Unsubscribe=One-Click\r\n" +
"List-Unsubscribe: <https://evil.example/u>\r\nList-Post: <mailto:evil@x.com>\r\nPrecedence: bulk\r\n\r\nx\r\n"
res, err := r.Route("alice@example.com", "dev@example.org", []byte(msg))
if err != nil {
t.Fatal(err)
}
h := header(t, res.Data)
if h.Get("List-Unsubscribe-Post") != "" {
t.Error("spoofed List-Unsubscribe-Post survived")
}
if strings.Contains(h.Get("List-Unsubscribe"), "evil") || strings.Contains(h.Get("List-Post"), "evil") {
t.Errorf("spoofed list headers survived: %v", h)
}
if n := len(h["Precedence"]); n != 1 || h.Get("Precedence") != "list" {
t.Errorf("Precedence = %v", h["Precedence"])
}
}

View file

@ -70,6 +70,12 @@ func TestPendingTokens(t *testing.T) {
if _, created, _ := s.CreatePending(l.ID, "a@x.com", "join"); created {
t.Fatal("must be rate limited")
}
// After the window the same request can be made again.
now = now.Add(31 * time.Minute)
if _, created, _ := s.CreatePending(l.ID, "a@x.com", "join"); !created {
t.Fatal("rate limit must lapse after an hour")
}
now = now.Add(-31 * time.Minute)
// A different action is independent.
if _, created, _ := s.CreatePending(l.ID, "a@x.com", "leave"); !created {
t.Fatal("leave is a separate request")