unicastsockopt_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/iana"
  10. "golang.org/x/net/ipv6"
  11. "golang.org/x/net/nettest"
  12. )
  13. func TestConnUnicastSocketOptions(t *testing.T) {
  14. switch runtime.GOOS {
  15. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  16. t.Skipf("not supported on %s", runtime.GOOS)
  17. }
  18. if !nettest.SupportsIPv6() {
  19. t.Skip("ipv6 is not supported")
  20. }
  21. ln, err := net.Listen("tcp6", "[::1]:0")
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. defer ln.Close()
  26. errc := make(chan error, 1)
  27. go func() {
  28. c, err := ln.Accept()
  29. if err != nil {
  30. errc <- err
  31. return
  32. }
  33. errc <- c.Close()
  34. }()
  35. c, err := net.Dial("tcp6", ln.Addr().String())
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. defer c.Close()
  40. testUnicastSocketOptions(t, ipv6.NewConn(c))
  41. if err := <-errc; err != nil {
  42. t.Errorf("server: %v", err)
  43. }
  44. }
  45. var packetConnUnicastSocketOptionTests = []struct {
  46. net, proto, addr string
  47. }{
  48. {"udp6", "", "[::1]:0"},
  49. {"ip6", ":ipv6-icmp", "::1"},
  50. }
  51. func TestPacketConnUnicastSocketOptions(t *testing.T) {
  52. switch runtime.GOOS {
  53. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  54. t.Skipf("not supported on %s", runtime.GOOS)
  55. }
  56. if !nettest.SupportsIPv6() {
  57. t.Skip("ipv6 is not supported")
  58. }
  59. ok := nettest.SupportsRawSocket()
  60. for _, tt := range packetConnUnicastSocketOptionTests {
  61. if tt.net == "ip6" && !ok {
  62. t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  63. continue
  64. }
  65. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. defer c.Close()
  70. testUnicastSocketOptions(t, ipv6.NewPacketConn(c))
  71. }
  72. }
  73. type testIPv6UnicastConn interface {
  74. TrafficClass() (int, error)
  75. SetTrafficClass(int) error
  76. HopLimit() (int, error)
  77. SetHopLimit(int) error
  78. }
  79. func testUnicastSocketOptions(t *testing.T, c testIPv6UnicastConn) {
  80. t.Helper()
  81. tclass := iana.DiffServCS0 | iana.NotECNTransport
  82. if err := c.SetTrafficClass(tclass); err != nil {
  83. switch runtime.GOOS {
  84. case "darwin": // older darwin kernels don't support IPV6_TCLASS option
  85. t.Logf("not supported on %s", runtime.GOOS)
  86. goto next
  87. }
  88. t.Fatal(err)
  89. }
  90. if v, err := c.TrafficClass(); err != nil {
  91. t.Fatal(err)
  92. } else if v != tclass {
  93. t.Fatalf("got %v; want %v", v, tclass)
  94. }
  95. next:
  96. hoplim := 255
  97. if err := c.SetHopLimit(hoplim); err != nil {
  98. t.Fatal(err)
  99. }
  100. if v, err := c.HopLimit(); err != nil {
  101. t.Fatal(err)
  102. } else if v != hoplim {
  103. t.Fatalf("got %v; want %v", v, hoplim)
  104. }
  105. }