Files
magpie/internal/backup/caller_keys_test.go
T
yetone 0361189f9e test: every package's tests pass on Windows, and none writes outside its sandbox (the user: 把 Windows 上那几个失败的测试修掉) — on a Windows box the suite failed in most packages, and the root package hung for 25 minutes. Tests set HOME, which os.UserHomeDir ignores on Windows (it reads USERPROFILE), so they read and wrote the runner's own home; each now sets USERPROFILE beside HOME, and Devin's, Goose's, Crush's and Claude Desktop's tests set APPDATA/LOCALAPPDATA, where those keep their files there. said() read the command's output only once it returned, and a pipe holds a few KB on Windows, so magpie model --help blocked forever; it reads while the command writes. Unix permission bits (the gateway key store's 0600 too), exec bits and symlinks Windows lacks are checked only off Windows; TOML strings hold Windows paths as literal strings, JSON as marshalled ones; a URL's path is compared as one. The provider and usage packages run in a home of their own (TestMain, as the gateway's already does), since a CLI's identity kept by a refresh still going when its test ended landed in the runner's ~/.config/magpie — and on a Mac in the user's own. Two product fixes came out of it: imagemcp took a file:///C:/… root or image for the path /C:/…, which doesn't exist on Windows, and now makes a Windows path of it (a host for a UNC share); and session discovery could keep a new Cowork session hidden on Linux, where a folder's mtime is a kernel tick and its size doesn't change, so an entry added within the tick of the last listing left both as they were — a listing made within 2s of its folder's last change is read again, as git does for racy files. TestFilePath and TestRacyDirectoryListedAgain fail without the change. On Windows 11 every package passes (32 of 32) and a run of each package in a fresh home leaves nothing in home, APPDATA or LOCALAPPDATA; on Linux (WSL) and macOS go test -tags nogui ./... passes. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build pass.
2026-10-01 23:15:07 +08:00

214 lines
6.7 KiB
Go

