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.

133 lines
2.9 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. "fmt"
  7. "net"
  8. "runtime"
  9. "testing"
  10. "golang.org/x/net/internal/iana"
  11. "golang.org/x/net/internal/nettest"
  12. "golang.org/x/net/ipv6"
  13. )
  14. var supportsIPv6 bool = nettest.SupportsIPv6()
  15. func TestConnInitiatorPathMTU(t *testing.T) {
  16. switch runtime.GOOS {
  17. case "nacl", "plan9", "windows":
  18. t.Skipf("not supported on %s", runtime.GOOS)
  19. }
  20. if !supportsIPv6 {
  21. t.Skip("ipv6 is not supported")
  22. }
  23. ln, err := net.Listen("tcp6", "[::1]:0")
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. defer ln.Close()
  28. done := make(chan bool)
  29. go acceptor(t, ln, done)
  30. c, err := net.Dial("tcp6", ln.Addr().String())
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. defer c.Close()
  35. if pmtu, err := ipv6.NewConn(c).PathMTU(); err != nil {
  36. switch runtime.GOOS {
  37. case "darwin": // older darwin kernels don't support IPV6_PATHMTU option
  38. t.Logf("not supported on %s", runtime.GOOS)
  39. default:
  40. t.Fatal(err)
  41. }
  42. } else {
  43. t.Logf("path mtu for %v: %v", c.RemoteAddr(), pmtu)
  44. }
  45. <-done
  46. }
  47. func TestConnResponderPathMTU(t *testing.T) {
  48. switch runtime.GOOS {
  49. case "nacl", "plan9", "windows":
  50. t.Skipf("not supported on %s", runtime.GOOS)
  51. }
  52. if !supportsIPv6 {
  53. t.Skip("ipv6 is not supported")
  54. }
  55. ln, err := net.Listen("tcp6", "[::1]:0")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. defer ln.Close()
  60. done := make(chan bool)
  61. go connector(t, "tcp6", ln.Addr().String(), done)
  62. c, err := ln.Accept()
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. defer c.Close()
  67. if pmtu, err := ipv6.NewConn(c).PathMTU(); err != nil {
  68. switch runtime.GOOS {
  69. case "darwin": // older darwin kernels don't support IPV6_PATHMTU option
  70. t.Logf("not supported on %s", runtime.GOOS)
  71. default:
  72. t.Fatal(err)
  73. }
  74. } else {
  75. t.Logf("path mtu for %v: %v", c.RemoteAddr(), pmtu)
  76. }
  77. <-done
  78. }
  79. func TestPacketConnChecksum(t *testing.T) {
  80. switch runtime.GOOS {
  81. case "nacl", "plan9", "windows":
  82. t.Skipf("not supported on %s", runtime.GOOS)
  83. }
  84. if !supportsIPv6 {
  85. t.Skip("ipv6 is not supported")
  86. }
  87. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  88. t.Skip(m)
  89. }
  90. c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolOSPFIGP), "::") // OSPF for IPv6
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. defer c.Close()
  95. p := ipv6.NewPacketConn(c)
  96. offset := 12 // see RFC 5340
  97. for _, toggle := range []bool{false, true} {
  98. if err := p.SetChecksum(toggle, offset); err != nil {
  99. if toggle {
  100. t.Fatalf("ipv6.PacketConn.SetChecksum(%v, %v) failed: %v", toggle, offset, err)
  101. } else {
  102. // Some platforms never allow to disable the kernel
  103. // checksum processing.
  104. t.Logf("ipv6.PacketConn.SetChecksum(%v, %v) failed: %v", toggle, offset, err)
  105. }
  106. }
  107. if on, offset, err := p.Checksum(); err != nil {
  108. t.Fatal(err)
  109. } else {
  110. t.Logf("kernel checksum processing enabled=%v, offset=%v", on, offset)
  111. }
  112. }
  113. }