unicastsockopt_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "os"
  8. "runtime"
  9. "testing"
  10. "golang.org/x/net/internal/iana"
  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 %q", 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 %q", runtime.GOOS)
  46. }
  47. if !supportsIPv6 {
  48. t.Skip("ipv6 is not supported")
  49. }
  50. for _, tt := range packetConnUnicastSocketOptionTests {
  51. if tt.net == "ip6" && os.Getuid() != 0 {
  52. t.Skip("must be root")
  53. }
  54. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. defer c.Close()
  59. testUnicastSocketOptions(t, ipv6.NewPacketConn(c))
  60. }
  61. }
  62. type testIPv6UnicastConn interface {
  63. TrafficClass() (int, error)
  64. SetTrafficClass(int) error
  65. HopLimit() (int, error)
  66. SetHopLimit(int) error
  67. }
  68. func testUnicastSocketOptions(t *testing.T, c testIPv6UnicastConn) {
  69. tclass := iana.DiffServCS0 | iana.NotECNTransport
  70. if err := c.SetTrafficClass(tclass); err != nil {
  71. switch runtime.GOOS {
  72. case "darwin": // older darwin kernels don't support IPV6_TCLASS option
  73. t.Logf("not supported on %q", runtime.GOOS)
  74. goto next
  75. }
  76. t.Fatal(err)
  77. }
  78. if v, err := c.TrafficClass(); err != nil {
  79. t.Fatal(err)
  80. } else if v != tclass {
  81. t.Fatalf("got %v; want %v", v, tclass)
  82. }
  83. next:
  84. hoplim := 255
  85. if err := c.SetHopLimit(hoplim); err != nil {
  86. t.Fatal(err)
  87. }
  88. if v, err := c.HopLimit(); err != nil {
  89. t.Fatal(err)
  90. } else if v != hoplim {
  91. t.Fatalf("got %v; want %v", v, hoplim)
  92. }
  93. }