Files
magpie/internal/davsync/lan_own_test.go
T
yetone 758a4084ee sync: LAN sharing stays as each computer has it, so turning it on at home no longer turns it on at work (Lullaby on Discord)
Settings.KeepOwn now keeps LAN (sharing on the local network), AllowIPs
(the addresses let in to it) and SessionTerminal (the Mac app a session
resumes in) from this computer whenever a sync, a join, a Restore, an Undo
or a backup file brings settings in. davsync's hashes blanks the same
fields, so flipping sharing is no change to push. A backup restored onto a
new computer leaves sharing off there: it opens the gateway to the
network, so it is turned on by hand, like the port and proxy.

The default sharing key is not per computer. It lives in the gateway-key
store, which rides the settings part with LANKey and LANKeyID, so every
computer holds the same key, and a second computer turning sharing on
takes up that key instead of making another one (configureLAN finds it by
LANKeyID or k.LAN).

Audit of Settings, what stays synced and why:
- already own: Window/WindowMaximised, UIFont/CodeFont, Proxy, Port,
  Dock/DockWindow, Lightweight, KeepAwake/KeepAwakeDisplay, Tray*,
  GatewayMode, NoWSLAgents, QuotaReads, NoAccountDiscovery,
  GatewayConversations.
- made own now: LAN, AllowIPs (the address list belongs to the network
  this computer is on), SessionTerminal (another Mac may not have the
  app).
- not in Settings: launch at login (internal/autostart, the OS's own
  setting) and the listen address (MAGPIE_ADDR, plus Port). Remote access
  through Tailscale etc. goes through those.
- still synced: CORSOrigins (pages and extensions the user uses, and each
  still needs a gateway key), OTel, Redact*, update settings
  (NoAutoUpdate, AutoRestart, UpdateMirror, ChinaMirror), alerts, model
  and agent choices, Theme/Lang/TextSize. These are the user's
  preferences, not facts about one machine.

Older magpie versions still take LAN from the bundle. Only new clients
ignore it.

Docs: sync-backup.md gets a "What stays on each computer" section, and
gateway-routing.md says AllowIPs is per computer. README and site docs
don't list what sync carries, and no GUI string does either, so they are
unchanged.

Run, on origin/main 95e7c6bcc + this change:
- gofmt -l internal/settings internal/davsync: nothing printed
- GOOS=darwin/linux/windows go vet -tags nogui on settings, davsync,
  backup, access, gui: exit 0 each
- go test -tags nogui settings, davsync, backup, access, gui: all ok
- before the rebase, on 226222118: go vet ./..., GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./..., go build, GOOS=linux
  go build -tags nogui, GOOS=windows go build: all ok
Full suite: SUITE_LINE

