- Judge membership on the envelope sender and require the From header to name the same address - Hold mismatches (and missing, unparsable or multiple From addresses) for the admin instead of delivering - Record why a message was held: new held.reason column, migrated, shown in the API, CLI and UI - Accept an empty message in the held queue - Document who may post
778 lines
30 KiB
Go
778 lines
30 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"codeberg.org/squidd/parsimony/internal/api"
|
|
"codeberg.org/squidd/parsimony/internal/dkimsign"
|
|
"codeberg.org/squidd/parsimony/internal/store"
|
|
)
|
|
|
|
const token = "cli-token"
|
|
|
|
type fakeReleaser struct{ released []int64 }
|
|
|
|
func (f *fakeReleaser) Release(_ context.Context, id int64) error {
|
|
f.released = append(f.released, id)
|
|
return nil
|
|
}
|
|
|
|
type env struct {
|
|
s *store.Store
|
|
srv *httptest.Server
|
|
rel *fakeReleaser
|
|
}
|
|
|
|
// newEnv serves the real API handler over HTTP: the CLI talks to the real thing.
|
|
func newEnv(t *testing.T) *env {
|
|
t.Helper()
|
|
s, err := store.Open(":memory:")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rel := &fakeReleaser{}
|
|
srv := httptest.NewServer(api.New(s, token, rel))
|
|
t.Cleanup(func() { srv.Close(); s.Close() })
|
|
return &env{s: s, srv: srv, rel: rel}
|
|
}
|
|
|
|
// run executes the CLI like a shell would and returns exit code, stdout and stderr.
|
|
func (e *env) run(t *testing.T, args ...string) (int, string, string) {
|
|
t.Helper()
|
|
return e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, "", args...)
|
|
}
|
|
|
|
func (e *env) runEnv(t *testing.T, vars map[string]string, stdin string, args ...string) (int, string, string) {
|
|
t.Helper()
|
|
var out, errb bytes.Buffer
|
|
code := Run(args, IO{In: strings.NewReader(stdin), Out: &out, Err: &errb}, func(k string) string { return vars[k] })
|
|
return code, out.String(), errb.String()
|
|
}
|
|
|
|
func (e *env) ok(t *testing.T, args ...string) string {
|
|
t.Helper()
|
|
code, out, errs := e.run(t, args...)
|
|
if code != 0 {
|
|
t.Fatalf("%v: exit %d\nstdout: %s\nstderr: %s", args, code, out, errs)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (e *env) fails(t *testing.T, wantCode int, wantErr string, args ...string) {
|
|
t.Helper()
|
|
code, out, errs := e.run(t, args...)
|
|
if code != wantCode {
|
|
t.Fatalf("%v: exit %d, want %d\nstdout: %s\nstderr: %s", args, code, wantCode, out, errs)
|
|
}
|
|
if !strings.Contains(errs, wantErr) {
|
|
t.Fatalf("%v: stderr %q lacks %q", args, errs, wantErr)
|
|
}
|
|
if out != "" {
|
|
t.Fatalf("%v: errors must not print to stdout: %q", args, out)
|
|
}
|
|
}
|
|
|
|
func TestHelpAndUsageErrors(t *testing.T) {
|
|
e := newEnv(t)
|
|
for _, args := range [][]string{{"help"}, {"-h"}, {"--help"}} {
|
|
code, out, _ := e.run(t, args...)
|
|
if code != 0 || !strings.Contains(out, "list") || !strings.Contains(out, "PARSIMONY_API_TOKEN") {
|
|
t.Fatalf("%v: %d %q", args, code, out)
|
|
}
|
|
}
|
|
e.fails(t, 2, "usage")
|
|
e.fails(t, 2, "unknown command", "frobnicate")
|
|
e.fails(t, 2, "unknown command", "list", "frobnicate")
|
|
e.fails(t, 2, "usage: parsimony list add", "list", "add")
|
|
e.fails(t, 2, "usage: parsimony member add", "member", "add", "dev@example.org")
|
|
e.fails(t, 2, "flag provided but not defined", "list", "ls", "--nope")
|
|
}
|
|
|
|
func TestAuthAndConnectionProblems(t *testing.T) {
|
|
e := newEnv(t)
|
|
code, _, errs := e.runEnv(t, map[string]string{"PARSIMONY_URL": e.srv.URL}, "", "list", "ls")
|
|
if code != 1 || !strings.Contains(errs, "PARSIMONY_API_TOKEN is not set") {
|
|
t.Fatalf("no token: %d %q", code, errs)
|
|
}
|
|
code, _, errs = e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": "wrong", "PARSIMONY_URL": e.srv.URL}, "", "list", "ls")
|
|
if code != 1 || !strings.Contains(errs, "invalid or missing token") {
|
|
t.Fatalf("wrong token: %d %q", code, errs)
|
|
}
|
|
code, _, errs = e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": "http://127.0.0.1:1"}, "", "list", "ls")
|
|
if code != 1 || !strings.Contains(errs, "cannot reach") {
|
|
t.Fatalf("unreachable: %d %q", code, errs)
|
|
}
|
|
}
|
|
|
|
func TestURLFlagBeatsEnvironmentAndTrailingSlashIsFine(t *testing.T) {
|
|
e := newEnv(t)
|
|
vars := map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": "http://127.0.0.1:1"}
|
|
code, _, errs := e.runEnv(t, vars, "", "--url", e.srv.URL+"/", "list", "ls")
|
|
if code != 0 {
|
|
t.Fatalf("--url must win over PARSIMONY_URL: %d %q", code, errs)
|
|
}
|
|
code, _, errs = e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token}, "", "list", "ls")
|
|
if code != 1 || !strings.Contains(errs, "cannot reach") {
|
|
t.Fatalf("default URL is the local daemon: %d %q", code, errs)
|
|
}
|
|
}
|
|
|
|
func TestListLifecycle(t *testing.T) {
|
|
e := newEnv(t)
|
|
if out := e.ok(t, "list", "ls"); !strings.Contains(out, "No lists") {
|
|
t.Fatalf("empty: %q", out)
|
|
}
|
|
out := e.ok(t, "list", "add", "dev@example.org", "--title", "Developers")
|
|
if !strings.Contains(out, "Created dev@example.org") {
|
|
t.Fatalf("add: %q", out)
|
|
}
|
|
e.ok(t, "list", "add", "news@example.org")
|
|
out = e.ok(t, "list", "ls")
|
|
for _, want := range []string{"ID", "ADDRESS", "TITLE", "POST", "JOIN", "ARCHIVE", "dev@example.org", "Developers", "members", "open", "private", "news@example.org"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Fatalf("ls lacks %q:\n%s", want, out)
|
|
}
|
|
}
|
|
e.fails(t, 1, "already exists", "list", "add", "dev@example.org")
|
|
e.fails(t, 2, "name@domain", "list", "add", "nodomain")
|
|
e.fails(t, 2, "name@domain", "list", "add", "@example.org")
|
|
e.fails(t, 1, "name and domain", "list", "add", "a+b@example.org")
|
|
|
|
for _, ref := range []string{"dev@example.org", "DEV@Example.org", "1"} {
|
|
out = e.ok(t, "list", "show", ref)
|
|
if !strings.Contains(out, "dev@example.org") || !strings.Contains(out, "Developers") || !strings.Contains(out, "archive") {
|
|
t.Fatalf("show %s: %q", ref, out)
|
|
}
|
|
}
|
|
e.fails(t, 1, "no such list", "list", "show", "ghost@example.org")
|
|
e.fails(t, 1, "no such list", "list", "show", "99")
|
|
e.fails(t, 1, "no such list", "list", "show", "notanumber")
|
|
|
|
out = e.ok(t, "list", "set", "dev@example.org", "--post", "moderated", "--archive", "public")
|
|
if !strings.Contains(out, "Updated dev@example.org") {
|
|
t.Fatalf("set: %q", out)
|
|
}
|
|
l, _ := e.s.FindList("dev@example.org")
|
|
if l.PostPolicy != "moderated" || l.ArchivePolicy != "public" || l.JoinPolicy != "open" || l.Title != "Developers" {
|
|
t.Fatalf("only the given fields change: %+v", l)
|
|
}
|
|
e.ok(t, "list", "set", "1", "--title", "Core devs", "--join", "closed")
|
|
l, _ = e.s.FindList("dev@example.org")
|
|
if l.Title != "Core devs" || l.JoinPolicy != "closed" || l.PostPolicy != "moderated" {
|
|
t.Fatalf("%+v", l)
|
|
}
|
|
e.fails(t, 2, "nothing to change", "list", "set", "dev@example.org")
|
|
e.fails(t, 1, "post_policy", "list", "set", "dev@example.org", "--post", "bogus")
|
|
e.fails(t, 1, "no such list", "list", "set", "ghost@example.org", "--title", "x")
|
|
}
|
|
|
|
func TestListRemoveNeedsYes(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
e.fails(t, 2, "--yes", "list", "rm", "dev@example.org")
|
|
if _, err := e.s.FindList("dev@example.org"); err != nil {
|
|
t.Fatal("without --yes nothing is deleted")
|
|
}
|
|
out := e.ok(t, "list", "rm", "dev@example.org", "--yes")
|
|
if !strings.Contains(out, "Deleted dev@example.org") {
|
|
t.Fatalf("rm: %q", out)
|
|
}
|
|
if _, err := e.s.FindList("dev@example.org"); err == nil {
|
|
t.Fatal("list must be gone")
|
|
}
|
|
e.fails(t, 1, "no such list", "list", "rm", "dev@example.org", "--yes")
|
|
}
|
|
|
|
func TestJSONOutputIsValidAndStable(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org", "--title", "Dev")
|
|
var lists []map[string]any
|
|
if err := json.Unmarshal([]byte(e.ok(t, "--json", "list", "ls")), &lists); err != nil || len(lists) != 1 || lists[0]["address"] != "dev@example.org" {
|
|
t.Fatalf("json ls: %v %v", err, lists)
|
|
}
|
|
var one map[string]any
|
|
if err := json.Unmarshal([]byte(e.ok(t, "--json", "list", "show", "dev@example.org")), &one); err != nil || one["title"] != "Dev" {
|
|
t.Fatalf("json show: %v %v", err, one)
|
|
}
|
|
e.s.CreateList("empty", "example.org", "E")
|
|
var ms []any
|
|
if err := json.Unmarshal([]byte(e.ok(t, "--json", "member", "ls", "empty@example.org")), &ms); err != nil || ms == nil || len(ms) != 0 {
|
|
t.Fatalf("an empty result is [] in JSON: %v %v", err, ms)
|
|
}
|
|
}
|
|
|
|
func TestMembers(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
if out := e.ok(t, "member", "ls", "dev@example.org"); !strings.Contains(out, "No members") {
|
|
t.Fatalf("empty: %q", out)
|
|
}
|
|
out := e.ok(t, "member", "add", "dev@example.org", "Alice@Example.com", "--name", "Alice")
|
|
if !strings.Contains(out, "Added alice@example.com") {
|
|
t.Fatalf("add: %q", out)
|
|
}
|
|
e.ok(t, "member", "add", "1", "bob@example.com")
|
|
out = e.ok(t, "member", "ls", "dev@example.org")
|
|
for _, want := range []string{"ADDRESS", "NAME", "STATUS", "alice@example.com", "Alice", "bob@example.com", "active"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Fatalf("ls lacks %q:\n%s", want, out)
|
|
}
|
|
}
|
|
if out := e.ok(t, "member", "ls", "dev@example.org", "--emails"); out != "alice@example.com\nbob@example.com\n" {
|
|
t.Fatalf("--emails: %q", out)
|
|
}
|
|
e.fails(t, 1, "valid email", "member", "add", "dev@example.org", "not-an-address")
|
|
e.fails(t, 1, "no such list", "member", "add", "ghost@example.org", "a@b.com")
|
|
|
|
// Bounce state is visible, and adding again re-enables.
|
|
l, _ := e.s.FindList("dev@example.org")
|
|
ms, _ := e.s.Members(l.ID)
|
|
e.s.RecordHardBounce(ms[1].ID, 3)
|
|
out = e.ok(t, "member", "ls", "dev@example.org")
|
|
if !strings.Contains(out, "active (1 bounce)") {
|
|
t.Fatalf("a member with a few bounces is still active: %s", out)
|
|
}
|
|
for i := 0; i < 3; i++ {
|
|
e.s.RecordHardBounce(ms[0].ID, 3)
|
|
}
|
|
out = e.ok(t, "member", "ls", "dev@example.org")
|
|
if !strings.Contains(out, "disabled (3 bounces)") {
|
|
t.Fatalf("bounce state: %s", out)
|
|
}
|
|
e.ok(t, "member", "add", "dev@example.org", "alice@example.com")
|
|
if out = e.ok(t, "member", "ls", "dev@example.org"); strings.Contains(out, "disabled") {
|
|
t.Fatalf("re-adding re-enables: %s", out)
|
|
}
|
|
|
|
out = e.ok(t, "member", "rm", "dev@example.org", "bob@example.com")
|
|
if !strings.Contains(out, "Removed bob@example.com") {
|
|
t.Fatalf("rm: %q", out)
|
|
}
|
|
e.fails(t, 1, "not found", "member", "rm", "dev@example.org", "bob@example.com")
|
|
if ok, _ := e.s.IsMember(l.ID, "alice@example.com"); !ok {
|
|
t.Fatal("alice must stay")
|
|
}
|
|
}
|
|
|
|
func TestMemberImport(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
input := "# subscribers exported from Mailman\n\nalice@example.com, Alice Smith\nbob@example.com\n carol@example.com ,Carol \nnot-an-address\ndave@example.com,Dave,extra\n"
|
|
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, input, "member", "import", "dev@example.org", "-")
|
|
if code != 1 {
|
|
t.Fatalf("a failed line must make the exit code non-zero, got %d", code)
|
|
}
|
|
if !strings.Contains(out, "Imported 4") || !strings.Contains(out, "1 failed") {
|
|
t.Fatalf("summary: %q", out)
|
|
}
|
|
if !strings.Contains(errs, "line 6") || !strings.Contains(errs, "not-an-address") || !strings.Contains(errs, "valid email") {
|
|
t.Fatalf("the failing line is reported with its number: %q", errs)
|
|
}
|
|
l, _ := e.s.FindList("dev@example.org")
|
|
ms, _ := e.s.Members(l.ID)
|
|
names := map[string]string{}
|
|
for _, m := range ms {
|
|
names[m.Address] = m.DisplayName
|
|
}
|
|
if len(ms) != 4 || names["alice@example.com"] != "Alice Smith" || names["carol@example.com"] != "Carol" || names["dave@example.com"] != "Dave" {
|
|
t.Fatalf("members: %v", names)
|
|
}
|
|
|
|
// Importing again is idempotent; a file works as well as stdin.
|
|
path := t.TempDir() + "/subs.txt"
|
|
if err := os.WriteFile(path, []byte("alice@example.com\nerin@example.com\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out = e.ok(t, "member", "import", "dev@example.org", path)
|
|
if !strings.Contains(out, "Imported 2") || strings.Contains(out, "failed") {
|
|
t.Fatalf("file import: %q", out)
|
|
}
|
|
e.fails(t, 1, "no such file", "member", "import", "dev@example.org", "/nonexistent/file.txt")
|
|
e.fails(t, 1, "no such list", "member", "import", "ghost@example.org", path)
|
|
}
|
|
|
|
func TestHeldQueue(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
l, _ := e.s.FindList("dev@example.org")
|
|
if out := e.ok(t, "held", "ls", "dev@example.org"); !strings.Contains(out, "Nothing is held") {
|
|
t.Fatalf("empty: %q", out)
|
|
}
|
|
id1, _ := e.s.HoldMessage(l.ID, "bob@x.com", []byte("From: Bob <bob@x.com>\r\nSubject: First held\r\n\r\nbody"), "the list is moderated")
|
|
id2, _ := e.s.HoldMessage(l.ID, "eve@x.com", []byte("Subject: Second held\r\n\r\nbody"), "")
|
|
id3, _ := e.s.HoldMessage(l.ID, "anon@x.com", []byte("From: anon@x.com\r\n\r\nno subject here"), "")
|
|
out := e.ok(t, "held", "ls", "dev@example.org")
|
|
for _, want := range []string{"ID", "FROM", "SUBJECT", "SIZE", "REASON", "bob@x.com", "First held", "eve@x.com", "Second held", "(no subject)", "the list is moderated"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Fatalf("held ls lacks %q:\n%s", want, out)
|
|
}
|
|
}
|
|
out = e.ok(t, "held", "approve", fmt.Sprint(id1))
|
|
if !strings.Contains(out, "Approved") || len(e.rel.released) != 1 || e.rel.released[0] != id1 {
|
|
t.Fatalf("approve: %q %v", out, e.rel.released)
|
|
}
|
|
out = e.ok(t, "held", "discard", fmt.Sprint(id2))
|
|
if !strings.Contains(out, "Discarded") {
|
|
t.Fatalf("discard: %q", out)
|
|
}
|
|
e.ok(t, "held", "discard", fmt.Sprint(id3))
|
|
if h, _ := e.s.ListHeld(l.ID); len(h) != 1 { // the approved one stays: the fake releaser only records it
|
|
t.Fatalf("held left: %d", len(h))
|
|
}
|
|
e.fails(t, 1, "not found", "held", "discard", fmt.Sprint(id2))
|
|
e.fails(t, 2, "usage: parsimony held approve", "held", "approve")
|
|
e.fails(t, 2, "not a valid id", "held", "approve", "abc")
|
|
}
|
|
|
|
func TestArchive(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
l, _ := e.s.FindList("dev@example.org")
|
|
if out := e.ok(t, "archive", "ls", "dev@example.org"); !strings.Contains(out, "archive is empty") {
|
|
t.Fatalf("empty: %q", out)
|
|
}
|
|
for i, subj := range []string{"Deploy failed", "Lunch plans", "Deploy fixed"} {
|
|
e.s.ArchiveMessage(l.ID, store.Message{MessageID: fmt.Sprintf("<%d@x>", i), Subject: subj, Sender: "Alice",
|
|
Date: time.Unix(1_700_000_000+int64(i), 0), Body: "body of " + subj})
|
|
}
|
|
out := e.ok(t, "archive", "ls", "dev@example.org")
|
|
for _, want := range []string{"ID", "DATE", "FROM", "SUBJECT", "Deploy fixed", "Lunch plans", "Deploy failed", "3 messages"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Fatalf("archive ls lacks %q:\n%s", want, out)
|
|
}
|
|
}
|
|
if strings.Index(out, "Deploy fixed") > strings.Index(out, "Deploy failed") {
|
|
t.Fatal("newest first")
|
|
}
|
|
out = e.ok(t, "archive", "ls", "dev@example.org", "--q", "deploy")
|
|
if strings.Contains(out, "Lunch") || !strings.Contains(out, "2 messages") {
|
|
t.Fatalf("search: %q", out)
|
|
}
|
|
out = e.ok(t, "archive", "ls", "dev@example.org", "--limit", "1", "--offset", "1")
|
|
if !strings.Contains(out, "Lunch plans") || strings.Contains(out, "Deploy fixed") || !strings.Contains(out, "Showing 1 of 3 messages") {
|
|
t.Fatalf("paging: %q", out)
|
|
}
|
|
if out = e.ok(t, "archive", "ls", "dev@example.org", "--q", "zzz"); !strings.Contains(out, "No messages found") {
|
|
t.Fatalf("no match: %q", out)
|
|
}
|
|
out = e.ok(t, "archive", "show", "1")
|
|
if !strings.Contains(out, "Subject: Deploy failed") || !strings.Contains(out, "body of Deploy failed") {
|
|
t.Fatalf("show: %q", out)
|
|
}
|
|
if exact := e.ok(t, "archive", "show", "1"); exact != "Subject: Deploy failed\nFrom: Alice\nDate: 2023-11-14 22:13\n\nbody of Deploy failed\n" {
|
|
t.Fatalf("show must print exactly the message, got %q", exact)
|
|
}
|
|
var shown map[string]any
|
|
if err := json.Unmarshal([]byte(e.ok(t, "--json", "archive", "show", "1")), &shown); err != nil || shown["subject"] != "Deploy failed" {
|
|
t.Fatalf("--json show must be a single JSON document: %v %v", err, shown)
|
|
}
|
|
e.fails(t, 1, "not found", "archive", "show", "99")
|
|
out = e.ok(t, "archive", "rm", "1", "--yes")
|
|
if !strings.Contains(out, "Deleted") {
|
|
t.Fatalf("rm: %q", out)
|
|
}
|
|
e.fails(t, 2, "--yes", "archive", "rm", "2")
|
|
if _, total, _ := e.s.ListMessages(l.ID, 10, 0); total != 2 {
|
|
t.Fatalf("total = %d", total)
|
|
}
|
|
}
|
|
|
|
func TestQueueListIsCapped(t *testing.T) {
|
|
e := newEnv(t)
|
|
var items []store.NewOutbound
|
|
for i := 0; i < 105; i++ {
|
|
items = append(items, store.NewOutbound{From: "b@x.org", Rcpt: fmt.Sprintf("u%d@x.com", i)})
|
|
}
|
|
e.s.Enqueue([]byte("x"), items)
|
|
out := e.ok(t, "queue", "ls")
|
|
if !strings.Contains(out, "Pending: 105") || !strings.Contains(out, "Showing 100 of 105 deliveries") {
|
|
t.Fatalf("queue ls:\n%.300s ... %s", out, out[max(0, len(out)-80):])
|
|
}
|
|
}
|
|
|
|
func TestURLWithTrailingSlashDoesNotProduceDoubleSlashes(t *testing.T) {
|
|
e := newEnv(t)
|
|
var paths []string
|
|
spy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
paths = append(paths, r.URL.Path)
|
|
e.srv.Config.Handler.ServeHTTP(w, r)
|
|
}))
|
|
defer spy.Close()
|
|
vars := map[string]string{"PARSIMONY_API_TOKEN": token}
|
|
if code, _, errs := e.runEnv(t, vars, "", "--url", spy.URL+"//", "list", "ls"); code != 0 {
|
|
t.Fatalf("%d %s", code, errs)
|
|
}
|
|
if len(paths) != 1 || paths[0] != "/api/v1/lists" {
|
|
t.Fatalf("request paths: %v", paths)
|
|
}
|
|
}
|
|
|
|
func TestQueue(t *testing.T) {
|
|
e := newEnv(t)
|
|
if out := e.ok(t, "queue", "ls"); !strings.Contains(out, "Pending: 0") || !strings.Contains(out, "queue is empty") {
|
|
t.Fatalf("empty: %q", out)
|
|
}
|
|
e.s.Enqueue([]byte("x"), []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}})
|
|
due, _ := e.s.DueOutbox(10)
|
|
e.s.RetryOutbox(due[1].ID, time.Now().Add(time.Hour), "connection refused")
|
|
out := e.ok(t, "queue", "ls")
|
|
for _, want := range []string{"Pending: 2", "Retrying: 1", "ID", "RECIPIENT", "ATTEMPTS", "LAST ERROR", "a@x.com", "b@x.com", "connection refused"} {
|
|
if !strings.Contains(out, want) {
|
|
t.Fatalf("queue ls lacks %q:\n%s", want, out)
|
|
}
|
|
}
|
|
if strings.Index(out, "b@x.com") > strings.Index(out, "a@x.com") {
|
|
t.Fatal("failing deliveries first")
|
|
}
|
|
e.ok(t, "queue", "rm", fmt.Sprint(due[1].ID))
|
|
e.fails(t, 1, "not found", "queue", "rm", fmt.Sprint(due[1].ID))
|
|
if st, _ := e.s.OutboxStats(); st.Pending != 1 {
|
|
t.Fatalf("pending = %d", st.Pending)
|
|
}
|
|
}
|
|
|
|
const cliMbox = "From alice@example.com Tue Jan 2 15:04:05 2024\nFrom: Alice <alice@example.com>\nSubject: Kickoff\nMessage-ID: <1@x>\nDate: Mon, 01 Jan 2024 10:00:00 +0000\n\nlet's start\n\n" +
|
|
"From bob@example.com Tue Jan 2 15:04:05 2024\nFrom: Bob <bob@example.com>\nSubject: Re: Kickoff\nMessage-ID: <2@x>\nIn-Reply-To: <1@x>\nDate: Mon, 01 Jan 2024 11:00:00 +0000\n\nagreed\n"
|
|
|
|
func TestArchiveImportFromFileAndStdin(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
path := t.TempDir() + "/dev.mbox"
|
|
if err := os.WriteFile(path, []byte(cliMbox), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := e.ok(t, "archive", "import", "dev@example.org", path)
|
|
if !strings.Contains(out, "Imported 2 messages into dev@example.org") || strings.Contains(out, "failed") || strings.Contains(out, "already") {
|
|
t.Fatalf("import: %q", out)
|
|
}
|
|
l, _ := e.s.FindList("dev@example.org")
|
|
if _, total, _ := e.s.ListMessages(l.ID, 10, 0); total != 2 {
|
|
t.Fatalf("stored %d", total)
|
|
}
|
|
|
|
// Again, through stdin: nothing new, and it says so.
|
|
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, cliMbox, "archive", "import", "1", "-")
|
|
if code != 0 || !strings.Contains(out, "Imported 0 messages") || !strings.Contains(out, "2 already present") {
|
|
t.Fatalf("second import: %d %q %q", code, out, errs)
|
|
}
|
|
}
|
|
|
|
func TestArchiveImportReportsFailures(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
bad := cliMbox + "\nFrom x@y.z Tue Jan 2 15:04:05 2024\n\nno headers\n"
|
|
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, bad, "archive", "import", "dev@example.org", "-")
|
|
if code != 1 {
|
|
t.Fatalf("a failed message must give a non-zero exit status, got %d", code)
|
|
}
|
|
if !strings.Contains(out, "Imported 2 messages") || !strings.Contains(out, "1 failed") {
|
|
t.Fatalf("summary: %q", out)
|
|
}
|
|
if !strings.Contains(errs, "message 3") || !strings.Contains(errs, "no headers") {
|
|
t.Fatalf("the failure is reported: %q", errs)
|
|
}
|
|
}
|
|
|
|
func TestArchiveImportErrorsAndJSON(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
e.fails(t, 1, "no such file", "archive", "import", "dev@example.org", "/nonexistent/x.mbox")
|
|
e.fails(t, 1, "no such list", "archive", "import", "ghost@example.org", "/dev/null")
|
|
e.fails(t, 2, "usage: parsimony archive import", "archive", "import", "dev@example.org")
|
|
_, out, _ := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, cliMbox, "--json", "archive", "import", "dev@example.org", "-")
|
|
var sum map[string]any
|
|
if err := json.Unmarshal([]byte(out), &sum); err != nil || sum["imported"].(float64) != 2 {
|
|
t.Fatalf("json: %v %q", err, out)
|
|
}
|
|
}
|
|
|
|
func TestLongRunningUploadsAreNotCutByTheShortRequestTimeout(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
var cl *client
|
|
c := &cmd{getenv: func(k string) string {
|
|
return map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}[k]
|
|
}}
|
|
cl, err := c.client()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cl.http.Timeout == 0 {
|
|
t.Fatal("ordinary requests need a timeout")
|
|
}
|
|
if cl.upload == nil || cl.upload.Timeout != 0 {
|
|
t.Fatal("uploads of big archives must not have an overall timeout")
|
|
}
|
|
}
|
|
|
|
func TestArchiveImportSaysHowManyMoreFailuresThereAre(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
var b strings.Builder
|
|
for i := 0; i < 25; i++ {
|
|
b.WriteString("From x@y.z Tue Jan 2 15:04:05 2024\n\nno headers\n\n")
|
|
}
|
|
code, out, errs := e.runEnv(t, map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": e.srv.URL}, b.String(), "archive", "import", "dev@example.org", "-")
|
|
if code != 1 || !strings.Contains(out, "25 failed") {
|
|
t.Fatalf("%d %q", code, out)
|
|
}
|
|
if !strings.Contains(errs, "... and 5 more") {
|
|
t.Fatalf("only 20 are listed, the rest is counted: %q", errs)
|
|
}
|
|
}
|
|
|
|
func TestArchiveImportSendsTheFileAsAnMbox(t *testing.T) {
|
|
e := newEnv(t)
|
|
e.ok(t, "list", "add", "dev@example.org")
|
|
var contentType, body string
|
|
spy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.HasSuffix(r.URL.Path, "/archive/import") {
|
|
contentType = r.Header.Get("Content-Type")
|
|
b, _ := io.ReadAll(r.Body)
|
|
body = string(b)
|
|
r.Body = io.NopCloser(strings.NewReader(body))
|
|
}
|
|
e.srv.Config.Handler.ServeHTTP(w, r)
|
|
}))
|
|
defer spy.Close()
|
|
vars := map[string]string{"PARSIMONY_API_TOKEN": token, "PARSIMONY_URL": spy.URL}
|
|
if code, _, errs := e.runEnv(t, vars, cliMbox, "archive", "import", "dev@example.org", "-"); code != 0 {
|
|
t.Fatalf("%d %s", code, errs)
|
|
}
|
|
if contentType != "application/mbox" || body != cliMbox {
|
|
t.Fatalf("content type %q, body unchanged: %v", contentType, body == cliMbox)
|
|
}
|
|
}
|
|
|
|
// --- dkim ----------------------------------------------------------------------
|
|
|
|
// noAuth runs a command that must not need the API token or URL.
|
|
func noAuth(t *testing.T, args ...string) (int, string, string) {
|
|
t.Helper()
|
|
var out, errb bytes.Buffer
|
|
code := Run(args, IO{In: strings.NewReader(""), Out: &out, Err: &errb}, func(string) string { return "" })
|
|
return code, out.String(), errb.String()
|
|
}
|
|
|
|
func TestDKIMKeygen(t *testing.T) {
|
|
dir := t.TempDir()
|
|
keyFile := filepath.Join(dir, "dkim.pem")
|
|
code, out, errs := noAuth(t, "dkim", "keygen", "example.org", "--out", keyFile)
|
|
if code != 0 {
|
|
t.Fatalf("exit %d: %s", code, errs)
|
|
}
|
|
info, err := os.Stat(keyFile)
|
|
if err != nil || info.Mode().Perm() != 0o600 {
|
|
t.Fatalf("the private key must be created with mode 0600: %v %v", err, info)
|
|
}
|
|
key, err := dkimsign.LoadKey("example.org", "mail", keyFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want, _ := dkimsign.Record(key.Signer.Public())
|
|
for _, s := range []string{
|
|
"mail._domainkey.example.org.", "IN TXT", "Wrote private key to " + keyFile,
|
|
"dkim:", "domain: example.org", "selector: mail", "key_file: " + keyFile, want,
|
|
"parsimony dkim check example.org",
|
|
} {
|
|
if !strings.Contains(out, s) {
|
|
t.Errorf("output lacks %q:\n%s", s, out)
|
|
}
|
|
}
|
|
// The zone-file form is split in strings of at most 255 bytes.
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if strings.Contains(line, "IN TXT") {
|
|
for _, q := range regexp.MustCompile(`"([^"]*)"`).FindAllStringSubmatch(line, -1) {
|
|
if len(q[1]) > 255 {
|
|
t.Fatalf("a TXT string of %d bytes exceeds the DNS limit", len(q[1]))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if strings.Contains(out, "PRIVATE KEY") {
|
|
t.Fatal("never print the private key")
|
|
}
|
|
}
|
|
|
|
func TestDKIMKeygenOptions(t *testing.T) {
|
|
dir := t.TempDir()
|
|
keyFile := filepath.Join(dir, "k.pem")
|
|
code, out, _ := noAuth(t, "dkim", "keygen", "lists.example.org", "--selector", "s2024", "--out", keyFile)
|
|
if code != 0 || !strings.Contains(out, "s2024._domainkey.lists.example.org.") || !strings.Contains(out, "selector: s2024") {
|
|
t.Fatalf("selector: %d %s", code, out)
|
|
}
|
|
|
|
first, _ := os.ReadFile(keyFile)
|
|
code, _, errs := noAuth(t, "dkim", "keygen", "lists.example.org", "--out", keyFile)
|
|
if code != 1 || !strings.Contains(errs, "already exists") {
|
|
t.Fatalf("an existing key must not be overwritten: %d %q", code, errs)
|
|
}
|
|
if again, _ := os.ReadFile(keyFile); !bytes.Equal(first, again) {
|
|
t.Fatal("the existing key was modified")
|
|
}
|
|
if code, _, errs := noAuth(t, "dkim", "keygen", "lists.example.org", "--out", keyFile, "--force"); code != 0 {
|
|
t.Fatalf("--force: %d %s", code, errs)
|
|
}
|
|
if replaced, _ := os.ReadFile(keyFile); bytes.Equal(first, replaced) {
|
|
t.Fatal("--force must replace the key")
|
|
}
|
|
if info, _ := os.Stat(keyFile); info.Mode().Perm() != 0o600 {
|
|
t.Fatalf("mode after --force: %v", info.Mode().Perm())
|
|
}
|
|
}
|
|
|
|
func TestDKIMKeygenDefaultFileName(t *testing.T) {
|
|
dir := t.TempDir()
|
|
old, _ := os.Getwd()
|
|
os.Chdir(dir)
|
|
defer os.Chdir(old)
|
|
if code, _, errs := noAuth(t, "dkim", "keygen", "example.org"); code != 0 {
|
|
t.Fatalf("%d %s", code, errs)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "dkim-example.org.pem")); err != nil {
|
|
t.Fatalf("default file name: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDKIMUsageErrors(t *testing.T) {
|
|
for name, args := range map[string][]string{
|
|
"no domain": {"dkim", "keygen"},
|
|
"domain with @": {"dkim", "keygen", "a@example.org"},
|
|
"domain with space": {"dkim", "keygen", "my domain.org"},
|
|
"no dot": {"dkim", "keygen", "localhost"},
|
|
"bad selector": {"dkim", "keygen", "example.org", "--selector", "my sel"},
|
|
"bits not a number": {"dkim", "keygen", "example.org", "--bits", "abc"},
|
|
"record no key": {"dkim", "record", "example.org"},
|
|
"check no key": {"dkim", "check", "example.org"},
|
|
"unknown": {"dkim", "frobnicate"},
|
|
} {
|
|
if code, _, errs := noAuth(t, args...); code != 2 {
|
|
t.Errorf("%s: exit %d, want 2 (%q)", name, code, errs)
|
|
}
|
|
}
|
|
if code, _, errs := noAuth(t, "dkim", "keygen", "example.org", "--bits", "512", "--out", filepath.Join(t.TempDir(), "k")); code != 1 || !strings.Contains(errs, "1024") {
|
|
t.Fatalf("a tiny key is refused: %d %q", code, errs)
|
|
}
|
|
}
|
|
|
|
func writeKey(t *testing.T) (string, string) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
keyFile := filepath.Join(dir, "k.pem")
|
|
if code, _, errs := noAuth(t, "dkim", "keygen", "example.org", "--out", keyFile); code != 0 {
|
|
t.Fatalf("%d %s", code, errs)
|
|
}
|
|
key, _ := dkimsign.LoadKey("example.org", "mail", keyFile)
|
|
rec, _ := dkimsign.Record(key.Signer.Public())
|
|
return keyFile, rec
|
|
}
|
|
|
|
func TestDKIMRecord(t *testing.T) {
|
|
keyFile, rec := writeKey(t)
|
|
code, out, errs := noAuth(t, "dkim", "record", "example.org", "--key", keyFile)
|
|
if code != 0 || !strings.Contains(out, "mail._domainkey.example.org.") || !strings.Contains(out, rec) {
|
|
t.Fatalf("record: %d %q %q", code, out, errs)
|
|
}
|
|
_, out, _ = noAuth(t, "dkim", "record", "example.org", "--key", keyFile, "--selector", "x1")
|
|
if !strings.Contains(out, "x1._domainkey.example.org.") {
|
|
t.Fatalf("selector: %q", out)
|
|
}
|
|
if code, _, errs := noAuth(t, "dkim", "record", "example.org", "--key", "/nonexistent.pem"); code != 1 || errs == "" {
|
|
t.Fatalf("missing key: %d %q", code, errs)
|
|
}
|
|
}
|
|
|
|
func withDNS(t *testing.T, records map[string][]string) *[]string {
|
|
t.Helper()
|
|
var asked []string
|
|
old := lookupTXT
|
|
lookupTXT = func(name string) ([]string, error) {
|
|
asked = append(asked, name)
|
|
if r, ok := records[name]; ok {
|
|
return r, nil
|
|
}
|
|
return nil, errors.New("no such host")
|
|
}
|
|
t.Cleanup(func() { lookupTXT = old })
|
|
return &asked
|
|
}
|
|
|
|
func TestDKIMCheck(t *testing.T) {
|
|
keyFile, rec := writeKey(t)
|
|
|
|
asked := withDNS(t, map[string][]string{"mail._domainkey.example.org": {"v=spf1 -all", rec}})
|
|
code, out, errs := noAuth(t, "dkim", "check", "example.org", "--key", keyFile)
|
|
if code != 0 || !strings.Contains(out, "OK") || len(*asked) != 1 || (*asked)[0] != "mail._domainkey.example.org" {
|
|
t.Fatalf("matching record: %d %q %q asked=%v", code, out, errs, *asked)
|
|
}
|
|
|
|
withDNS(t, map[string][]string{"mail._domainkey.example.org": {"v=DKIM1; k=rsa; p=AAAAAAAA"}})
|
|
code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", keyFile)
|
|
if code != 1 || !strings.Contains(errs, "different key") {
|
|
t.Fatalf("mismatch: %d %q", code, errs)
|
|
}
|
|
|
|
withDNS(t, map[string][]string{})
|
|
code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", keyFile)
|
|
if code != 1 || !strings.Contains(errs, "no DKIM record") || !strings.Contains(errs, "mail._domainkey.example.org") {
|
|
t.Fatalf("missing: %d %q", code, errs)
|
|
}
|
|
|
|
withDNS(t, map[string][]string{"mail._domainkey.example.org": {"v=spf1 -all"}})
|
|
if code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", keyFile); code != 1 || !strings.Contains(errs, "no DKIM record") {
|
|
t.Fatalf("only unrelated TXT records: %d %q", code, errs)
|
|
}
|
|
|
|
asked = withDNS(t, map[string][]string{"s9._domainkey.example.org": {rec}})
|
|
if code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", keyFile, "--selector", "s9"); code != 0 {
|
|
t.Fatalf("selector: %d %q asked=%v", code, errs, *asked)
|
|
}
|
|
if code, _, errs = noAuth(t, "dkim", "check", "example.org", "--key", "/nonexistent.pem"); code != 1 {
|
|
t.Fatalf("missing key file: %d %q", code, errs)
|
|
}
|
|
}
|
|
|
|
func TestDKIMCheckEd25519Key(t *testing.T) {
|
|
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
|
der, _ := x509.MarshalPKCS8PrivateKey(priv)
|
|
keyFile := filepath.Join(t.TempDir(), "ed.pem")
|
|
os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der}), 0o600)
|
|
rec, _ := dkimsign.Record(pub)
|
|
withDNS(t, map[string][]string{"mail._domainkey.example.org": {rec}})
|
|
if code, out, errs := noAuth(t, "dkim", "check", "example.org", "--key", keyFile); code != 0 || !strings.Contains(out, "OK") {
|
|
t.Fatalf("%d %q %q", code, out, errs)
|
|
}
|
|
}
|
|
|
|
func TestDKIMDomainIsLowercased(t *testing.T) {
|
|
keyFile := filepath.Join(t.TempDir(), "k.pem")
|
|
code, out, errs := noAuth(t, "dkim", "keygen", "Lists.Example.ORG", "--out", keyFile)
|
|
if code != 0 {
|
|
t.Fatalf("%d %s", code, errs)
|
|
}
|
|
if !strings.Contains(out, "mail._domainkey.lists.example.org.") || !strings.Contains(out, "domain: lists.example.org") {
|
|
t.Fatalf("DNS names and the config use lowercase:\n%s", out)
|
|
}
|
|
asked := withDNS(t, map[string][]string{})
|
|
noAuth(t, "dkim", "check", "Lists.Example.ORG", "--key", keyFile)
|
|
if len(*asked) != 1 || (*asked)[0] != "mail._domainkey.lists.example.org" {
|
|
t.Fatalf("lookup name: %v", *asked)
|
|
}
|
|
}
|