unicastsockopt_test.go 3.0 KB

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