multicastsockopt_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/ipv4"
  10. "golang.org/x/net/nettest"
  11. )
  12. var packetConnMulticastSocketOptionTests = []struct {
  13. net, proto, addr string
  14. grp, src net.Addr
  15. }{
  16. {"udp4", "", "224.0.0.0:0", &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}, nil}, // see RFC 4727
  17. {"ip4", ":icmp", "0.0.0.0", &net.IPAddr{IP: net.IPv4(224, 0, 0, 250)}, nil}, // see RFC 4727
  18. {"udp4", "", "232.0.0.0:0", &net.UDPAddr{IP: net.IPv4(232, 0, 1, 249)}, &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}}, // see RFC 5771
  19. {"ip4", ":icmp", "0.0.0.0", &net.IPAddr{IP: net.IPv4(232, 0, 1, 250)}, &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}}, // see RFC 5771
  20. }
  21. func TestPacketConnMulticastSocketOptions(t *testing.T) {
  22. switch runtime.GOOS {
  23. case "fuchsia", "hurd", "js", "nacl", "plan9":
  24. t.Skipf("not supported on %s", runtime.GOOS)
  25. }
  26. ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  27. if err != nil {
  28. t.Skipf("not available on %s", runtime.GOOS)
  29. }
  30. ok := nettest.SupportsRawSocket()
  31. for _, tt := range packetConnMulticastSocketOptionTests {
  32. if tt.net == "ip4" && !ok {
  33. t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  34. continue
  35. }
  36. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. defer c.Close()
  41. p := ipv4.NewPacketConn(c)
  42. defer p.Close()
  43. if tt.src == nil {
  44. testMulticastSocketOptions(t, p, ifi, tt.grp)
  45. } else {
  46. testSourceSpecificMulticastSocketOptions(t, p, ifi, tt.grp, tt.src)
  47. }
  48. }
  49. }
  50. var rawConnMulticastSocketOptionTests = []struct {
  51. grp, src net.Addr
  52. }{
  53. {&net.IPAddr{IP: net.IPv4(224, 0, 0, 250)}, nil}, // see RFC 4727
  54. {&net.IPAddr{IP: net.IPv4(232, 0, 1, 250)}, &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}}, // see RFC 5771
  55. }
  56. func TestRawConnMulticastSocketOptions(t *testing.T) {
  57. switch runtime.GOOS {
  58. case "fuchsia", "hurd", "js", "nacl", "plan9":
  59. t.Skipf("not supported on %s", runtime.GOOS)
  60. }
  61. if !nettest.SupportsRawSocket() {
  62. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  63. }
  64. ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  65. if err != nil {
  66. t.Skipf("not available on %s", runtime.GOOS)
  67. }
  68. for _, tt := range rawConnMulticastSocketOptionTests {
  69. c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. defer c.Close()
  74. r, err := ipv4.NewRawConn(c)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. defer r.Close()
  79. if tt.src == nil {
  80. testMulticastSocketOptions(t, r, ifi, tt.grp)
  81. } else {
  82. testSourceSpecificMulticastSocketOptions(t, r, ifi, tt.grp, tt.src)
  83. }
  84. }
  85. }
  86. type testIPv4MulticastConn interface {
  87. MulticastTTL() (int, error)
  88. SetMulticastTTL(ttl int) error
  89. MulticastLoopback() (bool, error)
  90. SetMulticastLoopback(bool) error
  91. JoinGroup(*net.Interface, net.Addr) error
  92. LeaveGroup(*net.Interface, net.Addr) error
  93. JoinSourceSpecificGroup(*net.Interface, net.Addr, net.Addr) error
  94. LeaveSourceSpecificGroup(*net.Interface, net.Addr, net.Addr) error
  95. ExcludeSourceSpecificGroup(*net.Interface, net.Addr, net.Addr) error
  96. IncludeSourceSpecificGroup(*net.Interface, net.Addr, net.Addr) error
  97. }
  98. func testMulticastSocketOptions(t *testing.T, c testIPv4MulticastConn, ifi *net.Interface, grp net.Addr) {
  99. t.Helper()
  100. const ttl = 255
  101. if err := c.SetMulticastTTL(ttl); err != nil {
  102. t.Error(err)
  103. return
  104. }
  105. if v, err := c.MulticastTTL(); err != nil {
  106. t.Error(err)
  107. return
  108. } else if v != ttl {
  109. t.Errorf("got %v; want %v", v, ttl)
  110. return
  111. }
  112. for _, toggle := range []bool{true, false} {
  113. if err := c.SetMulticastLoopback(toggle); err != nil {
  114. t.Error(err)
  115. return
  116. }
  117. if v, err := c.MulticastLoopback(); err != nil {
  118. t.Error(err)
  119. return
  120. } else if v != toggle {
  121. t.Errorf("got %v; want %v", v, toggle)
  122. return
  123. }
  124. }
  125. if err := c.JoinGroup(ifi, grp); err != nil {
  126. t.Error(err)
  127. return
  128. }
  129. if err := c.LeaveGroup(ifi, grp); err != nil {
  130. t.Error(err)
  131. return
  132. }
  133. }
  134. func testSourceSpecificMulticastSocketOptions(t *testing.T, c testIPv4MulticastConn, ifi *net.Interface, grp, src net.Addr) {
  135. t.Helper()
  136. // MCAST_JOIN_GROUP -> MCAST_BLOCK_SOURCE -> MCAST_UNBLOCK_SOURCE -> MCAST_LEAVE_GROUP
  137. if err := c.JoinGroup(ifi, grp); err != nil {
  138. t.Error(err)
  139. return
  140. }
  141. if err := c.ExcludeSourceSpecificGroup(ifi, grp, src); err != nil {
  142. switch runtime.GOOS {
  143. case "freebsd", "linux":
  144. default: // platforms that don't support IGMPv2/3 fail here
  145. t.Logf("not supported on %s", runtime.GOOS)
  146. return
  147. }
  148. t.Error(err)
  149. return
  150. }
  151. if err := c.IncludeSourceSpecificGroup(ifi, grp, src); err != nil {
  152. t.Error(err)
  153. return
  154. }
  155. if err := c.LeaveGroup(ifi, grp); err != nil {
  156. t.Error(err)
  157. return
  158. }
  159. // MCAST_JOIN_SOURCE_GROUP -> MCAST_LEAVE_SOURCE_GROUP
  160. if err := c.JoinSourceSpecificGroup(ifi, grp, src); err != nil {
  161. t.Error(err)
  162. return
  163. }
  164. if err := c.LeaveSourceSpecificGroup(ifi, grp, src); err != nil {
  165. t.Error(err)
  166. return
  167. }
  168. // MCAST_JOIN_SOURCE_GROUP -> MCAST_LEAVE_GROUP
  169. if err := c.JoinSourceSpecificGroup(ifi, grp, src); err != nil {
  170. t.Error(err)
  171. return
  172. }
  173. if err := c.LeaveGroup(ifi, grp); err != nil {
  174. t.Error(err)
  175. return
  176. }
  177. }