From bfc3953be000e426551c90d65a0633c62c9fde89 Mon Sep 17 00:00:00 2001 From: Martin Blumenstingl Date: Tue, 7 Jan 2014 22:36:28 +0100 Subject: Add a testcase for client.save_host_keys. --- tests/test_client.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'tests/test_client.py') diff --git a/tests/test_client.py b/tests/test_client.py index e5352278..7aaa1bfc 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -25,6 +25,8 @@ import threading import time import unittest import weakref +import warnings +import os from binascii import hexlify import paramiko @@ -184,7 +186,29 @@ class SSHClientTest (unittest.TestCase): self.assertEquals(1, len(self.tc.get_host_keys())) self.assertEquals(public_host_key, self.tc.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) - def test_5_cleanup(self): + def test_5_save_host_keys(self): + """ + verify that SSHClient correctly saves a known_hosts file. + """ + warnings.filterwarnings('ignore', 'tempnam.*') + + host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') + public_host_key = paramiko.RSAKey(data=str(host_key)) + localname = os.tempnam() + + self.tc = paramiko.SSHClient() + self.assertEquals(0, len(self.tc.get_host_keys())) + + self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) + self.assertEquals(1, len(self.tc.get_host_keys())) + self.assertEquals(public_host_key, self.tc.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) + + self.tc.save_host_keys(localname) + self.assertEquals(len('[%s]:%d' % (self.addr, self.port)) + 210, os.path.getsize(localname)) + + os.unlink(localname) + + def test_6_cleanup(self): """ verify that when an SSHClient is collected, its transport (and the transport's packetizer) is closed. -- cgit v1.2.3 From 78d9e4834cb9e4bb0060b419c8677660b016f4d2 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 8 Jan 2014 12:35:46 -0800 Subject: No need for 'self.tc' within a single test :) --- tests/test_client.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'tests/test_client.py') diff --git a/tests/test_client.py b/tests/test_client.py index 7aaa1bfc..7be2d73e 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -196,14 +196,14 @@ class SSHClientTest (unittest.TestCase): public_host_key = paramiko.RSAKey(data=str(host_key)) localname = os.tempnam() - self.tc = paramiko.SSHClient() - self.assertEquals(0, len(self.tc.get_host_keys())) + client = paramiko.SSHClient() + self.assertEquals(0, len(client.get_host_keys())) - self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) - self.assertEquals(1, len(self.tc.get_host_keys())) - self.assertEquals(public_host_key, self.tc.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) + client.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) + self.assertEquals(1, len(client.get_host_keys())) + self.assertEquals(public_host_key, client.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) - self.tc.save_host_keys(localname) + client.save_host_keys(localname) self.assertEquals(len('[%s]:%d' % (self.addr, self.port)) + 210, os.path.getsize(localname)) os.unlink(localname) -- cgit v1.2.3 From 74e06aff9e1869dece030b61e99365150fb2e315 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 8 Jan 2014 12:39:11 -0800 Subject: Small refactor --- tests/test_client.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'tests/test_client.py') diff --git a/tests/test_client.py b/tests/test_client.py index 7be2d73e..ad80e720 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -199,12 +199,14 @@ class SSHClientTest (unittest.TestCase): client = paramiko.SSHClient() self.assertEquals(0, len(client.get_host_keys())) - client.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) + host_id = '[%s]:%d' % (self.addr, self.port) + + client.get_host_keys().add(host_id, 'ssh-rsa', public_host_key) self.assertEquals(1, len(client.get_host_keys())) - self.assertEquals(public_host_key, client.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) + self.assertEquals(public_host_key, client.get_host_keys()[host_id]['ssh-rsa']) client.save_host_keys(localname) - self.assertEquals(len('[%s]:%d' % (self.addr, self.port)) + 210, os.path.getsize(localname)) + self.assertEquals(len(host_id) + 210, os.path.getsize(localname)) os.unlink(localname) -- cgit v1.2.3 From 6d326fcde20dece926c2cf661991590e20a00eab Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 8 Jan 2014 12:44:12 -0800 Subject: Saner (to me) positive assertion --- tests/test_client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tests/test_client.py') diff --git a/tests/test_client.py b/tests/test_client.py index ad80e720..fae1d329 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -20,6 +20,7 @@ Some unit tests for SSHClient. """ +from __future__ import with_statement # Python 2.5 support import socket import threading import time @@ -206,7 +207,9 @@ class SSHClientTest (unittest.TestCase): self.assertEquals(public_host_key, client.get_host_keys()[host_id]['ssh-rsa']) client.save_host_keys(localname) - self.assertEquals(len(host_id) + 210, os.path.getsize(localname)) + + with open(localname) as fd: + assert host_id in fd.read() os.unlink(localname) -- cgit v1.2.3