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.

184 lines
4.7 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. "bytes"
  7. "net"
  8. "os"
  9. "runtime"
  10. "testing"
  11. "time"
  12. "golang.org/x/net/icmp"
  13. "golang.org/x/net/internal/iana"
  14. "golang.org/x/net/internal/nettest"
  15. "golang.org/x/net/ipv6"
  16. )
  17. func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
  18. switch runtime.GOOS {
  19. case "nacl", "plan9", "windows":
  20. t.Skipf("not supported on %s", runtime.GOOS)
  21. }
  22. if !supportsIPv6 {
  23. t.Skip("ipv6 is not supported")
  24. }
  25. c, err := nettest.NewLocalPacketListener("udp6")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. defer c.Close()
  30. p := ipv6.NewPacketConn(c)
  31. defer p.Close()
  32. dst := c.LocalAddr()
  33. cm := ipv6.ControlMessage{
  34. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  35. Src: net.IPv6loopback,
  36. }
  37. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  38. ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  39. if ifi != nil {
  40. cm.IfIndex = ifi.Index
  41. }
  42. wb := []byte("HELLO-R-U-THERE")
  43. for i, toggle := range []bool{true, false, true} {
  44. if err := p.SetControlMessage(cf, toggle); err != nil {
  45. if nettest.ProtocolNotSupported(err) {
  46. t.Logf("not supported on %s", runtime.GOOS)
  47. continue
  48. }
  49. t.Fatal(err)
  50. }
  51. cm.HopLimit = i + 1
  52. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  53. t.Fatal(err)
  54. }
  55. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  56. t.Fatal(err)
  57. } else if n != len(wb) {
  58. t.Fatalf("got %v; want %v", n, len(wb))
  59. }
  60. rb := make([]byte, 128)
  61. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  62. t.Fatal(err)
  63. }
  64. if n, _, _, err := p.ReadFrom(rb); err != nil {
  65. t.Fatal(err)
  66. } else if !bytes.Equal(rb[:n], wb) {
  67. t.Fatalf("got %v; want %v", rb[:n], wb)
  68. }
  69. }
  70. }
  71. func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
  72. switch runtime.GOOS {
  73. case "nacl", "plan9", "windows":
  74. t.Skipf("not supported on %s", runtime.GOOS)
  75. }
  76. if !supportsIPv6 {
  77. t.Skip("ipv6 is not supported")
  78. }
  79. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  80. t.Skip(m)
  81. }
  82. c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. defer c.Close()
  87. p := ipv6.NewPacketConn(c)
  88. defer p.Close()
  89. dst, err := net.ResolveIPAddr("ip6", "::1")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. pshicmp := icmp.IPv6PseudoHeader(c.LocalAddr().(*net.IPAddr).IP, dst.IP)
  94. cm := ipv6.ControlMessage{
  95. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  96. Src: net.IPv6loopback,
  97. }
  98. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  99. ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  100. if ifi != nil {
  101. cm.IfIndex = ifi.Index
  102. }
  103. var f ipv6.ICMPFilter
  104. f.SetAll(true)
  105. f.Accept(ipv6.ICMPTypeEchoReply)
  106. if err := p.SetICMPFilter(&f); err != nil {
  107. t.Fatal(err)
  108. }
  109. var psh []byte
  110. for i, toggle := range []bool{true, false, true} {
  111. if toggle {
  112. psh = nil
  113. if err := p.SetChecksum(true, 2); err != nil {
  114. // Solaris never allows to modify
  115. // ICMP properties.
  116. if runtime.GOOS != "solaris" {
  117. t.Fatal(err)
  118. }
  119. }
  120. } else {
  121. psh = pshicmp
  122. // Some platforms never allow to disable the
  123. // kernel checksum processing.
  124. p.SetChecksum(false, -1)
  125. }
  126. wb, err := (&icmp.Message{
  127. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  128. Body: &icmp.Echo{
  129. ID: os.Getpid() & 0xffff, Seq: i + 1,
  130. Data: []byte("HELLO-R-U-THERE"),
  131. },
  132. }).Marshal(psh)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if err := p.SetControlMessage(cf, toggle); err != nil {
  137. if nettest.ProtocolNotSupported(err) {
  138. t.Logf("not supported on %s", runtime.GOOS)
  139. continue
  140. }
  141. t.Fatal(err)
  142. }
  143. cm.HopLimit = i + 1
  144. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  145. t.Fatal(err)
  146. }
  147. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  148. t.Fatal(err)
  149. } else if n != len(wb) {
  150. t.Fatalf("got %v; want %v", n, len(wb))
  151. }
  152. rb := make([]byte, 128)
  153. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  154. t.Fatal(err)
  155. }
  156. if n, _, _, err := p.ReadFrom(rb); err != nil {
  157. switch runtime.GOOS {
  158. case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
  159. t.Logf("not supported on %s", runtime.GOOS)
  160. continue
  161. }
  162. t.Fatal(err)
  163. } else {
  164. if m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, rb[:n]); err != nil {
  165. t.Fatal(err)
  166. } else if m.Type != ipv6.ICMPTypeEchoReply || m.Code != 0 {
  167. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv6.ICMPTypeEchoReply, 0)
  168. }
  169. }
  170. }
  171. }