Revert r215321.
[official-gcc.git] / libgo / go / net / ipsock_test.go
blob9ecaaec69f671b5e35f97b156014a3c60b3d3505
1 // Copyright 2013 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 package net
7 import (
8 "reflect"
9 "testing"
12 var testInetaddr = func(ip IP) netaddr { return &TCPAddr{IP: ip, Port: 5682} }
14 var firstFavoriteAddrTests = []struct {
15 filter func(IP) IP
16 ips []IP
17 inetaddr func(IP) netaddr
18 addr netaddr
19 err error
22 nil,
23 []IP{
24 IPv4(127, 0, 0, 1),
25 IPv6loopback,
27 testInetaddr,
28 addrList{
29 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
30 &TCPAddr{IP: IPv6loopback, Port: 5682},
32 nil,
35 nil,
36 []IP{
37 IPv6loopback,
38 IPv4(127, 0, 0, 1),
40 testInetaddr,
41 addrList{
42 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
43 &TCPAddr{IP: IPv6loopback, Port: 5682},
45 nil,
48 nil,
49 []IP{
50 IPv4(127, 0, 0, 1),
51 IPv4(192, 168, 0, 1),
53 testInetaddr,
54 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
55 nil,
58 nil,
59 []IP{
60 IPv6loopback,
61 ParseIP("fe80::1"),
63 testInetaddr,
64 &TCPAddr{IP: IPv6loopback, Port: 5682},
65 nil,
68 nil,
69 []IP{
70 IPv4(127, 0, 0, 1),
71 IPv4(192, 168, 0, 1),
72 IPv6loopback,
73 ParseIP("fe80::1"),
75 testInetaddr,
76 addrList{
77 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
78 &TCPAddr{IP: IPv6loopback, Port: 5682},
80 nil,
83 nil,
84 []IP{
85 IPv6loopback,
86 ParseIP("fe80::1"),
87 IPv4(127, 0, 0, 1),
88 IPv4(192, 168, 0, 1),
90 testInetaddr,
91 addrList{
92 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
93 &TCPAddr{IP: IPv6loopback, Port: 5682},
95 nil,
98 nil,
99 []IP{
100 IPv4(127, 0, 0, 1),
101 IPv6loopback,
102 IPv4(192, 168, 0, 1),
103 ParseIP("fe80::1"),
105 testInetaddr,
106 addrList{
107 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
108 &TCPAddr{IP: IPv6loopback, Port: 5682},
110 nil,
113 nil,
114 []IP{
115 IPv6loopback,
116 IPv4(127, 0, 0, 1),
117 ParseIP("fe80::1"),
118 IPv4(192, 168, 0, 1),
120 testInetaddr,
121 addrList{
122 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
123 &TCPAddr{IP: IPv6loopback, Port: 5682},
125 nil,
129 ipv4only,
130 []IP{
131 IPv4(127, 0, 0, 1),
132 IPv6loopback,
134 testInetaddr,
135 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
136 nil,
139 ipv4only,
140 []IP{
141 IPv6loopback,
142 IPv4(127, 0, 0, 1),
144 testInetaddr,
145 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
146 nil,
150 ipv6only,
151 []IP{
152 IPv4(127, 0, 0, 1),
153 IPv6loopback,
155 testInetaddr,
156 &TCPAddr{IP: IPv6loopback, Port: 5682},
157 nil,
160 ipv6only,
161 []IP{
162 IPv6loopback,
163 IPv4(127, 0, 0, 1),
165 testInetaddr,
166 &TCPAddr{IP: IPv6loopback, Port: 5682},
167 nil,
170 {nil, nil, testInetaddr, nil, errNoSuitableAddress},
172 {ipv4only, nil, testInetaddr, nil, errNoSuitableAddress},
173 {ipv4only, []IP{IPv6loopback}, testInetaddr, nil, errNoSuitableAddress},
175 {ipv6only, nil, testInetaddr, nil, errNoSuitableAddress},
176 {ipv6only, []IP{IPv4(127, 0, 0, 1)}, testInetaddr, nil, errNoSuitableAddress},
179 func TestFirstFavoriteAddr(t *testing.T) {
180 if !supportsIPv4 || !supportsIPv6 {
181 t.Skip("ipv4 or ipv6 is not supported")
184 for i, tt := range firstFavoriteAddrTests {
185 addr, err := firstFavoriteAddr(tt.filter, tt.ips, tt.inetaddr)
186 if err != tt.err {
187 t.Errorf("#%v: got %v; expected %v", i, err, tt.err)
189 if !reflect.DeepEqual(addr, tt.addr) {
190 t.Errorf("#%v: got %v; expected %v", i, addr, tt.addr)