1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 //go:build darwin || dragonfly || freebsd || hurd || linux || netbsd || openbsd || solaris
14 func TestSortByRFC6724(t
*testing
.T
) {
19 reverse
bool // also test it starting backwards
21 // Examples from RFC 6724 section 10.2:
23 // Prefer matching scope.
26 {IP
: ParseIP("2001:db8:1::1")},
27 {IP
: ParseIP("198.51.100.121")},
30 ParseIP("2001:db8:1::2"),
31 ParseIP("169.254.13.78"),
34 {IP
: ParseIP("2001:db8:1::1")},
35 {IP
: ParseIP("198.51.100.121")},
40 // Prefer matching scope.
43 {IP
: ParseIP("2001:db8:1::1")},
44 {IP
: ParseIP("198.51.100.121")},
48 ParseIP("198.51.100.117"),
51 {IP
: ParseIP("198.51.100.121")},
52 {IP
: ParseIP("2001:db8:1::1")},
57 // Prefer higher precedence.
60 {IP
: ParseIP("2001:db8:1::1")},
61 {IP
: ParseIP("10.1.2.3")},
64 ParseIP("2001:db8:1::2"),
68 {IP
: ParseIP("2001:db8:1::1")},
69 {IP
: ParseIP("10.1.2.3")},
74 // Prefer smaller scope.
77 {IP
: ParseIP("2001:db8:1::1")},
78 {IP
: ParseIP("fe80::1")},
81 ParseIP("2001:db8:1::2"),
85 {IP
: ParseIP("fe80::1")},
86 {IP
: ParseIP("2001:db8:1::1")},
91 // Issue 13283. Having a 10/8 source address does not
92 // mean we should prefer 23/8 destination addresses.
95 {IP
: ParseIP("54.83.193.112")},
96 {IP
: ParseIP("184.72.238.214")},
97 {IP
: ParseIP("23.23.172.185")},
98 {IP
: ParseIP("75.101.148.21")},
99 {IP
: ParseIP("23.23.134.56")},
100 {IP
: ParseIP("23.21.50.150")},
111 {IP
: ParseIP("54.83.193.112")},
112 {IP
: ParseIP("184.72.238.214")},
113 {IP
: ParseIP("23.23.172.185")},
114 {IP
: ParseIP("75.101.148.21")},
115 {IP
: ParseIP("23.23.134.56")},
116 {IP
: ParseIP("23.21.50.150")},
121 for i
, tt
:= range tests
{
122 inCopy
:= make([]IPAddr
, len(tt
.in
))
124 srcCopy
:= make([]IP
, len(tt
.in
))
125 copy(srcCopy
, tt
.srcs
)
126 sortByRFC6724withSrcs(inCopy
, srcCopy
)
127 if !reflect
.DeepEqual(inCopy
, tt
.want
) {
128 t
.Errorf("test %d:\nin = %s\ngot: %s\nwant: %s\n", i
, tt
.in
, inCopy
, tt
.want
)
132 copy(srcCopy
, tt
.srcs
)
133 for j
:= 0; j
< len(inCopy
)/2; j
++ {
134 k
:= len(inCopy
) - j
- 1
135 inCopy
[j
], inCopy
[k
] = inCopy
[k
], inCopy
[j
]
136 srcCopy
[j
], srcCopy
[k
] = srcCopy
[k
], srcCopy
[j
]
138 sortByRFC6724withSrcs(inCopy
, srcCopy
)
139 if !reflect
.DeepEqual(inCopy
, tt
.want
) {
140 t
.Errorf("test %d, starting backwards:\nin = %s\ngot: %s\nwant: %s\n", i
, tt
.in
, inCopy
, tt
.want
)
148 func TestRFC6724PolicyTableClassify(t
*testing
.T
) {
151 want policyTableEntry
154 ip
: ParseIP("127.0.0.1"),
155 want
: policyTableEntry
{
156 Prefix
: &IPNet
{IP
: ParseIP("::ffff:0:0"), Mask
: CIDRMask(96, 128)},
162 ip
: ParseIP("2601:645:8002:a500:986f:1db8:c836:bd65"),
163 want
: policyTableEntry
{
164 Prefix
: &IPNet
{IP
: ParseIP("::"), Mask
: CIDRMask(0, 128)},
171 want
: policyTableEntry
{
172 Prefix
: &IPNet
{IP
: ParseIP("::1"), Mask
: CIDRMask(128, 128)},
178 ip
: ParseIP("2002::ab12"),
179 want
: policyTableEntry
{
180 Prefix
: &IPNet
{IP
: ParseIP("2002::"), Mask
: CIDRMask(16, 128)},
186 for i
, tt
:= range tests
{
187 got
:= rfc6724policyTable
.Classify(tt
.ip
)
188 if !reflect
.DeepEqual(got
, tt
.want
) {
189 t
.Errorf("%d. Classify(%s) = %v; want %v", i
, tt
.ip
, got
, tt
.want
)
194 func TestRFC6724ClassifyScope(t
*testing
.T
) {
199 {ParseIP("127.0.0.1"), scopeLinkLocal
}, // rfc6724#section-3.2
200 {ParseIP("::1"), scopeLinkLocal
}, // rfc4007#section-4
201 {ParseIP("169.254.1.2"), scopeLinkLocal
}, // rfc6724#section-3.2
202 {ParseIP("fec0::1"), scopeSiteLocal
},
203 {ParseIP("8.8.8.8"), scopeGlobal
},
205 {ParseIP("ff02::"), scopeLinkLocal
}, // IPv6 multicast
206 {ParseIP("ff05::"), scopeSiteLocal
}, // IPv6 multicast
207 {ParseIP("ff04::"), scopeAdminLocal
}, // IPv6 multicast
208 {ParseIP("ff0e::"), scopeGlobal
}, // IPv6 multicast
210 {IPv4(0xe0, 0, 0, 0), scopeGlobal
}, // IPv4 link-local multicast as 16 bytes
211 {IPv4(0xe0, 2, 2, 2), scopeGlobal
}, // IPv4 global multicast as 16 bytes
212 {IPv4(0xe0, 0, 0, 0).To4(), scopeGlobal
}, // IPv4 link-local multicast as 4 bytes
213 {IPv4(0xe0, 2, 2, 2).To4(), scopeGlobal
}, // IPv4 global multicast as 4 bytes
215 for i
, tt
:= range tests
{
216 got
:= classifyScope(tt
.ip
)
218 t
.Errorf("%d. classifyScope(%s) = %x; want %x", i
, tt
.ip
, got
, tt
.want
)
223 func TestRFC6724CommonPrefixLength(t
*testing
.T
) {
228 {ParseIP("fe80::1"), ParseIP("fe80::2"), 64},
229 {ParseIP("fe81::1"), ParseIP("fe80::2"), 15},
230 {ParseIP("127.0.0.1"), ParseIP("fe80::1"), 0}, // diff size
231 {IPv4(1, 2, 3, 4), IP
{1, 2, 3, 4}, 32},
232 {IP
{1, 2, 255, 255}, IP
{1, 2, 0, 0}, 16},
233 {IP
{1, 2, 127, 255}, IP
{1, 2, 0, 0}, 17},
234 {IP
{1, 2, 63, 255}, IP
{1, 2, 0, 0}, 18},
235 {IP
{1, 2, 31, 255}, IP
{1, 2, 0, 0}, 19},
236 {IP
{1, 2, 15, 255}, IP
{1, 2, 0, 0}, 20},
237 {IP
{1, 2, 7, 255}, IP
{1, 2, 0, 0}, 21},
238 {IP
{1, 2, 3, 255}, IP
{1, 2, 0, 0}, 22},
239 {IP
{1, 2, 1, 255}, IP
{1, 2, 0, 0}, 23},
240 {IP
{1, 2, 0, 255}, IP
{1, 2, 0, 0}, 24},
242 for i
, tt
:= range tests
{
243 got
:= commonPrefixLen(tt
.a
, tt
.b
)
245 t
.Errorf("%d. commonPrefixLen(%s, %s) = %d; want %d", i
, tt
.a
, tt
.b
, got
, tt
.want
)