unicast_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/ipv6"
  15. "golang.org/x/net/nettest"
  16. )
  17. func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
  18. switch runtime.GOOS {
  19. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  20. t.Skipf("not supported on %s", runtime.GOOS)
  21. }
  22. if !nettest.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 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 "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  74. t.Skipf("not supported on %s", runtime.GOOS)
  75. }
  76. if !nettest.SupportsIPv6() {
  77. t.Skip("ipv6 is not supported")
  78. }
  79. if !nettest.SupportsRawSocket() {
  80. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  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. // AIX, Illumos and Solaris never allow
  115. // modification of ICMP properties.
  116. switch runtime.GOOS {
  117. case "aix", "illumos", "solaris":
  118. default:
  119. t.Fatal(err)
  120. }
  121. }
  122. } else {
  123. psh = pshicmp
  124. // Some platforms never allow to disable the
  125. // kernel checksum processing.
  126. p.SetChecksum(false, -1)
  127. }
  128. wb, err := (&icmp.Message{
  129. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  130. Body: &icmp.Echo{
  131. ID: os.Getpid() & 0xffff, Seq: i + 1,
  132. Data: []byte("HELLO-R-U-THERE"),
  133. },
  134. }).Marshal(psh)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. if err := p.SetControlMessage(cf, toggle); err != nil {
  139. if protocolNotSupported(err) {
  140. t.Logf("not supported on %s", runtime.GOOS)
  141. continue
  142. }
  143. t.Fatal(err)
  144. }
  145. cm.HopLimit = i + 1
  146. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  147. t.Fatal(err)
  148. }
  149. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  150. t.Fatal(err)
  151. } else if n != len(wb) {
  152. t.Fatalf("got %v; want %v", n, len(wb))
  153. }
  154. rb := make([]byte, 128)
  155. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  156. t.Fatal(err)
  157. }
  158. if n, _, _, err := p.ReadFrom(rb); err != nil {
  159. switch runtime.GOOS {
  160. case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
  161. t.Logf("not supported on %s", runtime.GOOS)
  162. continue
  163. }
  164. t.Fatal(err)
  165. } else {
  166. if m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, rb[:n]); err != nil {
  167. t.Fatal(err)
  168. } else if m.Type != ipv6.ICMPTypeEchoReply || m.Code != 0 {
  169. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv6.ICMPTypeEchoReply, 0)
  170. }
  171. }
  172. }
  173. }