LEFT | RIGHT |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 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 "fmt" | 8 "fmt" |
9 "reflect" | 9 "reflect" |
10 "runtime" | 10 "runtime" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 break | 140 break |
141 } | 141 } |
142 } | 142 } |
143 }() | 143 }() |
144 } | 144 } |
145 for i := 0; i < cap(sem); i++ { | 145 for i := 0; i < cap(sem); i++ { |
146 sem <- true | 146 sem <- true |
147 } | 147 } |
148 } | 148 } |
149 | 149 |
150 var resolveTCPAddrTests = []struct { | 150 type resolveTCPAddrTest struct { |
151 net string | 151 net string |
152 litAddr string | 152 litAddr string |
153 addr *TCPAddr | 153 addr *TCPAddr |
154 err error | 154 err error |
155 }{ | 155 } |
| 156 |
| 157 var resolveTCPAddrTests = []resolveTCPAddrTest{ |
156 {"tcp", "127.0.0.1:0", &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 0}, nil}, | 158 {"tcp", "127.0.0.1:0", &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 0}, nil}, |
157 {"tcp4", "127.0.0.1:65535", &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 65535
}, nil}, | 159 {"tcp4", "127.0.0.1:65535", &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 65535
}, nil}, |
158 | 160 |
159 {"tcp", "[::1]:1", &TCPAddr{IP: ParseIP("::1"), Port: 1}, nil}, | 161 {"tcp", "[::1]:1", &TCPAddr{IP: ParseIP("::1"), Port: 1}, nil}, |
160 {"tcp6", "[::1]:65534", &TCPAddr{IP: ParseIP("::1"), Port: 65534}, nil}, | 162 {"tcp6", "[::1]:65534", &TCPAddr{IP: ParseIP("::1"), Port: 65534}, nil}, |
161 | 163 |
162 {"tcp", "[::1%en0]:1", &TCPAddr{IP: ParseIP("::1"), Port: 1, Zone: "en0"
}, nil}, | 164 {"tcp", "[::1%en0]:1", &TCPAddr{IP: ParseIP("::1"), Port: 1, Zone: "en0"
}, nil}, |
163 {"tcp6", "[::1%911]:2", &TCPAddr{IP: ParseIP("::1"), Port: 2, Zone: "911
"}, nil}, | 165 {"tcp6", "[::1%911]:2", &TCPAddr{IP: ParseIP("::1"), Port: 2, Zone: "911
"}, nil}, |
164 {"tcp6", "[fe80::1]:3", &TCPAddr{IP: ParseIP("fe80::1"), Port: 3, Zone:
"name"}, nil}, | |
165 {"tcp6", "[fe80::1]:4", &TCPAddr{IP: ParseIP("fe80::1"), Port: 4, Zone:
"index"}, nil}, | |
166 | 166 |
167 {"", "127.0.0.1:0", &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 0}, nil}, //
Go 1.0 behavior | 167 {"", "127.0.0.1:0", &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 0}, nil}, //
Go 1.0 behavior |
168 {"", "[::1]:0", &TCPAddr{IP: ParseIP("::1"), Port: 0}, nil}, //
Go 1.0 behavior | 168 {"", "[::1]:0", &TCPAddr{IP: ParseIP("::1"), Port: 0}, nil}, //
Go 1.0 behavior |
169 | 169 |
170 {"http", "127.0.0.1:0", nil, UnknownNetworkError("http")}, | 170 {"http", "127.0.0.1:0", nil, UnknownNetworkError("http")}, |
171 } | 171 } |
172 | 172 |
| 173 func init() { |
| 174 if ifi := loopbackInterface(); ifi != nil { |
| 175 index := fmt.Sprintf("%v", ifi.Index) |
| 176 resolveTCPAddrTests = append(resolveTCPAddrTests, []resolveTCPAd
drTest{ |
| 177 {"tcp6", "[fe80::1%" + ifi.Name + "]:3", &TCPAddr{IP: Pa
rseIP("fe80::1"), Port: 3, Zone: zoneToString(ifi.Index)}, nil}, |
| 178 {"tcp6", "[fe80::1%" + index + "]:4", &TCPAddr{IP: Parse
IP("fe80::1"), Port: 4, Zone: index}, nil}, |
| 179 }...) |
| 180 } |
| 181 } |
| 182 |
173 func TestResolveTCPAddr(t *testing.T) { | 183 func TestResolveTCPAddr(t *testing.T) { |
174 » for i, tt := range resolveTCPAddrTests { | 184 » for _, tt := range resolveTCPAddrTests { |
175 » » if tt.addr != nil && (tt.addr.Zone == "name" || tt.addr.Zone ==
"index") { | 185 » » addr, err := ResolveTCPAddr(tt.net, tt.litAddr) |
176 » » » ifi := loopbackInterface() | |
177 » » » if ifi == nil { | |
178 » » » » continue | |
179 » » » } | |
180 » » » j := last(tt.litAddr, ']') | |
181 » » » if j > 0 { | |
182 » » » » switch tt.addr.Zone { | |
183 » » » » case "name": | |
184 » » » » » resolveTCPAddrTests[i].litAddr = tt.litA
ddr[:j] + "%" + ifi.Name + tt.litAddr[j:] | |
185 » » » » » resolveTCPAddrTests[i].addr.Zone = zoneT
oString(ifi.Index) | |
186 » » » » case "index": | |
187 » » » » » index := fmt.Sprintf("%v", ifi.Index) | |
188 » » » » » resolveTCPAddrTests[i].litAddr = tt.litA
ddr[:j] + "%" + index + tt.litAddr[j:] | |
189 » » » » » resolveTCPAddrTests[i].addr.Zone = index | |
190 » » » » } | |
191 » » » } | |
192 » » } | |
193 » » addr, err := ResolveTCPAddr(tt.net, resolveTCPAddrTests[i].litAd
dr) | |
194 if err != tt.err { | 186 if err != tt.err { |
195 » » » t.Fatalf("ResolveTCPAddr(%v, %v) failed: %v", tt.net, re
solveTCPAddrTests[i].litAddr, err) | 187 » » » t.Fatalf("ResolveTCPAddr(%v, %v) failed: %v", tt.net, tt
.litAddr, err) |
196 » » } | 188 » » } |
197 » » if !reflect.DeepEqual(addr, resolveTCPAddrTests[i].addr) { | 189 » » if !reflect.DeepEqual(addr, tt.addr) { |
198 » » » t.Fatalf("got %#v; expected %#v", addr, resolveTCPAddrTe
sts[i].addr) | 190 » » » t.Fatalf("got %#v; expected %#v", addr, tt.addr) |
199 } | 191 } |
200 } | 192 } |
201 } | 193 } |
202 | 194 |
203 var tcpListenerNameTests = []struct { | 195 var tcpListenerNameTests = []struct { |
204 net string | 196 net string |
205 laddr *TCPAddr | 197 laddr *TCPAddr |
206 }{ | 198 }{ |
207 {"tcp4", &TCPAddr{IP: IPv4(127, 0, 0, 1)}}, | 199 {"tcp4", &TCPAddr{IP: IPv4(127, 0, 0, 1)}}, |
208 {"tcp4", &TCPAddr{}}, | 200 {"tcp4", &TCPAddr{}}, |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 t.Fatalf("Conn.Write failed: %v", err) | 287 t.Fatalf("Conn.Write failed: %v", err) |
296 } | 288 } |
297 b := make([]byte, 32) | 289 b := make([]byte, 32) |
298 if _, err := c.Read(b); err != nil { | 290 if _, err := c.Read(b); err != nil { |
299 t.Fatalf("Conn.Read failed: %v", err) | 291 t.Fatalf("Conn.Read failed: %v", err) |
300 } | 292 } |
301 | 293 |
302 <-done | 294 <-done |
303 } | 295 } |
304 } | 296 } |
LEFT | RIGHT |