You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.2 KiB

  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/internal/nettest"
  10. "golang.org/x/net/ipv6"
  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 "nacl", "plan9", "windows":
  24. t.Skipf("not supported on %s", runtime.GOOS)
  25. }
  26. if !supportsIPv6 {
  27. t.Skip("ipv6 is not supported")
  28. }
  29. ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  30. if ifi == nil {
  31. t.Skipf("not available on %s", runtime.GOOS)
  32. }
  33. m, ok := nettest.SupportsRawIPSocket()
  34. for _, tt := range packetConnMulticastSocketOptionTests {
  35. if tt.net == "ip6" && !ok {
  36. t.Log(m)
  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. const hoplim = 255
  67. if err := c.SetMulticastHopLimit(hoplim); err != nil {
  68. t.Error(err)
  69. return
  70. }
  71. if v, err := c.MulticastHopLimit(); err != nil {
  72. t.Error(err)
  73. return
  74. } else if v != hoplim {
  75. t.Errorf("got %v; want %v", v, hoplim)
  76. return
  77. }
  78. for _, toggle := range []bool{true, false} {
  79. if err := c.SetMulticastLoopback(toggle); err != nil {
  80. t.Error(err)
  81. return
  82. }
  83. if v, err := c.MulticastLoopback(); err != nil {
  84. t.Error(err)
  85. return
  86. } else if v != toggle {
  87. t.Errorf("got %v; want %v", v, toggle)
  88. return
  89. }
  90. }
  91. if err := c.JoinGroup(ifi, grp); err != nil {
  92. t.Error(err)
  93. return
  94. }
  95. if err := c.LeaveGroup(ifi, grp); err != nil {
  96. t.Error(err)
  97. return
  98. }
  99. }
  100. func testSourceSpecificMulticastSocketOptions(t *testing.T, c testIPv6MulticastConn, ifi *net.Interface, grp, src net.Addr) {
  101. // MCAST_JOIN_GROUP -> MCAST_BLOCK_SOURCE -> MCAST_UNBLOCK_SOURCE -> MCAST_LEAVE_GROUP
  102. if err := c.JoinGroup(ifi, grp); err != nil {
  103. t.Error(err)
  104. return
  105. }
  106. if err := c.ExcludeSourceSpecificGroup(ifi, grp, src); err != nil {
  107. switch runtime.GOOS {
  108. case "freebsd", "linux":
  109. default: // platforms that don't support MLDv2 fail here
  110. t.Logf("not supported on %s", runtime.GOOS)
  111. return
  112. }
  113. t.Error(err)
  114. return
  115. }
  116. if err := c.IncludeSourceSpecificGroup(ifi, grp, src); err != nil {
  117. t.Error(err)
  118. return
  119. }
  120. if err := c.LeaveGroup(ifi, grp); err != nil {
  121. t.Error(err)
  122. return
  123. }
  124. // MCAST_JOIN_SOURCE_GROUP -> MCAST_LEAVE_SOURCE_GROUP
  125. if err := c.JoinSourceSpecificGroup(ifi, grp, src); err != nil {
  126. t.Error(err)
  127. return
  128. }
  129. if err := c.LeaveSourceSpecificGroup(ifi, grp, src); err != nil {
  130. t.Error(err)
  131. return
  132. }
  133. // MCAST_JOIN_SOURCE_GROUP -> MCAST_LEAVE_GROUP
  134. if err := c.JoinSourceSpecificGroup(ifi, grp, src); err != nil {
  135. t.Error(err)
  136. return
  137. }
  138. if err := c.LeaveGroup(ifi, grp); err != nil {
  139. t.Error(err)
  140. return
  141. }
  142. }