Found by running preflight against a real Stalwart 0.15.5 in a VM. Two defects, the second worse than the first. 1. AccountSnapshot could not read the version this tool migrates FROM. 0.15.5 advertises no urn:stalwart:jmap capability and POST /api returns 404 - the JMAP management API and x:Account are 0.16 features. 0.15.x exposes a REST API at GET /api/principal instead. So preflight's account-snapshot check warned and moved on, and every run against a real source instance had no "before" data at all. AccountSnapshot now dispatches on the capability the session document advertises - a positive signal, not an inference from a failed call - and internal/stalwartapi/principal.go implements the 0.15.x REST path, including its 1-based page/limit pagination so an install larger than one page isn't silently truncated. 2. With no "before" counts, the content-integrity comparison iterated an empty map, checked nothing, and reported "all message counts match". That is the strongest claim this tool makes - ARCHITECTURE 4.7 calls it the actual no-data-loss guarantee - made vacuously, and it would have passed on a migration that lost every message. The comparison now derives its account set from whatever the source could report, verifies every account and domain survived either way, and carries MessageCountsCompared so the report says plainly "MESSAGE COUNTS NOT COMPARED ... no-data-loss is NOT verified here" rather than implying otherwise. What can and cannot be checked across the 0.15/0.16 boundary, now that a real server has answered: 0.15.x has no per-mailbox message count at any endpoint, and the impersonation login 0.16 offers returns 401 there, so before/after message counts are impossible for the boundary migration this tool exists for. Both versions do report per-account used quota (usedQuota in 0.15's REST list, usedDiskQuota on 0.16's x:Account), so that is captured on both sides. It is recorded and reported, not asserted on: 4.5 notes the 0.16 migration resets quotas to zero pending recalculation, so comparing those bytes across the boundary would be a false alarm generator. Test servers across preflight, validate and stalwartapi now advertise urn:stalwart:jmap, since they stand in for 0.16 instances and that capability is what says so. Verified end to end against the smoke VM: all nine preflight checks pass, and the checkpoint records 2 accounts, 1 domain and per-account used quota where it previously recorded nothing.
184 lines
6.1 KiB
Go
184 lines
6.1 KiB
Go
// SPDX-FileCopyrightText: 2026 LINUXexpert-org
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package stalwartapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// stalwart015Server stands in for a 0.15.x instance: no urn:stalwart:jmap
|
|
// capability, POST /api is 404, and the management API is REST at
|
|
// /api/principal with 1-based page/limit paging. Every shape here was
|
|
// confirmed against a live 0.15.5 server.
|
|
func stalwart015Server(t *testing.T, individuals, domains []map[string]any) (*httptest.Server, *[]string) {
|
|
t.Helper()
|
|
var paths []string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
paths = append(paths, r.URL.Path+"?"+r.URL.RawQuery)
|
|
|
|
if r.URL.Path == "/.well-known/jmap" {
|
|
// 0.15.5 advertises no urn:stalwart:jmap.
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"capabilities": map[string]any{"urn:ietf:params:jmap:core": map[string]any{}},
|
|
})
|
|
return
|
|
}
|
|
if r.URL.Path == "/api" {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
w.Write([]byte(`{"status":404,"title":"Not Found"}`))
|
|
return
|
|
}
|
|
if r.URL.Path != "/api/principal" {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
set := individuals
|
|
if r.URL.Query().Get("types") == "domain" {
|
|
set = domains
|
|
}
|
|
limit, page := 100, 1
|
|
fmt.Sscanf(r.URL.Query().Get("limit"), "%d", &limit)
|
|
fmt.Sscanf(r.URL.Query().Get("page"), "%d", &page)
|
|
|
|
start := (page - 1) * limit
|
|
end := start + limit
|
|
if start > len(set) {
|
|
start = len(set)
|
|
}
|
|
if end > len(set) {
|
|
end = len(set)
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"data": map[string]any{"items": set[start:end], "total": len(set)},
|
|
})
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv, &paths
|
|
}
|
|
|
|
// The headline case: against the version this tool actually migrates from,
|
|
// AccountSnapshot must not fall over on a 404 from the 0.16-only endpoint.
|
|
func TestAccountSnapshotUsesRESTAgainst015(t *testing.T) {
|
|
srv, paths := stalwart015Server(t,
|
|
[]map[string]any{
|
|
{"id": 4, "type": "individual", "name": "alice", "emails": []string{"[email protected]"}, "usedQuota": 9207},
|
|
{"id": 5, "type": "individual", "name": "bob", "emails": []string{"[email protected]"}, "usedQuota": 5380},
|
|
},
|
|
[]map[string]any{{"id": 1, "type": "domain", "name": "smoke.test"}},
|
|
)
|
|
client := &Client{BaseURL: srv.URL, Username: "admin", Password: "hunter2"}
|
|
|
|
snap, err := client.AccountSnapshot(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("AccountSnapshot against 0.15.x: %v", err)
|
|
}
|
|
if snap.AccountCount != 2 {
|
|
t.Errorf("AccountCount = %d, want 2", snap.AccountCount)
|
|
}
|
|
if len(snap.Domains) != 1 || snap.Domains[0] != "smoke.test" {
|
|
t.Errorf("Domains = %v, want [smoke.test]", snap.Domains)
|
|
}
|
|
if snap.UsedQuota["[email protected]"] != 9207 || snap.UsedQuota["[email protected]"] != 5380 {
|
|
t.Errorf("UsedQuota = %v, want alice 9207 and bob 5380", snap.UsedQuota)
|
|
}
|
|
// Message counts genuinely cannot be had from 0.15.x - see principal.go.
|
|
if len(snap.MailboxCounts) != 0 {
|
|
t.Errorf("MailboxCounts = %v, want empty: 0.15.x exposes no per-mailbox counts", snap.MailboxCounts)
|
|
}
|
|
for _, p := range *paths {
|
|
if strings.HasPrefix(p, "/api?") {
|
|
t.Errorf("the 0.16-only JMAP management endpoint was called against a 0.15.x instance: %s", p)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A truncated "before" snapshot would make the post-migration comparison
|
|
// assert less than it claims, silently.
|
|
func TestRESTPrincipalsFollowsPagination(t *testing.T) {
|
|
var many []map[string]any
|
|
for i := 0; i < 250; i++ {
|
|
many = append(many, map[string]any{
|
|
"id": i, "type": "individual",
|
|
"name": fmt.Sprintf("user%03d", i),
|
|
"emails": []string{fmt.Sprintf("user%[email protected]", i)},
|
|
})
|
|
}
|
|
srv, _ := stalwart015Server(t, many, nil)
|
|
client := &Client{BaseURL: srv.URL, Username: "admin", Password: "x"}
|
|
|
|
snap, err := client.AccountSnapshot(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if snap.AccountCount != 250 {
|
|
t.Errorf("AccountCount = %d, want all 250 across pages", snap.AccountCount)
|
|
}
|
|
}
|
|
|
|
// An instance with no explicit domain principals still has domains, implied
|
|
// by its accounts' addresses.
|
|
func TestRESTSnapshotDerivesDomainsFromAddresses(t *testing.T) {
|
|
srv, _ := stalwart015Server(t,
|
|
[]map[string]any{
|
|
{"id": 1, "type": "individual", "name": "a", "emails": []string{"[email protected]"}},
|
|
{"id": 2, "type": "individual", "name": "b", "emails": []string{"[email protected]"}},
|
|
}, nil)
|
|
client := &Client{BaseURL: srv.URL, Username: "admin", Password: "x"}
|
|
|
|
snap, err := client.AccountSnapshot(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(snap.Domains) != 2 || snap.Domains[0] != "one.example" || snap.Domains[1] != "two.example" {
|
|
t.Errorf("Domains = %v, want [one.example two.example] sorted", snap.Domains)
|
|
}
|
|
}
|
|
|
|
func TestRESTSnapshotSurfacesAuthFailure(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/.well-known/jmap" {
|
|
json.NewEncoder(w).Encode(map[string]any{"capabilities": map[string]any{}})
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.Write([]byte("invalid credentials"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
client := &Client{BaseURL: srv.URL, Username: "admin", Password: "wrong"}
|
|
_, err := client.AccountSnapshot(context.Background())
|
|
if err == nil {
|
|
t.Fatal("want an error when the principal list rejects the credentials")
|
|
}
|
|
if !strings.Contains(err.Error(), "401") {
|
|
t.Errorf("error %q should carry the status it got", err)
|
|
}
|
|
}
|
|
|
|
// 0.16 reports the same per-account measure under a different name; both
|
|
// have to land in the same field or the comparison can't span the boundary.
|
|
func TestJMAPSnapshotCapturesUsedDiskQuota(t *testing.T) {
|
|
srv, _ := accountManagementAndMailboxServer(t, map[string][]map[string]any{
|
|
"[email protected]": {{"name": "Inbox", "totalEmails": 10}},
|
|
"[email protected]": {{"name": "Inbox", "totalEmails": 3}},
|
|
})
|
|
defer srv.Close()
|
|
|
|
client := &Client{BaseURL: srv.URL, Username: "admin", Password: "hunter2"}
|
|
snap, err := client.AccountSnapshot(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(snap.UsedQuota) != 2 {
|
|
t.Errorf("UsedQuota = %v, want an entry per account", snap.UsedQuota)
|
|
}
|
|
}
|