Skip to content

Commit

Permalink
ssh/test: avoid leaking a net.UnixConn in server.TryDialWithAddr
Browse files Browse the repository at this point in the history
For golang/go#64959.

Change-Id: I2153166f4960058cdc2b82ae34ca250dcc6ba1c6
Cq-Include-Trybots: luci.golang.try:x_crypto-gotip-linux-amd64-longtest,x_crypto-gotip-windows-amd64-longtest
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/554062
Run-TryBot: Bryan Mills <[email protected]>
Auto-Submit: Bryan Mills <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed Jan 8, 2024
1 parent 055043d commit 403f699
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions ssh/test/test_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (s *server) TryDial(config *ssh.ClientConfig) (*ssh.Client, error) {

// addr is the user specified host:port. While we don't actually dial it,
// we need to know this for host key matching
func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (*ssh.Client, error) {
func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (client *ssh.Client, err error) {
sshd, err := exec.LookPath("sshd")
if err != nil {
s.t.Skipf("skipping test: %v", err)
Expand All @@ -188,13 +188,26 @@ func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (*ssh.Cl
if err != nil {
s.t.Fatalf("unixConnection: %v", err)
}
defer func() {
// Close c2 after we've started the sshd command so that it won't prevent c1
// from returning EOF when the sshd command exits.
c2.Close()

// Leave c1 open if we're returning a client that wraps it.
// (The client is responsible for closing it.)
// Otherwise, close it to free up the socket.
if client == nil {
c1.Close()
}
}()

cmd := testenv.Command(s.t, sshd, "-f", s.configfile, "-i", "-e")
f, err := c2.File()
if err != nil {
s.t.Fatalf("UnixConn.File: %v", err)
}
defer f.Close()

cmd := testenv.Command(s.t, sshd, "-f", s.configfile, "-i", "-e")
cmd.Stdin = f
cmd.Stdout = f
cmd.Stderr = new(bytes.Buffer)
Expand Down Expand Up @@ -223,7 +236,7 @@ func (s *server) TryDialWithAddr(config *ssh.ClientConfig, addr string) (*ssh.Cl
// processes are killed too.
cmd.Process.Signal(os.Interrupt)
cmd.Wait()
if s.t.Failed() {
if s.t.Failed() || testing.Verbose() {
// log any output from sshd process
s.t.Logf("sshd:\n%s", cmd.Stderr)
}
Expand Down

0 comments on commit 403f699

Please sign in to comment.