File: addr_test.go

package info (click to toggle)
golang-github-jsimonetti-rtnetlink 2.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,284 kB
  • sloc: makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,515 bytes parent folder | download | duplicates (4)
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
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)
			}
		})
	}
}