123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- // Copyright 2013 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package net
- import (
- "reflect"
- "testing"
- )
- var testInetaddr = func(ip IP) netaddr { return &TCPAddr{IP: ip, Port: 5682} }
- var firstFavoriteAddrTests = []struct {
- filter func(IP) IP
- ips []IP
- inetaddr func(IP) netaddr
- addr netaddr
- err error
- }{
- {
- nil,
- []IP{
- IPv4(127, 0, 0, 1),
- IPv6loopback,
- },
- testInetaddr,
- addrList{
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- },
- nil,
- },
- {
- nil,
- []IP{
- IPv6loopback,
- IPv4(127, 0, 0, 1),
- },
- testInetaddr,
- addrList{
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- },
- nil,
- },
- {
- nil,
- []IP{
- IPv4(127, 0, 0, 1),
- IPv4(192, 168, 0, 1),
- },
- testInetaddr,
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- nil,
- },
- {
- nil,
- []IP{
- IPv6loopback,
- ParseIP("fe80::1"),
- },
- testInetaddr,
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- nil,
- },
- {
- nil,
- []IP{
- IPv4(127, 0, 0, 1),
- IPv4(192, 168, 0, 1),
- IPv6loopback,
- ParseIP("fe80::1"),
- },
- testInetaddr,
- addrList{
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- },
- nil,
- },
- {
- nil,
- []IP{
- IPv6loopback,
- ParseIP("fe80::1"),
- IPv4(127, 0, 0, 1),
- IPv4(192, 168, 0, 1),
- },
- testInetaddr,
- addrList{
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- },
- nil,
- },
- {
- nil,
- []IP{
- IPv4(127, 0, 0, 1),
- IPv6loopback,
- IPv4(192, 168, 0, 1),
- ParseIP("fe80::1"),
- },
- testInetaddr,
- addrList{
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- },
- nil,
- },
- {
- nil,
- []IP{
- IPv6loopback,
- IPv4(127, 0, 0, 1),
- ParseIP("fe80::1"),
- IPv4(192, 168, 0, 1),
- },
- testInetaddr,
- addrList{
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- },
- nil,
- },
- {
- ipv4only,
- []IP{
- IPv4(127, 0, 0, 1),
- IPv6loopback,
- },
- testInetaddr,
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- nil,
- },
- {
- ipv4only,
- []IP{
- IPv6loopback,
- IPv4(127, 0, 0, 1),
- },
- testInetaddr,
- &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682},
- nil,
- },
- {
- ipv6only,
- []IP{
- IPv4(127, 0, 0, 1),
- IPv6loopback,
- },
- testInetaddr,
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- nil,
- },
- {
- ipv6only,
- []IP{
- IPv6loopback,
- IPv4(127, 0, 0, 1),
- },
- testInetaddr,
- &TCPAddr{IP: IPv6loopback, Port: 5682},
- nil,
- },
- {nil, nil, testInetaddr, nil, errNoSuitableAddress},
- {ipv4only, nil, testInetaddr, nil, errNoSuitableAddress},
- {ipv4only, []IP{IPv6loopback}, testInetaddr, nil, errNoSuitableAddress},
- {ipv6only, nil, testInetaddr, nil, errNoSuitableAddress},
- {ipv6only, []IP{IPv4(127, 0, 0, 1)}, testInetaddr, nil, errNoSuitableAddress},
- }
- func TestFirstFavoriteAddr(t *testing.T) {
- if !supportsIPv4 || !supportsIPv6 {
- t.Skip("ipv4 or ipv6 is not supported")
- }
- for i, tt := range firstFavoriteAddrTests {
- addr, err := firstFavoriteAddr(tt.filter, tt.ips, tt.inetaddr)
- if err != tt.err {
- t.Errorf("#%v: got %v; expected %v", i, err, tt.err)
- }
- if !reflect.DeepEqual(addr, tt.addr) {
- t.Errorf("#%v: got %v; expected %v", i, addr, tt.addr)
- }
- }
- }
|