go.crypto/ssh: fix test breakages introduced by 125:40246d2ae2eb

* Remove special handling for dynamically allocated
  ports. This was a bug in OpenSSH 5.x sshd.

* Run the test with a preselected port number.

* Run TestPortForward only on unix platforms.

R=dave, agl
CC=golang-dev
https://golang.org/cl/10049045
This commit is contained in:
Han-Wen Nienhuys 2013-06-18 12:43:42 -04:00 коммит произвёл Adam Langley
Родитель b9b3892677
Коммит 7cbb17fecb
2 изменённых файлов: 35 добавлений и 20 удалений

Просмотреть файл

@ -48,15 +48,8 @@ func (c *ClientConn) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
return nil, err return nil, err
} }
// Register this forward, using the port number we requested.
// If we requested port 0 (auto allocated port), we have to
// register under 0, since the channelOpenMsg will list 0
// rather than the allocated port number.
ch := c.forwardList.add(*laddr)
// If the original port was 0, then the remote side will // If the original port was 0, then the remote side will
// supply a real port number in the response. // supply a real port number in the response.
origPort := uint32(laddr.Port)
if laddr.Port == 0 { if laddr.Port == 0 {
port, _, ok := parseUint32(resp.Data) port, _, ok := parseUint32(resp.Data)
if !ok { if !ok {
@ -65,7 +58,14 @@ func (c *ClientConn) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
laddr.Port = int(port) laddr.Port = int(port)
} }
return &tcpListener{laddr, origPort, c, ch}, nil // Register this forward, using the port number we obtained.
//
// This does not work on OpenSSH < 6.0, which will send a
// channelOpenMsg with port number 0, rather than the actual
// port number.
ch := c.forwardList.add(*laddr)
return &tcpListener{laddr, c, ch}, nil
} }
// forwardList stores a mapping between remote // forwardList stores a mapping between remote
@ -126,10 +126,8 @@ func (l *forwardList) lookup(addr net.TCPAddr) (chan forward, bool) {
type tcpListener struct { type tcpListener struct {
laddr *net.TCPAddr laddr *net.TCPAddr
// The port with which we made the request, which can be 0. conn *ClientConn
origPort uint32 in <-chan forward
conn *ClientConn
in <-chan forward
} }
// Accept waits for and returns the next connection to the listener. // Accept waits for and returns the next connection to the listener.
@ -155,13 +153,9 @@ func (l *tcpListener) Close() error {
"cancel-tcpip-forward", "cancel-tcpip-forward",
true, true,
l.laddr.IP.String(), l.laddr.IP.String(),
l.origPort, uint32(l.laddr.Port),
} }
origAddr := net.TCPAddr{ l.conn.forwardList.remove(*l.laddr)
IP: l.laddr.IP,
Port: int(l.origPort),
}
l.conn.forwardList.remove(origAddr)
if _, err := l.conn.sendGlobalRequest(m); err != nil { if _, err := l.conn.sendGlobalRequest(m); err != nil {
return err return err
} }

Просмотреть файл

@ -1,7 +1,14 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin freebsd linux netbsd openbsd
package test package test
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
@ -15,9 +22,23 @@ func TestPortForward(t *testing.T) {
conn := server.Dial(clientConfig()) conn := server.Dial(clientConfig())
defer conn.Close() defer conn.Close()
sshListener, err := conn.Listen("tcp", "127.0.0.1:0") var sshListener net.Listener
var err error
tries := 10
for i := 0; i < tries; i++ {
port := 1024 + rand.Intn(50000)
// We can't reliably test dynamic port allocation, as it does
// not work correctly with OpenSSH before 6.0. See also
// https://bugzilla.mindrot.org/show_bug.cgi?id=2017
sshListener, err = conn.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err == nil {
break
}
}
if err != nil { if err != nil {
t.Fatalf("conn.Listen failed: %v", err) t.Fatalf("conn.Listen failed: %v (after %d tries)", err, tries)
} }
go func() { go func() {