forward_unix_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 aix darwin dragonfly freebsd linux netbsd openbsd
  5. package test
  6. import (
  7. "bytes"
  8. "io"
  9. "io/ioutil"
  10. "math/rand"
  11. "net"
  12. "testing"
  13. "time"
  14. )
  15. type closeWriter interface {
  16. CloseWrite() error
  17. }
  18. func testPortForward(t *testing.T, n, listenAddr string) {
  19. server := newServer(t)
  20. defer server.Shutdown()
  21. conn := server.Dial(clientConfig())
  22. defer conn.Close()
  23. sshListener, err := conn.Listen(n, listenAddr)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. go func() {
  28. sshConn, err := sshListener.Accept()
  29. if err != nil {
  30. t.Fatalf("listen.Accept failed: %v", err)
  31. }
  32. _, err = io.Copy(sshConn, sshConn)
  33. if err != nil && err != io.EOF {
  34. t.Fatalf("ssh client copy: %v", err)
  35. }
  36. sshConn.Close()
  37. }()
  38. forwardedAddr := sshListener.Addr().String()
  39. netConn, err := net.Dial(n, forwardedAddr)
  40. if err != nil {
  41. t.Fatalf("net dial failed: %v", err)
  42. }
  43. readChan := make(chan []byte)
  44. go func() {
  45. data, _ := ioutil.ReadAll(netConn)
  46. readChan <- data
  47. }()
  48. // Invent some data.
  49. data := make([]byte, 100*1000)
  50. for i := range data {
  51. data[i] = byte(i % 255)
  52. }
  53. var sent []byte
  54. for len(sent) < 1000*1000 {
  55. // Send random sized chunks
  56. m := rand.Intn(len(data))
  57. n, err := netConn.Write(data[:m])
  58. if err != nil {
  59. break
  60. }
  61. sent = append(sent, data[:n]...)
  62. }
  63. if err := netConn.(closeWriter).CloseWrite(); err != nil {
  64. t.Errorf("netConn.CloseWrite: %v", err)
  65. }
  66. read := <-readChan
  67. if len(sent) != len(read) {
  68. t.Fatalf("got %d bytes, want %d", len(read), len(sent))
  69. }
  70. if bytes.Compare(sent, read) != 0 {
  71. t.Fatalf("read back data does not match")
  72. }
  73. if err := sshListener.Close(); err != nil {
  74. t.Fatalf("sshListener.Close: %v", err)
  75. }
  76. // Check that the forward disappeared.
  77. netConn, err = net.Dial(n, forwardedAddr)
  78. if err == nil {
  79. netConn.Close()
  80. t.Errorf("still listening to %s after closing", forwardedAddr)
  81. }
  82. }
  83. func TestPortForwardTCP(t *testing.T) {
  84. testPortForward(t, "tcp", "localhost:0")
  85. }
  86. func TestPortForwardUnix(t *testing.T) {
  87. addr, cleanup := newTempSocket(t)
  88. defer cleanup()
  89. testPortForward(t, "unix", addr)
  90. }
  91. func testAcceptClose(t *testing.T, n, listenAddr string) {
  92. server := newServer(t)
  93. defer server.Shutdown()
  94. conn := server.Dial(clientConfig())
  95. sshListener, err := conn.Listen(n, listenAddr)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. quit := make(chan error, 1)
  100. go func() {
  101. for {
  102. c, err := sshListener.Accept()
  103. if err != nil {
  104. quit <- err
  105. break
  106. }
  107. c.Close()
  108. }
  109. }()
  110. sshListener.Close()
  111. select {
  112. case <-time.After(1 * time.Second):
  113. t.Errorf("timeout: listener did not close.")
  114. case err := <-quit:
  115. t.Logf("quit as expected (error %v)", err)
  116. }
  117. }
  118. func TestAcceptCloseTCP(t *testing.T) {
  119. testAcceptClose(t, "tcp", "localhost:0")
  120. }
  121. func TestAcceptCloseUnix(t *testing.T) {
  122. addr, cleanup := newTempSocket(t)
  123. defer cleanup()
  124. testAcceptClose(t, "unix", addr)
  125. }
  126. // Check that listeners exit if the underlying client transport dies.
  127. func testPortForwardConnectionClose(t *testing.T, n, listenAddr string) {
  128. server := newServer(t)
  129. defer server.Shutdown()
  130. conn := server.Dial(clientConfig())
  131. sshListener, err := conn.Listen(n, listenAddr)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. quit := make(chan error, 1)
  136. go func() {
  137. for {
  138. c, err := sshListener.Accept()
  139. if err != nil {
  140. quit <- err
  141. break
  142. }
  143. c.Close()
  144. }
  145. }()
  146. // It would be even nicer if we closed the server side, but it
  147. // is more involved as the fd for that side is dup()ed.
  148. server.clientConn.Close()
  149. select {
  150. case <-time.After(1 * time.Second):
  151. t.Errorf("timeout: listener did not close.")
  152. case err := <-quit:
  153. t.Logf("quit as expected (error %v)", err)
  154. }
  155. }
  156. func TestPortForwardConnectionCloseTCP(t *testing.T) {
  157. testPortForwardConnectionClose(t, "tcp", "localhost:0")
  158. }
  159. func TestPortForwardConnectionCloseUnix(t *testing.T) {
  160. addr, cleanup := newTempSocket(t)
  161. defer cleanup()
  162. testPortForwardConnectionClose(t, "unix", addr)
  163. }