rtnetlink/rtnl/addr_test.go
Julian Kornberger bde56ed163
Cleanup (#94)
* Clean up code and remove code duplication
* Apply gofmt

Signed-off-by: Julian Kornberger <jk+github@digineo.de>
2020-12-16 14:43:43 +01:00

62 lines
1.5 KiB
Go

package rtnl
import (
"net"
"testing"
)
func TestParseAddrs(t *testing.T) {
tests := []struct {
name string
ipstr string
ipnet net.IPNet
err string
}{
{
name: "ipv6 subnet address",
ipstr: "ff00::/64",
err: "address ff00::: attempted to parse a subnet address into a host address",
},
{
name: "ipv4 subnet address",
ipstr: "10.0.0.0/8",
err: "address 10.0.0.0: attempted to parse a subnet address into a host address",
},
{
name: "ipv6 host address",
ipstr: "ff00::1/64",
ipnet: net.IPNet{
IP: net.IP{0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
Mask: net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
},
},
{
name: "ipv4 host address",
ipstr: "10.0.0.1/8",
ipnet: net.IPNet{
IP: net.IP{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xa, 0x0, 0x0, 0x1},
Mask: net.IPMask{0xff, 0x0, 0x0, 0x0},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := ParseAddr(tt.ipstr)
if err != nil {
if want, got := tt.err, err.Error(); want != got {
t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got)
}
return
}
if tt.err != "" {
t.Fatalf("expected error:\n %s\nbut got nothing.. :(", tt.err)
}
if want, got := tt.ipnet, res; !want.IP.Equal(got.IP) {
t.Fatalf("unexpected IP:\n- want: %+#v\n- got: %+#v", want, got)
}
})
}
}