Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

151 rader
3.2 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 "aix", "fuchsia", "hurd", "js", "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 "aix", "fuchsia", "hurd", "js", "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 "aix", "fuchsia", "hurd", "js", "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. t.Helper()
  106. tos := iana.DiffServCS0 | iana.NotECNTransport
  107. switch runtime.GOOS {
  108. case "windows":
  109. // IP_TOS option is supported on Windows 8 and beyond.
  110. t.Skipf("not supported on %s", runtime.GOOS)
  111. }
  112. if err := c.SetTOS(tos); err != nil {
  113. t.Fatal(err)
  114. }
  115. if v, err := c.TOS(); err != nil {
  116. t.Fatal(err)
  117. } else if v != tos {
  118. t.Fatalf("got %v; want %v", v, tos)
  119. }
  120. const ttl = 255
  121. if err := c.SetTTL(ttl); err != nil {
  122. t.Fatal(err)
  123. }
  124. if v, err := c.TTL(); err != nil {
  125. t.Fatal(err)
  126. } else if v != ttl {
  127. t.Fatalf("got %v; want %v", v, ttl)
  128. }
  129. }