summaryrefslogtreecommitdiffhomepage
path: root/dhcpv6/fuzz.go
blob: 3f5afef9fe256a45bcc3fa8c577c99111c0fde8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// +build gofuzz

package dhcpv6

import (
	"bytes"
	"fmt"
)

// Fuzz is an entrypoint for go-fuzz (github.com/dvyukov/go-fuzz)
func Fuzz(data []byte) int {
	msg, err := FromBytes(data)
	if err != nil {
		return 0
	}

	serialized := msg.ToBytes()
	if !bytes.Equal(data, serialized) {
		rtMsg, err := FromBytes(serialized)
		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("round-trip reserialized: ", rtMsg.Summary())
		fmt.Printf("Go repr: %#v\n", rtMsg)
		if err != nil {
			fmt.Printf("failed to parse after deserialize-serialize: %v\n", err)
		}
		panic("round-trip different")
	}

	return 1
}