package brainfstest import ( "fmt" "os" "path/filepath" "runtime" "syscall" "testing" "time" ) // recordingTB stands in for the test under AssertNoLeakFrom so a fatal // there can be observed instead of failing this test. Fatalf ends the // calling goroutine the way testing.T does. type recordingTB struct { testing.TB failed bool msg string } func (r *recordingTB) Fatalf(format string, args ...any) { r.failed, r.msg = true, fmt.Sprintf(format, args...) runtime.Goexit() } func runAssert(t *testing.T, path, target string, read func() string) *recordingTB { t.Helper() r := &recordingTB{TB: t} done := make(chan struct{}) go func() { defer close(done) AssertNoLeakFrom(r, path, "inside", target, 200*time.Millisecond, read) }() <-done return r } // hyperguild#177: with every swap failing, no leak can occur, so the leak // assertion passed without testing anything. func TestAssertNoLeakFromFailsWhenNoSymlinkSwapSucceeds(t *testing.T) { path := filepath.Join(t.TempDir(), "missing-dir", "r.md") r := runAssert(t, path, Outside(t), func() string { return "inside" }) if !r.failed { t.Fatal("AssertNoLeakFrom passed with zero successful symlink swaps") } } // hyperguild#177: a reader that always errors returns nothing, so it never // leaks either. func TestAssertNoLeakFromFailsWhenNoReadReturnsTheInsideNote(t *testing.T) { path := filepath.Join(t.TempDir(), "r.md") r := runAssert(t, path, Outside(t), func() string { return "" }) if !r.failed { t.Fatal("AssertNoLeakFrom passed with zero reads returning the inside note") } } // Positive control: a reader that refuses symlinks and reads regular files // passes, so the floors do not fail a working reader. func TestAssertNoLeakFromPassesAReaderThatRefusesSymlinks(t *testing.T) { path := filepath.Join(t.TempDir(), "r.md") read := func() string { f, err := os.OpenFile(path, os.O_RDONLY|noFollow, 0) if err != nil { return "" } defer func() { _ = f.Close() }() b := make([]byte, 64) n, _ := f.Read(b) return string(b[:n]) } r := runAssert(t, path, Outside(t), read) if r.failed { t.Fatalf("a symlink-refusing reader failed: %s", r.msg) } } const noFollow = syscall.O_NOFOLLOW