Files
selfpost/internal/postfix/postfix_test.go
T
mix c0d9aa7518 chore/docs: move to GitHub as the single home; drop archived-spec references
Codeberg is being retired as the project's public site, so every reference now
points at GitHub. That includes the Go module path (codeberg.org/mix/selfpost →
github.com/mixeme/selfpost): leaving an import path on a host that is going
away would break `go get` and `go install`, so this is not only a docs change.
Touches go.mod, test/e2e/go.mod, all imports, Makefile MODULE, the -ldflags
version stamp in build/Dockerfile and docs/development.md, the licence headers
in the SVG/HTML assets, and README (no more primary/mirror pair).

Comments no longer cite the archived specification. "spec 7.6.1", "spec 5.1"
and friends pointed into docs/archive/specification-v1.0.md, which is marked as
not a source of truth; each is now a reference to the live document that owns
the subject — architecture.md (with section), product.md, security.md or the
README. The review only asked for the 7.x refs (code-review.md § 4), but 4/5/6/
8/9 had the same defect, so they went too. Comments only, no behaviour change.

Also closes the remaining review items: architecture.md gained a Code layers
section with the layer diagram (A2), and TestParseDelivery gained the exotic
mail.log cases (§ 3).

Fixes a bug that last test found: the delivery-line pattern matched status=
greedily, taking the *last* occurrence on the line. Postfix appends the remote
server's reply verbatim, so a rejection whose reply quoted "status=sent" was
filed as a delivered message in the send log. It now takes the first status=
after the recipient, which is the real field.

R7 (CONTRIBUTING.md) moved to roadmap 2.x — one developer, no external PR flow,
so the file would have no audience yet. R1 (compose image tag) and the git tag
stay in roadmap § v1.x as the release-commit steps.

gofmt/go vet clean on both modules; go test ./... green except the three known
Windows-only failures (file perms, backslash paths, renaming an open file).
Not exercised on the dev server — no Docker locally.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-06 22:14:13 +03:00

119 lines
3.2 KiB
Go

package postfix
import (
"os"
"testing"
)
func TestRenderSenderLoginMaps(t *testing.T) {
// Deliberately unsorted, with two logins sharing one wildcard key
// (many-to-one, architecture.md § Mail path) to exercise merge + sort.
bindings := []Binding{
{"@zeta.example", "z1"},
{"alerts@alpha.example", "a-listed"},
{"@alpha.example", "a2"},
{"@alpha.example", "a1"},
}
got, err := renderSenderLoginMaps(bindings)
if err != nil {
t.Fatalf("renderSenderLoginMaps: %v", err)
}
want := "@alpha.example a1,a2\n" +
"@zeta.example z1\n" +
"alerts@alpha.example a-listed\n"
if string(got) != want {
t.Errorf("map =\n%q\nwant\n%q", got, want)
}
}
func TestRenderSenderLoginMapsEmpty(t *testing.T) {
got, err := renderSenderLoginMaps(nil)
if err != nil {
t.Fatalf("renderSenderLoginMaps(nil): %v", err)
}
if len(got) != 0 {
t.Errorf("expected empty map, got %q", got)
}
}
func TestRenderSenderLoginMapsDedupesLogin(t *testing.T) {
bindings := []Binding{
{"@a.example", "dup"},
{"@a.example", "dup"},
}
got, err := renderSenderLoginMaps(bindings)
if err != nil {
t.Fatal(err)
}
if string(got) != "@a.example dup\n" {
t.Errorf("map = %q, want single deduped login", got)
}
}
func TestAssertMapSafeRejectsInjection(t *testing.T) {
bad := []struct{ addr, login string }{
{"@exa mple.com", "log"},
{"@example.com\nx y z", "log"},
{"@example.com", "log,evil"},
{"@example.com", "log in"},
{"@example.com", "log@realm"}, // '@' would confuse sasldb realm handling
{"", "log"},
{"@example.com", ""},
}
for _, b := range bad {
if err := assertMapSafe(b.addr, b.login); err == nil {
t.Errorf("assertMapSafe(%q,%q) = nil, want error", b.addr, b.login)
}
}
if err := assertMapSafe("alerts@example.com", "app_1-x"); err != nil {
t.Errorf("assertMapSafe of a clean pair errored: %v", err)
}
}
func newTestPostfix(t *testing.T) (*Postfix, *int) {
t.Helper()
dir := t.TempDir()
reloads := 0
p := New(dir)
p.reload = func() error { reloads++; return nil }
return p, &reloads
}
func TestRebuildSenderLoginMapsWritesAndReloads(t *testing.T) {
p, reloads := newTestPostfix(t)
if err := p.RebuildSenderLoginMaps([]Binding{{"@example.com", "app1"}}); err != nil {
t.Fatalf("RebuildSenderLoginMaps: %v", err)
}
if *reloads != 1 {
t.Errorf("reload called %d times, want 1", *reloads)
}
data, err := os.ReadFile(p.senderLoginMapsPath)
if err != nil {
t.Fatal(err)
}
if string(data) != "@example.com app1\n" {
t.Errorf("map file = %q", data)
}
}
func TestRebuildRejectsUnsafeWithoutWriting(t *testing.T) {
p, reloads := newTestPostfix(t)
// Seed a known-good file so we can prove the failed rebuild left it untouched.
if err := p.RebuildSenderLoginMaps([]Binding{{"@good.example", "ok"}}); err != nil {
t.Fatal(err)
}
before, _ := os.ReadFile(p.senderLoginMapsPath)
err := p.RebuildSenderLoginMaps([]Binding{{"@bad.example", "evil\nlogin"}})
if err == nil {
t.Fatal("expected rebuild to reject unsafe login")
}
after, _ := os.ReadFile(p.senderLoginMapsPath)
if string(after) != string(before) {
t.Errorf("map file changed on failed rebuild: %q", after)
}
if *reloads != 1 {
t.Errorf("reload called %d times, want 1 (no reload on failure)", *reloads)
}
}