You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.1 KiB

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