Files
moby/daemon/libnetwork/etchosts/fuzz_test.go
Sebastiaan van Stijn c9a4739cd0 daemon/libnetwork/etchosts: use native Go fuzzing
Replace go-fuzz-headers with native fuzz parameters and construct Record
values directly from the fuzz input. This also fuzzes netip.Addr values
instead of the zero value.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2026-07-13 10:57:13 +02:00

50 lines
1.0 KiB
Go

package etchosts
import (
"net/netip"
"os"
"path/filepath"
"testing"
)
func FuzzAdd(f *testing.F) {
f.Fuzz(func(t *testing.T, fileBytes []byte, data []byte, noOfRecords uint8) {
recs := make([]Record, 0)
for range noOfRecords % 40 {
if len(data) == 0 {
break
}
r := Record{}
data = generateRecord(data, &r)
recs = append(recs, r)
}
tmpDir := t.TempDir()
testFile := filepath.Join(tmpDir, "testFile")
err := os.WriteFile(testFile, fileBytes, 0o644)
if err != nil {
t.Fatalf("write test hosts file: %v", err)
}
// Exercise Add with arbitrary file contents and records. Errors are
// expected for some inputs; the fuzz target is only checking that it
// doesn't panic.
_ = Add(testFile, recs)
})
}
func generateRecord(data []byte, r *Record) []byte {
hostLen := int(data[0] % 64)
data = data[1:]
hostLen = min(hostLen, len(data))
r.Hosts = string(data[:hostLen])
data = data[hostLen:]
if len(data) >= 4 {
r.IP = netip.AddrFrom4([4]byte(data[:4]))
data = data[4:]
}
return data
}