| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "sync/atomic" |
| 9 | "syscall" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/mac/proc" |
| 14 | ) |
| 15 | |
| 16 | // Follow-up fix round 2 (Astra 9f round 2). Synthetic tables, rows made |
| 17 | // up 2026-11-29 in the darwin table's shape (see sweep_test.go). |
| 18 | |
| 19 | // #1 (new): an assessment whose lock fails proves nothing, though the |
| 20 | // discharge after it would succeed. |
| 21 | func TestAssessFailsClosedOnLockErrors(t *testing.T) { |
| 22 | tb := parseTable(t, turnBegan) |
| 23 | turns, x, _, before := clearRig(t, tb, sameBoot, nil) |
| 24 | o := owed(before) |
| 25 | o.UnsandboxedRan = true |
| 26 | if err := turns.debts.Owe(o); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | calls := 0 |
| 30 | turns.debts.LockHook = func(int) error { |
| 31 | // Verify asks whether it owes, then assesses, then discharges. |
| 32 | if calls++; calls == 2 { |
| 33 | return errors.New("lock failed") |
| 34 | } |
| 35 | return nil |
| 36 | } |
| 37 | if x.Verify(RecordID(0, "attempt-c")) || !turns.debts.Owes("attempt-c") { |
| 38 | t.Fatal("an assessment that could not lock proved an unsandboxed turn over") |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // #2: an idle read clears a turn only if every unresolved turn is |
| 43 | // sandbox-only and has everything it names gone for every user: an |
| 44 | // unsandboxed turn A, still unresolved, keeps sandboxed B from clearing |
| 45 | // while A's uid-switched survivor lives; so does A's recorded group held |
| 46 | // by another user. Once A is resolved, B clears. |
| 47 | func TestIdleRefusedWhileAnotherTurnIsUnresolved(t *testing.T) { |
| 48 | for name, c := range map[string]struct { |
| 49 | edit func(o *proc.Obligation) |
| 50 | holding ProcInfo |
| 51 | }{ |
| 52 | "A unsandboxed": {func(o *proc.Obligation) { o.UnsandboxedRan = true }, row(99601, 1, 99601, 99601, 5000, 0, "")}, |
| 53 | "A's flags unknown": {func(o *proc.Obligation) { o.FlagsKnown = false }, row(99601, 1, 99601, 99601, 5000, 0, "")}, |
| 54 | "A's group still held": {func(o *proc.Obligation) { o.Groups = proc.Span{99602: 4000} }, row(99603, 1, 99602, 99603, 5000, 501, "")}, |
| 55 | } { |
| 56 | tb := parseTable(t, turnBegan) |
| 57 | turns, x, _, before := clearRig(t, tb, sameBoot, nil) |
| 58 | tb.add(c.holding, "/usr/bin/login") |
| 59 | a := owed(before) |
| 60 | a.Markers = []string{"attempt-a"} |
| 61 | c.edit(&a) |
| 62 | b := owed(before) |
| 63 | b.Markers, b.Began = []string{"attempt-b"}, turnStart.Add(time.Hour) |
| 64 | for _, o := range []proc.Obligation{a, b} { |
| 65 | if err := turns.debts.Owe(o); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | } |
| 69 | if x.Verify(RecordID(0, "attempt-b")) { |
| 70 | t.Fatalf("%s: B cleared while A is unresolved", name) |
| 71 | } |
| 72 | if err := turns.debts.Discharge("attempt-a", time.Time{}); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if !x.Verify(RecordID(0, "attempt-b")) { |
| 76 | t.Fatalf("%s: B not cleared once A was resolved", name) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // #4: the pending cleanup is under the one deadline: a simulator |
| 82 | // shutdown slower than it makes the proof fail, and keeps the debt and |
| 83 | // the cleanup; a later Verify finishes both. |
| 84 | func TestPendingCleanupUnderTheDeadline(t *testing.T) { |
| 85 | tb := parseTable(t, turnBegan) |
| 86 | turns, x, _, before := clearRig(t, tb, sameBoot, nil) |
| 87 | if err := turns.debts.Owe(owed(before)); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | _, temp := userTempFixture(t) |
| 91 | // The cleanup has its own bound (whole-branch fix 2, #1). |
| 92 | x.UserTemp, x.CleanLimit = temp, x.settleLimit() |
| 93 | s, err := openTemp(temp, nil) |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | if err := turns.writePending(pendingRecord{Attempt: "attempt-c", Before: s.before, Runner: ProcID{PID: 1 << 30, Start: 1}, Began: turnStart, BootID: "boot-1"}); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | s.close() |
| 101 | left := filepath.Join(temp, "left") |
| 102 | if err := os.WriteFile(left, nil, 0600); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | var slow atomic.Bool |
| 106 | slow.Store(true) |
| 107 | x.QuietSimulators = func(ctx context.Context) { |
| 108 | if slow.Load() { |
| 109 | select { |
| 110 | case <-ctx.Done(): |
| 111 | case <-time.After(3 * time.Second): |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | start := time.Now() |
| 116 | if x.Verify(RecordID(0, "attempt-c")) { |
| 117 | t.Fatal("proven with its cleanup past the deadline") |
| 118 | } |
| 119 | if took := time.Since(start); took > 2*time.Second { |
| 120 | t.Fatalf("the slow cleanup held Verify %s, past its deadline", took) |
| 121 | } |
| 122 | if !turns.debts.Owes("attempt-c") || !turns.Pending() { |
| 123 | t.Fatal("the debt or the cleanup went with the proof expired") |
| 124 | } |
| 125 | slow.Store(false) |
| 126 | if !x.Verify(RecordID(0, "attempt-c")) || turns.Pending() || exists(left) || turns.debts.Owes("attempt-c") { |
| 127 | t.Fatal("a later Verify did not finish the cleanup and the proof") |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // #4: the discharge is under the deadline too: a lock that comes too |
| 132 | // late keeps the debt, and the proof fails. |
| 133 | func TestDischargeUnderTheDeadline(t *testing.T) { |
| 134 | tb := parseTable(t, turnBegan) |
| 135 | turns, x, _, before := clearRig(t, tb, sameBoot, nil) |
| 136 | if err := turns.debts.Owe(owed(before)); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | calls := 0 |
| 140 | turns.debts.LockHook = func(int) error { |
| 141 | if calls++; calls == 3 { |
| 142 | // The discharge's lock: held by another runner process. |
| 143 | time.Sleep(400 * time.Millisecond) |
| 144 | } |
| 145 | return nil |
| 146 | } |
| 147 | if x.Verify(RecordID(0, "attempt-c")) { |
| 148 | t.Fatal("proven though its discharge came after the deadline") |
| 149 | } |
| 150 | turns.debts.LockHook = nil |
| 151 | if !turns.debts.Owes("attempt-c") { |
| 152 | t.Fatal("the debt went with the proof expired") |
| 153 | } |
| 154 | |
| 155 | // The same with the lock really held: another open file description |
| 156 | // takes the flock just before the discharge asks for it, and lets go |
| 157 | // only after the deadline. |
| 158 | calls = 0 |
| 159 | turns.debts.LockHook = func(int) error { |
| 160 | if calls++; calls != 3 { |
| 161 | return nil |
| 162 | } |
| 163 | f, err := os.OpenFile(turns.debts.Path+".lock", os.O_CREATE|os.O_RDWR, 0600) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil { |
| 168 | f.Close() |
| 169 | return err |
| 170 | } |
| 171 | time.AfterFunc(time.Second, func() { f.Close() }) |
| 172 | return nil |
| 173 | } |
| 174 | start := time.Now() |
| 175 | if x.Verify(RecordID(0, "attempt-c")) { |
| 176 | t.Fatal("proven though another process held the discharge's lock past the deadline") |
| 177 | } |
| 178 | if took := time.Since(start); took > 900*time.Millisecond { |
| 179 | t.Fatalf("a held lock kept Verify waiting %s, past its deadline", took) |
| 180 | } |
| 181 | turns.debts.LockHook = nil |
| 182 | time.Sleep(1100 * time.Millisecond) |
| 183 | if !turns.debts.Owes("attempt-c") { |
| 184 | t.Fatal("the debt went with the lock held") |
| 185 | } |
| 186 | if !x.Verify(RecordID(0, "attempt-c")) || turns.debts.Owes("attempt-c") { |
| 187 | t.Fatal("a later Verify did not carry on once the lock was free") |
| 188 | } |
| 189 | } |
| 190 | |