package backup
import (
"encoding/json"
"errors"
"os"
"reflect"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/settings"
)
func TestGatewayKeysBackupRoundTrip(t *testing.T) {
home(t)
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
defaultKey := settings.Load()
client, err := access.Update("add-key", access.Change{Name: "Laptop"})
if err != nil {
t.Fatal(err)
}
keys, _ := access.List()
if _, err := access.Update("off-key", access.Change{Key: defaultKey.LANKeyID}); err != nil {
t.Fatal(err)
}
b, err := Collect(true, "test")
if err != nil {
t.Fatal(err)
}
data, err := Seal(b, "gateway passphrase")
if err != nil || strings.Contains(string(data), client) || strings.Contains(string(data), defaultKey.LANKey) {
t.Fatal("backup did not seal gateway credentials", err)
}
home(t)
b, err = Open(data, "gateway passphrase")
if err != nil {
t.Fatal(err)
}
if _, err := Restore(b, Parts{Settings: true}); err != nil {
t.Fatal(err)
}
got, err := access.List()
if err != nil || len(got) != len(keys) || got[0].ID != defaultKey.LANKeyID || !got[0].Off || got[1].ID != keys[1].ID || got[1].Name != "Laptop" {
t.Fatal("named key identity/state lost during restore", got, err)
}
if who, ok := access.Authenticate(client); !ok || who.KeyID != keys[1].ID {
t.Fatal("restored client cannot authenticate", who, ok)
}
if _, ok := access.Authenticate(defaultKey.LANKey); ok {
t.Fatal("restore resurrected the disabled default key")
}
if err := access.MigrateLegacyLANKey(); err != nil {
t.Fatal(err)
}
if got, _ := access.List(); len(got) != len(keys) {
t.Fatal("restore imported a duplicate default key", got)
}
if _, err := access.Update("on-key", access.Change{Key: defaultKey.LANKeyID}); err != nil {
t.Fatal(err)
}
if settings.Load().LANKey != defaultKey.LANKey {
t.Fatal("restored default key lost its legacy link")
}
info, err := os.Stat(access.Path())
if err != nil || runtime.GOOS != "windows" && info.Mode().Perm() != 0o600 {
t.Fatal("restored key-store permissions", info, err)
}
}
func TestGatewayKeysWithoutSecretsOrSettingsRestore(t *testing.T) {
home(t)
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
secret := settings.Load().LANKey
full, err := Collect(true, "test")
if err != nil {
t.Fatal(err)
}
without, err := Collect(false, "test")
if err != nil {
t.Fatal(err)
}
plain, _ := json.Marshal(without)
if strings.Contains(string(plain), secret) || without.Settings.LANKey != "" || without.Settings.LANKeyID != "" {
t.Fatal("--no-keys backup contains a gateway credential")
}
home(t)
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
current := settings.Load()
if _, err := Restore(full, Parts{}); err != nil {
t.Fatal(err)
}
if _, err := Restore(without, Parts{Settings: true}); err != nil {
t.Fatal(err)
}
if got := settings.Load(); got.LANKey != current.LANKey || got.LANKeyID != current.LANKeyID {
t.Fatal("keyless restore replaced this machine's LAN credential")
}
if _, ok := access.Authenticate(current.LANKey); !ok {
t.Fatal("restore without gateway keys revoked this machine's key")
}
}
func TestOlderBackupRecoversLegacyGatewayCredential(t *testing.T) {
home(t)
b := Bundle{Version: 1, Keys: true, Settings: &settings.Settings{LAN: true, LANKey: "{{API_KEY_88kqppzx}}", LANKeyID: "id-from-incomplete-backup"}}
if _, err := Restore(b, Parts{Settings: true}); err != nil {
t.Fatal(err)
}
if who, ok := access.Authenticate(b.Settings.LANKey); !ok || who.KeyName != "Magpie" {
t.Fatal("old backup's migration marker lost its credential", who, ok)
}
}
func TestOlderBackupKeepsExistingGatewayKeyStore(t *testing.T) {
home(t)
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
cur := settings.Load()
b := Bundle{Version: 1, Keys: true, Settings: &settings.Settings{LAN: true, LANKey: "fixture-older-backup-key", LANKeyID: "old-marker"}}
if _, err := Restore(b, Parts{Settings: true}); err != nil {
t.Fatal(err)
}
if got := settings.Load(); got.LANKey != cur.LANKey || got.LANKeyID != cur.LANKeyID {
t.Fatal("backup without a key store detached the existing default key")
}
if _, ok := access.Authenticate(cur.LANKey); !ok {
t.Fatal("backup without a key store revoked the existing gateway key")
}
}
func TestGatewayKeysBackupWithoutSettingsFile(t *testing.T) {
home(t)
secret, err := access.Update("add-key", access.Change{Name: "CLI client"})
if err != nil {
t.Fatal(err)
}
b, err := Collect(true, "test")
if err != nil || b.Settings != nil {
t.Fatal("CLI-only key fixture unexpectedly has settings", err)
}
home(t)
if _, err := Restore(b, Parts{Settings: true}); err != nil {
t.Fatal(err)
}
if who, ok := access.Authenticate(secret); !ok || who.KeyName != "CLI client" {
t.Fatal("standalone CLI key was not restored", who, ok)
}
}
func TestGatewayKeyRestoreFailureKeepsSettingsAndKeys(t *testing.T) {
for _, failure := range []string{"invalid-settings", "readonly-settings", "readonly-store"} {
t.Run(failure, func(t *testing.T) {
home(t)
if err := access.ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
cur := settings.Load()
before, err := os.ReadFile(access.Path())
if err != nil {
t.Fatal(err)
}
incoming := cur
incoming.Theme, incoming.LANKeyID, incoming.LANKey = "dark", "restored", "sk-magpie-restored"
keys := []access.Key{{ID: incoming.LANKeyID, Name: "Restored", Secret: incoming.LANKey, LAN: true}}
if failure == "invalid-settings" {
incoming.Theme = "invalid-theme"
} else {
path := settings.Path()
if failure == "readonly-store" {
path = access.Path()
}
if err := os.Chmod(path, 0o400); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Chmod(path, 0o600) })
f, err := os.OpenFile(path, os.O_WRONLY, 0)
if err == nil {
f.Close()
t.Skip("file remains writable")
}
if !errors.Is(err, os.ErrPermission) {
t.Fatal(err)
}
}
r, err := Restore(Bundle{Version: 1, Keys: true, Settings: &incoming, GatewayKeys: &keys}, Parts{Settings: true})
if err == nil || r.Settings {
t.Fatal("failed restore reported success", r, err)
}
if failure != "invalid-settings" && !errors.Is(err, os.ErrPermission) {
t.Fatal("restore lost permission error", err)
}
after, err := os.ReadFile(access.Path())
if err != nil || string(after) != string(before) || !reflect.DeepEqual(settings.Load(), cur) {
t.Fatal("failed restore left settings and keys inconsistent", err)
}
if _, ok := access.Authenticate(cur.LANKey); !ok {
t.Fatal("failed restore revoked the current key")
}
if _, ok := access.Authenticate(incoming.LANKey); ok {
t.Fatal("failed restore activated the incoming key")
}
})
}
}