unicastsockopt_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "golang.org/x/net/internal/iana"
  11. "golang.org/x/net/internal/nettest"
  12. "golang.org/x/net/ipv4"
  13. )
  14. func TestConnUnicastSocketOptions(t *testing.T) {
  15. switch runtime.GOOS {
  16. case "nacl", "plan9", "solaris":
  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.Fatal(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.Fatal(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 "nacl", "plan9", "solaris":
  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.Log("must be root")
  56. continue
  57. }
  58. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. defer c.Close()
  63. testUnicastSocketOptions(t, ipv4.NewPacketConn(c))
  64. }
  65. }
  66. func TestRawConnUnicastSocketOptions(t *testing.T) {
  67. switch runtime.GOOS {
  68. case "nacl", "plan9", "solaris":
  69. t.Skipf("not supported on %q", runtime.GOOS)
  70. }
  71. if os.Getuid() != 0 {
  72. t.Skip("must be root")
  73. }
  74. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  75. if ifi == nil {
  76. t.Skipf("not available on %q", runtime.GOOS)
  77. }
  78. c, err := net.ListenPacket("ip4:icmp", "127.0.0.1")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. defer c.Close()
  83. r, err := ipv4.NewRawConn(c)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. testUnicastSocketOptions(t, r)
  88. }
  89. type testIPv4UnicastConn interface {
  90. TOS() (int, error)
  91. SetTOS(int) error
  92. TTL() (int, error)
  93. SetTTL(int) error
  94. }
  95. func testUnicastSocketOptions(t *testing.T, c testIPv4UnicastConn) {
  96. tos := iana.DiffServCS0 | iana.NotECNTransport
  97. switch runtime.GOOS {
  98. case "windows":
  99. // IP_TOS option is supported on Windows 8 and beyond.
  100. t.Skipf("not supported on %q", runtime.GOOS)
  101. }
  102. if err := c.SetTOS(tos); err != nil {
  103. t.Fatal(err)
  104. }
  105. if v, err := c.TOS(); err != nil {
  106. t.Fatal(err)
  107. } else if v != tos {
  108. t.Fatalf("got %v; want %v", v, tos)
  109. }
  110. const ttl = 255
  111. if err := c.SetTTL(ttl); err != nil {
  112. t.Fatal(err)
  113. }
  114. if v, err := c.TTL(); err != nil {
  115. t.Fatal(err)
  116. } else if v != ttl {
  117. t.Fatalf("got %v; want %v", v, ttl)
  118. }
  119. }