unicastsockopt_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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", "solaris", "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. done := make(chan bool)
  27. go acceptor(t, ln, done)
  28. c, err := net.Dial("tcp6", ln.Addr().String())
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. defer c.Close()
  33. testUnicastSocketOptions(t, ipv6.NewConn(c))
  34. <-done
  35. }
  36. var packetConnUnicastSocketOptionTests = []struct {
  37. net, proto, addr string
  38. }{
  39. {"udp6", "", "[::1]:0"},
  40. {"ip6", ":ipv6-icmp", "::1"},
  41. }
  42. func TestPacketConnUnicastSocketOptions(t *testing.T) {
  43. switch runtime.GOOS {
  44. case "nacl", "plan9", "solaris", "windows":
  45. t.Skipf("not supported on %s", runtime.GOOS)
  46. }
  47. if !supportsIPv6 {
  48. t.Skip("ipv6 is not supported")
  49. }
  50. m, ok := nettest.SupportsRawIPSocket()
  51. for _, tt := range packetConnUnicastSocketOptionTests {
  52. if tt.net == "ip6" && !ok {
  53. t.Log(m)
  54. continue
  55. }
  56. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. defer c.Close()
  61. testUnicastSocketOptions(t, ipv6.NewPacketConn(c))
  62. }
  63. }
  64. type testIPv6UnicastConn interface {
  65. TrafficClass() (int, error)
  66. SetTrafficClass(int) error
  67. HopLimit() (int, error)
  68. SetHopLimit(int) error
  69. }
  70. func testUnicastSocketOptions(t *testing.T, c testIPv6UnicastConn) {
  71. tclass := iana.DiffServCS0 | iana.NotECNTransport
  72. if err := c.SetTrafficClass(tclass); err != nil {
  73. switch runtime.GOOS {
  74. case "darwin": // older darwin kernels don't support IPV6_TCLASS option
  75. t.Logf("not supported on %s", runtime.GOOS)
  76. goto next
  77. }
  78. t.Fatal(err)
  79. }
  80. if v, err := c.TrafficClass(); err != nil {
  81. t.Fatal(err)
  82. } else if v != tclass {
  83. t.Fatalf("got %v; want %v", v, tclass)
  84. }
  85. next:
  86. hoplim := 255
  87. if err := c.SetHopLimit(hoplim); err != nil {
  88. t.Fatal(err)
  89. }
  90. if v, err := c.HopLimit(); err != nil {
  91. t.Fatal(err)
  92. } else if v != hoplim {
  93. t.Fatalf("got %v; want %v", v, hoplim)
  94. }
  95. }