package provider import ( "context" "encoding/json" "errors" "fmt" "os" "os/user" "path/filepath" "strconv" "strings" "testing" "golang.org/x/sys/unix" ) func accountFixture(t *testing.T) (Binding, HomeRules) { t.Helper() root := t.TempDir() home := filepath.Join(root, "dedicated") if e := os.Mkdir(home, 0700); e != nil { t.Fatal(e) } identity := Identity{AccountID: "account", HomeID: "enrolled-home", ProviderVersion: "0.165.0"} b := Binding{ID: "binding", Home: home, AccountID: "account", Provider: "codex", Expected: identity, RuntimeRoot: filepath.Join(root, "homes")} writeMetadata(t, home, identity) writeFake(t, filepath.Join(home, "auth.json"), `{"token":"fake-one"}`) return b, HomeRules{InteractiveHomes: []string{filepath.Join(root, "interactive")}} } func writeFake(t *testing.T, path, s string) { t.Helper() if e := os.WriteFile(path, []byte(s), 0600); e != nil { t.Fatal(e) } } func writeMetadata(t *testing.T, home string, id Identity) { t.Helper() raw, _ := json.Marshal(id) writeFake(t, filepath.Join(home, IdentityFile), string(raw)) } func openFixture(t *testing.T, b Binding, r HomeRules) *AccountLease { t.Helper() l, e := openAccount(context.Background(), b, r) if e != nil { t.Fatal(e) } t.Cleanup(func() { l.Close() }) return l } func preflightFixture(t *testing.T, l *AccountLease) { t.Helper() if e := l.Preflight(context.Background(), func(context.Context, Binding) (Identity, error) { return l.Identity(), nil }); e != nil { t.Fatal(e) } } func TestAccountHomeBinding(t *testing.T) { t.Run("dedicated provenance", func(t *testing.T) { b, r := accountFixture(t) b.DedicatedHome = true if b.VerifiedDedicatedHome() { t.Fatal("input boolean granted authority") } l := openFixture(t, b, r) if !l.Binding().VerifiedDedicatedHome() { t.Fatal("verified lease lost provenance") } wire, _ := json.Marshal(l.Binding()) var forged Binding json.Unmarshal(wire, &forged) if forged.VerifiedDedicatedHome() { t.Fatal("wire input granted authority") } altered := l.Binding() altered.AccountID = "other" if altered.VerifiedDedicatedHome() { t.Fatal("altered binding retained authority") } if e := l.Validate(context.Background()); !errors.Is(e, ErrPreflightRequired) { t.Fatalf("unobserved identity accepted: %v", e) } preflightFixture(t, l) if e := l.Validate(context.Background()); e != nil { t.Fatal(e) } l.Close() if l.Binding().VerifiedDedicatedHome() { t.Fatal("closed lease retained authority") } }) t.Run("identity", func(t *testing.T) { b, r := accountFixture(t) b.Expected.AccountID = "other" if l, e := openAccount(context.Background(), b, r); e == nil { l.Close() t.Fatal("wrong expected identity accepted") } }) t.Run("missing metadata", func(t *testing.T) { b, r := accountFixture(t) os.Remove(filepath.Join(b.Home, IdentityFile)) if l, e := openAccount(context.Background(), b, r); e == nil { l.Close() t.Fatal("missing metadata accepted") } }) t.Run("shared and aliases", func(t *testing.T) { b, r := accountFixture(t) r.InteractiveHomes = []string{b.Home} for _, name := range []string{b.Home, filepath.Join(filepath.Dir(b.Home), "alias")} { if name != b.Home { if e := os.Symlink(b.Home, name); e != nil { t.Fatal(e) } } candidate := b candidate.Home = name if l, e := openAccount(context.Background(), candidate, r); e == nil { l.Close() t.Fatal("shared home accepted") } } // Inode comparison is exercised independently of path spelling (bind alias semantics). f, e := os.Open(b.Home) if e != nil { t.Fatal(e) } defer f.Close() info, _ := f.Stat() if !r.refusedInode(info) { t.Fatal("shared inode accepted") } }) t.Run("real defaults path only", func(t *testing.T) { r := DefaultHomeRules() for _, p := range r.InteractiveHomes { if !r.RefusesPath(p) { t.Fatal("default shared home accepted") } } if !r.RefusesPath("/home/example/.paseo/zerosub/homes/fake") { t.Fatal("ZeroSub accepted") } }) t.Run("private modes", func(t *testing.T) { b, r := accountFixture(t) os.Chmod(filepath.Join(b.Home, "auth.json"), 0644) if l, e := openAccount(context.Background(), b, r); e == nil { l.Close() t.Fatal("public credentials accepted") } }) } func TestAccountUnavailableDoesNotFailover(t *testing.T) { for _, failure := range []error{ErrAccountMaintenance, ErrIdentity, Error("quota")} { t.Run(failure.Error(), func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) calls, prompts := 0, 0 e := l.Preflight(context.Background(), func(_ context.Context, got Binding) (Identity, error) { calls++ if got.AccountID != b.AccountID { t.Fatal("fallback account") } return Identity{}, failure }) if e == nil { t.Fatal("failure accepted") } if l.BeginTurn(context.Background()) == nil { prompts++ } if calls != 1 || prompts != 0 { t.Fatalf("calls=%d prompts=%d", calls, prompts) } }) } } func TestAccountHomeMutation(t *testing.T) { t.Run("replacement", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) os.Rename(b.Home, b.Home+"-old") os.Symlink(b.Home+"-old", b.Home) if e := l.Validate(context.Background()); !errors.Is(e, ErrContainment) { t.Fatalf("replacement accepted: %v", e) } }) t.Run("lock", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) if other, e := openAccount(context.Background(), b, r); !errors.Is(e, ErrAccountBusy) { if other != nil { other.Close() } t.Fatalf("concurrent lease accepted: %v", e) } l.Close() openFixture(t, b, r) }) t.Run("lock release with inherited descriptor", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) inherited, err := unix.FcntlInt(l.dir.Fd(), unix.F_DUPFD_CLOEXEC, 0) if err != nil { t.Fatal(err) } defer unix.Close(inherited) if err := l.Close(); err != nil { t.Fatal(err) } openFixture(t, b, r) }) for _, kind := range []string{"successful refresh", "failed refresh", "identity change", "active change"} { t.Run(kind, func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) if kind == "active change" { if e := l.BeginTurn(context.Background()); e != nil { t.Fatal(e) } } writeFake(t, filepath.Join(b.Home, "replacement"), `{"token":"fake-two"}`) os.Rename(filepath.Join(b.Home, "replacement"), filepath.Join(b.Home, "auth.json")) if kind == "identity change" { id := b.Expected id.AccountID = "different" writeMetadata(t, b.Home, id) } want := ErrPreflightRequired if kind == "identity change" { want = ErrIdentity } if kind == "active change" { want = ErrContainment } if e := l.Validate(context.Background()); !errors.Is(e, want) { t.Fatalf("got %v want %v", e, want) } if kind == "successful refresh" { preflightFixture(t, l) if e := l.BeginTurn(context.Background()); e != nil { t.Fatal(e) } if e := l.EndTurn(context.Background()); e != nil { t.Fatal(e) } } if kind == "failed refresh" { e := l.Preflight(context.Background(), func(context.Context, Binding) (Identity, error) { return Identity{}, ErrAccountMaintenance }) if !errors.Is(e, ErrAccountMaintenance) { t.Fatal(e) } if l.BeginTurn(context.Background()) == nil { t.Fatal("failed refresh dispatched") } } }) } } func TestPrivateHomeDoesNotTouchInteractiveConfig(t *testing.T) { b, r := accountFixture(t) interactive := r.InteractiveHomes[0] os.Mkdir(interactive, 0700) path := filepath.Join(interactive, "config.toml") writeFake(t, path, "original untrusted config") l := openFixture(t, b, r) preflightFixture(t, l) home := l.Binding().Home raw, e := os.ReadFile(filepath.Join(home, "auth.json")) if e != nil || string(raw) != `{"token":"fake-one"}` { t.Fatal("login not staged", e) } for _, p := range []string{home, filepath.Join(home, "auth.json")} { info, e := os.Stat(p) if e != nil { t.Fatal(e) } want := os.FileMode(0600) if info.IsDir() { want = 0700 } if info.Mode().Perm() != want { t.Fatal("nonprivate runtime") } } got, _ := os.ReadFile(path) entries, _ := os.ReadDir(interactive) if string(got) != "original untrusted config" || len(entries) != 1 { t.Fatal("interactive config changed") } l.Close() if _, e = os.Stat(home); !os.IsNotExist(e) { t.Fatal("runtime leaked") } } func TestAccountHomeRuntimeGuards(t *testing.T) { t.Run("runtime symlink", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) target := filepath.Join(l.Binding().Home, "auth.json") os.Remove(target) os.Symlink(filepath.Join(b.Home, "auth.json"), target) if l.Preflight(context.Background(), func(context.Context, Binding) (Identity, error) { return b.Expected, nil }) == nil { t.Fatal("runtime symlink accepted") } }) t.Run("same bytes new generation", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) writeFake(t, filepath.Join(b.Home, "new"), `{"token":"fake-one"}`) os.Rename(filepath.Join(b.Home, "new"), filepath.Join(b.Home, "auth.json")) if l.Validate(context.Background()) == nil { t.Fatal("atomic generation not detected") } }) t.Run("runtime active mutation", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) l.BeginTurn(context.Background()) writeFake(t, filepath.Join(l.Binding().Home, "auth.json"), `{"token":"fake-new"}`) if !errors.Is(l.EndTurn(context.Background()), Error("provider_refresh_pending")) { t.Fatal("active runtime mutation accepted") } }) t.Run("controlled refresh", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) writeFake(t, filepath.Join(l.Binding().Home, "auth.json"), `{"token":"fake-refreshed"}`) if e := l.CommitRefresh(context.Background(), func(_ context.Context, got Binding) (Identity, error) { if !got.VerifiedDedicatedHome() { t.Fatal("probe lost provenance") } return b.Expected, nil }); e != nil { t.Fatal(e) } if !errors.Is(l.Validate(context.Background()), ErrPreflightRequired) { t.Fatal("refresh skipped preflight") } preflightFixture(t, l) raw, _ := os.ReadFile(filepath.Join(b.Home, "auth.json")) if string(raw) != `{"token":"fake-refreshed"}` { t.Fatal("refresh not committed") } }) } func TestAccountHomeRuntimeAliasNoSideEffects(t *testing.T) { b, r := accountFixture(t) interactive := r.InteractiveHomes[0] if e := os.Mkdir(interactive, 0700); e != nil { t.Fatal(e) } alias := filepath.Join(filepath.Dir(b.Home), "runtime-alias") if e := os.Symlink(interactive, alias); e != nil { t.Fatal(e) } b.RuntimeRoot = filepath.Join(alias, "new") if l, e := openAccount(context.Background(), b, r); e == nil { l.Close() t.Fatal("runtime alias accepted") } entries, e := os.ReadDir(interactive) if e != nil || len(entries) != 0 { t.Fatal("interactive directory changed", e) } } func TestAccountHomeFileGuards(t *testing.T) { for _, kind := range []string{"directory mode", "credential hardlink", "metadata trailing", "metadata duplicate", "metadata missing home"} { t.Run(kind, func(t *testing.T) { b, r := accountFixture(t) switch kind { case "directory mode": os.Chmod(b.Home, 0755) case "credential hardlink": os.Link(filepath.Join(b.Home, "auth.json"), filepath.Join(filepath.Dir(b.Home), "linked")) case "metadata trailing": raw, _ := json.Marshal(b.Expected) writeFake(t, filepath.Join(b.Home, IdentityFile), string(raw)+`{}`) case "metadata duplicate": writeFake(t, filepath.Join(b.Home, IdentityFile), `{"account_id":"account","account_id":"account","home_id":"enrolled-home","provider_version":"0.165.0"}`) case "metadata missing home": id := b.Expected id.HomeID = "" writeMetadata(t, b.Home, id) } if l, e := openAccount(context.Background(), b, r); e == nil { l.Close() t.Fatal("unsafe account input accepted") } }) } } func TestAccountHomePreflightGuards(t *testing.T) { t.Run("observed identity", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) id := b.Expected id.AccountID = "other" if e := l.Preflight(context.Background(), func(context.Context, Binding) (Identity, error) { return id, nil }); !errors.Is(e, ErrIdentity) { t.Fatal("wrong CLI identity accepted", e) } }) t.Run("generation during probe", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) if e := l.Preflight(context.Background(), func(context.Context, Binding) (Identity, error) { writeFake(t, filepath.Join(b.Home, "auth.json"), `{"token":"changed-during-probe"}`) return b.Expected, nil }); !errors.Is(e, ErrPreflightRequired) { t.Fatal("unstable probe accepted", e) } }) t.Run("refresh identity", func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) writeFake(t, filepath.Join(l.Binding().Home, "auth.json"), `{"token":"wrong-account"}`) id := b.Expected id.AccountID = "other" if e := l.CommitRefresh(context.Background(), func(context.Context, Binding) (Identity, error) { return id, nil }); !errors.Is(e, ErrIdentity) { t.Fatal("wrong refresh accepted", e) } raw, _ := os.ReadFile(filepath.Join(b.Home, "auth.json")) if string(raw) != `{"token":"fake-one"}` { t.Fatal("failed refresh modified dedicated home") } }) t.Run("claude", func(t *testing.T) { b, r := accountFixture(t) b.Provider = "claude" b.Expected.ProviderVersion = "2.1.312" writeMetadata(t, b.Home, b.Expected) writeFake(t, filepath.Join(b.Home, ".credentials.json"), `{"token":"fake-claude"}`) l := openFixture(t, b, r) preflightFixture(t, l) if _, e := os.Stat(filepath.Join(l.Binding().Home, "auth.json")); !os.IsNotExist(e) { t.Fatal("other provider login copied") } }) } func TestAccountRefreshPending(t *testing.T) { for _, active := range []bool{false, true} { t.Run(fmt.Sprint(active), func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) ctx := context.Background() if active { if e := l.BeginTurn(ctx); e != nil { t.Fatal(e) } } path := filepath.Join(l.Binding().Home, "auth.json") refreshed := `{"token":"rotated"}` writeFake(t, path, refreshed) var e error if active { e = l.EndTurn(ctx) } else { e = l.Validate(ctx) } if !errors.Is(e, Error("provider_refresh_pending")) { t.Fatalf("refresh must be pending: %v", e) } calls := 0 probe := func(context.Context, Binding) (Identity, error) { calls++; return b.Expected, nil } if e = l.Preflight(ctx, probe); !errors.Is(e, Error("provider_refresh_pending")) { t.Errorf("preflight must preserve pending refresh: %v", e) } raw, _ := os.ReadFile(path) if string(raw) != refreshed || calls != 0 { t.Fatal("preflight overwrote rotated login or probed stale login") } if e = l.CommitRefresh(ctx, probe); e != nil { t.Fatal(e) } if e = l.BeginTurn(ctx); !errors.Is(e, ErrPreflightRequired) { t.Fatalf("refresh skipped preflight: %v", e) } preflightFixture(t, l) if e = l.BeginTurn(ctx); e != nil { t.Fatal(e) } if e = l.EndTurn(ctx); e != nil { t.Fatal(e) } raw, _ = os.ReadFile(filepath.Join(b.Home, "auth.json")) if string(raw) != refreshed { t.Fatal("rotated login lost") } }) } } func TestAccountContainmentLatched(t *testing.T) { for _, kind := range []string{"source", "identity", "runtime-identity", "symlink", "nonregular"} { t.Run(kind, func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) ctx := context.Background() if e := l.BeginTurn(ctx); e != nil { t.Fatal(e) } runtime := l.Binding().Home switch kind { case "source": writeFake(t, filepath.Join(b.Home, "auth.json"), `{"token":"external"}`) case "identity", "runtime-identity": home := b.Home if kind == "runtime-identity" { home = runtime } id := b.Expected id.AccountID = "changed" writeMetadata(t, home, id) case "symlink", "nonregular": path := filepath.Join(runtime, "auth.json") if e := os.Remove(path); e != nil { t.Fatal(e) } if kind == "symlink" { if e := os.Symlink(filepath.Join(b.Home, "auth.json"), path); e != nil { t.Fatal(e) } } else { if e := os.Mkdir(path, 0700); e != nil { t.Fatal(e) } } } if e := l.EndTurn(ctx); !errors.Is(e, ErrContainment) { t.Errorf("active tampering not contained: %v", e) } // Restore the inputs: a contained lease still cannot recover authority. writeMetadata(t, b.Home, b.Expected) writeMetadata(t, runtime, b.Expected) if kind == "symlink" || kind == "nonregular" { if e := os.Remove(filepath.Join(runtime, "auth.json")); e != nil { t.Fatal(e) } } writeFake(t, filepath.Join(runtime, "auth.json"), `{"token":"fake-one"}`) if e := l.Preflight(ctx, func(context.Context, Binding) (Identity, error) { return b.Expected, nil }); !errors.Is(e, ErrContainment) { t.Fatalf("containment not latched: %v", e) } if l.Binding().VerifiedDedicatedHome() { t.Fatal("contained lease retained provenance") } if e := l.BeginTurn(ctx); !errors.Is(e, ErrContainment) { t.Fatalf("contained lease admitted turn: %v", e) } }) } } func TestAccountHomeDefaultsIgnoreEnvironment(t *testing.T) { u, e := user.LookupId(strconv.Itoa(os.Geteuid())) if e != nil { t.Fatal(e) } for _, home := range []string{"", t.TempDir()} { t.Setenv("HOME", home) r := DefaultHomeRules() for _, name := range []string{".claude", ".codex"} { if !r.RefusesPath(filepath.Join(u.HomeDir, name)) { t.Fatal("passwd home accepted with changed HOME") } } } for _, key := range []string{"CODEX_HOME", "CLAUDE_CONFIG_DIR"} { path := filepath.Join(t.TempDir(), "interactive") t.Setenv(key, path) if !DefaultHomeRules().RefusesPath(path) { t.Fatalf("inherited %s accepted", key) } } } func TestAccountHomeLookupFailClosed(t *testing.T) { for _, u := range []*user.User{nil, {}, {HomeDir: "relative"}, {HomeDir: "/"}} { rules := homeRules(func(string) (*user.User, error) { return u, nil }, func(string) string { return "" }) if !rules.RefusesPath("/owned/fake-home") { t.Fatal("missing passwd home accepted") } } rules := homeRules(func(string) (*user.User, error) { return nil, errors.New("lookup failed") }, func(string) string { return "" }) if !rules.RefusesPath("/owned/fake-home") { t.Fatal("failed passwd lookup accepted") } } func TestAccountRefreshPreflightPreservesLogin(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) path := filepath.Join(l.Binding().Home, "auth.json") writeFake(t, path, `{"token":"rotated"}`) e := l.Preflight(context.Background(), func(context.Context, Binding) (Identity, error) { t.Error("pending refresh probed") return b.Expected, nil }) raw, _ := os.ReadFile(path) if string(raw) != `{"token":"rotated"}` { t.Fatal("pending refresh overwritten") } if !errors.Is(e, Error("provider_refresh_pending")) { t.Fatalf("not refresh pending: %v", e) } } func TestAccountContainmentPreflightLatched(t *testing.T) { for _, kind := range []string{"source", "identity", "runtime"} { t.Run(kind, func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) home := b.Home if kind == "runtime" { home = l.Binding().Home } if kind == "identity" { id := b.Expected id.AccountID = "changed" writeMetadata(t, home, id) } else { if e := os.Rename(home, home+"-old"); e != nil { t.Fatal(e) } if e := os.Symlink(home+"-old", home); e != nil { t.Fatal(e) } } probe := func(context.Context, Binding) (Identity, error) { return b.Expected, nil } if e := l.Preflight(context.Background(), probe); e == nil { t.Fatal("tampered preflight accepted") } if kind == "identity" { writeMetadata(t, home, b.Expected) } else { if e := os.Remove(home); e != nil { t.Fatal(e) } if e := os.Rename(home+"-old", home); e != nil { t.Fatal(e) } } if e := l.Preflight(context.Background(), probe); !errors.Is(e, ErrContainment) { t.Fatalf("preflight containment not latched: %v", e) } }) } } func TestAccountContainmentSourceFile(t *testing.T) { for _, kind := range []string{"symlink", "directory"} { t.Run(kind, func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) path := filepath.Join(b.Home, "auth.json") if e := os.Remove(path); e != nil { t.Fatal(e) } if kind == "symlink" { if e := os.Symlink(filepath.Join(l.Binding().Home, "auth.json"), path); e != nil { t.Fatal(e) } } else { if e := os.Mkdir(path, 0700); e != nil { t.Fatal(e) } } if e := l.Validate(context.Background()); !errors.Is(e, ErrContainment) { t.Errorf("unsafe source not contained: %v", e) } if e := os.Remove(path); e != nil { t.Fatal(e) } writeFake(t, path, `{"token":"fake-one"}`) if e := l.Preflight(context.Background(), func(context.Context, Binding) (Identity, error) { return b.Expected, nil }); !errors.Is(e, ErrContainment) { t.Fatalf("unsafe source containment not latched: %v", e) } }) } } func TestAccountRefreshFailure(t *testing.T) { for _, kind := range []string{"maintenance", "identity"} { t.Run(kind, func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) path := filepath.Join(l.Binding().Home, "auth.json") writeFake(t, path, `{"token":"rotated"}`) e := l.CommitRefresh(context.Background(), func(context.Context, Binding) (Identity, error) { if kind == "maintenance" { return Identity{}, ErrAccountMaintenance } id := b.Expected id.AccountID = "different" return id, nil }) want := ErrAccountMaintenance if kind == "identity" { want = ErrIdentity } if !errors.Is(e, want) { t.Fatalf("refresh failure lost: %v", e) } raw, _ := os.ReadFile(filepath.Join(b.Home, "auth.json")) if string(raw) != `{"token":"fake-one"}` { t.Fatal("failed refresh committed") } writeFake(t, path, `{"token":"fake-one"}`) e = l.Preflight(context.Background(), func(context.Context, Binding) (Identity, error) { return b.Expected, nil }) if kind == "identity" && !errors.Is(e, ErrContainment) { t.Fatalf("wrong refresh identity not latched: %v", e) } }) } } func TestRuntimeHomeRejectsPolicyInjection(t *testing.T) { for _, name := range []string{"config.toml", "AGENTS.md", "CLAUDE.md", "settings.json", "hooks", "sessions/AGENTS.md"} { for _, resume := range []bool{false, true} { t.Run(fmt.Sprint(name, resume), func(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) path := filepath.Join(l.Binding().Home, name) if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } writeFake(t, path, "injected policy") var err error if resume { err = l.ValidatePolicy(context.Background()) } else { err = l.BeginTurn(context.Background()) } if err == nil { t.Fatal("runtime policy injection accepted") } }) } } } func TestRefreshDoesNotOverwriteNewerSource(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) preflightFixture(t, l) writeFake(t, filepath.Join(l.Binding().Home, "auth.json"), `{"token":"refreshed"}`) writeFake(t, filepath.Join(b.Home, "auth.json"), `{"token":"newer-source"}`) if err := l.CommitRefresh(context.Background(), func(context.Context, Binding) (Identity, error) { return b.Expected, nil }); err == nil { t.Fatal("new source overwritten by older refresh") } raw, _ := os.ReadFile(filepath.Join(b.Home, "auth.json")) if !strings.Contains(string(raw), "newer-source") { t.Fatal("newer login lost") } } func TestPreflightFingerprintTracksSource(t *testing.T) { b, r := accountFixture(t) l := openFixture(t, b, r) first, err := l.PreflightFingerprint(context.Background()) if err != nil { t.Fatal(err) } again, err := l.PreflightFingerprint(context.Background()) if err != nil || again != first { t.Fatal("unchanged source invalidated cache", err) } writeFake(t, filepath.Join(b.Home, "auth.json"), `{"token":"different"}`) changed, err := l.PreflightFingerprint(context.Background()) if err != nil || changed == first { t.Fatal("source generation omitted from cache key", err) } }