package mac import ( "context" "errors" "os" "path/filepath" "sync/atomic" "syscall" "testing" "time" "amadan.net/oficina/equipo/internal/runner/mac/proc" ) // Follow-up fix round 2 (Astra 9f round 2). Synthetic tables, rows made // up 2026-11-29 in the darwin table's shape (see sweep_test.go). // #1 (new): an assessment whose lock fails proves nothing, though the // discharge after it would succeed. func TestAssessFailsClosedOnLockErrors(t *testing.T) { tb := parseTable(t, turnBegan) turns, x, _, before := clearRig(t, tb, sameBoot, nil) o := owed(before) o.UnsandboxedRan = true if err := turns.debts.Owe(o); err != nil { t.Fatal(err) } calls := 0 turns.debts.LockHook = func(int) error { // Verify asks whether it owes, then assesses, then discharges. if calls++; calls == 2 { return errors.New("lock failed") } return nil } if x.Verify(RecordID(0, "attempt-c")) || !turns.debts.Owes("attempt-c") { t.Fatal("an assessment that could not lock proved an unsandboxed turn over") } } // #2: an idle read clears a turn only if every unresolved turn is // sandbox-only and has everything it names gone for every user: an // unsandboxed turn A, still unresolved, keeps sandboxed B from clearing // while A's uid-switched survivor lives; so does A's recorded group held // by another user. Once A is resolved, B clears. func TestIdleRefusedWhileAnotherTurnIsUnresolved(t *testing.T) { for name, c := range map[string]struct { edit func(o *proc.Obligation) holding ProcInfo }{ "A unsandboxed": {func(o *proc.Obligation) { o.UnsandboxedRan = true }, row(99601, 1, 99601, 99601, 5000, 0, "")}, "A's flags unknown": {func(o *proc.Obligation) { o.FlagsKnown = false }, row(99601, 1, 99601, 99601, 5000, 0, "")}, "A's group still held": {func(o *proc.Obligation) { o.Groups = proc.Span{99602: 4000} }, row(99603, 1, 99602, 99603, 5000, 501, "")}, } { tb := parseTable(t, turnBegan) turns, x, _, before := clearRig(t, tb, sameBoot, nil) tb.add(c.holding, "/usr/bin/login") a := owed(before) a.Markers = []string{"attempt-a"} c.edit(&a) b := owed(before) b.Markers, b.Began = []string{"attempt-b"}, turnStart.Add(time.Hour) for _, o := range []proc.Obligation{a, b} { if err := turns.debts.Owe(o); err != nil { t.Fatal(err) } } if x.Verify(RecordID(0, "attempt-b")) { t.Fatalf("%s: B cleared while A is unresolved", name) } if err := turns.debts.Discharge("attempt-a", time.Time{}); err != nil { t.Fatal(err) } if !x.Verify(RecordID(0, "attempt-b")) { t.Fatalf("%s: B not cleared once A was resolved", name) } } } // #4: the pending cleanup is under the one deadline: a simulator // shutdown slower than it makes the proof fail, and keeps the debt and // the cleanup; a later Verify finishes both. func TestPendingCleanupUnderTheDeadline(t *testing.T) { tb := parseTable(t, turnBegan) turns, x, _, before := clearRig(t, tb, sameBoot, nil) if err := turns.debts.Owe(owed(before)); err != nil { t.Fatal(err) } _, temp := userTempFixture(t) // The cleanup has its own bound (whole-branch fix 2, #1). x.UserTemp, x.CleanLimit = temp, x.settleLimit() s, err := openTemp(temp, nil) if err != nil { t.Fatal(err) } 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 { t.Fatal(err) } s.close() left := filepath.Join(temp, "left") if err := os.WriteFile(left, nil, 0600); err != nil { t.Fatal(err) } var slow atomic.Bool slow.Store(true) x.QuietSimulators = func(ctx context.Context) { if slow.Load() { select { case <-ctx.Done(): case <-time.After(3 * time.Second): } } } start := time.Now() if x.Verify(RecordID(0, "attempt-c")) { t.Fatal("proven with its cleanup past the deadline") } if took := time.Since(start); took > 2*time.Second { t.Fatalf("the slow cleanup held Verify %s, past its deadline", took) } if !turns.debts.Owes("attempt-c") || !turns.Pending() { t.Fatal("the debt or the cleanup went with the proof expired") } slow.Store(false) if !x.Verify(RecordID(0, "attempt-c")) || turns.Pending() || exists(left) || turns.debts.Owes("attempt-c") { t.Fatal("a later Verify did not finish the cleanup and the proof") } } // #4: the discharge is under the deadline too: a lock that comes too // late keeps the debt, and the proof fails. func TestDischargeUnderTheDeadline(t *testing.T) { tb := parseTable(t, turnBegan) turns, x, _, before := clearRig(t, tb, sameBoot, nil) if err := turns.debts.Owe(owed(before)); err != nil { t.Fatal(err) } calls := 0 turns.debts.LockHook = func(int) error { if calls++; calls == 3 { // The discharge's lock: held by another runner process. time.Sleep(400 * time.Millisecond) } return nil } if x.Verify(RecordID(0, "attempt-c")) { t.Fatal("proven though its discharge came after the deadline") } turns.debts.LockHook = nil if !turns.debts.Owes("attempt-c") { t.Fatal("the debt went with the proof expired") } // The same with the lock really held: another open file description // takes the flock just before the discharge asks for it, and lets go // only after the deadline. calls = 0 turns.debts.LockHook = func(int) error { if calls++; calls != 3 { return nil } f, err := os.OpenFile(turns.debts.Path+".lock", os.O_CREATE|os.O_RDWR, 0600) if err != nil { return err } if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil { f.Close() return err } time.AfterFunc(time.Second, func() { f.Close() }) return nil } start := time.Now() if x.Verify(RecordID(0, "attempt-c")) { t.Fatal("proven though another process held the discharge's lock past the deadline") } if took := time.Since(start); took > 900*time.Millisecond { t.Fatalf("a held lock kept Verify waiting %s, past its deadline", took) } turns.debts.LockHook = nil time.Sleep(1100 * time.Millisecond) if !turns.debts.Owes("attempt-c") { t.Fatal("the debt went with the lock held") } if !x.Verify(RecordID(0, "attempt-c")) || turns.debts.Owes("attempt-c") { t.Fatal("a later Verify did not carry on once the lock was free") } }