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. "runtime"
  8. "testing"
  9. "golang.org/x/net/internal/iana"
  10. "golang.org/x/net/internal/nettest"
  11. "golang.org/x/net/ipv4"
  12. )
  13. func TestConnUnicastSocketOptions(t *testing.T) {
  14. switch runtime.GOOS {
  15. case "nacl", "plan9", "solaris":
  16. t.Skipf("not supported on %s", runtime.GOOS)
  17. }
  18. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  19. if ifi == nil {
  20. t.Skipf("not available on %s", runtime.GOOS)
  21. }
  22. ln, err := net.Listen("tcp4", "127.0.0.1:0")
  23. if err != nil {
  24. t.Fatal(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.Fatal(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 "nacl", "plan9", "solaris":
  46. t.Skipf("not supported on %s", runtime.GOOS)
  47. }
  48. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  49. if ifi == nil {
  50. t.Skipf("not available on %s", runtime.GOOS)
  51. }
  52. m, ok := nettest.SupportsRawIPSocket()
  53. for _, tt := range packetConnUnicastSocketOptionTests {
  54. if tt.net == "ip4" && !ok {
  55. t.Log(m)
  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 %s", runtime.GOOS)
  70. }
  71. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  72. t.Skip(m)
  73. }
  74. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  75. if ifi == nil {
  76. t.Skipf("not available on %s", 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 %s", 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. }