Validate Audita report bounds with constant memory
This commit is contained in:
@@ -8,6 +8,42 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckFileBounded(t *testing.T) {
|
||||
const limit int64 = 4
|
||||
for _, tc := range []struct {
|
||||
name, content string
|
||||
limit int64
|
||||
valid bool
|
||||
}{
|
||||
{"below", "123", limit, true},
|
||||
{"at", "1234", limit, true},
|
||||
{"above", "12345", limit, false},
|
||||
{"empty", "", 0, true},
|
||||
{"zero", "1", 0, false},
|
||||
{"maximum", "1234", math.MaxInt64, true},
|
||||
{"negative", "", -1, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "report")
|
||||
if err := os.WriteFile(path, []byte(tc.content), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := CheckFileBounded(path, tc.limit)
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("check: %v, want valid=%t", err, tc.valid)
|
||||
}
|
||||
if tc.name == "above" && !strings.Contains(err.Error(), "4-byte limit") {
|
||||
t.Fatalf("missing limit: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, path := range []string{" ", t.TempDir(), filepath.Join(t.TempDir(), "missing")} {
|
||||
if err := CheckFileBounded(path, limit); err == nil {
|
||||
t.Fatalf("invalid path accepted: %q", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFileBoundedEnforcesLimitWithoutPartialContent(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "value.txt")
|
||||
|
||||
Reference in New Issue
Block a user