Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HouseK committed Oct 6, 2023
1 parent d804107 commit 4ad08cc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ssh/tcpip.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (c *Client) Dial(n, addr string) (net.Conn, error) {
if err != nil {
return nil, err
}
port, err := net.LookupPort(portString)
port, err := net.LookupPort(n, portString)
if err != nil {
return nil, err
}
Expand Down
58 changes: 58 additions & 0 deletions ssh/tcpip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ssh

import (
"fmt"
"testing"
)

Expand All @@ -18,3 +19,60 @@ func TestAutoPortListenBroken(t *testing.T) {
t.Errorf("version %q marked as broken", works)
}
}

func TestDialNamedPort(t *testing.T) {
// Test that sshClient.Dial supports named ports.

srvConn, clientConn, err := netPipe()
if err != nil {
t.Fatalf("netPipe: %v", err)
}

serverConf := &ServerConfig{
NoClientAuth: true,
}
serverConf.AddHostKey(testSigners["rsa"])
srvErr := make(chan error, 1)
go func() {
defer close(srvErr)
_, chans, req, err := NewServerConn(srvConn, serverConf)
if err != nil {
srvErr <- fmt.Errorf("NewServerConn: %w", err)
return
}
go DiscardRequests(req)
for newChan := range chans {
ch, reqs, err := newChan.Accept()
if err != nil {
srvErr <- fmt.Errorf("Accept: %w", err)
}
go DiscardRequests(reqs)
ch.Close()
}
}()

clientConf := &ClientConfig{
User: "testuser",
HostKeyCallback: InsecureIgnoreHostKey(),
}

sshClientConn, newChans, reqs, err := NewClientConn(clientConn, "", clientConf)
if err != nil {
t.Fatal(err)
}
sshClient := NewClient(sshClientConn, newChans, reqs)

// port being a named port `ssh` is the main point of the test.
tcpipconn, err := sshClient.Dial("tcp", "localhost:ssh")
if err != nil {
t.Fatal(err)
}
tcpipconn.Close()

srvConn.Close()
clientConn.Close()

for err := range srvErr {
t.Errorf("ssh server: %s", err)
}
}

0 comments on commit 4ad08cc

Please sign in to comment.