-
Notifications
You must be signed in to change notification settings - Fork 10
/
ip_test.go
166 lines (152 loc) · 2.8 KB
/
ip_test.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"net/netip"
"testing"
)
func Test_IsRoutable(t *testing.T) {
ipTests := map[string]struct {
ip string
expectedRoutable bool
}{
// IP v4
"ip4 public": {
"8.8.8.8",
true,
},
"ip4 unspecified": {
"0.0.0.0",
false,
},
"ip4 loopback": {
"127.0.0.1",
false,
},
// RFC1918
"ip4 inside RFC1918 (10)": {
"10.0.0.2",
false,
},
"ip4 inside RFC1918 (172)": {
"172.16.0.2",
false,
},
"ip4 outside RFC1918 (172)": {
"172.32.0.1",
true,
},
"ip4 inside RFC1918 (192)": {
"192.168.1.2",
false,
},
"ip4 outside RFC1918 (192)": {
"192.169.0.1",
true,
},
// IPv6
"ip6 public": {
"2001:4860:4860::8888",
true,
},
"ip6 unspecified": {
"::",
false,
},
"ip6 loopback": {
"::1",
false,
},
// RFC3964
"ip6 start RFC3964": {
"2002:0000:0000:0000:0000:0000:0000:0000",
false,
},
"ip6 end RFC3964": {
"2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
false,
},
// RFC4380
"ip6 start RFC4380": {
"2001:0000:0000:0000:0000:0000:0000:0000",
false,
},
"ip6 end RFC4380": {
"2001:0000:ffff:ffff:ffff:ffff:ffff:ffff",
false,
},
"ip6 outside RFC4380": {
"2001:0001:0000:0000:0000:0000:0000:0000",
true,
},
// RFC4843
"ip6 start RFC4843": {
"2001:0010:0000:0000:0000:0000:0000:0000",
false,
},
"ip6 end RFC4843": {
"2001:001f:ffff:ffff:ffff:ffff:ffff:ffff",
false,
},
"ip6 outside RFC4843": {
"2001:0020:0000:0000:0000:0000:0000:0000",
true,
},
// RFC4862
"ip6 start RFC4862": {
"fe80:0000:0000:0000:0000:0000:0000:0000",
false,
},
"ip6 end RFC4862": {
"fe80:0000:0000:0000:ffff:ffff:ffff:ffff",
false,
},
"ip6 outside RFC4862": {
"fe80:0000:0000:0001:0000:0000:0000:0000",
true,
},
// RFC4193
"ip6 start RFC4193": {
"fc00:0000:0000:0000:0000:0000:0000:0000",
false,
},
"ip6 end RFC4193": {
"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
false,
},
"ip6 outside RFC4193": {
"fe00:0000:0000:0000:0000:0000:0000:0000",
true,
},
// RFC6598
"ip4 start RFC6598": {
"100.64.0.0",
false,
},
"ip4 end RFC6598": {
"100.127.255.255",
false,
},
"ip4 outside start RFC6598": {
"100.63.255.255",
true,
},
"ip4 outside end RFC6598": {
"100.128.0.0",
true,
},
}
for testName, test := range ipTests {
addr, err := netip.ParseAddr(test.ip)
if err != nil {
t.Fatalf("%s: failed to parse %v: %v",
testName, test.ip, err)
}
actualRoutable := isRoutable(addr)
if actualRoutable != test.expectedRoutable {
t.Fatalf("%s: expected routable==%t for IP %s",
testName, test.expectedRoutable, test.ip)
}
}
}