Skip to content

Commit c576467

Browse files
committed
ssh: add tests for DialContext
1 parent 2964360 commit c576467

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

ssh/client_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ package ssh
66

77
import (
88
"bytes"
9+
"context"
910
"crypto/rand"
1011
"errors"
1112
"fmt"
1213
"net"
1314
"strings"
1415
"testing"
16+
"time"
1517
)
1618

1719
func TestClientVersion(t *testing.T) {
@@ -365,3 +367,27 @@ func TestUnsupportedAlgorithm(t *testing.T) {
365367
})
366368
}
367369
}
370+
371+
func TestDialContext(t *testing.T) {
372+
ctx, cancel := context.WithCancel(context.Background())
373+
cancel()
374+
_, err := DialContext(ctx, "tcp", ":22", &ClientConfig{})
375+
wantErr := context.Canceled
376+
if !errors.Is(err, wantErr) {
377+
t.Errorf("DialContext: err == %v, expected %v", err, wantErr)
378+
}
379+
380+
ctx, cancel = context.WithDeadline(context.Background(), time.Now())
381+
defer cancel()
382+
_, err = DialContext(ctx, "tcp", ":22", &ClientConfig{})
383+
wantErr = context.DeadlineExceeded
384+
if !errors.Is(err, wantErr) {
385+
t.Errorf("DialContext: err == %v, expected %v", err, wantErr)
386+
}
387+
388+
ctx = context.Background()
389+
_, err = DialContext(ctx, "tcp", ":22", &ClientConfig{})
390+
if _, ok := err.(*net.OpError); !ok {
391+
t.Errorf("DialContext: err == %#v, expected *net.OpError", err)
392+
}
393+
}

0 commit comments

Comments
 (0)