github.com/kubeshark/ebpf@v0.9.2/btf/fuzz_test.go (about) 1 //go:build go1.18 2 // +build go1.18 3 4 package btf 5 6 import ( 7 "bytes" 8 "encoding/binary" 9 "fmt" 10 "io" 11 "testing" 12 13 "github.com/kubeshark/ebpf/internal" 14 ) 15 16 func FuzzSpec(f *testing.F) { 17 var buf bytes.Buffer 18 err := binary.Write(&buf, internal.NativeEndian, &btfHeader{ 19 Magic: btfMagic, 20 Version: 1, 21 HdrLen: uint32(binary.Size(btfHeader{})), 22 }) 23 if err != nil { 24 f.Fatal(err) 25 } 26 f.Add(buf.Bytes()) 27 f.Fuzz(func(t *testing.T, data []byte) { 28 if len(data) < binary.Size(btfHeader{}) { 29 t.Skip("data is too short") 30 } 31 32 spec, err := loadRawSpec(bytes.NewReader(data), internal.NativeEndian, nil, nil) 33 if err != nil { 34 if spec != nil { 35 t.Fatal("spec is not nil") 36 } 37 return 38 } 39 40 if spec == nil { 41 t.Fatal("spec is nil") 42 } 43 44 for _, typ := range spec.types { 45 fmt.Fprintf(io.Discard, "%+10v", typ) 46 } 47 }) 48 } 49 50 func FuzzExtInfo(f *testing.F) { 51 var buf bytes.Buffer 52 err := binary.Write(&buf, internal.NativeEndian, &btfExtHeader{ 53 Magic: btfMagic, 54 Version: 1, 55 HdrLen: uint32(binary.Size(btfExtHeader{})), 56 }) 57 if err != nil { 58 f.Fatal(err) 59 } 60 f.Add(buf.Bytes(), []byte("\x00foo\x00barfoo\x00")) 61 62 f.Fuzz(func(t *testing.T, data, strings []byte) { 63 if len(data) < binary.Size(btfExtHeader{}) { 64 t.Skip("data is too short") 65 } 66 67 table, err := readStringTable(bytes.NewReader(strings), nil) 68 if err != nil { 69 t.Skip("invalid string table") 70 } 71 72 info, err := loadExtInfos(bytes.NewReader(data), internal.NativeEndian, nil, table) 73 if err != nil { 74 if info != nil { 75 t.Fatal("info is not nil") 76 } 77 } else if info == nil { 78 t.Fatal("info is nil") 79 } 80 }) 81 }