summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/fuzz.go
diff options
context:
space:
mode:
authorAnatole Denis <natolumin@unverle.fr>2019-10-04 14:07:04 +0200
committerChris K <c@chrisko.ch>2020-06-20 21:42:12 -0700
commit9a7fb219220c42cea49aec41ad5fdb1fed06b75b (patch)
treeffad5382f68464415f4351bae7b48c44ac0544ca /dhcpv4/fuzz.go
parent4b5a011e0a4c724e2afc8aaeb55edf2f2dbecebe (diff)
dhcpv4: Add go-fuzz endpoint
Signed-off-by: Anatole Denis <natolumin@unverle.fr>
Diffstat (limited to 'dhcpv4/fuzz.go')
-rw-r--r--dhcpv4/fuzz.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/dhcpv4/fuzz.go b/dhcpv4/fuzz.go
new file mode 100644
index 0000000..cf62ba5
--- /dev/null
+++ b/dhcpv4/fuzz.go
@@ -0,0 +1,41 @@
+// +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
+}