go.crypto/ssh: close channel feeding tcpListener.

Close both on closing the listener, and on closing the
connection. Test the former case.

R=dave
CC=golang-dev
https://golang.org/cl/11349043
This commit is contained in:
Han-Wen Nienhuys
2013-07-22 21:50:13 +10:00
committed by Dave Cheney
parent 396847505b
commit 7f7cbbf18e
3 changed files with 68 additions and 8 deletions
+53 -7
View File
@@ -14,14 +14,12 @@ import (
"math/rand"
"net"
"testing"
"time"
"code.google.com/p/go.crypto/ssh"
)
func TestPortForward(t *testing.T) {
server := newServer(t)
defer server.Shutdown()
conn := server.Dial(clientConfig())
defer conn.Close()
func listenSSHAuto(conn *ssh.ClientConn) (net.Listener, error) {
var sshListener net.Listener
var err error
tries := 10
@@ -38,7 +36,21 @@ func TestPortForward(t *testing.T) {
}
if err != nil {
t.Fatalf("conn.Listen failed: %v (after %d tries)", err, tries)
return nil, fmt.Errorf("conn.Listen failed: %v (after %d tries)", err, tries)
}
return sshListener, nil
}
func TestPortForward(t *testing.T) {
server := newServer(t)
defer server.Shutdown()
conn := server.Dial(clientConfig())
defer conn.Close()
sshListener, err := listenSSHAuto(conn)
if err != nil {
t.Fatal(err)
}
go func() {
@@ -106,3 +118,37 @@ func TestPortForward(t *testing.T) {
t.Errorf("still listening to %s after closing", forwardedAddr)
}
}
func TestAcceptClose(t *testing.T) {
server := newServer(t)
defer server.Shutdown()
conn := server.Dial(clientConfig())
sshListener, err := listenSSHAuto(conn)
if err != nil {
t.Fatal(err)
}
quit := make(chan error, 1)
go func() {
for {
c, err := sshListener.Accept()
if err != nil {
quit <- err
break
}
c.Close()
}
}()
sshListener.Close()
select {
case <-time.After(1 * time.Second):
t.Errorf("timeout: listener did not close.")
case err := <-quit:
t.Logf("quit as expected (error %v)", err)
}
}
// TODO(hanwen): test that closing the connection also
// exits the listeners.