Imported Upstream version 4.7.2
[platform/upstream/gcc48.git] / libgo / go / net / ip_test.go
1 // Copyright 2009 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.
4
5 package net
6
7 import (
8         "bytes"
9         _ "debug/elf"
10         "reflect"
11         "runtime"
12         "testing"
13 )
14
15 func isEqual(a, b []byte) bool {
16         if a == nil && b == nil {
17                 return true
18         }
19         if a == nil || b == nil {
20                 return false
21         }
22         return bytes.Equal(a, b)
23 }
24
25 var parseiptests = []struct {
26         in  string
27         out IP
28 }{
29         {"127.0.1.2", IPv4(127, 0, 1, 2)},
30         {"127.0.0.1", IPv4(127, 0, 0, 1)},
31         {"127.0.0.256", nil},
32         {"abc", nil},
33         {"123:", nil},
34         {"::ffff:127.0.0.1", IPv4(127, 0, 0, 1)},
35         {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
36         {"::ffff:4a7d:1363", IPv4(74, 125, 19, 99)},
37         {"", nil},
38 }
39
40 func TestParseIP(t *testing.T) {
41         for _, tt := range parseiptests {
42                 if out := ParseIP(tt.in); !isEqual(out, tt.out) {
43                         t.Errorf("ParseIP(%q) = %v, want %v", tt.in, out, tt.out)
44                 }
45         }
46 }
47
48 var ipstringtests = []struct {
49         in  IP
50         out string
51 }{
52         // cf. RFC 5952 (A Recommendation for IPv6 Address Text Representation)
53         {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, "2001:db8::123:12:1"},
54         {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1"},
55         {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1}, "2001:db8:0:1:0:1:0:1"},
56         {IP{0x20, 0x1, 0xd, 0xb8, 0, 0x1, 0, 0, 0, 0x1, 0, 0, 0, 0x1, 0, 0}, "2001:db8:1:0:1:0:1:0"},
57         {IP{0x20, 0x1, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001::1:0:0:1"},
58         {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0}, "2001:db8:0:0:1::"},
59         {IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0x1}, "2001:db8::1:0:0:1"},
60         {IP{0x20, 0x1, 0xD, 0xB8, 0, 0, 0, 0, 0, 0xA, 0, 0xB, 0, 0xC, 0, 0xD}, "2001:db8::a:b:c:d"},
61         {nil, "<nil>"},
62 }
63
64 func TestIPString(t *testing.T) {
65         for _, tt := range ipstringtests {
66                 if out := tt.in.String(); out != tt.out {
67                         t.Errorf("IP.String(%v) = %q, want %q", tt.in, out, tt.out)
68                 }
69         }
70 }
71
72 var ipmasktests = []struct {
73         in   IP
74         mask IPMask
75         out  IP
76 }{
77         {IPv4(192, 168, 1, 127), IPv4Mask(255, 255, 255, 128), IPv4(192, 168, 1, 0)},
78         {IPv4(192, 168, 1, 127), IPMask(ParseIP("255.255.255.192")), IPv4(192, 168, 1, 64)},
79         {IPv4(192, 168, 1, 127), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0")), IPv4(192, 168, 1, 96)},
80         {IPv4(192, 168, 1, 127), IPv4Mask(255, 0, 255, 0), IPv4(192, 0, 1, 0)},
81         {ParseIP("2001:db8::1"), IPMask(ParseIP("ffff:ff80::")), ParseIP("2001:d80::")},
82         {ParseIP("2001:db8::1"), IPMask(ParseIP("f0f0:0f0f::")), ParseIP("2000:d08::")},
83 }
84
85 func TestIPMask(t *testing.T) {
86         for _, tt := range ipmasktests {
87                 if out := tt.in.Mask(tt.mask); out == nil || !tt.out.Equal(out) {
88                         t.Errorf("IP(%v).Mask(%v) = %v, want %v", tt.in, tt.mask, out, tt.out)
89                 }
90         }
91 }
92
93 var ipmaskstringtests = []struct {
94         in  IPMask
95         out string
96 }{
97         {IPv4Mask(255, 255, 255, 240), "fffffff0"},
98         {IPv4Mask(255, 0, 128, 0), "ff008000"},
99         {IPMask(ParseIP("ffff:ff80::")), "ffffff80000000000000000000000000"},
100         {IPMask(ParseIP("ef00:ff80::cafe:0")), "ef00ff800000000000000000cafe0000"},
101         {nil, "<nil>"},
102 }
103
104 func TestIPMaskString(t *testing.T) {
105         for _, tt := range ipmaskstringtests {
106                 if out := tt.in.String(); out != tt.out {
107                         t.Errorf("IPMask.String(%v) = %q, want %q", tt.in, out, tt.out)
108                 }
109         }
110 }
111
112 var parsecidrtests = []struct {
113         in  string
114         ip  IP
115         net *IPNet
116         err error
117 }{
118         {"135.104.0.0/32", IPv4(135, 104, 0, 0), &IPNet{IPv4(135, 104, 0, 0), IPv4Mask(255, 255, 255, 255)}, nil},
119         {"0.0.0.0/24", IPv4(0, 0, 0, 0), &IPNet{IPv4(0, 0, 0, 0), IPv4Mask(255, 255, 255, 0)}, nil},
120         {"135.104.0.0/24", IPv4(135, 104, 0, 0), &IPNet{IPv4(135, 104, 0, 0), IPv4Mask(255, 255, 255, 0)}, nil},
121         {"135.104.0.1/32", IPv4(135, 104, 0, 1), &IPNet{IPv4(135, 104, 0, 1), IPv4Mask(255, 255, 255, 255)}, nil},
122         {"135.104.0.1/24", IPv4(135, 104, 0, 1), &IPNet{IPv4(135, 104, 0, 0), IPv4Mask(255, 255, 255, 0)}, nil},
123         {"::1/128", ParseIP("::1"), &IPNet{ParseIP("::1"), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))}, nil},
124         {"abcd:2345::/127", ParseIP("abcd:2345::"), &IPNet{ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"))}, nil},
125         {"abcd:2345::/65", ParseIP("abcd:2345::"), &IPNet{ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:ffff:ffff:8000::"))}, nil},
126         {"abcd:2345::/64", ParseIP("abcd:2345::"), &IPNet{ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:ffff:ffff::"))}, nil},
127         {"abcd:2345::/63", ParseIP("abcd:2345::"), &IPNet{ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:ffff:fffe::"))}, nil},
128         {"abcd:2345::/33", ParseIP("abcd:2345::"), &IPNet{ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff:8000::"))}, nil},
129         {"abcd:2345::/32", ParseIP("abcd:2345::"), &IPNet{ParseIP("abcd:2345::"), IPMask(ParseIP("ffff:ffff::"))}, nil},
130         {"abcd:2344::/31", ParseIP("abcd:2344::"), &IPNet{ParseIP("abcd:2344::"), IPMask(ParseIP("ffff:fffe::"))}, nil},
131         {"abcd:2300::/24", ParseIP("abcd:2300::"), &IPNet{ParseIP("abcd:2300::"), IPMask(ParseIP("ffff:ff00::"))}, nil},
132         {"abcd:2345::/24", ParseIP("abcd:2345::"), &IPNet{ParseIP("abcd:2300::"), IPMask(ParseIP("ffff:ff00::"))}, nil},
133         {"2001:DB8::/48", ParseIP("2001:DB8::"), &IPNet{ParseIP("2001:DB8::"), IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
134         {"2001:DB8::1/48", ParseIP("2001:DB8::1"), &IPNet{ParseIP("2001:DB8::"), IPMask(ParseIP("ffff:ffff:ffff::"))}, nil},
135         {"192.168.1.1/255.255.255.0", nil, nil, &ParseError{"CIDR address", "192.168.1.1/255.255.255.0"}},
136         {"192.168.1.1/35", nil, nil, &ParseError{"CIDR address", "192.168.1.1/35"}},
137         {"2001:db8::1/-1", nil, nil, &ParseError{"CIDR address", "2001:db8::1/-1"}},
138         {"", nil, nil, &ParseError{"CIDR address", ""}},
139 }
140
141 func TestParseCIDR(t *testing.T) {
142         for _, tt := range parsecidrtests {
143                 ip, net, err := ParseCIDR(tt.in)
144                 if !reflect.DeepEqual(err, tt.err) {
145                         t.Errorf("ParseCIDR(%q) = %v, %v; want %v, %v", tt.in, ip, net, tt.ip, tt.net)
146                 }
147                 if err == nil && (!tt.ip.Equal(ip) || !tt.net.IP.Equal(net.IP) || !isEqual(net.Mask, tt.net.Mask)) {
148                         t.Errorf("ParseCIDR(%q) = %v, {%v, %v}; want %v {%v, %v}", tt.in, ip, net.IP, net.Mask, tt.ip, tt.net.IP, tt.net.Mask)
149                 }
150         }
151 }
152
153 var ipnetcontainstests = []struct {
154         ip  IP
155         net *IPNet
156         ok  bool
157 }{
158         {IPv4(172, 16, 1, 1), &IPNet{IPv4(172, 16, 0, 0), CIDRMask(12, 32)}, true},
159         {IPv4(172, 24, 0, 1), &IPNet{IPv4(172, 16, 0, 0), CIDRMask(13, 32)}, false},
160         {IPv4(192, 168, 0, 3), &IPNet{IPv4(192, 168, 0, 0), IPv4Mask(0, 0, 255, 252)}, true},
161         {IPv4(192, 168, 0, 4), &IPNet{IPv4(192, 168, 0, 0), IPv4Mask(0, 255, 0, 252)}, false},
162         {ParseIP("2001:db8:1:2::1"), &IPNet{ParseIP("2001:db8:1::"), CIDRMask(47, 128)}, true},
163         {ParseIP("2001:db8:1:2::1"), &IPNet{ParseIP("2001:db8:2::"), CIDRMask(47, 128)}, false},
164         {ParseIP("2001:db8:1:2::1"), &IPNet{ParseIP("2001:db8:1::"), IPMask(ParseIP("ffff:0:ffff::"))}, true},
165         {ParseIP("2001:db8:1:2::1"), &IPNet{ParseIP("2001:db8:1::"), IPMask(ParseIP("0:0:0:ffff::"))}, false},
166 }
167
168 func TestIPNetContains(t *testing.T) {
169         for _, tt := range ipnetcontainstests {
170                 if ok := tt.net.Contains(tt.ip); ok != tt.ok {
171                         t.Errorf("IPNet(%v).Contains(%v) = %v, want %v", tt.net, tt.ip, ok, tt.ok)
172                 }
173         }
174 }
175
176 var ipnetstringtests = []struct {
177         in  *IPNet
178         out string
179 }{
180         {&IPNet{IPv4(192, 168, 1, 0), CIDRMask(26, 32)}, "192.168.1.0/26"},
181         {&IPNet{IPv4(192, 168, 1, 0), IPv4Mask(255, 0, 255, 0)}, "192.168.1.0/ff00ff00"},
182         {&IPNet{ParseIP("2001:db8::"), CIDRMask(55, 128)}, "2001:db8::/55"},
183         {&IPNet{ParseIP("2001:db8::"), IPMask(ParseIP("8000:f123:0:cafe::"))}, "2001:db8::/8000f1230000cafe0000000000000000"},
184 }
185
186 func TestIPNetString(t *testing.T) {
187         for _, tt := range ipnetstringtests {
188                 if out := tt.in.String(); out != tt.out {
189                         t.Errorf("IPNet.String(%v) = %q, want %q", tt.in, out, tt.out)
190                 }
191         }
192 }
193
194 var cidrmasktests = []struct {
195         ones int
196         bits int
197         out  IPMask
198 }{
199         {0, 32, IPv4Mask(0, 0, 0, 0)},
200         {12, 32, IPv4Mask(255, 240, 0, 0)},
201         {24, 32, IPv4Mask(255, 255, 255, 0)},
202         {32, 32, IPv4Mask(255, 255, 255, 255)},
203         {0, 128, IPMask{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
204         {4, 128, IPMask{0xf0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
205         {48, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
206         {128, 128, IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
207         {33, 32, nil},
208         {32, 33, nil},
209         {-1, 128, nil},
210         {128, -1, nil},
211 }
212
213 func TestCIDRMask(t *testing.T) {
214         for _, tt := range cidrmasktests {
215                 if out := CIDRMask(tt.ones, tt.bits); !isEqual(out, tt.out) {
216                         t.Errorf("CIDRMask(%v, %v) = %v, want %v", tt.ones, tt.bits, out, tt.out)
217                 }
218         }
219 }
220
221 var (
222         v4addr         = IP{192, 168, 0, 1}
223         v4mappedv6addr = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
224         v6addr         = IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}
225         v4mask         = IPMask{255, 255, 255, 0}
226         v4mappedv6mask = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 255, 255, 255, 0}
227         v6mask         = IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0}
228         badaddr        = IP{192, 168, 0}
229         badmask        = IPMask{255, 255, 0}
230         v4maskzero     = IPMask{0, 0, 0, 0}
231 )
232
233 var networknumberandmasktests = []struct {
234         in  IPNet
235         out IPNet
236 }{
237         {IPNet{v4addr, v4mask}, IPNet{v4addr, v4mask}},
238         {IPNet{v4addr, v4mappedv6mask}, IPNet{v4addr, v4mask}},
239         {IPNet{v4mappedv6addr, v4mappedv6mask}, IPNet{v4addr, v4mask}},
240         {IPNet{v4mappedv6addr, v6mask}, IPNet{v4addr, v4maskzero}},
241         {IPNet{v4addr, v6mask}, IPNet{v4addr, v4maskzero}},
242         {IPNet{v6addr, v6mask}, IPNet{v6addr, v6mask}},
243         {IPNet{v6addr, v4mappedv6mask}, IPNet{v6addr, v4mappedv6mask}},
244         {in: IPNet{v6addr, v4mask}},
245         {in: IPNet{v4addr, badmask}},
246         {in: IPNet{v4mappedv6addr, badmask}},
247         {in: IPNet{v6addr, badmask}},
248         {in: IPNet{badaddr, v4mask}},
249         {in: IPNet{badaddr, v4mappedv6mask}},
250         {in: IPNet{badaddr, v6mask}},
251         {in: IPNet{badaddr, badmask}},
252 }
253
254 func TestNetworkNumberAndMask(t *testing.T) {
255         for _, tt := range networknumberandmasktests {
256                 ip, m := networkNumberAndMask(&tt.in)
257                 out := &IPNet{ip, m}
258                 if !reflect.DeepEqual(&tt.out, out) {
259                         t.Errorf("networkNumberAndMask(%v) = %v; want %v", tt.in, out, &tt.out)
260                 }
261         }
262 }
263
264 var splitjointests = []struct {
265         Host string
266         Port string
267         Join string
268 }{
269         {"www.google.com", "80", "www.google.com:80"},
270         {"127.0.0.1", "1234", "127.0.0.1:1234"},
271         {"::1", "80", "[::1]:80"},
272 }
273
274 func TestSplitHostPort(t *testing.T) {
275         for _, tt := range splitjointests {
276                 if host, port, err := SplitHostPort(tt.Join); host != tt.Host || port != tt.Port || err != nil {
277                         t.Errorf("SplitHostPort(%q) = %q, %q, %v; want %q, %q, nil", tt.Join, host, port, err, tt.Host, tt.Port)
278                 }
279         }
280 }
281
282 func TestJoinHostPort(t *testing.T) {
283         for _, tt := range splitjointests {
284                 if join := JoinHostPort(tt.Host, tt.Port); join != tt.Join {
285                         t.Errorf("JoinHostPort(%q, %q) = %q; want %q", tt.Host, tt.Port, join, tt.Join)
286                 }
287         }
288 }
289
290 var ipaftests = []struct {
291         in  IP
292         af4 bool
293         af6 bool
294 }{
295         {IPv4bcast, true, false},
296         {IPv4allsys, true, false},
297         {IPv4allrouter, true, false},
298         {IPv4zero, true, false},
299         {IPv4(224, 0, 0, 1), true, false},
300         {IPv4(127, 0, 0, 1), true, false},
301         {IPv4(240, 0, 0, 1), true, false},
302         {IPv6unspecified, false, true},
303         {IPv6loopback, false, true},
304         {IPv6interfacelocalallnodes, false, true},
305         {IPv6linklocalallnodes, false, true},
306         {IPv6linklocalallrouters, false, true},
307         {ParseIP("ff05::a:b:c:d"), false, true},
308         {ParseIP("fe80::1:2:3:4"), false, true},
309         {ParseIP("2001:db8::123:12:1"), false, true},
310 }
311
312 func TestIPAddrFamily(t *testing.T) {
313         for _, tt := range ipaftests {
314                 if af := tt.in.To4() != nil; af != tt.af4 {
315                         t.Errorf("verifying IPv4 address family for %q = %v, want %v", tt.in, af, tt.af4)
316                 }
317                 if af := len(tt.in) == IPv6len && tt.in.To4() == nil; af != tt.af6 {
318                         t.Errorf("verifying IPv6 address family for %q = %v, want %v", tt.in, af, tt.af6)
319                 }
320         }
321 }
322
323 var ipscopetests = []struct {
324         scope func(IP) bool
325         in    IP
326         ok    bool
327 }{
328         {IP.IsUnspecified, IPv4zero, true},
329         {IP.IsUnspecified, IPv4(127, 0, 0, 1), false},
330         {IP.IsUnspecified, IPv6unspecified, true},
331         {IP.IsUnspecified, IPv6interfacelocalallnodes, false},
332         {IP.IsLoopback, IPv4(127, 0, 0, 1), true},
333         {IP.IsLoopback, IPv4(127, 255, 255, 254), true},
334         {IP.IsLoopback, IPv4(128, 1, 2, 3), false},
335         {IP.IsLoopback, IPv6loopback, true},
336         {IP.IsLoopback, IPv6linklocalallrouters, false},
337         {IP.IsMulticast, IPv4(224, 0, 0, 0), true},
338         {IP.IsMulticast, IPv4(239, 0, 0, 0), true},
339         {IP.IsMulticast, IPv4(240, 0, 0, 0), false},
340         {IP.IsMulticast, IPv6linklocalallnodes, true},
341         {IP.IsMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
342         {IP.IsMulticast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
343         {IP.IsLinkLocalMulticast, IPv4(224, 0, 0, 0), true},
344         {IP.IsLinkLocalMulticast, IPv4(239, 0, 0, 0), false},
345         {IP.IsLinkLocalMulticast, IPv6linklocalallrouters, true},
346         {IP.IsLinkLocalMulticast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
347         {IP.IsLinkLocalUnicast, IPv4(169, 254, 0, 0), true},
348         {IP.IsLinkLocalUnicast, IPv4(169, 255, 0, 0), false},
349         {IP.IsLinkLocalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
350         {IP.IsLinkLocalUnicast, IP{0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
351         {IP.IsGlobalUnicast, IPv4(240, 0, 0, 0), true},
352         {IP.IsGlobalUnicast, IPv4(232, 0, 0, 0), false},
353         {IP.IsGlobalUnicast, IPv4(169, 254, 0, 0), false},
354         {IP.IsGlobalUnicast, IP{0x20, 0x1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0x1, 0x23, 0, 0x12, 0, 0x1}, true},
355         {IP.IsGlobalUnicast, IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
356         {IP.IsGlobalUnicast, IP{0xff, 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false},
357 }
358
359 func name(f interface{}) string {
360         return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
361 }
362
363 func TestIPAddrScope(t *testing.T) {
364         for _, tt := range ipscopetests {
365                 if ok := tt.scope(tt.in); ok != tt.ok {
366                         t.Errorf("%s(%q) = %v, want %v", name(tt.scope), tt.in, ok, tt.ok)
367                 }
368         }
369 }