LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package net | 5 package net |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "errors" | 9 "errors" |
10 "fmt" | 10 "fmt" |
11 "os" | 11 "os" |
12 "reflect" | 12 "reflect" |
13 "testing" | 13 "testing" |
14 "time" | 14 "time" |
15 ) | 15 ) |
16 | 16 |
17 var resolveIPAddrTests = []struct { | 17 type resolveIPAddrTest struct { |
18 net string | 18 net string |
19 litAddr string | 19 litAddr string |
20 addr *IPAddr | 20 addr *IPAddr |
21 err error | 21 err error |
22 }{ | 22 } |
| 23 |
| 24 var resolveIPAddrTests = []resolveIPAddrTest{ |
23 {"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, | 25 {"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, |
24 {"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, | 26 {"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, |
25 {"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, | 27 {"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, |
26 | 28 |
27 {"ip", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, | 29 {"ip", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, |
28 {"ip6", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, | 30 {"ip6", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, |
29 {"ip6:icmp", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, | 31 {"ip6:icmp", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, |
30 | 32 |
31 {"ip", "::1%en0", &IPAddr{IP: ParseIP("::1"), Zone: "en0"}, nil}, | 33 {"ip", "::1%en0", &IPAddr{IP: ParseIP("::1"), Zone: "en0"}, nil}, |
32 {"ip6", "::1%911", &IPAddr{IP: ParseIP("::1"), Zone: "911"}, nil}, | 34 {"ip6", "::1%911", &IPAddr{IP: ParseIP("::1"), Zone: "911"}, nil}, |
33 {"ip6", "fe80::1", &IPAddr{IP: ParseIP("fe80::1"), Zone: "name"}, nil}, | |
34 {"ip6", "fe80::1", &IPAddr{IP: ParseIP("fe80::1"), Zone: "index"}, nil}, | |
35 | 35 |
36 {"", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, // Go 1.0 behav
ior | 36 {"", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, // Go 1.0 behav
ior |
37 {"", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, // Go 1.0 behav
ior | 37 {"", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, // Go 1.0 behav
ior |
38 | 38 |
39 {"l2tp", "127.0.0.1", nil, UnknownNetworkError("l2tp")}, | 39 {"l2tp", "127.0.0.1", nil, UnknownNetworkError("l2tp")}, |
40 {"l2tp:gre", "127.0.0.1", nil, UnknownNetworkError("l2tp:gre")}, | 40 {"l2tp:gre", "127.0.0.1", nil, UnknownNetworkError("l2tp:gre")}, |
41 {"tcp", "1.2.3.4:123", nil, UnknownNetworkError("tcp")}, | 41 {"tcp", "1.2.3.4:123", nil, UnknownNetworkError("tcp")}, |
42 } | 42 } |
43 | 43 |
| 44 func init() { |
| 45 if ifi := loopbackInterface(); ifi != nil { |
| 46 index := fmt.Sprintf("%v", ifi.Index) |
| 47 resolveIPAddrTests = append(resolveIPAddrTests, []resolveIPAddrT
est{ |
| 48 {"ip6", "fe80::1%" + ifi.Name, &IPAddr{IP: ParseIP("fe80
::1"), Zone: zoneToString(ifi.Index)}, nil}, |
| 49 {"ip6", "fe80::1%" + index, &IPAddr{IP: ParseIP("fe80::1
"), Zone: index}, nil}, |
| 50 }...) |
| 51 } |
| 52 } |
| 53 |
44 func TestResolveIPAddr(t *testing.T) { | 54 func TestResolveIPAddr(t *testing.T) { |
45 for _, tt := range resolveIPAddrTests { | 55 for _, tt := range resolveIPAddrTests { |
46 if tt.addr != nil && (tt.addr.Zone == "name" || tt.addr.Zone ==
"index") { | |
47 ifi := loopbackInterface() | |
48 if ifi == nil { | |
49 continue | |
50 } | |
51 switch tt.addr.Zone { | |
52 case "name": | |
53 tt.litAddr += "%" + ifi.Name | |
54 tt.addr.Zone = zoneToString(ifi.Index) | |
55 case "index": | |
56 index := fmt.Sprintf("%v", ifi.Index) | |
57 tt.litAddr += "%" + index | |
58 tt.addr.Zone = index | |
59 } | |
60 } | |
61 addr, err := ResolveIPAddr(tt.net, tt.litAddr) | 56 addr, err := ResolveIPAddr(tt.net, tt.litAddr) |
62 if err != tt.err { | 57 if err != tt.err { |
63 t.Fatalf("ResolveIPAddr(%v, %v) failed: %v", tt.net, tt.
litAddr, err) | 58 t.Fatalf("ResolveIPAddr(%v, %v) failed: %v", tt.net, tt.
litAddr, err) |
64 } | 59 } |
65 if !reflect.DeepEqual(addr, tt.addr) { | 60 if !reflect.DeepEqual(addr, tt.addr) { |
66 t.Fatalf("got %#v; expected %#v", addr, tt.addr) | 61 t.Fatalf("got %#v; expected %#v", addr, tt.addr) |
67 } | 62 } |
68 } | 63 } |
69 } | 64 } |
70 | 65 |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 raddr := &IPAddr{IP: IPv4(127, 0, 0, 10).To4()} | 362 raddr := &IPAddr{IP: IPv4(127, 0, 0, 10).To4()} |
368 c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr) | 363 c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr) |
369 if err != nil { | 364 if err != nil { |
370 t.Fatalf("DialIP failed: %v", err) | 365 t.Fatalf("DialIP failed: %v", err) |
371 } | 366 } |
372 defer c.Close() | 367 defer c.Close() |
373 if !reflect.DeepEqual(raddr, c.RemoteAddr()) { | 368 if !reflect.DeepEqual(raddr, c.RemoteAddr()) { |
374 t.Fatalf("got %#v, expected %#v", c.RemoteAddr(), raddr) | 369 t.Fatalf("got %#v, expected %#v", c.RemoteAddr(), raddr) |
375 } | 370 } |
376 } | 371 } |
LEFT | RIGHT |