| 1 | //go:build testbuild |
| 2 | |
| 3 | package thin |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/fakeclaude" |
| 12 | ) |
| 13 | |
| 14 | // Whole-branch fix 1, #1: a credential watch clears only by the |
| 15 | // Executor's proof (Verify), which a reboot satisfies only boot- |
| 16 | // qualified: another kern.bootsessionuuid and a later boot time. A clock |
| 17 | // stepped forward moves kern.boottime past the watch's start with the |
| 18 | // same boot identity: the watch, the hold and the closed connections |
| 19 | // stay. |
| 20 | func TestDarwinWatchHeldWhenOnlyTheClockMoves(t *testing.T) { |
| 21 | r := newDarwinRig(t) |
| 22 | local, _ := connected(t, r, fakeclaude.Token("a@example.com")) |
| 23 | if err := os.WriteFile(filepath.Join(r.dir, "hang"), nil, 0o600); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | helper := exec.Command(os.Args[0], "-test.run=^TestDarwinTokenCheckCrashHelper$", "-test.count=1") |
| 27 | helper.Env = append(os.Environ(), "EQUIPO_DARWIN_CRASH_DIR="+r.dir, "EQUIPO_DARWIN_CRASH_LOCAL="+local) |
| 28 | if err := helper.Start(); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | pid := waitPid(t, filepath.Join(r.dir, "hang.pid")) |
| 32 | _ = helper.Process.Kill() |
| 33 | _ = helper.Wait() |
| 34 | // What its CLI stored meanwhile. |
| 35 | r.creating("-p ", r.own) |
| 36 | r.kc.mu.Lock() |
| 37 | delete(r.kc.created, pid) |
| 38 | r.kc.mu.Unlock() |
| 39 | r.connector() |
| 40 | if !r.m.Held() || len(unprovenOf(r.m)) != 1 { |
| 41 | t.Fatalf("after the crash: held=%v unproven=%v", r.m.Held(), unprovenOf(r.m)) |
| 42 | } |
| 43 | r.clockForward(t) |
| 44 | r.connector() |
| 45 | r.m.settle(r.ctx, true) |
| 46 | if !r.m.Held() || len(unprovenOf(r.m)) != 1 || len(r.kc.deleted) != 0 { |
| 47 | t.Fatalf("the clock moved forward, no reboot: held=%v unproven=%v deleted %+v", r.m.Held(), unprovenOf(r.m), r.kc.deleted) |
| 48 | } |
| 49 | b, _ := r.binding(local) |
| 50 | if _, err := r.opts.CheckToken(r.ctx, b); err == nil { |
| 51 | t.Fatal("a check ran with the watch held") |
| 52 | } |
| 53 | r.rebooted(t, pid) |
| 54 | r.connector() |
| 55 | if r.m.Held() || len(r.kc.deleted) != 1 { |
| 56 | t.Fatalf("after a reboot: held=%v deleted %+v", r.m.Held(), r.kc.deleted) |
| 57 | } |
| 58 | } |
| 59 | |