sockopt_test.go 3.0 KB

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