From 1b62fb782346069aa905fb4f9602a43d9362a547 Mon Sep 17 00:00:00 2001 From: Tim Savage Date: Fri, 23 Dec 2016 09:34:35 +1100 Subject: Added test for authentication timeout from a non-responsive server --- tests/test_auth.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests/test_auth.py') diff --git a/tests/test_auth.py b/tests/test_auth.py index 23517790..5c0bd264 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -23,6 +23,7 @@ Some unit tests for authenticating over a Transport. import sys import threading import unittest +from time import sleep from paramiko import Transport, ServerInterface, RSAKey, DSSKey, \ BadAuthenticationType, InteractiveQuery, \ @@ -73,6 +74,9 @@ class NullServer (ServerInterface): return AUTH_SUCCESSFUL if username == 'bad-server': raise Exception("Ack!") + if username == 'unresponsive-server': + sleep(5) + return AUTH_SUCCESSFUL return AUTH_FAILED def check_auth_publickey(self, username, key): @@ -232,3 +236,24 @@ class AuthTest (unittest.TestCase): except: etype, evalue, etb = sys.exc_info() self.assertTrue(issubclass(etype, AuthenticationException)) + + def test_9_auth_non_responsive(self): + """ + verify that authentication times out if server takes to long to + respond (or never responds). + """ + auth_timeout = self.tc.auth_timeout + self.tc.auth_timeout = 2 # Reduce to 2 seconds to speed up test + + try: + self.start_server() + self.tc.connect() + try: + remain = self.tc.auth_password('unresponsive-server', 'hello') + except: + etype, evalue, etb = sys.exc_info() + self.assertTrue(issubclass(etype, AuthenticationException)) + self.assertIn('Authentication timeout', str(evalue)) + finally: + # Restore value + self.tc.auth_timeout = auth_timeout -- cgit v1.2.3 From 0a90a25cded021434630a9deede365dd84021788 Mon Sep 17 00:00:00 2001 From: Tim Savage Date: Fri, 23 Dec 2016 09:55:34 +1100 Subject: Fixed test to support python 2.6 --- tests/test_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_auth.py') diff --git a/tests/test_auth.py b/tests/test_auth.py index 5c0bd264..4fe6a325 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -253,7 +253,7 @@ class AuthTest (unittest.TestCase): except: etype, evalue, etb = sys.exc_info() self.assertTrue(issubclass(etype, AuthenticationException)) - self.assertIn('Authentication timeout', str(evalue)) + self.assertTrue('Authentication timeout' in str(evalue)) finally: # Restore value self.tc.auth_timeout = auth_timeout -- cgit v1.2.3 From 500e2641981f504a7a9a744305935360f5973222 Mon Sep 17 00:00:00 2001 From: Pierce Lopez Date: Tue, 6 Jun 2017 18:13:30 -0400 Subject: cleanup/simplify auth_timeout tests --- tests/test_auth.py | 22 ++++++++-------------- tests/test_client.py | 16 +++++----------- 2 files changed, 13 insertions(+), 25 deletions(-) (limited to 'tests/test_auth.py') diff --git a/tests/test_auth.py b/tests/test_auth.py index 58b2f44f..e78397c6 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -243,18 +243,12 @@ class AuthTest (unittest.TestCase): verify that authentication times out if server takes to long to respond (or never responds). """ - auth_timeout = self.tc.auth_timeout - self.tc.auth_timeout = 2 # Reduce to 2 seconds to speed up test - + self.tc.auth_timeout = 1 # 1 second, to speed up test + self.start_server() + self.tc.connect() try: - self.start_server() - self.tc.connect() - try: - remain = self.tc.auth_password('unresponsive-server', 'hello') - except: - etype, evalue, etb = sys.exc_info() - self.assertTrue(issubclass(etype, AuthenticationException)) - self.assertTrue('Authentication timeout' in str(evalue)) - finally: - # Restore value - self.tc.auth_timeout = auth_timeout + remain = self.tc.auth_password('unresponsive-server', 'hello') + except: + etype, evalue, etb = sys.exc_info() + self.assertTrue(issubclass(etype, AuthenticationException)) + self.assertTrue('Authentication timeout' in str(evalue)) diff --git a/tests/test_client.py b/tests/test_client.py index aa3ff59b..ad0561f2 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -36,7 +36,7 @@ from tests.util import test_path import paramiko from paramiko.common import PY2 -from paramiko.ssh_exception import SSHException +from paramiko.ssh_exception import SSHException, AuthenticationException FINGERPRINTS = { @@ -391,18 +391,12 @@ class SSHClientTest (unittest.TestCase): """ verify that the SSHClient has a configurable auth timeout """ - threading.Thread(target=self._run).start() - host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key')) - public_host_key = paramiko.RSAKey(data=host_key.asbytes()) - - self.tc = paramiko.SSHClient() - self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) # Connect with a half second auth timeout - kwargs = dict(self.connect_kwargs, password='unresponsive-server', auth_timeout=0.5) self.assertRaises( - paramiko.AuthenticationException, - self.tc.connect, - **kwargs + AuthenticationException, + self._test_connection, + password='unresponsive-server', + auth_timeout=0.5, ) def test_update_environment(self): -- cgit v1.2.3