diff options
author | Arjun Singh <ajsinghyadav00@gmail.com> | 2023-07-19 17:18:25 +0530 |
---|---|---|
committer | Arjun Singh <ajsinghyadav00@gmail.com> | 2023-07-20 14:24:16 +0530 |
commit | d2077e2392548e0fdf2432013e6b00426fbad5f7 (patch) | |
tree | 3bbcda3642c565e7c50501b88621708de0f62789 /dhcpv4 | |
parent | b20c9ba983dfec6371555e7baa343aabd388b22c (diff) |
[Fuzzing] remove deprecated go-fuzz and use Native Go fuzz
Signed-off-by: Arjun Singh <ajsinghyadav00@gmail.com>
Diffstat (limited to 'dhcpv4')
-rw-r--r-- | dhcpv4/dhcpv4_test.go | 44 | ||||
-rw-r--r-- | dhcpv4/fuzz.go | 41 |
2 files changed, 44 insertions, 41 deletions
diff --git a/dhcpv4/dhcpv4_test.go b/dhcpv4/dhcpv4_test.go index a961f78..95523ab 100644 --- a/dhcpv4/dhcpv4_test.go +++ b/dhcpv4/dhcpv4_test.go @@ -412,3 +412,47 @@ func Test_withIP(t *testing.T) { b := buff.Buffer require.Equal(t, b.Len(), 4, "Testing no of bytes written by writeIP func") } + +func FuzzDHCPv4(f *testing.F) { + + data_0 := []byte{ + 1, // dhcp request + 1, // ethernet hw type + 6, // hw addr length + 3, // hop count + 0xaa, 0xbb, 0xcc, 0xdd, // transaction ID, big endian (network) + 0, 3, // number of seconds + 0, 1, // broadcast + 0, 0, 0, 0, // client IP address + 0, 0, 0, 0, // your IP address + 0, 0, 0, 0, // server IP address + 0, 0, 0, 0, // gateway IP address + 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // client MAC address + padding + } + + data_1 := []byte{ + 1, // dhcp request + 1, // ethernet hw type + 6, // hw addr length + 0, // hop count + 0xaa, 0xbb, 0xcc, 0xdd, // transaction ID + 3, 0, // number of seconds + 1, 0, // broadcast + 0, 0, 0, 0, // client IP address + 0, 0, 0, 0, // your IP address + 0, 0, 0, 0, // server IP address + 0, 0, 0, 0, // gateway IP address + 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // client MAC address + padding + } + + f.Add(data_0) + f.Add(data_1) + + f.Fuzz(func(t *testing.T, data []byte) { + msg, err := FromBytes(data) + if err != nil { + return + } + msg.ToBytes() + }) +} diff --git a/dhcpv4/fuzz.go b/dhcpv4/fuzz.go deleted file mode 100644 index cf62ba5..0000000 --- a/dhcpv4/fuzz.go +++ /dev/null @@ -1,41 +0,0 @@ -// +build gofuzz - -package dhcpv4 - -import ( - "fmt" - "reflect" -) - -// Fuzz is the entrypoint for go-fuzz -func Fuzz(data []byte) int { - msg, err := FromBytes(data) - if err != nil { - return 0 - } - - serialized := msg.ToBytes() - - // Compared to dhcpv6, dhcpv4 has padding and fixed-size fields containing - // variable-length data; We can't expect the library to output byte-for-byte - // identical packets after a round-trip. - // Instead, we check that after a round-trip, the packet reserializes to the - // same internal representation - rtMsg, err := FromBytes(serialized) - - if err != nil || !reflect.DeepEqual(msg, rtMsg) { - fmt.Printf("Input: %x\n", data) - fmt.Printf("Round-trip: %x\n", serialized) - fmt.Println("Message: ", msg.Summary()) - fmt.Printf("Go repr: %#v\n", msg) - fmt.Println("Reserialized: ", rtMsg.Summary()) - fmt.Printf("Go repr: %#v\n", rtMsg) - if err != nil { - fmt.Printf("Got error while reserializing: %v\n", err) - panic("round-trip error: " + err.Error()) - } - panic("round-trip different: " + msg.Summary()) - } - - return 1 -} |