multicastlistener_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 ipv6_test
  5. import (
  6. "fmt"
  7. "net"
  8. "runtime"
  9. "testing"
  10. "golang.org/x/net/internal/nettest"
  11. "golang.org/x/net/ipv6"
  12. )
  13. var udpMultipleGroupListenerTests = []net.Addr{
  14. &net.UDPAddr{IP: net.ParseIP("ff02::114")}, // see RFC 4727
  15. &net.UDPAddr{IP: net.ParseIP("ff02::1:114")},
  16. &net.UDPAddr{IP: net.ParseIP("ff02::2:114")},
  17. }
  18. func TestUDPSinglePacketConnWithMultipleGroupListeners(t *testing.T) {
  19. switch runtime.GOOS {
  20. case "nacl", "plan9", "windows":
  21. t.Skipf("not supported on %s", runtime.GOOS)
  22. }
  23. if !supportsIPv6 {
  24. t.Skip("ipv6 is not supported")
  25. }
  26. for _, gaddr := range udpMultipleGroupListenerTests {
  27. c, err := net.ListenPacket("udp6", "[::]:0") // wildcard address with non-reusable port
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. defer c.Close()
  32. p := ipv6.NewPacketConn(c)
  33. var mift []*net.Interface
  34. ift, err := net.Interfaces()
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. for i, ifi := range ift {
  39. if _, ok := nettest.IsMulticastCapable("ip6", &ifi); !ok {
  40. continue
  41. }
  42. if err := p.JoinGroup(&ifi, gaddr); err != nil {
  43. t.Fatal(err)
  44. }
  45. mift = append(mift, &ift[i])
  46. }
  47. for _, ifi := range mift {
  48. if err := p.LeaveGroup(ifi, gaddr); err != nil {
  49. t.Fatal(err)
  50. }
  51. }
  52. }
  53. }
  54. func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) {
  55. switch runtime.GOOS {
  56. case "nacl", "plan9", "windows":
  57. t.Skipf("not supported on %s", runtime.GOOS)
  58. }
  59. if !supportsIPv6 {
  60. t.Skip("ipv6 is not supported")
  61. }
  62. for _, gaddr := range udpMultipleGroupListenerTests {
  63. c1, err := net.ListenPacket("udp6", "[ff02::]:1024") // wildcard address with reusable port
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. defer c1.Close()
  68. c2, err := net.ListenPacket("udp6", "[ff02::]:1024") // wildcard address with reusable port
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. defer c2.Close()
  73. var ps [2]*ipv6.PacketConn
  74. ps[0] = ipv6.NewPacketConn(c1)
  75. ps[1] = ipv6.NewPacketConn(c2)
  76. var mift []*net.Interface
  77. ift, err := net.Interfaces()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. for i, ifi := range ift {
  82. if _, ok := nettest.IsMulticastCapable("ip6", &ifi); !ok {
  83. continue
  84. }
  85. for _, p := range ps {
  86. if err := p.JoinGroup(&ifi, gaddr); err != nil {
  87. t.Fatal(err)
  88. }
  89. }
  90. mift = append(mift, &ift[i])
  91. }
  92. for _, ifi := range mift {
  93. for _, p := range ps {
  94. if err := p.LeaveGroup(ifi, gaddr); err != nil {
  95. t.Fatal(err)
  96. }
  97. }
  98. }
  99. }
  100. }
  101. func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
  102. switch runtime.GOOS {
  103. case "nacl", "plan9", "windows":
  104. t.Skipf("not supported on %s", runtime.GOOS)
  105. }
  106. if !supportsIPv6 {
  107. t.Skip("ipv6 is not supported")
  108. }
  109. gaddr := net.IPAddr{IP: net.ParseIP("ff02::114")} // see RFC 4727
  110. type ml struct {
  111. c *ipv6.PacketConn
  112. ifi *net.Interface
  113. }
  114. var mlt []*ml
  115. ift, err := net.Interfaces()
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. for i, ifi := range ift {
  120. ip, ok := nettest.IsMulticastCapable("ip6", &ifi)
  121. if !ok {
  122. continue
  123. }
  124. c, err := net.ListenPacket("udp6", fmt.Sprintf("[%s%%%s]:1024", ip.String(), ifi.Name)) // unicast address with non-reusable port
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. defer c.Close()
  129. p := ipv6.NewPacketConn(c)
  130. if err := p.JoinGroup(&ifi, &gaddr); err != nil {
  131. t.Fatal(err)
  132. }
  133. mlt = append(mlt, &ml{p, &ift[i]})
  134. }
  135. for _, m := range mlt {
  136. if err := m.c.LeaveGroup(m.ifi, &gaddr); err != nil {
  137. t.Fatal(err)
  138. }
  139. }
  140. }
  141. func TestIPSinglePacketConnWithSingleGroupListener(t *testing.T) {
  142. switch runtime.GOOS {
  143. case "nacl", "plan9", "windows":
  144. t.Skipf("not supported on %s", runtime.GOOS)
  145. }
  146. if !supportsIPv6 {
  147. t.Skip("ipv6 is not supported")
  148. }
  149. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  150. t.Skip(m)
  151. }
  152. c, err := net.ListenPacket("ip6:ipv6-icmp", "::") // wildcard address
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. defer c.Close()
  157. p := ipv6.NewPacketConn(c)
  158. gaddr := net.IPAddr{IP: net.ParseIP("ff02::114")} // see RFC 4727
  159. var mift []*net.Interface
  160. ift, err := net.Interfaces()
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. for i, ifi := range ift {
  165. if _, ok := nettest.IsMulticastCapable("ip6", &ifi); !ok {
  166. continue
  167. }
  168. if err := p.JoinGroup(&ifi, &gaddr); err != nil {
  169. t.Fatal(err)
  170. }
  171. mift = append(mift, &ift[i])
  172. }
  173. for _, ifi := range mift {
  174. if err := p.LeaveGroup(ifi, &gaddr); err != nil {
  175. t.Fatal(err)
  176. }
  177. }
  178. }
  179. func TestIPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
  180. switch runtime.GOOS {
  181. case "darwin", "dragonfly", "openbsd": // platforms that return fe80::1%lo0: bind: can't assign requested address
  182. t.Skipf("not supported on %s", runtime.GOOS)
  183. case "nacl", "plan9", "windows":
  184. t.Skipf("not supported on %s", runtime.GOOS)
  185. }
  186. if !supportsIPv6 {
  187. t.Skip("ipv6 is not supported")
  188. }
  189. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  190. t.Skip(m)
  191. }
  192. gaddr := net.IPAddr{IP: net.ParseIP("ff02::114")} // see RFC 4727
  193. type ml struct {
  194. c *ipv6.PacketConn
  195. ifi *net.Interface
  196. }
  197. var mlt []*ml
  198. ift, err := net.Interfaces()
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. for i, ifi := range ift {
  203. ip, ok := nettest.IsMulticastCapable("ip6", &ifi)
  204. if !ok {
  205. continue
  206. }
  207. c, err := net.ListenPacket("ip6:ipv6-icmp", fmt.Sprintf("%s%%%s", ip.String(), ifi.Name)) // unicast address
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. defer c.Close()
  212. p := ipv6.NewPacketConn(c)
  213. if err := p.JoinGroup(&ifi, &gaddr); err != nil {
  214. t.Fatal(err)
  215. }
  216. mlt = append(mlt, &ml{p, &ift[i]})
  217. }
  218. for _, m := range mlt {
  219. if err := m.c.LeaveGroup(m.ifi, &gaddr); err != nil {
  220. t.Fatal(err)
  221. }
  222. }
  223. }