unicastsockopt_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "code.google.com/p/go.net/internal/iana"
  11. "code.google.com/p/go.net/ipv6"
  12. )
  13. func TestConnUnicastSocketOptions(t *testing.T) {
  14. switch runtime.GOOS {
  15. case "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.Fatalf("net.Listen failed: %v", 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.Fatalf("net.Dial failed: %v", 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 "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.Fatalf("net.ListenPacket(%q, %q) failed: %v", tt.net+tt.proto, tt.addr, 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. t.Fatalf("ipv6.Conn.SetTrafficClass failed: %v", err)
  72. }
  73. if v, err := c.TrafficClass(); err != nil {
  74. t.Fatalf("ipv6.Conn.TrafficClass failed: %v", err)
  75. } else if v != tclass {
  76. t.Fatalf("got unexpected traffic class %v; expected %v", v, tclass)
  77. }
  78. hoplim := 255
  79. if err := c.SetHopLimit(hoplim); err != nil {
  80. t.Fatalf("ipv6.Conn.SetHopLimit failed: %v", err)
  81. }
  82. if v, err := c.HopLimit(); err != nil {
  83. t.Fatalf("ipv6.Conn.HopLimit failed: %v", err)
  84. } else if v != hoplim {
  85. t.Fatalf("got unexpected hop limit %v; expected %v", v, hoplim)
  86. }
  87. }