multicastlistener_test.go 5.4 KB

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