unicastsockopt_test.go 3.1 KB

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