package proc import ( "os" "os/exec" "path/filepath" "sync" "testing" "time" "amadan.net/rastrillo/rastrillo/budget" ) func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } // Only a path the system alone can write counts for a relaunch: under // /System (not its volumes: /System/Volumes/Data is every user's data), // /usr/libexec or /usr/sbin, exactly, with no climbing out. func TestSystemPath(t *testing.T) { for p, want := range map[string]bool{ "/System/Library/PrivateFrameworks/X.framework/XPCServices/Y.xpc/Contents/MacOS/Y": true, "/usr/libexec/trustd": true, "/System/Volumes/Data/Users/paulequipo/x": false, "/System/Volumes/Preboot/Cryptexes/OS/x": false, "/System/Library/../../Users/paulequipo/x": false, "/usr/libexec/./x": false, "/usr/libexec/..": false, "/system/Library/x": false, "/usr/sbin/cfprefsd": true, "/Users/paulequipo/.local/lib/equipo-runner/x": false, "/System": false, "": false, } { if SystemPath(p) != want { t.Errorf("%q: %v", p, !want) } } } // #4: helpers start concurrently (capability queries beside a gate's // caffeinate): every registration lands, with no race (run with -race). func TestOwnConcurrentStarts(t *testing.T) { o := &Own{} var wg sync.WaitGroup var mu sync.Mutex var cmds []*exec.Cmd for range 16 { wg.Add(1) go func() { defer wg.Done() c := exec.Command("/bin/sleep", "1") if err := o.Start(c, Platform); err != nil { t.Error(err) return } mu.Lock() cmds = append(cmds, c) mu.Unlock() }() } wg.Add(1) go func() { defer wg.Done(); _, _, _ = o.Snapshot(time.Now().Add(5*time.Second), Platform, nil) }() wg.Wait() for _, c := range cmds { q, ok := Platform.Lookup(c.Process.Pid) if !ok || !o.Owns(q.ID) { t.Errorf("helper %d not on record", c.Process.Pid) } _ = c.Process.Kill() _ = c.Wait() } } // #13: every read-modify-write of the debts file is serialized across // processes: another instance's obligation written in the gap of a // Discharge is never lost. func TestDebtsSerializedAcrossInstances(t *testing.T) { p := filepath.Join(t.TempDir(), "owed") a, b := &Debts{Path: p}, &Debts{Path: p} if err := a.Owe(Obligation{Markers: []string{"attempt-a"}}); err != nil { t.Fatal(err) } done := make(chan error, 1) a.between = func() { go func() { done <- b.Owe(Obligation{Markers: []string{"attempt-b"}}) }() time.Sleep(200 * time.Millisecond) } if err := a.Discharge("attempt-a", time.Time{}); err != nil { t.Fatal(err) } a.between = nil if err := <-done; err != nil { t.Fatal(err) } if !b.Owes("attempt-b") || a.Owes("attempt-a") { t.Fatal("another instance's obligation was lost, or the discharged one stayed") } } // Fix round 2, #4: a lock that comes after the deadline runs nothing, // and a discharge whose work outlasts it removes nothing. func TestDebtsUnderTheDeadline(t *testing.T) { d := &Debts{Path: filepath.Join(t.TempDir(), "owed")} if err := d.Owe(Obligation{Markers: []string{"attempt-a"}}); err != nil { t.Fatal(err) } d.LockHook = func(int) error { time.Sleep(150 * time.Millisecond); return nil } ran := false if err := d.lockedBy(time.Now().Add(50*time.Millisecond), func() error { ran = true; return nil }); err == nil || ran { t.Fatalf("a lock taken after its deadline ran its work (err %v)", err) } d.LockHook = nil d.between = func() { time.Sleep(150 * time.Millisecond) } if err := d.Discharge("attempt-a", time.Now().Add(50*time.Millisecond)); err == nil { t.Fatal("a discharge that outlasted its deadline succeeded") } d.between = nil if !d.Owes("attempt-a") { t.Fatal("a late discharge removed the obligation") } } // Round 3, #3: the deadline covers the save itself (the write, its sync // and rename). A discharge whose save ends late fails and leaves the // debt; a reboot's completion saved late fails, and stays as the // evidence a later proof finishes from. func TestDebtsSaveUnderTheDeadline(t *testing.T) { dir := t.TempDir() slow := false delay := func() { if slow { slow = false time.Sleep(150 * time.Millisecond) } } d := &Debts{Path: filepath.Join(dir, "owed"), saved: delay} if err := d.Owe(Obligation{Markers: []string{"attempt-a"}}); err != nil { t.Fatal(err) } slow = true if err := d.Discharge("attempt-a", time.Now().Add(50*time.Millisecond)); err == nil { t.Fatal("a discharge whose save ended after its deadline succeeded") } if !d.Owes("attempt-a") { t.Fatal("a late discharge erased the debt") } c := &Debts{Path: filepath.Join(dir, "owed-c"), saved: delay} if err := c.Owe(Obligation{Markers: []string{"attempt-b"}, BootID: "boot-1"}); err != nil { t.Fatal(err) } slow = true if err := c.Complete("attempt-b", "boot-2", time.Now().Add(50*time.Millisecond)); err == nil { t.Fatal("a completion saved after its deadline succeeded") } all, err := c.load() if err != nil || len(all) != 1 || all[0].ProvenIn != "boot-2" { t.Fatalf("a late completion was not kept for the next proof: %+v %v", all, err) } }