dial_unix_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // +build !windows,!solaris,!js
  5. package test
  6. // direct-tcpip and direct-streamlocal functional tests
  7. import (
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "net"
  12. "strings"
  13. "testing"
  14. )
  15. type dialTester interface {
  16. TestServerConn(t *testing.T, c net.Conn)
  17. TestClientConn(t *testing.T, c net.Conn)
  18. }
  19. func testDial(t *testing.T, n, listenAddr string, x dialTester) {
  20. server := newServer(t)
  21. defer server.Shutdown()
  22. sshConn := server.Dial(clientConfig())
  23. defer sshConn.Close()
  24. l, err := net.Listen(n, listenAddr)
  25. if err != nil {
  26. t.Fatalf("Listen: %v", err)
  27. }
  28. defer l.Close()
  29. testData := fmt.Sprintf("hello from %s, %s", n, listenAddr)
  30. go func() {
  31. for {
  32. c, err := l.Accept()
  33. if err != nil {
  34. break
  35. }
  36. x.TestServerConn(t, c)
  37. io.WriteString(c, testData)
  38. c.Close()
  39. }
  40. }()
  41. conn, err := sshConn.Dial(n, l.Addr().String())
  42. if err != nil {
  43. t.Fatalf("Dial: %v", err)
  44. }
  45. x.TestClientConn(t, conn)
  46. defer conn.Close()
  47. b, err := ioutil.ReadAll(conn)
  48. if err != nil {
  49. t.Fatalf("ReadAll: %v", err)
  50. }
  51. t.Logf("got %q", string(b))
  52. if string(b) != testData {
  53. t.Fatalf("expected %q, got %q", testData, string(b))
  54. }
  55. }
  56. type tcpDialTester struct {
  57. listenAddr string
  58. }
  59. func (x *tcpDialTester) TestServerConn(t *testing.T, c net.Conn) {
  60. host := strings.Split(x.listenAddr, ":")[0]
  61. prefix := host + ":"
  62. if !strings.HasPrefix(c.LocalAddr().String(), prefix) {
  63. t.Fatalf("expected to start with %q, got %q", prefix, c.LocalAddr().String())
  64. }
  65. if !strings.HasPrefix(c.RemoteAddr().String(), prefix) {
  66. t.Fatalf("expected to start with %q, got %q", prefix, c.RemoteAddr().String())
  67. }
  68. }
  69. func (x *tcpDialTester) TestClientConn(t *testing.T, c net.Conn) {
  70. // we use zero addresses. see *Client.Dial.
  71. if c.LocalAddr().String() != "0.0.0.0:0" {
  72. t.Fatalf("expected \"0.0.0.0:0\", got %q", c.LocalAddr().String())
  73. }
  74. if c.RemoteAddr().String() != "0.0.0.0:0" {
  75. t.Fatalf("expected \"0.0.0.0:0\", got %q", c.RemoteAddr().String())
  76. }
  77. }
  78. func TestDialTCP(t *testing.T) {
  79. x := &tcpDialTester{
  80. listenAddr: "127.0.0.1:0",
  81. }
  82. testDial(t, "tcp", x.listenAddr, x)
  83. }
  84. type unixDialTester struct {
  85. listenAddr string
  86. }
  87. func (x *unixDialTester) TestServerConn(t *testing.T, c net.Conn) {
  88. if c.LocalAddr().String() != x.listenAddr {
  89. t.Fatalf("expected %q, got %q", x.listenAddr, c.LocalAddr().String())
  90. }
  91. if c.RemoteAddr().String() != "@" && c.RemoteAddr().String() != "" {
  92. t.Fatalf("expected \"@\" or \"\", got %q", c.RemoteAddr().String())
  93. }
  94. }
  95. func (x *unixDialTester) TestClientConn(t *testing.T, c net.Conn) {
  96. if c.RemoteAddr().String() != x.listenAddr {
  97. t.Fatalf("expected %q, got %q", x.listenAddr, c.RemoteAddr().String())
  98. }
  99. if c.LocalAddr().String() != "@" {
  100. t.Fatalf("expected \"@\", got %q", c.LocalAddr().String())
  101. }
  102. }
  103. func TestDialUnix(t *testing.T) {
  104. addr, cleanup := newTempSocket(t)
  105. defer cleanup()
  106. x := &unixDialTester{
  107. listenAddr: addr,
  108. }
  109. testDial(t, "unix", x.listenAddr, x)
  110. }