Files
magpie/internal/davsync/corrupt_remote_test.go
尔立andClaude Code c221b9e2a9 davsync: a damaged file on the server is rebuilt, not failed on forever (#607)
* davsync: a damaged file on the server is rebuilt, not failed on forever

When the server's backup can't be read as a backup at all — a write a
relay or tunnel cut short, or one damaged since — syncOnce returned the
error and stopped. This computer's own setup is fine, so it could never
be pushed over the unreadable file to fix it, and every sync after
failed the same way: the only way out was deleting the file by hand.
(#531 detects a short write as it happens, but a file already damaged —
by an older magpie without that check, or by anything since — stayed
stuck on "not a magpie backup".)

The rebuild merges; it never pushes this computer's bundle as it is. A
sync that finds the file unreadable falls back to the last good copy of
the server's file it read whole — its cache, when that copy is
trustworthy — and runs the normal three-way merge over it, so another
computer's newer setup the damaged write swallowed survives. Three
guards say when a computer may do that, and any one missing leaves the
error standing: it has synced before (a fresh computer has no last-good
copy and no right to rebuild what it never saw); the cached copy is the
version the damage replaced (the damaged body is a prefix of it, which
is what a cut-short write leaves — and what an out-of-date computer's
older copy is not); and the body really begins like a magpie backup, so
a server answering with a page (a captive portal) is reported, never
rebuilt over. backup.ErrCorrupt — the message those two damaged checks
already gave — marks that case, told from a wrong passphrase and from a
newer magpie's file, both still refused. The push carries the damaged
file's ETag, so it stays checked against the server, and it happens
even when the merge matches the cached copy: the server's file is the
damaged one and must be replaced. The unreadable body is kept aside
under the sync folder, the newest few, so a server that always answers
the same unreadable way doesn't pile one up per tick.

Tests: TestSyncRebuildsFromCachedCopy pins the self-heal (the server
ends whole, the damaged body kept aside); TestSyncFreshComputerDoesNot
Rebuild and TestSyncOutOfDateComputerDoesNotRebuild pin the two
data-loss cases — the out-of-date one fails without the prefix guard
(verified by mutation); TestSyncPageIsNotDamage pins that a 200 HTML
page is reported, not pushed over; the newer-magpie refusal is guarded
against regression. go vet, and go test -tags nogui ./internal/davsync
./internal/backup under a temp HOME pass, as do the linux and windows
builds.

* davsync: a body cut inside the sealed file's constant head rebuilds nothing (yetone on #607)

The prefix check alone let an out-of-date computer rebuild: Seal writes
the envelope with MarshalIndent, so every version begins with the same
constant head up to the salt, and a body cut inside it (40 or 90 bytes)
is a prefix of every version — including an older cached copy — so that
copy's computer passed the guard and pushed its merge over a newer one's
data. The guard now also requires the damaged body to reach past the
random fields, to the "data" key that follows the salt and nonce, which
ties it to the one version it was cut from. A body cut before them is
attributable to no version, so it stays an error for every computer,
even the one that saw the good version — fail loud rather than rebuild
blind; a relay cutting a real write cuts deep into the data, so legit
rebuilds still go. TestSyncOutOfDateComputerDoesNotRebuild now cuts at
half, 90 and 40 bytes; the two short cuts fail against the old guard
(the out-of-date computer rebuilds and kimi is lost).

Co-Authored-By: Claude Code <noreply@anthropic.com>

---------

Co-authored-by: Claude Code <noreply@anthropic.com>
2026-10-03 11:20:57 +08:00

258 lines
9.5 KiB
Go

package davsync
import (
"context"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/backup"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// A server whose file a relay cut short used to stop every sync after: the
// body reads back as no backup, and this computer's own setup is fine, so
// there was no way to push over the unreadable file. Now a sync that has a
// trustworthy copy of the version that broke merges over it and writes the
// result back — the way any sync would — so another computer's newer setup
// the damaged write swallowed is not lost. Three cases the rebuild must
// never get wrong are pinned here: a fresh computer that never synced, an
// out-of-date computer whose copy isn't the one that broke, and a server
// that answers with a page instead of a backup at all.
const backupFile = "/dav/magpie/magpie.magpie-backup"
func cut(fake *fakeDAV, file string, keep int) {
fake.mu.Lock()
defer fake.mu.Unlock()
fake.n++
fake.files[file] = append([]byte(nil), fake.files[file][:keep]...)
fake.etags[file] = `"corrupt"`
}
func onServer(fake *fakeDAV, file string) []byte {
fake.mu.Lock()
defer fake.mu.Unlock()
return append([]byte(nil), fake.files[file]...)
}
// The damaged file is a prefix of the last good copy this computer read, so
// it rebuilds: the server ends up whole, and the damaged body is kept aside.
func TestSyncRebuildsFromCachedCopy(t *testing.T) {
fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}}
srv := httptest.NewServer(fake)
defer srv.Close()
ctx := context.Background()
cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "horse", Keys: true, Agents: true}
a := newComputer(t)
a.use(t)
provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"})
settings.Save(settings.Settings{Theme: "dark"})
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
if err := Now(ctx); err != nil {
t.Fatal(err)
}
if len(onServer(fake, backupFile)) == 0 {
t.Fatal("first sync wrote nothing")
}
cut(fake, backupFile, len(onServer(fake, backupFile))/2)
if err := Now(ctx); err != nil {
t.Fatalf("a damaged file stopped the sync: %v", err)
}
if _, err := backup.Open(onServer(fake, backupFile), "horse"); err != nil {
t.Fatalf("the server file wasn't rebuilt whole: %v", err)
}
if v := Status(); v.Error != "" {
t.Fatalf("the rebuild left an error: %q", v.Error)
}
kept, _ := filepath.Glob(filepath.Join(settings.Dir(), "sync", "*-server-damaged"+backup.Ext))
if len(kept) != 1 {
t.Fatalf("the damaged body wasn't kept aside: %v", kept)
}
}
// A computer that has never synced (st.Local == nil) has no last-good copy
// and no right to rebuild a file it never saw: its own (empty) setup must
// not go up over the damaged one, which would wipe another computer's data.
func TestSyncFreshComputerDoesNotRebuild(t *testing.T) {
fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}}
srv := httptest.NewServer(fake)
defer srv.Close()
ctx := context.Background()
cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "horse", Keys: true, Agents: true}
// A sets up and syncs, then the file is damaged.
a := newComputer(t)
a.use(t)
provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"})
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
if err := Now(ctx); err != nil {
t.Fatal(err)
}
before := onServer(fake, backupFile)
cut(fake, backupFile, len(before)/2)
// B joins fresh: it has never synced, so it must not rebuild from its
// own empty setup.
b := newComputer(t)
b.use(t)
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
if err := Now(ctx); err == nil {
t.Fatal("a fresh computer rebuilt the damaged file from its empty setup")
}
if got := onServer(fake, backupFile); string(got) != string(before[:len(before)/2]) {
t.Fatal("the fresh computer pushed over the server file anyway")
}
}
// An out-of-date computer must not rebuild either: its cached copy is an
// older version, not the one the damage replaced, so the damaged body isn't
// a prefix of it. Rebuilding from it would drop a newer computer's part.
//
// The prefix check alone isn't enough for a short cut. Seal writes the
// envelope with json.MarshalIndent, so every version starts with the same
// constant head up to the salt. A body cut inside that head — 90 or 40 bytes
// — is a prefix of every version, including an out-of-date computer's older
// copy, so it would let that computer rebuild and drop the newer one. The
// guard also requires the body to reach past the random fields (to the
// "data" key). That means a body cut before them is an error for every
// computer, even B which saw the version that broke — the trade-off is
// intended (fail loud rather than rebuild blind), and a relay cutting a
// real write cuts deep into the data, so it still rebuilds.
func TestSyncOutOfDateComputerDoesNotRebuild(t *testing.T) {
// "half" cuts deep into the data and B rebuilds from the cached copy;
// the short cuts reach no version's random fields, so no computer may
// rebuild.
cuts := map[string]struct {
keep func(int) int
bRebuilds bool // B (saw the good version) rebuilds
expectKimi bool // the server file ends with kimi
}{
"half": {keep: func(n int) int { return n / 2 }, bRebuilds: true, expectKimi: true},
// A relay cutting a real write cuts deep into it; a cut of 90 or 40
// bytes lands inside the constant head and is a prefix of every
// version, so neither computer rebuilds.
"90": {keep: func(int) int { return 90 }},
"40": {keep: func(int) int { return 40 }},
}
for name, tc := range cuts {
t.Run(name, func(t *testing.T) {
fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}}
srv := httptest.NewServer(fake)
defer srv.Close()
ctx := context.Background()
cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "horse", Keys: true, Agents: true}
a := newComputer(t)
a.use(t)
provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"})
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
if err := Now(ctx); err != nil {
t.Fatal(err)
}
// B joins, takes A's setup, adds kimi, and syncs: the server now
// has both, and A's cached copy (deepseek only) is behind it.
b := newComputer(t)
b.use(t)
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
if err := Now(ctx); err != nil {
t.Fatal(err)
}
provider.Save(provider.Provider{ID: "kimi", Name: "Kimi", Chat: "https://api.moonshot.cn/v1", Key: "k2"})
if err := Now(ctx); err != nil {
t.Fatal(err)
}
// the server's file — B's, with kimi — is damaged.
withKimi := onServer(fake, backupFile)
keep := tc.keep(len(withKimi))
cut(fake, backupFile, keep)
damaged := onServer(fake, backupFile)
// A, out of date, syncs: it must never rebuild from its copy
// without kimi.
a.use(t)
if err := Now(ctx); err == nil {
t.Fatal("an out-of-date computer rebuilt the damaged file")
}
// B, which saw the version that broke, rebuilds only when the
// body reaches past the random fields; a short cut stays an
// error for B too and the damaged file is left as it is.
b.use(t)
err := Now(ctx)
if err != nil && tc.bRebuilds {
t.Fatalf("the computer that saw the good version should rebuild: %v", err)
}
if err == nil && !tc.bRebuilds {
t.Fatal("a computer rebuilt a file cut before its random fields")
}
got := onServer(fake, backupFile)
if !tc.expectKimi {
if string(got) != string(damaged) {
t.Fatal("the server file changed across a refused rebuild")
}
return
}
rebuilt, oerr := backup.Open(got, "horse")
if oerr != nil {
t.Fatalf("rebuilt file doesn't open: %v", oerr)
}
var hasKimi bool
for _, p := range rebuilt.Providers {
if p.ID == "kimi" {
hasKimi = true
}
}
if !hasKimi {
t.Fatalf("kimi was lost across the rebuild: %+v", rebuilt.Providers)
}
})
}
}
// A server that answers with a page — a captive portal, an auth proxy — is
// not damage to rebuild over: the body isn't backup-shaped at all, so the
// sync must report it, not push this computer's setup and overwrite a good
// file the page merely stood in front of.
func TestSyncPageIsNotDamage(t *testing.T) {
fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}}
srv := httptest.NewServer(fake)
defer srv.Close()
ctx := context.Background()
cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "horse", Keys: true, Agents: true}
a := newComputer(t)
a.use(t)
provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"})
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
if err := Now(ctx); err != nil {
t.Fatal(err)
}
fake.mu.Lock()
fake.n++
fake.files[backupFile] = []byte("<html>please sign in</html>")
fake.etags[backupFile] = `"page"`
fake.mu.Unlock()
puts := fake.puts
if err := Now(ctx); err == nil || strings.Contains(err.Error(), "rebuilt") {
t.Fatalf("a page should be reported, not rebuilt: %v", err)
}
if fake.puts != puts {
t.Fatal("a page was treated as damage and pushed over")
}
}