multicastsockopt_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 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 "nacl", "plan9":
  24. t.Skipf("not supported on %s", runtime.GOOS)
  25. }
  26. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  27. if ifi == nil {
  28. t.Skipf("not available on %s", runtime.GOOS)
  29. }
  30. m, ok := nettest.SupportsRawIPSocket()
  31. for _, tt := range packetConnMulticastSocketOptionTests {
  32. if tt.net == "ip4" && !ok {
  33. t.Log(m)
  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 "nacl", "plan9":
  59. t.Skipf("not supported on %s", runtime.GOOS)
  60. }
  61. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  62. t.Skip(m)
  63. }
  64. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  65. if ifi == 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. const ttl = 255
  100. if err := c.SetMulticastTTL(ttl); err != nil {
  101. t.Error(err)
  102. return
  103. }
  104. if v, err := c.MulticastTTL(); err != nil {
  105. t.Error(err)
  106. return
  107. } else if v != ttl {
  108. t.Errorf("got %v; want %v", v, ttl)
  109. return
  110. }
  111. for _, toggle := range []bool{true, false} {
  112. if err := c.SetMulticastLoopback(toggle); err != nil {
  113. t.Error(err)
  114. return
  115. }
  116. if v, err := c.MulticastLoopback(); err != nil {
  117. t.Error(err)
  118. return
  119. } else if v != toggle {
  120. t.Errorf("got %v; want %v", v, toggle)
  121. return
  122. }
  123. }
  124. if err := c.JoinGroup(ifi, grp); err != nil {
  125. t.Error(err)
  126. return
  127. }
  128. if err := c.LeaveGroup(ifi, grp); err != nil {
  129. t.Error(err)
  130. return
  131. }
  132. }
  133. func testSourceSpecificMulticastSocketOptions(t *testing.T, c testIPv4MulticastConn, ifi *net.Interface, grp, src net.Addr) {
  134. // MCAST_JOIN_GROUP -> MCAST_BLOCK_SOURCE -> MCAST_UNBLOCK_SOURCE -> MCAST_LEAVE_GROUP
  135. if err := c.JoinGroup(ifi, grp); err != nil {
  136. t.Error(err)
  137. return
  138. }
  139. if err := c.ExcludeSourceSpecificGroup(ifi, grp, src); err != nil {
  140. switch runtime.GOOS {
  141. case "freebsd", "linux":
  142. default: // platforms that don't support IGMPv2/3 fail here
  143. t.Logf("not supported on %s", runtime.GOOS)
  144. return
  145. }
  146. t.Error(err)
  147. return
  148. }
  149. if err := c.IncludeSourceSpecificGroup(ifi, grp, src); err != nil {
  150. t.Error(err)
  151. return
  152. }
  153. if err := c.LeaveGroup(ifi, grp); err != nil {
  154. t.Error(err)
  155. return
  156. }
  157. // MCAST_JOIN_SOURCE_GROUP -> MCAST_LEAVE_SOURCE_GROUP
  158. if err := c.JoinSourceSpecificGroup(ifi, grp, src); err != nil {
  159. t.Error(err)
  160. return
  161. }
  162. if err := c.LeaveSourceSpecificGroup(ifi, grp, src); err != nil {
  163. t.Error(err)
  164. return
  165. }
  166. // MCAST_JOIN_SOURCE_GROUP -> MCAST_LEAVE_GROUP
  167. if err := c.JoinSourceSpecificGroup(ifi, grp, src); err != nil {
  168. t.Error(err)
  169. return
  170. }
  171. if err := c.LeaveGroup(ifi, grp); err != nil {
  172. t.Error(err)
  173. return
  174. }
  175. }