File: fuzz.go

package info (click to toggle)
golang-github-mdlayher-netlink 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 368 kB
  • sloc: makefile: 5
file content (82 lines) | stat: -rw-r--r-- 1,962 bytes parent folder | download | duplicates (2)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//go:build gofuzz
// +build gofuzz

package netlink

import "github.com/google/go-cmp/cmp"

func fuzz(b1 []byte) int {
	// 1. unmarshal, marshal, unmarshal again to check m1 and m2 for equality
	// after a round trip. checkMessage is also used because there is a fair
	// amount of tricky logic around testing for presence of error headers and
	// extended acknowledgement attributes.
	var m1 Message
	if err := m1.UnmarshalBinary(b1); err != nil {
		return 0
	}

	if err := checkMessage(m1); err != nil {
		return 0
	}

	b2, err := m1.MarshalBinary()
	if err != nil {
		panicf("failed to marshal m1: %v", err)
	}

	var m2 Message
	if err := m2.UnmarshalBinary(b2); err != nil {
		panicf("failed to unmarshal m2: %v", err)
	}

	if err := checkMessage(m2); err != nil {
		panicf("failed to check m2: %v", err)
	}

	if diff := cmp.Diff(m1, m2); diff != "" {
		panicf("unexpected Message (-want +got):\n%s", diff)
	}

	// 2. marshal again and compare b2 and b3 (b1 may have reserved bytes set
	// which we ignore and fill with zeros when marshaling) for equality.
	b3, err := m2.MarshalBinary()
	if err != nil {
		panicf("failed to marshal m2: %v", err)
	}

	if diff := cmp.Diff(b2, b3); diff != "" {
		panicf("unexpected message bytes (-want +got):\n%s", diff)
	}

	// 3. unmarshal any possible attributes from m1's data and marshal them
	// again for comparison.
	a1, err := UnmarshalAttributes(m1.Data)
	if err != nil {
		return 0
	}

	ab1, err := MarshalAttributes(a1)
	if err != nil {
		panicf("failed to marshal a1: %v", err)
	}

	a2, err := UnmarshalAttributes(ab1)
	if err != nil {
		panicf("failed to unmarshal a2: %v", err)
	}

	if diff := cmp.Diff(a1, a2); diff != "" {
		panicf("unexpected Attributes (-want +got):\n%s", diff)
	}

	ab2, err := MarshalAttributes(a2)
	if err != nil {
		panicf("failed to marshal a2: %v", err)
	}

	if diff := cmp.Diff(ab1, ab2); diff != "" {
		panicf("unexpected attribute bytes (-want +got):\n%s", diff)
	}

	return 1
}