Adds the corruption-proofing layer for the edit/persist path: - chunked_buffer_fuzz_test.go: differential fuzz of ChunkedBuffer Insert/Delete against a plain []byte shadow model (arbitrary byte positions/text, chunk sizes 1..64KB, 500-2000 ops each) verifying FileLen, FullContent, Content probes and the chunk-size invariant after every op; rune-aligned variant adds UTF-8 validity and an independent RuneIndexToByte oracle. - line_index_fuzz_test.go: differential fuzz of the incremental LineIndex updates against a full-recomputation oracle (mixed, no-newline, single-line, CRLF, trailing-newline shapes), plus a trailing-empty-line structural invariant test. - state_api_fuzz_test.go: differential fuzz of the production edit entry points (HandleInsert/Backspace/Delete/ReplaceRange, incl. selection variants) checking content, UTF-8 validity, chunk invariant, line index and exact cursor after every op. - real_file_fuzz_test.go (e2e): random edit sequences (incl. window-relative IME replaces) against the REAL filesystem with per-batch IME-window-consistency checks, forced-flush disk byte-comparison, then a second logic instance (restart simulation) must reload byte-identical content with a fresh line index; asserts no stray temp files. - filesystem_test.go (real): atomicity contract - exact round trip at edge sizes, concurrent reader never sees a torn file across 150 alternating 2MB writes, failed write (read-only dir) leaves the original byte-identical, stale temp file is consumed. Fixes a real invariant violation the fuzzing exposed: Insert halved an oversized spliced result once, so a large paste into a non-empty buffer left chunks up to ~P/2 (8x target at 1MB/64KB), breaking the documented 'no chunk > 2x target' invariant. Insert now re-chunks the oversized result into pieces of at most chunkSize, making the invariant hold after every edit. Mutation-tested: dropping one byte in Insert and one entry in UpdateLineIndexAfterInsert are both caught by the fuzz suite.
199 lines
5.6 KiB
Go
199 lines
5.6 KiB
Go
package real
|
|
|
|
// Data-integrity tests for the persistence layer's atomicity contract. The
|
|
// editor's corruption protection against crashes rests on WriteFileAtomic's
|
|
// write-sibling-temp-then-rename scheme; these tests pin the observable
|
|
// consequences of that contract:
|
|
//
|
|
// - a written file is byte-exact (no truncation/padding at any size),
|
|
// - a concurrent reader NEVER observes a torn file: every read returns
|
|
// exactly one of the known complete payloads (the "crash/kill mid-write
|
|
// leaves a complete snapshot" property, without needing a real crash),
|
|
// - a FAILED write leaves the original file byte-identical,
|
|
// - a stale leftover temp file is consumed by the next successful write.
|
|
|
|
import (
|
|
"bytes"
|
|
"math/rand"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
func randomPayload(t *testing.T, seed int64, size int) []byte {
|
|
t.Helper()
|
|
rng := rand.New(rand.NewSource(seed))
|
|
b := make([]byte, size)
|
|
rng.Read(b)
|
|
return b
|
|
}
|
|
|
|
func TestWriteFileAtomic_RoundTripExact(t *testing.T) {
|
|
for _, size := range []int{0, 1, 2, 65537, 5 * 1024 * 1024} {
|
|
d := t.TempDir()
|
|
fs := NewRealFileSystem(d)
|
|
data := randomPayload(t, int64(size), size)
|
|
if err := fs.WriteFileAtomic("/rt.txt", data); err != nil {
|
|
t.Fatalf("size %d: write: %v", size, err)
|
|
}
|
|
got, err := fs.ReadFile("/rt.txt")
|
|
if err != nil {
|
|
t.Fatalf("size %d: read: %v", size, err)
|
|
}
|
|
if !bytes.Equal(got, data) {
|
|
t.Fatalf("size %d: round trip mismatch (got %d bytes)", size, len(got))
|
|
}
|
|
// Overwrite with different content of the same size.
|
|
data2 := randomPayload(t, int64(size)+1, size)
|
|
if err := fs.WriteFileAtomic("/rt.txt", data2); err != nil {
|
|
t.Fatalf("size %d: overwrite: %v", size, err)
|
|
}
|
|
got, err = fs.ReadFile("/rt.txt")
|
|
if err != nil {
|
|
t.Fatalf("size %d: read after overwrite: %v", size, err)
|
|
}
|
|
if !bytes.Equal(got, data2) {
|
|
t.Fatalf("size %d: overwrite mismatch", size)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestWriteFileAtomic_ConcurrentReader_NeverTorn hammers the file with two
|
|
// alternating 2 MB payloads while a reader spins; every single read must
|
|
// return exactly one of the two payloads in full. Any torn read (a mix, a
|
|
// truncation, a padding) is a data-corruption failure.
|
|
func TestWriteFileAtomic_ConcurrentReader_NeverTorn(t *testing.T) {
|
|
d := t.TempDir()
|
|
fs := NewRealFileSystem(d)
|
|
const size = 2 * 1024 * 1024
|
|
payloadA := randomPayload(t, 1, size)
|
|
payloadB := randomPayload(t, 2, size)
|
|
|
|
if err := fs.WriteFileAtomic("/torn.txt", payloadA); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
stop := make(chan struct{})
|
|
var (
|
|
wg sync.WaitGroup
|
|
reads atomic.Int64
|
|
torn atomic.Int64
|
|
tornDetail string // written only by the reader, read after wg.Wait()
|
|
)
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for {
|
|
select {
|
|
case <-stop:
|
|
return
|
|
default:
|
|
}
|
|
data, err := fs.ReadFile("/torn.txt")
|
|
if err != nil {
|
|
continue // transient (rename boundary); next read decides
|
|
}
|
|
reads.Add(1)
|
|
if !bytes.Equal(data, payloadA) && !bytes.Equal(data, payloadB) {
|
|
torn.Store(1)
|
|
at := 0
|
|
ref := payloadA
|
|
if len(data) < len(ref) {
|
|
ref = data
|
|
}
|
|
for at < len(data) && at < len(ref) && data[at] == ref[at] {
|
|
at++
|
|
}
|
|
tornDetail = "first diff at " + itoa(at) + " (read " + itoa(len(data)) + " bytes, want " + itoa(size) + ")"
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
const iters = 150
|
|
for i := 0; i < iters; i++ {
|
|
payload := payloadA
|
|
if i%2 == 1 {
|
|
payload = payloadB
|
|
}
|
|
if err := fs.WriteFileAtomic("/torn.txt", payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
close(stop)
|
|
wg.Wait()
|
|
|
|
if torn.Load() == 1 {
|
|
t.Fatalf("torn read observed: %s", tornDetail) // safe: reader exited (wg.Wait)
|
|
}
|
|
if n := reads.Load(); n < 20 {
|
|
t.Fatalf("only %d concurrent reads happened; test not meaningful", n)
|
|
}
|
|
}
|
|
|
|
// TestWriteFileAtomic_FailedWriteLeavesOriginalIntact verifies the crash
|
|
// property directly: when the write cannot complete (read-only directory),
|
|
// the original file is untouched, byte for byte.
|
|
func TestWriteFileAtomic_FailedWriteLeavesOriginalIntact(t *testing.T) {
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("running as root: permission bits are bypassed")
|
|
}
|
|
d := t.TempDir()
|
|
fs := NewRealFileSystem(d)
|
|
original := []byte("original content that must survive\n")
|
|
if err := fs.WriteFileAtomic("/f.txt", original); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(d, 0o555); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = os.Chmod(d, 0o755) }()
|
|
|
|
if err := fs.WriteFileAtomic("/f.txt", []byte("new content")); err == nil {
|
|
t.Fatal("expected write to fail in read-only directory")
|
|
}
|
|
got, err := fs.ReadFile("/f.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(got, original) {
|
|
t.Fatalf("failed write modified the original file: got %q", got)
|
|
}
|
|
}
|
|
|
|
// TestWriteFileAtomic_StaleTempFileIsConsumed verifies a crash-leftover temp
|
|
// file (garbage from an interrupted write) does not poison the next
|
|
// successful write and does not survive it.
|
|
func TestWriteFileAtomic_StaleTempFileIsConsumed(t *testing.T) {
|
|
d := t.TempDir()
|
|
fs := NewRealFileSystem(d)
|
|
if err := os.WriteFile(filepath.Join(d, ".f.txt.tmp"), []byte("stale garbage from a crashed write"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fresh := []byte("fresh content")
|
|
if err := fs.WriteFileAtomic("/f.txt", fresh); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := fs.ReadFile("/f.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(got, fresh) {
|
|
t.Fatalf("target file != fresh content: %q", got)
|
|
}
|
|
entries, err := os.ReadDir(d)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, e := range entries {
|
|
if e.Name() == ".f.txt.tmp" {
|
|
t.Fatal("stale temp file was not consumed by the successful write")
|
|
}
|
|
}
|
|
}
|
|
|
|
func itoa(n int) string { return strconv.Itoa(n) }
|