Migration
This is my new public git source viewer. Source available. Please contact with concerns/bugs.
Contact me
internal/gitx/keyring_test.go
321286b403e696f79ab1feab51660a63bb3ee72c
· 4.1 KB · 153 lines
raw
| 1 | package gitx |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "sync" |
| 10 | "sync/atomic" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | // testPubKey is a throwaway ed25519 public key, generated for these tests |
| 16 | // only. Any importable key works; nothing here checks a signature by it. |
| 17 | const testPubKey = `-----BEGIN PGP PUBLIC KEY BLOCK----- |
| 18 | |
| 19 | mDMEaugNpRYJKwYBBAHaRw8BAQdALdT0aysVitcgR5XI/83vQs0OikaFIA7iCaz+ |
| 20 | jhDcKme0B1QgPHRAZT6IkwQTFgoAOxYhBDOCxCHguLawIR59W19RDrlDNqKgBQJq |
| 21 | 6A2lAhsDBQsJCAcCAiICBhUKCQgLAgQWAgMBAh4HAheAAAoJEF9RDrlDNqKg7DsB |
| 22 | APBorNFTKhE59bHCAkYvT30Pi+CH1JlOEvJtOAviTV2DAQD6CpRoMvZ6i9638gZZ |
| 23 | q9656Bht+aWyGLHAAixEJS6RCg== |
| 24 | =wJW3 |
| 25 | -----END PGP PUBLIC KEY BLOCK----- |
| 26 | ` |
| 27 | |
| 28 | func writeKeyFile(t *testing.T) string { |
| 29 | t.Helper() |
| 30 | if _, err := exec.LookPath("gpg"); err != nil { |
| 31 | t.Skip("gpg not on PATH") |
| 32 | } |
| 33 | p := filepath.Join(t.TempDir(), "keys.asc") |
| 34 | if err := os.WriteFile(p, []byte(testPubKey), 0o600); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | return p |
| 38 | } |
| 39 | |
| 40 | // withKeyring wraps Repo.WithKeyring and removes the homedir it created |
| 41 | // when the test ends: finalizers don't run at process exit, so a keyring |
| 42 | // still reachable then would otherwise be left in /tmp. |
| 43 | func withKeyring(t *testing.T, r *Repo, path string) (*Repo, error) { |
| 44 | t.Helper() |
| 45 | next, err := r.WithKeyring(path) |
| 46 | if err == nil { |
| 47 | home := next.kr.home |
| 48 | t.Cleanup(func() { os.RemoveAll(home) }) |
| 49 | } |
| 50 | return next, err |
| 51 | } |
| 52 | |
| 53 | // TestKeyringSwapLeavesOriginal guards the immutability contract: requests |
| 54 | // copy the published Repo concurrently with the watcher reconfiguring it, |
| 55 | // so a keyring change must be a new Repo, never a write to the old one. |
| 56 | func TestKeyringSwapLeavesOriginal(t *testing.T) { |
| 57 | keys := writeKeyFile(t) |
| 58 | r := newTestRepo(t) |
| 59 | |
| 60 | withKr, err := withKeyring(t, r, keys) |
| 61 | if err != nil { |
| 62 | t.Fatalf("WithKeyring: %v", err) |
| 63 | } |
| 64 | if got := r.KeyringPath(); got != "" { |
| 65 | t.Fatalf("original after WithKeyring: KeyringPath() = %q, want \"\"", got) |
| 66 | } |
| 67 | if got := withKr.KeyringPath(); got != keys { |
| 68 | t.Fatalf("WithKeyring result: KeyringPath() = %q, want %q", got, keys) |
| 69 | } |
| 70 | |
| 71 | home := withKr.kr.home |
| 72 | without := withKr.WithoutKeyring() |
| 73 | if without.KeyringPath() != "" { |
| 74 | t.Fatal("WithoutKeyring result still has a keyring") |
| 75 | } |
| 76 | // A request holding withKr must still be able to run gpg against it. |
| 77 | if _, err := os.Stat(home); err != nil { |
| 78 | t.Fatalf("homedir removed while a Repo still references it: %v", err) |
| 79 | } |
| 80 | if _, err := withKr.WithContext(context.Background(), 0).Log("main", 1, 0, true); err != nil { |
| 81 | t.Fatalf("verifying Log on the superseded Repo: %v", err) |
| 82 | } |
| 83 | runtime.KeepAlive(withKr) |
| 84 | |
| 85 | if _, err := r.WithKeyring(filepath.Join(t.TempDir(), "missing.asc")); err == nil { |
| 86 | t.Fatal("WithKeyring on a missing file: want error") |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // TestKeyringHomedirReleased: the homedir outlives the swap (above) but |
| 91 | // must not leak once nothing can reach it. |
| 92 | func TestKeyringHomedirReleased(t *testing.T) { |
| 93 | keys := writeKeyFile(t) |
| 94 | r := newTestRepo(t) |
| 95 | |
| 96 | withKr, err := r.WithKeyring(keys) |
| 97 | if err != nil { |
| 98 | t.Fatalf("WithKeyring: %v", err) |
| 99 | } |
| 100 | home := withKr.kr.home |
| 101 | withKr = nil |
| 102 | |
| 103 | deadline := time.Now().Add(5 * time.Second) |
| 104 | for { |
| 105 | runtime.GC() |
| 106 | if _, err := os.Stat(home); os.IsNotExist(err) { |
| 107 | return |
| 108 | } |
| 109 | if time.Now().After(deadline) { |
| 110 | t.Fatalf("homedir %s still present after its keyring became unreachable", home) |
| 111 | } |
| 112 | time.Sleep(10 * time.Millisecond) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // TestKeyringSwapRace mirrors core/watch.go publishing new Repos while |
| 117 | // requests copy the current one. Run under -race: the previous in-place |
| 118 | // UseKeyring raced on the copy in WithContext. |
| 119 | func TestKeyringSwapRace(t *testing.T) { |
| 120 | keys := writeKeyFile(t) |
| 121 | var cur atomic.Pointer[Repo] |
| 122 | cur.Store(newTestRepo(t)) |
| 123 | |
| 124 | var wg sync.WaitGroup |
| 125 | wg.Add(2) |
| 126 | go func() { |
| 127 | defer wg.Done() |
| 128 | for i := 0; i < 6; i++ { |
| 129 | r := cur.Load() |
| 130 | if r.KeyringPath() == "" { |
| 131 | next, err := withKeyring(t, r, keys) |
| 132 | if err != nil { |
| 133 | t.Errorf("WithKeyring: %v", err) |
| 134 | return |
| 135 | } |
| 136 | cur.Store(next) |
| 137 | } else { |
| 138 | cur.Store(r.WithoutKeyring()) |
| 139 | } |
| 140 | } |
| 141 | }() |
| 142 | go func() { |
| 143 | defer wg.Done() |
| 144 | for i := 0; i < 30; i++ { |
| 145 | rc := cur.Load().WithContext(context.Background(), 0) |
| 146 | if _, err := rc.Log("main", 1, 0, true); err != nil { |
| 147 | t.Errorf("Log: %v", err) |
| 148 | return |
| 149 | } |
| 150 | } |
| 151 | }() |
| 152 | wg.Wait() |
| 153 | } |