def test_is_missing_host_key(self): client = paramiko.SSHClient() file1 = 'tests/known_hosts_example' file2 = 'tests/known_hosts_example2' client.load_host_keys(file1) client.load_system_host_keys(file2) autoadd = AutoAddPolicy() for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0] key = entry.key self.assertIsNone( autoadd.is_missing_host_key(client, hostname, key) ) for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0][1:] key = entry.key self.assertTrue( autoadd.is_missing_host_key(client, hostname, key) ) file3 = 'tests/known_hosts_example3' entry = paramiko.hostkeys.HostKeys(file3)._entries[0] hostname = entry.hostnames[0] key = entry.key with self.assertRaises(paramiko.BadHostKeyException): autoadd.is_missing_host_key(client, hostname, key)
def test_is_missing_host_key(self): client = paramiko.SSHClient() file1 = make_tests_data_path('known_hosts_example') file2 = make_tests_data_path('known_hosts_example2') client.load_host_keys(file1) client.load_system_host_keys(file2) autoadd = AutoAddPolicy() for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0] key = entry.key self.assertIsNone( autoadd.is_missing_host_key(client, hostname, key)) for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0] key = entry.key key.get_name = lambda: 'unknown' self.assertTrue(autoadd.is_missing_host_key(client, hostname, key)) del key.get_name for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0][1:] key = entry.key self.assertTrue(autoadd.is_missing_host_key(client, hostname, key)) file3 = make_tests_data_path('known_hosts_example3') entry = paramiko.hostkeys.HostKeys(file3)._entries[0] hostname = entry.hostnames[0] key = entry.key with self.assertRaises(paramiko.BadHostKeyException): autoadd.is_missing_host_key(client, hostname, key)
def test_missing_host_key(self): client = paramiko.SSHClient() file1 = os.path.join(base_dir, 'tests', 'known_hosts_example') file2 = os.path.join(base_dir, 'tests', 'known_hosts_example2') filename = os.path.join(base_dir, 'tests', 'known_hosts') copyfile(file1, filename) client.load_host_keys(filename) n1 = len(client._host_keys) autoadd = AutoAddPolicy() entry = paramiko.hostkeys.HostKeys(file2)._entries[0] hostname = entry.hostnames[0] key = entry.key autoadd.missing_host_key(client, hostname, key) self.assertEqual(len(client._host_keys), n1 + 1) self.assertEqual(paramiko.hostkeys.HostKeys(filename), client._host_keys) os.unlink(filename)
def test_missing_host_key(self): client = paramiko.SSHClient() file1 = 'tests/known_hosts_example' file2 = 'tests/known_hosts_example2' filename = 'tests/known_hosts' copyfile(file1, filename) client.load_host_keys(filename) n1 = len(client._host_keys) autoadd = AutoAddPolicy() entry = paramiko.hostkeys.HostKeys(file2)._entries[0] hostname = entry.hostnames[0] key = entry.key autoadd.missing_host_key(client, hostname, key) self.assertEqual(len(client._host_keys), n1 + 1) self.assertEqual(paramiko.hostkeys.HostKeys(filename), client._host_keys) os.unlink(filename)
def test_is_missing_host_key(self): client = paramiko.SSHClient() file1 = os.path.join(base_dir, 'tests', 'known_hosts_example') file2 = os.path.join(base_dir, 'tests', 'known_hosts_example2') client.load_host_keys(file1) client.load_system_host_keys(file2) autoadd = AutoAddPolicy() for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0] key = entry.key self.assertIsNone( autoadd.is_missing_host_key(client, hostname, key)) for f in [file1, file2]: entry = paramiko.hostkeys.HostKeys(f)._entries[0] hostname = entry.hostnames[0][1:] key = entry.key self.assertTrue(autoadd.is_missing_host_key(client, hostname, key)) file3 = os.path.join(base_dir, 'tests', 'known_hosts_example3') entry = paramiko.hostkeys.HostKeys(file3)._entries[0] hostname = entry.hostnames[0] key = entry.key with self.assertRaises(paramiko.BadHostKeyException): autoadd.is_missing_host_key(client, hostname, key)