Broken by hand: with the three KeepOwn lines removed,
TestLANSharingStaysOnItsComputer fails ("work after joining: sharing on
the local network is true, want false", the same after a restore, "turning
sharing off at home pushed 1 times", "home after work turned sharing on:
... true, want false", plus the AllowIPs and SessionTerminal ones), and
TestLANSharingIsNoChange fails ("sharing on the local network changes the
settings part's hash"). With LAN alone dropped from KeepOwn, the LAN
checks and the hash test fail and the others pass. Restoring the change
turns both green.

Not tried: two real computers syncing through a real WebDAV, S3 or GitHub
folder. The test uses two sandbox homes and a fake WebDAV server.
2026-10-15 23:08:00 +08:00

169 lines
5.5 KiB
Go

package davsync
import (
"context"
"net/http/httptest"
"slices"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/backup"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// Sharing the gateway on the local network is this computer's own (Lullaby
// on Discord: turned on at home, it came on at work too). Two computers in
// one folder, sharing on at one and off at the other, each keep their own
// through syncs both ways, a join and a Restore; flipping it is no change
// to push; the default key still follows the folder, so the other computer
// turning sharing on takes up the same key. The addresses let in and the
// terminal a session resumes in are this computer's too.
func TestLANSharingStaysOnItsComputer(t *testing.T) {
fake := &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}, cond: true, putETag: true}
srv := httptest.NewServer(fake)
defer srv.Close()
cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "lan sharing fixture passphrase", Keys: true}
home, work := newComputer(t), newComputer(t)
use := func(c computer) {
t.Helper()
c.use(t)
appdir.UseExecutable("")
provider.ForgetAccounts()
}
t.Cleanup(provider.ForgetAccounts)
now := func() {
t.Helper()
if err := Now(context.Background()); err != nil {
t.Fatal(err)
}
}
puts := func() int {
fake.mu.Lock()
defer fake.mu.Unlock()
return fake.puts
}
edit := func(f func(*settings.Settings)) {
t.Helper()
s := settings.Load()
f(&s)
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
}
want := func(where string, lan bool, allow []string, term string) {
t.Helper()
s := settings.Load()
if s.LAN != lan {
t.Errorf("%s: sharing on the local network is %v, want %v (this computer's own)", where, s.LAN, lan)
}
if !slices.Equal(s.AllowIPs, allow) {
t.Errorf("%s: addresses let in are %v, want %v (this computer's own)", where, s.AllowIPs, allow)
}
if s.SessionTerminal != term {
t.Errorf("%s: session terminal is %q, want %q (this computer's own)", where, s.SessionTerminal, term)
}
}
// Home shares the gateway, on its own network, and syncs first.
use(home)
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
homeKey := access.LANSecret()
if homeKey == "" {
t.Fatal("fixture: sharing on made no default key")
}
edit(func(s *settings.Settings) {
s.Theme = "dark"
s.AllowIPs = []string{"192.168.1.0/24"}
s.SessionTerminal = "com.googlecode.iterm2"
})
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
now()
// Work, sharing off, joins: home's theme comes in, home's sharing
// doesn't.
use(work)
edit(func(s *settings.Settings) { s.AllowIPs = []string{"10.0.0.0/8"} })
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
now()
if s := settings.Load(); s.Theme != "dark" {
t.Fatalf("fixture: the join brought no settings in (theme %q)", s.Theme)
}
want("work after joining", false, []string{"10.0.0.0/8"}, "")
if access.LANSecret() != "" {
t.Error("work after joining: the gateway is shared with a key")
}
// a Restore of the folder's setup leaves it as it is too
edit(func(s *settings.Settings) { s.Theme = "light" })
if _, err := Restore(context.Background()); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.Theme != "dark" {
t.Fatalf("fixture: the restore brought no settings in (theme %q)", s.Theme)
}
want("work after a restore", false, []string{"10.0.0.0/8"}, "")
// Work changes a synced setting: it goes up, with work's sharing off,
// and home takes it in still sharing.
edit(func(s *settings.Settings) { s.Theme = "light" })
now()
use(home)
now()
if s := settings.Load(); s.Theme != "light" {
t.Fatalf("fixture: home didn't take work's change in (theme %q)", s.Theme)
}
want("home after work's push", true, []string{"192.168.1.0/24"}, "com.googlecode.iterm2")
if got := access.LANSecret(); got != homeKey {
t.Errorf("home after work's push: shared with %q, want its key", got)
}
// Home turning sharing off is no change to push.
before := puts()
if err := access.ConfigureLAN(false, false); err != nil {
t.Fatal(err)
}
now()
if n := puts() - before; n != 0 {
t.Errorf("turning sharing off at home pushed %d times", n)
}
// Work turns sharing on: with the folder's default key, and home stays
// off through syncs both ways.
use(work)
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
if got := access.LANSecret(); got != homeKey {
t.Errorf("work shares with %q, want the folder's default key", got)
}
edit(func(s *settings.Settings) { s.Theme = "system" })
now()
use(home)
now()
if s := settings.Load(); s.Theme != "system" {
t.Fatalf("fixture: home didn't take work's change in (theme %q)", s.Theme)
}
want("home after work turned sharing on", false, []string{"192.168.1.0/24"}, "com.googlecode.iterm2")
use(work)
now()
want("work after syncing again", true, []string{"10.0.0.0/8"}, "")
}
// The settings part's hash, which tells a change to push, doesn't see
// sharing, the addresses let in or the terminal.
func TestLANSharingIsNoChange(t *testing.T) {
a := settings.Settings{Theme: "dark"}
b := a
b.LAN, b.AllowIPs, b.SessionTerminal = true, []string{"10.0.0.0/8"}, "com.googlecode.iterm2"
if hashes(backup.Bundle{Settings: &a})["settings"] != hashes(backup.Bundle{Settings: &b})["settings"] {
t.Error("sharing on the local network changes the settings part's hash")
}
}