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.

120 lines
2.5 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. "net"
  7. "runtime"
  8. "testing"
  9. "golang.org/x/net/internal/iana"
  10. "golang.org/x/net/internal/nettest"
  11. "golang.org/x/net/ipv6"
  12. )
  13. func TestConnUnicastSocketOptions(t *testing.T) {
  14. switch runtime.GOOS {
  15. case "nacl", "plan9", "windows":
  16. t.Skipf("not supported on %s", runtime.GOOS)
  17. }
  18. if !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 "nacl", "plan9", "windows":
  54. t.Skipf("not supported on %s", runtime.GOOS)
  55. }
  56. if !supportsIPv6 {
  57. t.Skip("ipv6 is not supported")
  58. }
  59. m, ok := nettest.SupportsRawIPSocket()
  60. for _, tt := range packetConnUnicastSocketOptionTests {
  61. if tt.net == "ip6" && !ok {
  62. t.Log(m)
  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. tclass := iana.DiffServCS0 | iana.NotECNTransport
  81. if err := c.SetTrafficClass(tclass); err != nil {
  82. switch runtime.GOOS {
  83. case "darwin": // older darwin kernels don't support IPV6_TCLASS option
  84. t.Logf("not supported on %s", runtime.GOOS)
  85. goto next
  86. }
  87. t.Fatal(err)
  88. }
  89. if v, err := c.TrafficClass(); err != nil {
  90. t.Fatal(err)
  91. } else if v != tclass {
  92. t.Fatalf("got %v; want %v", v, tclass)
  93. }
  94. next:
  95. hoplim := 255
  96. if err := c.SetHopLimit(hoplim); err != nil {
  97. t.Fatal(err)
  98. }
  99. if v, err := c.HopLimit(); err != nil {
  100. t.Fatal(err)
  101. } else if v != hoplim {
  102. t.Fatalf("got %v; want %v", v, hoplim)
  103. }
  104. }