// Copyright (c) 2026 Petr BalvĂ­n (https://petrbalvin.org) // SPDX-License-Identifier: BSD-3-Clause package verify import ( "testing" ) func TestFuzzResultString(t *testing.T) { t.Run("ok", func(t *testing.T) { r := FuzzResult{Func: "add", Iterations: 100, Matches: 100} s := r.String() if s != "add: 100/100 iterations match" { t.Errorf("String() = %q", s) } }) t.Run("mismatch", func(t *testing.T) { r := FuzzResult{Func: "mul", Iterations: 100, Matches: 95, Mismatches: 5, FirstFail: "iter 23"} s := r.String() if s != "mul: 95/100 match, 5 MISMATCH: iter 23" { t.Errorf("String() = %q", s) } }) t.Run("crash", func(t *testing.T) { r := FuzzResult{Func: "dec", Iterations: 100, Matches: 99, Mismatches: 1, FirstFail: "SIGSEGV", CrashInput: []byte{0x01, 0x02}} s := r.String() if s != "dec: 99/100 match, 1 MISMATCH: SIGSEGV\n input: 0102" { t.Errorf("String() = %q", s) } }) } func TestArrayLen(t *testing.T) { tests := []struct { typ string want int }{ {"*[32]uint16", 32}, {"*[16]int32", 16}, {"bad", 1}, {"*[]", 1}, {"*[0x]", 1}, } for _, tt := range tests { if got := arrayLen(tt.typ); got != tt.want { t.Errorf("arrayLen(%q) = %d, want %d", tt.typ, got, tt.want) } } } func TestElemSizeFor(t *testing.T) { tests := []struct { typ string want int }{ {"[]byte", 1}, {"[]uint8", 1}, {"[]int8", 1}, {"[]uint16", 2}, {"[]int16", 2}, {"[]uint32", 4}, {"[]int32", 4}, {"[]float32", 4}, {"[]uint64", 8}, {"[]int64", 8}, {"[]float64", 8}, {"[]unknown", 8}, } for _, tt := range tests { if got := elemSizeFor(tt.typ); got != tt.want { t.Errorf("elemSizeFor(%q) = %d, want %d", tt.typ, got, tt.want) } } } func TestEqualBytes(t *testing.T) { if !equalBytes([]byte{1, 2, 3}, []byte{1, 2, 3}) { t.Error("expected equal") } if equalBytes([]byte{1, 2}, []byte{1, 2, 3}) { t.Error("different length: expected not equal") } if equalBytes([]byte{1, 2, 3}, []byte{1, 2, 4}) { t.Error("different content: expected not equal") } } func TestParamsSize(t *testing.T) { sig := funcSig{ name: "test", params: []param{{name: "a", typ: "[]byte"}, {name: "b", typ: "int"}}, results: []param{{name: "n", typ: "int"}}, } if got := paramsSize(sig); got != 32 { t.Errorf("paramsSize = %d, want 32 (24 for slice + 8 for int)", got) } // ABI0 strings are ptr+len (16), complex64 packs two float32s (8), // complex128 two float64s (16). sig = funcSig{ name: "hdr", params: []param{{name: "s", typ: "string"}, {name: "c64", typ: "complex64"}, {name: "c128", typ: "complex128"}}, } if got := paramsSize(sig); got != 40 { t.Errorf("paramsSize = %d, want 40 (16 string + 8 complex64 + 16 complex128)", got) } } func TestBlockCount(t *testing.T) { k := loadBasic(t) n, err := k.BlockCount("sum") if err != nil { t.Fatalf("BlockCount(sum): %v", err) } if n < 2 { t.Errorf("sum: expected at least 2 blocks, got %d", n) } } // TestNewRNGDeterministic pins the reproducibility contract of the fuzz // seeds: the same seed must rebuild the same PCG stream, including the // sub-word tail of fillRandom. func TestNewRNGDeterministic(t *testing.T) { draw := func() []byte { rng := newRNG(7) out := make([]byte, 20) // 8+8+4: exercises a full word and a tail fillRandom(rng, out) return out } a, b := draw(), draw() for i := range a { if a[i] != b[i] { t.Fatalf("seed 7 produced different bytes at %d: %02x vs %02x", i, a[i], b[i]) } } if newRNG(0) == nil { t.Fatal("newRNG(0) must build a generator") } } func TestFillBuffer(t *testing.T) { t.Run("zero", func(t *testing.T) { // fillBuffer("zero") is a no-op; relies on make already zeroing. buf := make([]byte, 16) if err := fillBuffer(buf, "zero"); err != nil { t.Fatalf("fillBuffer(zero): %v", err) } for _, b := range buf { if b != 0 { t.Error("zero pattern: make should produce zeroed buffer") break } } }) t.Run("ones", func(t *testing.T) { buf := make([]byte, 16) if err := fillBuffer(buf, "ones"); err != nil { t.Fatalf("fillBuffer(ones): %v", err) } for _, b := range buf { if b != 0xFF { t.Error("ones pattern should fill with 0xFF") break } } }) t.Run("seq", func(t *testing.T) { buf := make([]byte, 256) if err := fillBuffer(buf, "seq"); err != nil { t.Fatalf("fillBuffer(seq): %v", err) } for i, b := range buf { if b != byte(i) { t.Errorf("seq[%d] = %d, want %d", i, b, i) break } } }) t.Run("hex", func(t *testing.T) { buf := make([]byte, 6) if err := fillBuffer(buf, "deadbeef"); err != nil { t.Fatalf("fillBuffer(deadbeef): %v", err) } want := []byte{0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD} for i, b := range buf { if b != want[i] { t.Errorf("hex[%d] = %02x, want %02x", i, b, want[i]) break } } }) t.Run("unknown-pattern", func(t *testing.T) { buf := make([]byte, 8) if err := fillBuffer(buf, "wibble"); err == nil { t.Error("fillBuffer(wibble): expected an error for an unknown pattern name") } }) t.Run("bad-hex", func(t *testing.T) { buf := make([]byte, 8) if err := fillBuffer(buf, "zz"); err == nil { t.Error("fillBuffer(zz): expected an error for undecodable hex") } }) t.Run("empty-hex", func(t *testing.T) { buf := make([]byte, 8) if err := fillBuffer(buf, ""); err == nil { t.Error("fillBuffer(\"\"): expected an error for an empty pattern") } }) } // TestBufPoolAllocBadPattern checks that Alloc surfaces fill errors under // the buffer's name, so hand-built specs fail as loudly as parsed ones. func TestBufPoolAllocBadPattern(t *testing.T) { var pool BufPool defer pool.Close() if err := pool.Alloc([]BufSpec{{Name: "dst", Size: 16, Pattern: "nope"}}); err == nil { t.Fatal("Alloc with an unknown pattern must fail") } } func TestFuzzFuncChecked(t *testing.T) { k := loadBasic(t) sig := funcSig{ name: "sum", params: []param{{name: "data", typ: "[]int64"}}, results: []param{{name: "r", typ: "int64"}}, } result := k.FuzzFuncChecked("sum", sig, 10, 0) if !result.OK() { t.Errorf("FuzzFuncChecked(sum): %s", result) } // Test with non-existent function; should report failure. result = k.FuzzFuncChecked("nope", sig, 10, 0) if result.OK() { t.Error("FuzzFuncChecked(nope): expected failure") } }