github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/ipsock_test.go (about) 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. 4 5 package net 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 var testInetaddr = func(ip IP) netaddr { return &TCPAddr{IP: ip, Port: 5682} } 13 14 var firstFavoriteAddrTests = []struct { 15 filter func(IP) IP 16 ips []IP 17 inetaddr func(IP) netaddr 18 addr netaddr 19 err error 20 }{ 21 { 22 nil, 23 []IP{ 24 IPv4(127, 0, 0, 1), 25 IPv6loopback, 26 }, 27 testInetaddr, 28 addrList{ 29 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 30 &TCPAddr{IP: IPv6loopback, Port: 5682}, 31 }, 32 nil, 33 }, 34 { 35 nil, 36 []IP{ 37 IPv6loopback, 38 IPv4(127, 0, 0, 1), 39 }, 40 testInetaddr, 41 addrList{ 42 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 43 &TCPAddr{IP: IPv6loopback, Port: 5682}, 44 }, 45 nil, 46 }, 47 { 48 nil, 49 []IP{ 50 IPv4(127, 0, 0, 1), 51 IPv4(192, 168, 0, 1), 52 }, 53 testInetaddr, 54 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 55 nil, 56 }, 57 { 58 nil, 59 []IP{ 60 IPv6loopback, 61 ParseIP("fe80::1"), 62 }, 63 testInetaddr, 64 &TCPAddr{IP: IPv6loopback, Port: 5682}, 65 nil, 66 }, 67 { 68 nil, 69 []IP{ 70 IPv4(127, 0, 0, 1), 71 IPv4(192, 168, 0, 1), 72 IPv6loopback, 73 ParseIP("fe80::1"), 74 }, 75 testInetaddr, 76 addrList{ 77 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 78 &TCPAddr{IP: IPv6loopback, Port: 5682}, 79 }, 80 nil, 81 }, 82 { 83 nil, 84 []IP{ 85 IPv6loopback, 86 ParseIP("fe80::1"), 87 IPv4(127, 0, 0, 1), 88 IPv4(192, 168, 0, 1), 89 }, 90 testInetaddr, 91 addrList{ 92 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 93 &TCPAddr{IP: IPv6loopback, Port: 5682}, 94 }, 95 nil, 96 }, 97 { 98 nil, 99 []IP{ 100 IPv4(127, 0, 0, 1), 101 IPv6loopback, 102 IPv4(192, 168, 0, 1), 103 ParseIP("fe80::1"), 104 }, 105 testInetaddr, 106 addrList{ 107 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 108 &TCPAddr{IP: IPv6loopback, Port: 5682}, 109 }, 110 nil, 111 }, 112 { 113 nil, 114 []IP{ 115 IPv6loopback, 116 IPv4(127, 0, 0, 1), 117 ParseIP("fe80::1"), 118 IPv4(192, 168, 0, 1), 119 }, 120 testInetaddr, 121 addrList{ 122 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 123 &TCPAddr{IP: IPv6loopback, Port: 5682}, 124 }, 125 nil, 126 }, 127 128 { 129 ipv4only, 130 []IP{ 131 IPv4(127, 0, 0, 1), 132 IPv6loopback, 133 }, 134 testInetaddr, 135 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 136 nil, 137 }, 138 { 139 ipv4only, 140 []IP{ 141 IPv6loopback, 142 IPv4(127, 0, 0, 1), 143 }, 144 testInetaddr, 145 &TCPAddr{IP: IPv4(127, 0, 0, 1), Port: 5682}, 146 nil, 147 }, 148 149 { 150 ipv6only, 151 []IP{ 152 IPv4(127, 0, 0, 1), 153 IPv6loopback, 154 }, 155 testInetaddr, 156 &TCPAddr{IP: IPv6loopback, Port: 5682}, 157 nil, 158 }, 159 { 160 ipv6only, 161 []IP{ 162 IPv6loopback, 163 IPv4(127, 0, 0, 1), 164 }, 165 testInetaddr, 166 &TCPAddr{IP: IPv6loopback, Port: 5682}, 167 nil, 168 }, 169 170 {nil, nil, testInetaddr, nil, errNoSuitableAddress}, 171 172 {ipv4only, nil, testInetaddr, nil, errNoSuitableAddress}, 173 {ipv4only, []IP{IPv6loopback}, testInetaddr, nil, errNoSuitableAddress}, 174 175 {ipv6only, nil, testInetaddr, nil, errNoSuitableAddress}, 176 {ipv6only, []IP{IPv4(127, 0, 0, 1)}, testInetaddr, nil, errNoSuitableAddress}, 177 } 178 179 func TestFirstFavoriteAddr(t *testing.T) { 180 if !supportsIPv4 || !supportsIPv6 { 181 t.Skip("ipv4 or ipv6 is not supported") 182 } 183 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) 188 } 189 if !reflect.DeepEqual(addr, tt.addr) { 190 t.Errorf("#%v: got %v; expected %v", i, addr, tt.addr) 191 } 192 } 193 }