Skip to content

Commit

Permalink
clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HouseK committed Oct 18, 2023
1 parent 612be71 commit 6b972a0
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions ssh/tcpip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ func TestAutoPortListenBroken(t *testing.T) {
}

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

srvConn, clientConn, err := netPipe()
if err != nil {
t.Fatalf("netPipe: %v", err)
Expand All @@ -46,44 +44,55 @@ func TestDialNamedPort(t *testing.T) {
for newChan := range chans {
if newChan.ChannelType() != "direct-tcpip" {
srvErr <- fmt.Errorf("expected direct-tcpip channel, got=%s", newChan.ChannelType())
if err := newChan.Reject(UnknownChannelType, "This test server only supports direct-tcpip"); err != nil {
srvErr <- err
}
continue
}
data := channelOpenDirectMsg{}
if err := Unmarshal(newChan.ExtraData(), &data); err != nil {
srvErr <- err
if err := newChan.Reject(ConnectionFailed, err.Error()); err != nil {
srvErr <- err
}
continue
}
// Below we dial for service `ssh` which should be translated to 22.
if data.Port != 22 {
srvErr <- fmt.Errorf("expected port 22 got=%d", data.Port)
if err := newChan.Reject(ConnectionFailed, fmt.Sprintf("expected port 22 got=%d", data.Port)); err != nil {
srvErr <- err
}
continue
}
ch, reqs, err := newChan.Accept()
if err != nil {
srvErr <- fmt.Errorf("Accept: %w", err)
continue
}
go DiscardRequests(reqs)
ch.Close()
if err := ch.Close(); err != nil {
srvErr <- err
}
}
}()

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")
// The port section in the host:port string being a named service `ssh` is the main point of the test.
_, err = sshClient.Dial("tcp", "localhost:ssh")
if err != nil {
t.Fatal(err)
t.Error(err)
}
tcpipconn.Close()

// Stop the ssh server.
clientConn.Close()

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

0 comments on commit 6b972a0

Please sign in to comment.