ipsock_test.go 3.3 KB

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