149 lines
4.6 KiB
Go
149 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// readDirEntriesBounded is the bounded replacement for the old unbounded
|
|
// name-sorted ReadDir in the listing routes (the D1 sync-loss restoration).
|
|
// These tests pin the contract its callers rely on: entries come back in
|
|
// name order, never more than keep of them, and truncated is true exactly
|
|
// when the directory held more members than were returned.
|
|
func TestReadDirEntriesBounded(t *testing.T) {
|
|
seed := func(t *testing.T, names ...string) *os.File {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
for _, n := range names {
|
|
if err := os.WriteFile(filepath.Join(dir, n), []byte("x"), 0o644); err != nil {
|
|
t.Fatalf("seed %s: %v", n, err)
|
|
}
|
|
}
|
|
f, err := os.Open(dir)
|
|
if err != nil {
|
|
t.Fatalf("open dir: %v", err)
|
|
}
|
|
t.Cleanup(func() { f.Close() })
|
|
return f
|
|
}
|
|
|
|
sorted12 := []string{"f01", "f02", "f03", "f04", "f05", "f06", "f07", "f08", "f09", "f10", "f11", "f12"}
|
|
// Seeded in a non-dirent-friendly order so a raw (unsorted) read cannot
|
|
// pass by accident.
|
|
shuffled := []string{"f10", "f03", "f01", "f12", "f05", "f08", "f02", "f11", "f04", "f09", "f07", "f06"}
|
|
|
|
t.Run("sorted prefix with truncation flag", func(t *testing.T) {
|
|
f := seed(t, shuffled...)
|
|
ents, truncated, err := readDirEntriesBounded(f, 10)
|
|
if err != nil {
|
|
t.Fatalf("readDirEntriesBounded: %v", err)
|
|
}
|
|
if !truncated {
|
|
t.Fatalf("directory holds more entries than keep: truncated must be true")
|
|
}
|
|
if len(ents) != 10 {
|
|
t.Fatalf("got %d entries, want exactly keep=10", len(ents))
|
|
}
|
|
for i, ent := range ents {
|
|
if ent.Name() != sorted12[i] {
|
|
t.Fatalf("entry[%d] = %s, want %s (name-sorted prefix)", i, ent.Name(), sorted12[i])
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("under cap keeps everything untruncated", func(t *testing.T) {
|
|
f := seed(t, shuffled...)
|
|
ents, truncated, err := readDirEntriesBounded(f, len(shuffled))
|
|
if err != nil {
|
|
t.Fatalf("readDirEntriesBounded: %v", err)
|
|
}
|
|
if truncated {
|
|
t.Fatalf("directory fits the cap: truncated must be false")
|
|
}
|
|
if len(ents) != len(sorted12) {
|
|
t.Fatalf("got %d entries, want %d", len(ents), len(sorted12))
|
|
}
|
|
for i, ent := range ents {
|
|
if ent.Name() != sorted12[i] {
|
|
t.Fatalf("entry[%d] = %s, want %s", i, ent.Name(), sorted12[i])
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("sorted prefix across batches", func(t *testing.T) {
|
|
names := make([]string, 0, maxDirListEntries+2)
|
|
for start := range 2 {
|
|
for index := start; index < maxDirListEntries+2; index += 2 {
|
|
names = append(names, fmt.Sprintf("file-%05d", index))
|
|
}
|
|
}
|
|
dir := seed(t, names...)
|
|
entries, truncated, err := readDirEntriesBounded(dir, maxDirListEntries)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !truncated || len(entries) != maxDirListEntries {
|
|
t.Fatalf("got %d entries and truncated=%v, want %d entries with truncation", len(entries), truncated, maxDirListEntries)
|
|
}
|
|
for index, entry := range entries {
|
|
if want := fmt.Sprintf("file-%05d", index); entry.Name() != want {
|
|
t.Fatalf("entry[%d] = %s, want %s", index, entry.Name(), want)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("empty directory", func(t *testing.T) {
|
|
f := seed(t)
|
|
ents, truncated, err := readDirEntriesBounded(f, 10)
|
|
if err != nil {
|
|
t.Fatalf("readDirEntriesBounded: %v", err)
|
|
}
|
|
if truncated || len(ents) != 0 {
|
|
t.Fatalf("empty dir must yield no entries and no truncation, got %d entries truncated=%v", len(ents), truncated)
|
|
}
|
|
})
|
|
|
|
t.Run("keep below one is clamped", func(t *testing.T) {
|
|
f := seed(t, shuffled...)
|
|
ents, truncated, err := readDirEntriesBounded(f, 0)
|
|
if err != nil {
|
|
t.Fatalf("readDirEntriesBounded: %v", err)
|
|
}
|
|
if len(ents) != 1 || !truncated {
|
|
t.Fatalf("keep<=0 must clamp to 1 entry with truncation, got %d entries truncated=%v", len(ents), truncated)
|
|
}
|
|
if ents[0].Name() != "f01" {
|
|
t.Fatalf("clamped keep must still return the name-sorted first entry, got %s", ents[0].Name())
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestReadDirEntriesBoundedSkipsNonUTF8Names(t *testing.T) {
|
|
if runtime.GOOS != "linux" {
|
|
t.Skip("requires Linux byte-preserving filenames")
|
|
}
|
|
dir := t.TempDir()
|
|
valid := "file-\uFFFD.txt"
|
|
invalid := "file-\xff.txt"
|
|
for _, name := range []string{valid, invalid} {
|
|
if err := os.WriteFile(filepath.Join(dir, name), nil, 0o644); err != nil {
|
|
t.Fatalf("seed %q: %v", name, err)
|
|
}
|
|
}
|
|
dirFile, err := os.Open(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer dirFile.Close()
|
|
entries, truncated, err := readDirEntriesBounded(dirFile, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if truncated || len(entries) != 1 || entries[0].Name() != valid {
|
|
t.Fatalf("listing should keep exactly the valid filename without truncation, got %v (truncated=%v)", entries, truncated)
|
|
}
|
|
}
|