unicastsockopt_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "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 "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 "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. 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. }