kilo/vendor/github.com/mdlayher/netlink/fuzz.go

82 lines
1.9 KiB
Go
Raw Normal View History

migrate to golang.zx2c4.com/wireguard/wgctrl (#239) * migrate to golang.zx2c4.com/wireguard/wgctrl This commit introduces the usage of wgctrl. It avoids the usage of exec calls of the wg command and parsing the output of `wg show`. Signed-off-by: leonnicolas <leonloechner@gmx.de> * vendor wgctrl Signed-off-by: leonnicolas <leonloechner@gmx.de> * apply suggestions from code review Remove wireguard.Enpoint struct and use net.UDPAddr for the resolved endpoint and addr string (dnsanme:port) if a DN was supplied. Signed-off-by: leonnicolas <leonloechner@gmx.de> * pkg/*: use wireguard.Enpoint This commit introduces the wireguard.Enpoint struct. It encapsulates a DN name with port and a net.UPDAddr. The fields are private and only accessible over exported Methods to avoid accidental modification. Also iptables.GetProtocol is improved to avoid ipv4 rules being applied by `ip6tables`. Signed-off-by: leonnicolas <leonloechner@gmx.de> * pkg/wireguard/conf_test.go: add tests for Endpoint Signed-off-by: leonnicolas <leonloechner@gmx.de> * cmd/kg/main.go: validate port range Signed-off-by: leonnicolas <leonloechner@gmx.de> * add suggestions from review Signed-off-by: leonnicolas <leonloechner@gmx.de> * pkg/mesh/mesh.go: use Equal func Implement an Equal func for Enpoint and use it instead of comparing strings. Signed-off-by: leonnicolas <leonloechner@gmx.de> * cmd/kgctl/main.go: check port range Signed-off-by: leonnicolas <leonloechner@gmx.de> * vendor Signed-off-by: leonnicolas <leonloechner@gmx.de>
2022-01-30 16:38:45 +00:00
//+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
}