Option 1 of #157, slice 2. readExcerpt (brain_context), api.ReadNote (brain_get / read-after-write) and notes.ReadScoped (brain_read) read the Syncthing-shared brain by path. Under a symlink swap race each leaked an outside file hundreds of thousands of times in 1.5s, including brain_read, whose Lstat check was raced by the reopen-by-name; a FIFO blocked os.ReadFile. - All three read through brainfs (os.Root, regular files only, non-blocking open, type check on the handle). - brainfs.ReadRegularMax checks a size limit on the opened handle; notes keeps its ErrTooLarge. - New internal/brainfs/brainfstest: the shared swap-race harness (symlink and held-writer FIFO swappers, awaited before TempDir cleanup; AssertNoLeak / AssertNoHang) for this and later slices. Tests red first: each reader under a symlink-swap race and a held-writer FIFO race, plus static checks. Flipped: readExcerpt back to os.ReadFile is red. Refs #157 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
32 lines
1.0 KiB
Go
32 lines
1.0 KiB
Go
package notes
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/mathiasbq/hyperguild/ingestion/internal/brainfs/brainfstest"
|
|
)
|
|
|
|
// hyperguild#157 slice 2: ReadScoped (brain_read) Lstat-checked the final
|
|
// component, then reopened by name with os.ReadFile: a swap after the check
|
|
// was followed, and a FIFO blocked the read. It now reads through brainfs.
|
|
func TestReadScopedIsConfinedToRegularFilesInTheBrain(t *testing.T) {
|
|
brain := t.TempDir()
|
|
facts := filepath.Join(brain, "wiki", "w", "facts")
|
|
require.NoError(t, os.MkdirAll(facts, 0o755))
|
|
p := filepath.Join(facts, "r.md")
|
|
read := func() string {
|
|
b, _ := ReadScoped(brain, "wiki/w/facts/r.md", ScopeNotes)
|
|
return string(b)
|
|
}
|
|
require.NoError(t, os.WriteFile(p, []byte("inside"), 0o644))
|
|
assert.Equal(t, "inside", read())
|
|
brainfstest.AssertNoLeak(t, p, "inside", 1500*time.Millisecond, read)
|
|
brainfstest.AssertNoHang(t, p, "inside", 1500*time.Millisecond, 6*time.Second, func() { _ = read() })
|
|
}
|