mirror of
https://github.com/moby/moby.git
synced 2026-08-03 06:30:59 +00:00
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>
50 lines
1.0 KiB
Go
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
|
|
}
|