socket_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2016 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 netreflect_test
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "net"
  9. "os"
  10. "runtime"
  11. "testing"
  12. "golang.org/x/net/internal/netreflect"
  13. )
  14. func localPath() string {
  15. f, err := ioutil.TempFile("", "netreflect")
  16. if err != nil {
  17. panic(err)
  18. }
  19. path := f.Name()
  20. f.Close()
  21. os.Remove(path)
  22. return path
  23. }
  24. func newLocalListener(network string) (net.Listener, error) {
  25. switch network {
  26. case "tcp":
  27. if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
  28. return ln, nil
  29. }
  30. return net.Listen("tcp6", "[::1]:0")
  31. case "tcp4":
  32. return net.Listen("tcp4", "127.0.0.1:0")
  33. case "tcp6":
  34. return net.Listen("tcp6", "[::1]:0")
  35. case "unix", "unixpacket":
  36. return net.Listen(network, localPath())
  37. }
  38. return nil, fmt.Errorf("%s is not supported", network)
  39. }
  40. func newLocalPacketListener(network string) (net.PacketConn, error) {
  41. switch network {
  42. case "udp":
  43. if c, err := net.ListenPacket("udp4", "127.0.0.1:0"); err == nil {
  44. return c, nil
  45. }
  46. return net.ListenPacket("udp6", "[::1]:0")
  47. case "udp4":
  48. return net.ListenPacket("udp4", "127.0.0.1:0")
  49. case "udp6":
  50. return net.ListenPacket("udp6", "[::1]:0")
  51. case "unixgram":
  52. return net.ListenPacket(network, localPath())
  53. }
  54. return nil, fmt.Errorf("%s is not supported", network)
  55. }
  56. func TestSocketOf(t *testing.T) {
  57. for _, network := range []string{"tcp", "unix", "unixpacket"} {
  58. switch network {
  59. case "unix":
  60. switch runtime.GOOS {
  61. case "nacl", "plan9", "windows":
  62. continue
  63. }
  64. case "unixpacket":
  65. switch runtime.GOOS {
  66. case "darwin", "nacl", "plan9", "windows":
  67. continue
  68. }
  69. }
  70. ln, err := newLocalListener(network)
  71. if err != nil {
  72. t.Error(err)
  73. continue
  74. }
  75. defer func() {
  76. path := ln.Addr().String()
  77. ln.Close()
  78. if network == "unix" || network == "unixpacket" {
  79. os.Remove(path)
  80. }
  81. }()
  82. c, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
  83. if err != nil {
  84. t.Error(err)
  85. continue
  86. }
  87. defer c.Close()
  88. if _, err := netreflect.SocketOf(c); err != nil {
  89. t.Error(err)
  90. continue
  91. }
  92. }
  93. }
  94. func TestPacketSocketOf(t *testing.T) {
  95. for _, network := range []string{"udp", "unixgram"} {
  96. switch network {
  97. case "unixgram":
  98. switch runtime.GOOS {
  99. case "nacl", "plan9", "windows":
  100. continue
  101. }
  102. }
  103. c, err := newLocalPacketListener(network)
  104. if err != nil {
  105. t.Error(err)
  106. continue
  107. }
  108. defer c.Close()
  109. if _, err := netreflect.PacketSocketOf(c); err != nil {
  110. t.Error(err)
  111. continue
  112. }
  113. }
  114. }