jinpwng (#6428, on X) syncs two computers and wants only Library and memory to go between them: each keeps its own providers, settings and models. They also want to see what a sync would change before it applies, and to roll the last one back. What syncs: providers, settings and profiles now have switches like agents' models, library and memory (Config.Providers/Settings/Profiles, *bool, nil = synced as before, so an existing setup syncs exactly what it did). The sync form shows Providers (and its API keys), Settings and Agents' models up front; Profiles, usage and sign-ins sit under Advanced, closed unless one is set away from its default. The form sends a part's choice only when it is or was off. A part left out stays as it is here and on the server; turning it back on joins it like a first sync (st.Off, rejoin). The sync's line says which parts each computer keeps ("Stays as it is on each computer: ..."). The setup text and the CLI carry it: magpie <webdav|s3|github> set providers=no settings=no profiles=no. Preview: davsync.Preview (plan.go) says, per part, what syncing now would bring in, send up, or leave; library items and memory files by name, sign-ins and plugins gone. Preview... in Settings > Sync shows it, and Sync now from there runs SyncPreviewed(token): if the server or this computer changed since, it says so and shows the new plan instead of syncing something else. CLI: magpie <kind> preview. Undo: a sync that brings parts in records them (st.Applied) and keeps a sealed copy of this computer first (keepSealed, as Restore does). Undo the last sync puts those parts back from that copy, on this computer only (st.Local, so the next sync sends it up rather than fetching the server's again). If one of them changed here since, it asks first (409 changed, Undo anyway). CLI: magpie <kind> undo-sync [anyway]. Restore's own Undo is unchanged. docs/subsystems/sync-backup.md and the site's sync docs (en, zh, ja) have a section on it. i18n: zh, zh-TW, ja, de. Run (worktree at base48e394146): - go test -count=1 . ./internal/davsync ./internal/cli: ok (mote). Broken by hand, each went red: syncs() forced true (scope tests), rejoin's again forced false, UndoSync without the st.Local update. - go test -count=1 ./internal/gui: TestCoreScriptBudget was over by 85 bytes; the sync-left line moved into syncfiles.js, now ok. - go vet ./internal/gui, GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. gofmt -l: nothing. - node --check app.js, syncfiles.js, sync-scope.test.cjs: ok. - GUI (mica), every test file that opens Settings > Sync or calls davsync (22 files) plus i18n-keys, Chromium and WebKit, 1100 and 420px, en/zh/zh-TW/ja/de: 453 of 454 pass. The red is s3-sync webkit en "picking S3 must not scroll the page" (0 !== 98), the same on origin/main52c9880cb(#3413, #2448). An earlier run also had library-new-skill chromium en 420 red, the same on main (#6539). sync-scope.test.cjs is new: on the old code there is no Providers switch, Preview or Undo, so it fails at once. - Rebased on origin/main7eecbcc56(app.js and i18n.js changed there): TestCoreScriptBudget, go test . ./internal/davsync, node --check, and i18n-keys, sync-scope, webdav-settings-refresh (both engines): ok. - The full go test ./... was started through mote's suite lock when this was pushed; its result goes on #6428. Not tried: a real WebDAV, S3 or GitHub server (the tests use fakes); the real app on Linux WebKitGTK. "Save and sync" from the form, after a preview, syncs without the preview's token check.
105 lines
3.8 KiB
Go
105 lines
3.8 KiB
Go
package davsync
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A sync setup copied as text (WJaunita45604 on X) opens on another computer
|
|
// with the same passphrase, into the same server, password and choices. The
|
|
// text holds neither the password nor the passphrase readable: base64 alone
|
|
// would hand the server to anyone who saw it.
|
|
func TestSetupTextOpensWithItsPassphrase(t *testing.T) {
|
|
for _, c := range []Config{
|
|
{URL: "https://dav.jianguoyun.com/dav/", User: "me@example.com", Password: "dav-app-password-9z", Passphrase: "correct horse", Keys: true, Agents: true, Usage: true},
|
|
{URL: "s3://bucket/team", User: "AKIAEXAMPLE", Password: "s3-secret-Qx7", Passphrase: "correct horse", Endpoint: "https://acc.r2.cloudflarestorage.com", Region: "auto", PathStyle: true, Library: new(bool), SignIns: true},
|
|
{URL: "github://me/backup/magpie", Password: "github_pat_11ABCDEF", Passphrase: "correct horse", Branch: "sync", Memory: new(bool)},
|
|
} {
|
|
newComputer(t).use(t)
|
|
writeSetup(t, c)
|
|
text, err := SetupText()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasPrefix(text, SetupPrefix) || strings.ContainsAny(text, " \n") {
|
|
t.Fatalf("not one line with its prefix: %q", text)
|
|
}
|
|
raw, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(text, SetupPrefix))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, secret := range []string{c.Password, c.Passphrase, c.User, "jianguoyun", "bucket", "github"} {
|
|
if secret != "" && (strings.Contains(text, secret) || strings.Contains(string(raw), secret)) {
|
|
t.Fatalf("%q is readable in the text", secret)
|
|
}
|
|
}
|
|
|
|
// another computer: a chat wrapped the line, and text came before it
|
|
newComputer(t).use(t)
|
|
wrapped := "here it is: " + text[:40] + "\n" + text[40:90] + " \r\n" + text[90:] + "\n"
|
|
if _, err := OpenSetupText(wrapped, "wrong horse"); !errors.Is(err, ErrSetupPassphrase) {
|
|
t.Fatalf("a wrong passphrase: %v", err)
|
|
}
|
|
s, err := OpenSetupText(wrapped, c.Passphrase)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := Setup{URL: c.URL, User: c.User, Password: c.Password, Endpoint: c.Endpoint, Region: c.Region, PathStyle: c.PathStyle,
|
|
Branch: c.Branch, Keys: c.Keys, Agents: c.Agents, Library: c.library(), Memory: c.memoryOn(), Usage: c.Usage, SignIns: c.SignIns}
|
|
if s != want {
|
|
t.Fatalf("opened %+v, want %+v", s, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSetupTextNeedsSyncAndItsOwnText(t *testing.T) {
|
|
newComputer(t).use(t)
|
|
if _, err := SetupText(); !errors.Is(err, ErrSetupOff) {
|
|
t.Fatalf("sync off: %v", err)
|
|
}
|
|
for _, s := range []string{"", "magpie-sync:", "magpie-sync:!!!", "magpie-sync:aGVsbG8", "https://dav.example.com"} {
|
|
if _, err := OpenSetupText(s, "x"); !errors.Is(err, ErrNotSetup) {
|
|
t.Fatalf("%q: %v", s, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func writeSetup(t *testing.T, c Config) {
|
|
t.Helper()
|
|
b, err := json.Marshal(c)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(path("sync.json")), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path("sync.json"), b, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// A setup that leaves providers and settings out of sync (#6428) opens on
|
|
// the next computer with them left out too; one that syncs them says
|
|
// nothing, so a setup from before the choice opens the same.
|
|
func TestSetupTextCarriesWhatStaysHere(t *testing.T) {
|
|
newComputer(t).use(t)
|
|
no := false
|
|
writeSetup(t, Config{URL: "https://dav.example.com/dav/", User: "me", Password: "pw", Passphrase: "correct horse", Providers: &no, Settings: &no})
|
|
text, err := SetupText()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err := OpenSetupText(text, "correct horse")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.Providers == nil || *s.Providers || s.Settings == nil || *s.Settings || s.Profiles != nil {
|
|
t.Fatalf("opened providers %v, settings %v, profiles %v", s.Providers, s.Settings, s.Profiles)
|
|
}
|
|
}
|