package gitx import ( "context" "os" "os/exec" "path/filepath" "runtime" "sync" "sync/atomic" "testing" "time" ) // testPubKey is a throwaway ed25519 public key, generated for these tests // only. Any importable key works; nothing here checks a signature by it. const testPubKey = `-----BEGIN PGP PUBLIC KEY BLOCK----- mDMEaugNpRYJKwYBBAHaRw8BAQdALdT0aysVitcgR5XI/83vQs0OikaFIA7iCaz+ jhDcKme0B1QgPHRAZT6IkwQTFgoAOxYhBDOCxCHguLawIR59W19RDrlDNqKgBQJq 6A2lAhsDBQsJCAcCAiICBhUKCQgLAgQWAgMBAh4HAheAAAoJEF9RDrlDNqKg7DsB APBorNFTKhE59bHCAkYvT30Pi+CH1JlOEvJtOAviTV2DAQD6CpRoMvZ6i9638gZZ q9656Bht+aWyGLHAAixEJS6RCg== =wJW3 -----END PGP PUBLIC KEY BLOCK----- ` func writeKeyFile(t *testing.T) string { t.Helper() if _, err := exec.LookPath("gpg"); err != nil { t.Skip("gpg not on PATH") } p := filepath.Join(t.TempDir(), "keys.asc") if err := os.WriteFile(p, []byte(testPubKey), 0o600); err != nil { t.Fatal(err) } return p } // withKeyring wraps Repo.WithKeyring and removes the homedir it created // when the test ends: finalizers don't run at process exit, so a keyring // still reachable then would otherwise be left in /tmp. func withKeyring(t *testing.T, r *Repo, path string) (*Repo, error) { t.Helper() next, err := r.WithKeyring(path) if err == nil { home := next.kr.home t.Cleanup(func() { os.RemoveAll(home) }) } return next, err } // TestKeyringSwapLeavesOriginal guards the immutability contract: requests // copy the published Repo concurrently with the watcher reconfiguring it, // so a keyring change must be a new Repo, never a write to the old one. func TestKeyringSwapLeavesOriginal(t *testing.T) { keys := writeKeyFile(t) r := newTestRepo(t) withKr, err := withKeyring(t, r, keys) if err != nil { t.Fatalf("WithKeyring: %v", err) } if got := r.KeyringPath(); got != "" { t.Fatalf("original after WithKeyring: KeyringPath() = %q, want \"\"", got) } if got := withKr.KeyringPath(); got != keys { t.Fatalf("WithKeyring result: KeyringPath() = %q, want %q", got, keys) } home := withKr.kr.home without := withKr.WithoutKeyring() if without.KeyringPath() != "" { t.Fatal("WithoutKeyring result still has a keyring") } // A request holding withKr must still be able to run gpg against it. if _, err := os.Stat(home); err != nil { t.Fatalf("homedir removed while a Repo still references it: %v", err) } if _, err := withKr.WithContext(context.Background(), 0).Log("main", 1, 0, true); err != nil { t.Fatalf("verifying Log on the superseded Repo: %v", err) } runtime.KeepAlive(withKr) if _, err := r.WithKeyring(filepath.Join(t.TempDir(), "missing.asc")); err == nil { t.Fatal("WithKeyring on a missing file: want error") } } // TestKeyringHomedirReleased: the homedir outlives the swap (above) but // must not leak once nothing can reach it. func TestKeyringHomedirReleased(t *testing.T) { keys := writeKeyFile(t) r := newTestRepo(t) withKr, err := r.WithKeyring(keys) if err != nil { t.Fatalf("WithKeyring: %v", err) } home := withKr.kr.home withKr = nil deadline := time.Now().Add(5 * time.Second) for { runtime.GC() if _, err := os.Stat(home); os.IsNotExist(err) { return } if time.Now().After(deadline) { t.Fatalf("homedir %s still present after its keyring became unreachable", home) } time.Sleep(10 * time.Millisecond) } } // TestKeyringSwapRace mirrors core/watch.go publishing new Repos while // requests copy the current one. Run under -race: the previous in-place // UseKeyring raced on the copy in WithContext. func TestKeyringSwapRace(t *testing.T) { keys := writeKeyFile(t) var cur atomic.Pointer[Repo] cur.Store(newTestRepo(t)) var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() for i := 0; i < 6; i++ { r := cur.Load() if r.KeyringPath() == "" { next, err := withKeyring(t, r, keys) if err != nil { t.Errorf("WithKeyring: %v", err) return } cur.Store(next) } else { cur.Store(r.WithoutKeyring()) } } }() go func() { defer wg.Done() for i := 0; i < 30; i++ { rc := cur.Load().WithContext(context.Background(), 0) if _, err := rc.Log("main", 1, 0, true); err != nil { t.Errorf("Log: %v", err) return } } }() wg.Wait() }