unicastsockopt_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/ipv4"
  11. "golang.org/x/net/nettest"
  12. )
  13. func TestConnUnicastSocketOptions(t *testing.T) {
  14. switch runtime.GOOS {
  15. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  16. t.Skipf("not supported on %s", runtime.GOOS)
  17. }
  18. if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
  19. t.Skipf("not available on %s", runtime.GOOS)
  20. }
  21. ln, err := net.Listen("tcp4", "127.0.0.1:0")
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. defer ln.Close()
  26. errc := make(chan error, 1)
  27. go func() {
  28. c, err := ln.Accept()
  29. if err != nil {
  30. errc <- err
  31. return
  32. }
  33. errc <- c.Close()
  34. }()
  35. c, err := net.Dial("tcp4", ln.Addr().String())
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. defer c.Close()
  40. testUnicastSocketOptions(t, ipv4.NewConn(c))
  41. if err := <-errc; err != nil {
  42. t.Errorf("server: %v", err)
  43. }
  44. }
  45. var packetConnUnicastSocketOptionTests = []struct {
  46. net, proto, addr string
  47. }{
  48. {"udp4", "", "127.0.0.1:0"},
  49. {"ip4", ":icmp", "127.0.0.1"},
  50. }
  51. func TestPacketConnUnicastSocketOptions(t *testing.T) {
  52. switch runtime.GOOS {
  53. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  54. t.Skipf("not supported on %s", runtime.GOOS)
  55. }
  56. if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
  57. t.Skipf("not available on %s", runtime.GOOS)
  58. }
  59. ok := nettest.SupportsRawSocket()
  60. for _, tt := range packetConnUnicastSocketOptionTests {
  61. if tt.net == "ip4" && !ok {
  62. t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  63. continue
  64. }
  65. c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. defer c.Close()
  70. testUnicastSocketOptions(t, ipv4.NewPacketConn(c))
  71. }
  72. }
  73. func TestRawConnUnicastSocketOptions(t *testing.T) {
  74. switch runtime.GOOS {
  75. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  76. t.Skipf("not supported on %s", runtime.GOOS)
  77. }
  78. if !nettest.SupportsRawSocket() {
  79. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  80. }
  81. if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
  82. t.Skipf("not available on %s", runtime.GOOS)
  83. }
  84. c, err := net.ListenPacket("ip4:icmp", "127.0.0.1")
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. defer c.Close()
  89. r, err := ipv4.NewRawConn(c)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. testUnicastSocketOptions(t, r)
  94. }
  95. type testIPv4UnicastConn interface {
  96. TOS() (int, error)
  97. SetTOS(int) error
  98. TTL() (int, error)
  99. SetTTL(int) error
  100. }
  101. func testUnicastSocketOptions(t *testing.T, c testIPv4UnicastConn) {
  102. t.Helper()
  103. tos := iana.DiffServCS0 | iana.NotECNTransport
  104. switch runtime.GOOS {
  105. case "windows":
  106. // IP_TOS option is supported on Windows 8 and beyond.
  107. t.Skipf("not supported on %s", runtime.GOOS)
  108. }
  109. if err := c.SetTOS(tos); err != nil {
  110. t.Fatal(err)
  111. }
  112. if v, err := c.TOS(); err != nil {
  113. t.Fatal(err)
  114. } else if v != tos {
  115. t.Fatalf("got %v; want %v", v, tos)
  116. }
  117. const ttl = 255
  118. if err := c.SetTTL(ttl); err != nil {
  119. t.Fatal(err)
  120. }
  121. if v, err := c.TTL(); err != nil {
  122. t.Fatal(err)
  123. } else if v != ttl {
  124. t.Fatalf("got %v; want %v", v, ttl)
  125. }
  126. }