multicastsockopt_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "net"
  7. "runtime"
  8. "testing"
  9. "golang.org/x/net/ipv6"
  10. "golang.org/x/net/nettest"
  11. )
  12. var packetConnMulticastSocketOptionTests = []struct {
  13. net, proto, addr string
  14. grp, src net.Addr
  15. }{
  16. {"udp6", "", "[ff02::]:0", &net.UDPAddr{IP: net.ParseIP("ff02::114")}, nil}, // see RFC 4727
  17. {"ip6", ":ipv6-icmp", "::", &net.IPAddr{IP: net.ParseIP("ff02::115")}, nil}, // see RFC 4727
  18. {"udp6", "", "[ff30::8000:0]:0", &net.UDPAddr{IP: net.ParseIP("ff30::8000:1")}, &net.UDPAddr{IP: net.IPv6loopback}}, // see RFC 5771
  19. {"ip6", ":ipv6-icmp", "::", &net.IPAddr{IP: net.ParseIP("ff30::8000:2")}, &net.IPAddr{IP: net.IPv6loopback}}, // see RFC 5771
  20. }
  21. func TestPacketConnMulticastSocketOptions(t *testing.T) {
  22. switch runtime.GOOS {
  23. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  24. t.Skipf("not supported on %s", runtime.GOOS)
  25. }
  26. if !nettest.SupportsIPv6() {
  27. t.Skip("ipv6 is not supported")
  28. }
  29. ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  30. if err != nil {
  31. t.Skipf("not available on %s", runtime.GOOS)
  32. }
  33. ok := nettest.SupportsRawSocket()
  34. for _, tt := range packetConnMulticastSocketOptionTests {
  35. if tt.net == "ip6" && !ok {
  36. t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  37. continue
  38. }
  39. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. defer c.Close()
  44. p := ipv6.NewPacketConn(c)
  45. defer p.Close()
  46. if tt.src == nil {
  47. testMulticastSocketOptions(t, p, ifi, tt.grp)
  48. } else {
  49. testSourceSpecificMulticastSocketOptions(t, p, ifi, tt.grp, tt.src)
  50. }
  51. }
  52. }
  53. type testIPv6MulticastConn interface {
  54. MulticastHopLimit() (int, error)
  55. SetMulticastHopLimit(ttl int) error
  56. MulticastLoopback() (bool, error)
  57. SetMulticastLoopback(bool) error
  58. JoinGroup(*net.Interface, net.Addr) error
  59. LeaveGroup(*net.Interface, net.Addr) error
  60. JoinSourceSpecificGroup(*net.Interface, net.Addr, net.Addr) error
  61. LeaveSourceSpecificGroup(*net.Interface, net.Addr, net.Addr) error
  62. ExcludeSourceSpecificGroup(*net.Interface, net.Addr, net.Addr) error
  63. IncludeSourceSpecificGroup(*net.Interface, net.Addr, net.Addr) error
  64. }
  65. func testMulticastSocketOptions(t *testing.T, c testIPv6MulticastConn, ifi *net.Interface, grp net.Addr) {
  66. t.Helper()
  67. const hoplim = 255
  68. if err := c.SetMulticastHopLimit(hoplim); err != nil {
  69. t.Error(err)
  70. return
  71. }
  72. if v, err := c.MulticastHopLimit(); err != nil {
  73. t.Error(err)
  74. return
  75. } else if v != hoplim {
  76. t.Errorf("got %v; want %v", v, hoplim)
  77. return
  78. }
  79. for _, toggle := range []bool{true, false} {
  80. if err := c.SetMulticastLoopback(toggle); err != nil {
  81. t.Error(err)
  82. return
  83. }
  84. if v, err := c.MulticastLoopback(); err != nil {
  85. t.Error(err)
  86. return
  87. } else if v != toggle {
  88. t.Errorf("got %v; want %v", v, toggle)
  89. return
  90. }
  91. }
  92. if err := c.JoinGroup(ifi, grp); err != nil {
  93. t.Error(err)
  94. return
  95. }
  96. if err := c.LeaveGroup(ifi, grp); err != nil {
  97. t.Error(err)
  98. return
  99. }
  100. }
  101. func testSourceSpecificMulticastSocketOptions(t *testing.T, c testIPv6MulticastConn, ifi *net.Interface, grp, src net.Addr) {
  102. t.Helper()
  103. // MCAST_JOIN_GROUP -> MCAST_BLOCK_SOURCE -> MCAST_UNBLOCK_SOURCE -> MCAST_LEAVE_GROUP
  104. if err := c.JoinGroup(ifi, grp); err != nil {
  105. t.Error(err)
  106. return
  107. }
  108. if err := c.ExcludeSourceSpecificGroup(ifi, grp, src); err != nil {
  109. switch runtime.GOOS {
  110. case "freebsd", "linux":
  111. default: // platforms that don't support MLDv2 fail here
  112. t.Logf("not supported on %s", runtime.GOOS)
  113. return
  114. }
  115. t.Error(err)
  116. return
  117. }
  118. if err := c.IncludeSourceSpecificGroup(ifi, grp, src); err != nil {
  119. t.Error(err)
  120. return
  121. }
  122. if err := c.LeaveGroup(ifi, grp); err != nil {
  123. t.Error(err)
  124. return
  125. }
  126. // MCAST_JOIN_SOURCE_GROUP -> MCAST_LEAVE_SOURCE_GROUP
  127. if err := c.JoinSourceSpecificGroup(ifi, grp, src); err != nil {
  128. t.Error(err)
  129. return
  130. }
  131. if err := c.LeaveSourceSpecificGroup(ifi, grp, src); err != nil {
  132. t.Error(err)
  133. return
  134. }
  135. // MCAST_JOIN_SOURCE_GROUP -> MCAST_LEAVE_GROUP
  136. if err := c.JoinSourceSpecificGroup(ifi, grp, src); err != nil {
  137. t.Error(err)
  138. return
  139. }
  140. if err := c.LeaveGroup(ifi, grp); err != nil {
  141. t.Error(err)
  142. return
  143. }
  144. }