unicastsockopt_test.go 2.2 KB

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