Skip to content

Commit

Permalink
Overhaul multi-key test to set up multiple scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
bitprophet committed Sep 6, 2014
1 parent d8047a2 commit dd0c618
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,26 @@ def tearDown(self):
if hasattr(self, attr):
getattr(self, attr).close()

def _run(self):
def _run(self, allowed_keys=None):
if allowed_keys is None:
allowed_keys = FINGERPRINTS.keys()
self.socks, addr = self.sockl.accept()
self.ts = paramiko.Transport(self.socks)
host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key'))
self.ts.add_server_key(host_key)
server = NullServer()
server = NullServer(allowed_keys=allowed_keys)
self.ts.start_server(self.event, server)

def _test_connection(self, **kwargs):
"""
kwargs get passed directly into SSHClient.connect().
(Most) kwargs get passed directly into SSHClient.connect().
The exception is ``allowed_keys`` which is stripped and handed to the
``NullServer`` used for testing.
"""
run_kwargs = {'allowed_keys': kwargs.pop('allowed_keys', None)}
# Server setup
threading.Thread(target=self._run).start()
threading.Thread(target=self._run, kwargs=run_kwargs).start()
host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key'))
public_host_key = paramiko.RSAKey(data=host_key.asbytes())

Expand Down Expand Up @@ -172,9 +178,23 @@ def test_3_multiple_key_files(self):
"""
verify that SSHClient accepts and tries multiple key files.
"""
self._test_connection(key_filename=[
test_path('test_rsa.key'), test_path('test_dss.key')
])
# This is dumb :(
types_ = {
'rsa': 'ssh-rsa',
'dss': 'ssh-dss',
'ecdsa': 'ecdsa-sha2-nistp256',
}
# Various combos of attempted & valid keys
for attempt, accept in (
(['rsa', 'dss'], ['dss']), # Original test #3
(['dss', 'rsa'], ['dss']), # Ordering matters sometimes, sadly
):
self._test_connection(
key_filename=[
test_path('test_{0}.key'.format(x)) for x in attempt
],
allowed_keys=[types_[x] for x in accept],
)

def test_4_auto_add_policy(self):
"""
Expand Down

0 comments on commit dd0c618

Please sign in to comment.