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.
12 var testInetaddr
= func(ip IP
) netaddr
{ return &TCPAddr
{IP
: ip
, Port
: 5682} }
14 var firstFavoriteAddrTests
= []struct {
17 inetaddr
func(IP
) netaddr
29 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
30 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
42 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
43 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
54 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
64 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
77 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
78 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
92 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
93 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
102 IPv4(192, 168, 0, 1),
107 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
108 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
118 IPv4(192, 168, 0, 1),
122 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
123 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
135 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
145 &TCPAddr
{IP
: IPv4(127, 0, 0, 1), Port
: 5682},
156 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
166 &TCPAddr
{IP
: IPv6loopback
, Port
: 5682},
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
)
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
)