unicastsockopt_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2012 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 ipv4_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/internal/nettest"
  12. "code.google.com/p/go.net/ipv4"
  13. )
  14. func TestConnUnicastSocketOptions(t *testing.T) {
  15. switch runtime.GOOS {
  16. case "plan9":
  17. t.Skipf("not supported on %q", runtime.GOOS)
  18. }
  19. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  20. if ifi == nil {
  21. t.Skipf("not available on %q", runtime.GOOS)
  22. }
  23. ln, err := net.Listen("tcp4", "127.0.0.1:0")
  24. if err != nil {
  25. t.Fatalf("net.Listen failed: %v", err)
  26. }
  27. defer ln.Close()
  28. done := make(chan bool)
  29. go acceptor(t, ln, done)
  30. c, err := net.Dial("tcp4", ln.Addr().String())
  31. if err != nil {
  32. t.Fatalf("net.Dial failed: %v", err)
  33. }
  34. defer c.Close()
  35. testUnicastSocketOptions(t, ipv4.NewConn(c))
  36. <-done
  37. }
  38. var packetConnUnicastSocketOptionTests = []struct {
  39. net, proto, addr string
  40. }{
  41. {"udp4", "", "127.0.0.1:0"},
  42. {"ip4", ":icmp", "127.0.0.1"},
  43. }
  44. func TestPacketConnUnicastSocketOptions(t *testing.T) {
  45. switch runtime.GOOS {
  46. case "plan9":
  47. t.Skipf("not supported on %q", runtime.GOOS)
  48. }
  49. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  50. if ifi == nil {
  51. t.Skipf("not available on %q", runtime.GOOS)
  52. }
  53. for _, tt := range packetConnUnicastSocketOptionTests {
  54. if tt.net == "ip4" && os.Getuid() != 0 {
  55. t.Skip("must be root")
  56. }
  57. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  58. if err != nil {
  59. t.Fatalf("net.ListenPacket(%q, %q) failed: %v", tt.net+tt.proto, tt.addr, err)
  60. }
  61. defer c.Close()
  62. testUnicastSocketOptions(t, ipv4.NewPacketConn(c))
  63. }
  64. }
  65. func TestRawConnUnicastSocketOptions(t *testing.T) {
  66. switch runtime.GOOS {
  67. case "plan9":
  68. t.Skipf("not supported on %q", runtime.GOOS)
  69. }
  70. if os.Getuid() != 0 {
  71. t.Skip("must be root")
  72. }
  73. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  74. if ifi == nil {
  75. t.Skipf("not available on %q", runtime.GOOS)
  76. }
  77. c, err := net.ListenPacket("ip4:icmp", "127.0.0.1")
  78. if err != nil {
  79. t.Fatalf("net.ListenPacket failed: %v", err)
  80. }
  81. defer c.Close()
  82. r, err := ipv4.NewRawConn(c)
  83. if err != nil {
  84. t.Fatalf("ipv4.NewRawConn failed: %v", err)
  85. }
  86. testUnicastSocketOptions(t, r)
  87. }
  88. type testIPv4UnicastConn interface {
  89. TOS() (int, error)
  90. SetTOS(int) error
  91. TTL() (int, error)
  92. SetTTL(int) error
  93. }
  94. func testUnicastSocketOptions(t *testing.T, c testIPv4UnicastConn) {
  95. tos := iana.DiffServCS0 | iana.NotECNTransport
  96. switch runtime.GOOS {
  97. case "windows":
  98. // IP_TOS option is supported on Windows 8 and beyond.
  99. t.Skipf("skipping IP_TOS test on %q", runtime.GOOS)
  100. }
  101. if err := c.SetTOS(tos); err != nil {
  102. t.Fatalf("ipv4.Conn.SetTOS failed: %v", err)
  103. }
  104. if v, err := c.TOS(); err != nil {
  105. t.Fatalf("ipv4.Conn.TOS failed: %v", err)
  106. } else if v != tos {
  107. t.Fatalf("got unexpected TOS value %v; expected %v", v, tos)
  108. }
  109. const ttl = 255
  110. if err := c.SetTTL(ttl); err != nil {
  111. t.Fatalf("ipv4.Conn.SetTTL failed: %v", err)
  112. }
  113. if v, err := c.TTL(); err != nil {
  114. t.Fatalf("ipv4.Conn.TTL failed: %v", err)
  115. } else if v != ttl {
  116. t.Fatalf("got unexpected TTL value %v; expected %v", v, ttl)
  117. }
  118. }