Review round 1 on PR #165: - ReadRegularMax checked the size on the opened handle, then read with an unbounded io.ReadAll, so a file growing after the check returned past the limit. The read now goes through io.LimitReader(max+1). A test seam grows the file between the check and the read and counts bytes consumed. - New: ReadScoped returns ErrTooLarge for a note over maxNoteBytes; red if readContained drops the limit. - notes.Target carries the brain root instead of deriving it by TrimSuffix. - Rule 9: reader tests renamed to <Reader>RefusesSymlinksAndSurvivesSwapRaces; ReadNote test has a positive control; brainfstest comment states what the harness observes and what it does not. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
41 lines
1.4 KiB
Go
41 lines
1.4 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 TestReadScopedRefusesSymlinksAndSurvivesSwapRaces(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() })
|
|
}
|
|
|
|
func TestReadScopedRefusesANoteOverMaxNoteBytes(t *testing.T) {
|
|
brain := t.TempDir()
|
|
facts := filepath.Join(brain, "wiki", "w", "facts")
|
|
require.NoError(t, os.MkdirAll(facts, 0o755))
|
|
require.NoError(t, os.WriteFile(filepath.Join(facts, "big.md"), make([]byte, maxNoteBytes+1), 0o644))
|
|
_, err := ReadScoped(brain, "wiki/w/facts/big.md", ScopeNotes)
|
|
assert.ErrorIs(t, err, ErrTooLarge)
|
|
}
|