unicastsockopt_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "code.google.com/p/go.net/ipv6"
  7. "net"
  8. "os"
  9. "runtime"
  10. "testing"
  11. )
  12. func TestConnUnicastSocketOptions(t *testing.T) {
  13. switch runtime.GOOS {
  14. case "plan9", "windows":
  15. t.Skipf("not supported on %q", runtime.GOOS)
  16. }
  17. if !supportsIPv6 {
  18. t.Skip("ipv6 is not supported")
  19. }
  20. ln, err := net.Listen("tcp6", "[::1]:0")
  21. if err != nil {
  22. t.Fatalf("net.Listen failed: %v", err)
  23. }
  24. defer ln.Close()
  25. done := make(chan bool)
  26. go acceptor(t, ln, done)
  27. c, err := net.Dial("tcp6", ln.Addr().String())
  28. if err != nil {
  29. t.Fatalf("net.Dial failed: %v", err)
  30. }
  31. defer c.Close()
  32. testUnicastSocketOptions(t, ipv6.NewConn(c))
  33. <-done
  34. }
  35. var packetConnUnicastSocketOptionTests = []struct {
  36. net, proto, addr string
  37. }{
  38. {"udp6", "", "[::1]:0"},
  39. {"ip6", ":ipv6-icmp", "::1"},
  40. }
  41. func TestPacketConnUnicastSocketOptions(t *testing.T) {
  42. switch runtime.GOOS {
  43. case "plan9", "windows":
  44. t.Skipf("not supported on %q", runtime.GOOS)
  45. }
  46. if !supportsIPv6 {
  47. t.Skip("ipv6 is not supported")
  48. }
  49. for _, tt := range packetConnUnicastSocketOptionTests {
  50. if tt.net == "ip6" && os.Getuid() != 0 {
  51. t.Skip("must be root")
  52. }
  53. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  54. if err != nil {
  55. t.Fatalf("net.ListenPacket(%q, %q) failed: %v", tt.net+tt.proto, tt.addr, err)
  56. }
  57. defer c.Close()
  58. testUnicastSocketOptions(t, ipv6.NewPacketConn(c))
  59. }
  60. }
  61. type testIPv6UnicastConn interface {
  62. TrafficClass() (int, error)
  63. SetTrafficClass(int) error
  64. HopLimit() (int, error)
  65. SetHopLimit(int) error
  66. }
  67. func testUnicastSocketOptions(t *testing.T, c testIPv6UnicastConn) {
  68. tclass := DiffServCS0 | NotECNTransport
  69. if err := c.SetTrafficClass(tclass); err != nil {
  70. t.Fatalf("ipv6.Conn.SetTrafficClass failed: %v", err)
  71. }
  72. if v, err := c.TrafficClass(); err != nil {
  73. t.Fatalf("ipv6.Conn.TrafficClass failed: %v", err)
  74. } else if v != tclass {
  75. t.Fatalf("got unexpected traffic class %v; expected %v", v, tclass)
  76. }
  77. hoplim := 255
  78. if err := c.SetHopLimit(hoplim); err != nil {
  79. t.Fatalf("ipv6.Conn.SetHopLimit failed: %v", err)
  80. }
  81. if v, err := c.HopLimit(); err != nil {
  82. t.Fatalf("ipv6.Conn.HopLimit failed: %v", err)
  83. } else if v != hoplim {
  84. t.Fatalf("got unexpected hop limit %v; expected %v", v, hoplim)
  85. }
  86. }