go.crypto/ssh: improve TestServerWindow robustness

Fix a few resource leaks and prevent the test from
hanging if an error occurs reading from the remote
server.

R=agl, gustav.paul, kardianos
CC=golang-dev
https://golang.org/cl/6423065
This commit is contained in:
Dave Cheney
2012-07-21 14:19:30 +10:00
parent d1bf83abcb
commit e751d5236a
+7 -8
View File
@@ -17,18 +17,18 @@ const windowTestBytes = 16000 * 200
// CopyNRandomly copies n bytes from src to dst. It uses a variable, and random, // CopyNRandomly copies n bytes from src to dst. It uses a variable, and random,
// buffer size to exercise more code paths. // buffer size to exercise more code paths.
func CopyNRandomly(dst io.Writer, src io.Reader, n int64) (written int64, err error) { func CopyNRandomly(dst io.Writer, src io.Reader, n int) (written int, err error) {
buf := make([]byte, 32*1024) buf := make([]byte, 32*1024)
for written < n { for written < n {
l := (rand.Intn(30) + 1) * 1024 l := (rand.Intn(30) + 1) * 1024
if d := n - written; d < int64(l) { if d := n - written; d < l {
l = int(d) l = d
} }
nr, er := src.Read(buf[0:l]) nr, er := src.Read(buf[0:l])
if nr > 0 { if nr > 0 {
nw, ew := dst.Write(buf[0:nr]) nw, ew := dst.Write(buf[0:nr])
if nw > 0 { if nw > 0 {
written += int64(nw) written += nw
} }
if ew != nil { if ew != nil {
err = ew err = ew
@@ -75,6 +75,7 @@ func runSSHClient(t *testing.T, addr string) {
// Read back the data from the server. // Read back the data from the server.
go func() { go func() {
defer session.Close() defer session.Close()
defer close(wait)
serverStdout, err := session.StdoutPipe() serverStdout, err := session.StdoutPipe()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -87,7 +88,6 @@ func runSSHClient(t *testing.T, addr string) {
if n != windowTestBytes { if n != windowTestBytes {
t.Fatalf("Read only %d bytes from server, expected %d", n, windowTestBytes) t.Fatalf("Read only %d bytes from server, expected %d", n, windowTestBytes)
} }
wait <- true
}() }()
serverStdin, err := session.StdinPipe() serverStdin, err := session.StdinPipe()
@@ -126,11 +126,10 @@ func startSSHServer(t *testing.T) (addr string) {
} }
addr = listener.Addr().String() addr = listener.Addr().String()
go func() { go func() {
defer listener.Close()
for { for {
sConn, err := listener.Accept() sConn, err := listener.Accept()
err = sConn.Handshake() err = sConn.Handshake()
if err != nil { if err != nil {
if err != io.EOF { if err != io.EOF {
@@ -147,6 +146,7 @@ func startSSHServer(t *testing.T) (addr string) {
} }
func connRun(t *testing.T, sConn *ServerConn) { func connRun(t *testing.T, sConn *ServerConn) {
defer sConn.Close()
for { for {
channel, err := sConn.Accept() channel, err := sConn.Accept()
if err != nil { if err != nil {
@@ -167,7 +167,6 @@ func connRun(t *testing.T, sConn *ServerConn) {
go func() { go func() {
defer channel.Close() defer channel.Close()
n, err := CopyNRandomly(channel, channel, windowTestBytes) n, err := CopyNRandomly(channel, channel, windowTestBytes)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
if err == io.ErrShortWrite { if err == io.ErrShortWrite {