No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

123 líneas
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 "aix", "fuchsia", "hurd", "js", "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 "aix", "fuchsia", "hurd", "js", "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. 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. }