Add a timeout to the sshDialer to prevent indefinite hangs.

This commit is contained in:
CJ Cullen
2016-04-04 15:51:49 -07:00
parent 6950fab441
commit cde4f6d613
2 changed files with 70 additions and 2 deletions

View File

@@ -328,3 +328,39 @@ func TestSSHUser(t *testing.T) {
}
}
type slowDialer struct {
delay time.Duration
err error
}
func (s *slowDialer) Dial(network, addr string, config *ssh.ClientConfig) (*ssh.Client, error) {
time.Sleep(s.delay)
if s.err != nil {
return nil, s.err
}
return &ssh.Client{}, nil
}
func TestTimeoutDialer(t *testing.T) {
testCases := []struct {
delay time.Duration
timeout time.Duration
err error
expectedErrString string
}{
// delay > timeout should cause ssh.Dial to timeout.
{1 * time.Second, 0, nil, "timed out dialing"},
// delay < timeout should return the result of the call to the dialer.
{0, 1 * time.Second, nil, ""},
{0, 1 * time.Second, fmt.Errorf("test dial error"), "test dial error"},
}
for _, tc := range testCases {
dialer := &timeoutDialer{&slowDialer{tc.delay, tc.err}, tc.timeout}
_, err := dialer.Dial("tcp", "addr:port", &ssh.ClientConfig{})
if len(tc.expectedErrString) == 0 && err != nil ||
!strings.Contains(fmt.Sprint(err), tc.expectedErrString) {
t.Errorf("Expected error to contain %q; got %v", tc.expectedErrString, err)
}
}
}