2009-07-19 22:45:02 -04:00
|
|
|
# Copyright (C) 2003-2009 Robey Pointer <robeypointer@gmail.com>
|
2005-12-04 04:04:14 -05:00
|
|
|
#
|
|
|
|
# This file is part of paramiko.
|
|
|
|
#
|
|
|
|
# Paramiko is free software; you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
# Software Foundation; either version 2.1 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
2013-09-28 00:29:18 -04:00
|
|
|
# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
|
2005-12-04 04:04:14 -05:00
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Some unit tests for utility functions.
|
|
|
|
"""
|
|
|
|
|
2006-08-01 14:21:43 -04:00
|
|
|
from binascii import hexlify
|
2012-09-03 00:48:00 -04:00
|
|
|
import errno
|
2006-02-19 19:35:13 -05:00
|
|
|
import os
|
2005-12-04 04:04:14 -05:00
|
|
|
import unittest
|
|
|
|
from Crypto.Hash import SHA
|
|
|
|
import paramiko.util
|
2012-11-05 20:04:45 -05:00
|
|
|
from paramiko.util import lookup_ssh_host_config as host_config
|
2013-10-31 13:01:21 -04:00
|
|
|
from paramiko.py3compat import StringIO, byte_ord, b
|
2005-12-04 04:04:14 -05:00
|
|
|
|
2013-10-30 19:19:30 -04:00
|
|
|
from tests.util import ParamikoTest
|
2005-12-04 04:04:14 -05:00
|
|
|
|
|
|
|
test_config_file = """\
|
|
|
|
Host *
|
|
|
|
User robey
|
|
|
|
IdentityFile =~/.ssh/id_rsa
|
|
|
|
|
|
|
|
# comment
|
|
|
|
Host *.example.com
|
|
|
|
\tUser bjork
|
|
|
|
Port=3333
|
|
|
|
Host *
|
|
|
|
\t \t Crazy something dumb
|
|
|
|
Host spoo.example.com
|
|
|
|
Crazy something else
|
|
|
|
"""
|
|
|
|
|
2006-02-19 19:35:13 -05:00
|
|
|
test_hosts_file = """\
|
|
|
|
secure.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA1PD6U2/TVxET6lkpKhOk5r\
|
|
|
|
9q/kAYG6sP9f5zuUYP8i7FOFp/6ncCEbbtg/lB+A3iidyxoSWl+9jtoyyDOOVX4UIDV9G11Ml8om3\
|
|
|
|
D+jrpI9cycZHqilK0HmxDeCuxbwyMuaCygU9gS2qoRvNLWZk70OpIKSSpBo0Wl3/XUmz9uhc=
|
|
|
|
happy.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZDB9J0s50l31M\
|
|
|
|
BGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW\
|
|
|
|
5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M=
|
|
|
|
"""
|
2005-12-04 04:04:14 -05:00
|
|
|
|
|
|
|
|
2006-08-13 15:50:45 -04:00
|
|
|
# for test 1:
|
|
|
|
from paramiko import *
|
|
|
|
|
|
|
|
|
2012-10-15 16:21:58 -04:00
|
|
|
class UtilTest(ParamikoTest):
|
2006-08-13 15:50:45 -04:00
|
|
|
def test_1_import(self):
|
|
|
|
"""
|
|
|
|
verify that all the classes can be imported from paramiko.
|
|
|
|
"""
|
2013-10-30 19:46:33 -04:00
|
|
|
symbols = list(globals().keys())
|
2006-08-13 15:50:45 -04:00
|
|
|
self.assertTrue('Transport' in symbols)
|
|
|
|
self.assertTrue('SSHClient' in symbols)
|
|
|
|
self.assertTrue('MissingHostKeyPolicy' in symbols)
|
|
|
|
self.assertTrue('AutoAddPolicy' in symbols)
|
|
|
|
self.assertTrue('RejectPolicy' in symbols)
|
2008-02-20 01:00:30 -05:00
|
|
|
self.assertTrue('WarningPolicy' in symbols)
|
2006-08-13 15:50:45 -04:00
|
|
|
self.assertTrue('SecurityOptions' in symbols)
|
|
|
|
self.assertTrue('SubsystemHandler' in symbols)
|
|
|
|
self.assertTrue('Channel' in symbols)
|
|
|
|
self.assertTrue('RSAKey' in symbols)
|
|
|
|
self.assertTrue('DSSKey' in symbols)
|
|
|
|
self.assertTrue('Message' in symbols)
|
|
|
|
self.assertTrue('SSHException' in symbols)
|
|
|
|
self.assertTrue('AuthenticationException' in symbols)
|
|
|
|
self.assertTrue('PasswordRequiredException' in symbols)
|
|
|
|
self.assertTrue('BadAuthenticationType' in symbols)
|
|
|
|
self.assertTrue('ChannelException' in symbols)
|
|
|
|
self.assertTrue('SFTP' in symbols)
|
|
|
|
self.assertTrue('SFTPFile' in symbols)
|
|
|
|
self.assertTrue('SFTPHandle' in symbols)
|
|
|
|
self.assertTrue('SFTPClient' in symbols)
|
|
|
|
self.assertTrue('SFTPServer' in symbols)
|
|
|
|
self.assertTrue('SFTPError' in symbols)
|
|
|
|
self.assertTrue('SFTPAttributes' in symbols)
|
|
|
|
self.assertTrue('SFTPServerInterface' in symbols)
|
|
|
|
self.assertTrue('ServerInterface' in symbols)
|
|
|
|
self.assertTrue('BufferedFile' in symbols)
|
|
|
|
self.assertTrue('Agent' in symbols)
|
|
|
|
self.assertTrue('AgentKey' in symbols)
|
|
|
|
self.assertTrue('HostKeys' in symbols)
|
|
|
|
self.assertTrue('SSHConfig' in symbols)
|
|
|
|
self.assertTrue('util' in symbols)
|
|
|
|
|
|
|
|
def test_2_parse_config(self):
|
2005-12-04 04:04:14 -05:00
|
|
|
global test_config_file
|
2013-10-30 20:14:52 -04:00
|
|
|
f = StringIO(test_config_file)
|
2005-12-04 04:04:14 -05:00
|
|
|
config = paramiko.util.parse_ssh_config(f)
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(config._config,
|
2012-10-16 07:52:21 -04:00
|
|
|
[{'host': ['*'], 'config': {}}, {'host': ['*'], 'config': {'identityfile': ['~/.ssh/id_rsa'], 'user': 'robey'}},
|
|
|
|
{'host': ['*.example.com'], 'config': {'user': 'bjork', 'port': '3333'}},
|
|
|
|
{'host': ['*'], 'config': {'crazy': 'something dumb '}},
|
|
|
|
{'host': ['spoo.example.com'], 'config': {'crazy': 'something else'}}])
|
2005-12-04 04:04:14 -05:00
|
|
|
|
2006-08-13 15:50:45 -04:00
|
|
|
def test_3_host_config(self):
|
2005-12-04 04:04:14 -05:00
|
|
|
global test_config_file
|
2013-10-30 20:14:52 -04:00
|
|
|
f = StringIO(test_config_file)
|
2005-12-04 04:04:14 -05:00
|
|
|
config = paramiko.util.parse_ssh_config(f)
|
2012-10-16 07:52:21 -04:00
|
|
|
|
2012-05-08 01:03:13 -04:00
|
|
|
for host, values in {
|
2012-10-16 07:52:21 -04:00
|
|
|
'irc.danger.com': {'crazy': 'something dumb ',
|
|
|
|
'hostname': 'irc.danger.com',
|
|
|
|
'user': 'robey'},
|
|
|
|
'irc.example.com': {'crazy': 'something dumb ',
|
|
|
|
'hostname': 'irc.example.com',
|
|
|
|
'user': 'robey',
|
|
|
|
'port': '3333'},
|
|
|
|
'spoo.example.com': {'crazy': 'something dumb ',
|
|
|
|
'hostname': 'spoo.example.com',
|
|
|
|
'user': 'robey',
|
|
|
|
'port': '3333'}
|
2012-05-08 01:03:13 -04:00
|
|
|
}.items():
|
|
|
|
values = dict(values,
|
|
|
|
hostname=host,
|
2012-10-16 07:52:21 -04:00
|
|
|
identityfile=[os.path.expanduser("~/.ssh/id_rsa")]
|
2012-05-08 01:03:13 -04:00
|
|
|
)
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(
|
2012-05-08 01:03:13 -04:00
|
|
|
paramiko.util.lookup_ssh_host_config(host, config),
|
|
|
|
values
|
|
|
|
)
|
2005-12-04 04:04:14 -05:00
|
|
|
|
2006-08-13 15:50:45 -04:00
|
|
|
def test_4_generate_key_bytes(self):
|
2013-11-19 13:09:08 -05:00
|
|
|
x = paramiko.util.generate_key_bytes(SHA, b'ABCDEFGH', 'This is my secret passphrase.', 64)
|
2013-10-30 20:14:52 -04:00
|
|
|
hex = ''.join(['%02x' % byte_ord(c) for c in x])
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(hex, '9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b')
|
2006-02-19 19:35:13 -05:00
|
|
|
|
2006-08-13 15:50:45 -04:00
|
|
|
def test_5_host_keys(self):
|
2013-11-19 11:56:53 -05:00
|
|
|
with open('hostfile.temp', 'w') as f:
|
|
|
|
f.write(test_hosts_file)
|
2006-02-19 19:35:13 -05:00
|
|
|
try:
|
|
|
|
hostdict = paramiko.util.load_host_keys('hostfile.temp')
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(2, len(hostdict))
|
|
|
|
self.assertEqual(1, len(list(hostdict.values())[0]))
|
|
|
|
self.assertEqual(1, len(list(hostdict.values())[1]))
|
2006-08-01 14:21:43 -04:00
|
|
|
fp = hexlify(hostdict['secure.example.com']['ssh-rsa'].get_fingerprint()).upper()
|
2013-11-19 13:09:08 -05:00
|
|
|
self.assertEqual(b'E6684DB30E109B67B70FF1DC5C7F1363', fp)
|
2006-02-19 19:35:13 -05:00
|
|
|
finally:
|
|
|
|
os.unlink('hostfile.temp')
|
|
|
|
|
2008-02-17 21:41:39 -05:00
|
|
|
def test_6_random(self):
|
2010-08-02 18:13:08 -04:00
|
|
|
from paramiko.common import rng
|
2008-02-17 21:41:39 -05:00
|
|
|
# just verify that we can pull out 32 bytes and not get an exception.
|
2010-08-02 18:13:08 -04:00
|
|
|
x = rng.read(32)
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(len(x), 32)
|
2012-10-16 07:52:21 -04:00
|
|
|
|
|
|
|
def test_7_host_config_expose_issue_33(self):
|
2012-07-12 11:12:26 -04:00
|
|
|
test_config_file = """
|
|
|
|
Host www13.*
|
|
|
|
Port 22
|
|
|
|
|
|
|
|
Host *.example.com
|
|
|
|
Port 2222
|
|
|
|
|
|
|
|
Host *
|
|
|
|
Port 3333
|
|
|
|
"""
|
2013-10-30 20:14:52 -04:00
|
|
|
f = StringIO(test_config_file)
|
2012-09-25 21:11:30 -04:00
|
|
|
config = paramiko.util.parse_ssh_config(f)
|
2012-07-12 11:12:26 -04:00
|
|
|
host = 'www13.example.com'
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(
|
2012-09-25 21:11:30 -04:00
|
|
|
paramiko.util.lookup_ssh_host_config(host, config),
|
2012-07-12 11:12:26 -04:00
|
|
|
{'hostname': host, 'port': '22'}
|
|
|
|
)
|
2012-09-03 00:48:00 -04:00
|
|
|
|
|
|
|
def test_8_eintr_retry(self):
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual('foo', paramiko.util.retry_on_signal(lambda: 'foo'))
|
2012-09-03 00:48:00 -04:00
|
|
|
|
|
|
|
# Variables that are set by raises_intr
|
|
|
|
intr_errors_remaining = [3]
|
|
|
|
call_count = [0]
|
|
|
|
def raises_intr():
|
|
|
|
call_count[0] += 1
|
|
|
|
if intr_errors_remaining[0] > 0:
|
|
|
|
intr_errors_remaining[0] -= 1
|
|
|
|
raise IOError(errno.EINTR, 'file', 'interrupted system call')
|
2012-09-25 21:11:30 -04:00
|
|
|
self.assertTrue(paramiko.util.retry_on_signal(raises_intr) is None)
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(0, intr_errors_remaining[0])
|
|
|
|
self.assertEqual(4, call_count[0])
|
2012-09-03 00:48:00 -04:00
|
|
|
|
|
|
|
def raises_ioerror_not_eintr():
|
|
|
|
raise IOError(errno.ENOENT, 'file', 'file not found')
|
|
|
|
self.assertRaises(IOError,
|
2012-09-25 21:11:30 -04:00
|
|
|
lambda: paramiko.util.retry_on_signal(raises_ioerror_not_eintr))
|
2012-09-03 00:48:00 -04:00
|
|
|
|
|
|
|
def raises_other_exception():
|
|
|
|
raise AssertionError('foo')
|
|
|
|
self.assertRaises(AssertionError,
|
2012-09-25 21:11:30 -04:00
|
|
|
lambda: paramiko.util.retry_on_signal(raises_other_exception))
|
2012-11-05 20:04:45 -05:00
|
|
|
|
2012-11-05 20:44:25 -05:00
|
|
|
def test_9_proxycommand_config_equals_parsing(self):
|
2012-11-05 20:04:45 -05:00
|
|
|
"""
|
|
|
|
ProxyCommand should not split on equals signs within the value.
|
|
|
|
"""
|
|
|
|
conf = """
|
|
|
|
Host space-delimited
|
|
|
|
ProxyCommand foo bar=biz baz
|
|
|
|
|
|
|
|
Host equals-delimited
|
|
|
|
ProxyCommand=foo bar=biz baz
|
|
|
|
"""
|
2013-10-30 20:14:52 -04:00
|
|
|
f = StringIO(conf)
|
2012-11-05 20:04:45 -05:00
|
|
|
config = paramiko.util.parse_ssh_config(f)
|
|
|
|
for host in ('space-delimited', 'equals-delimited'):
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(
|
2012-11-05 20:04:45 -05:00
|
|
|
host_config(host, config)['proxycommand'],
|
|
|
|
'foo bar=biz baz'
|
|
|
|
)
|
2012-11-05 20:44:25 -05:00
|
|
|
|
|
|
|
def test_10_proxycommand_interpolation(self):
|
|
|
|
"""
|
|
|
|
ProxyCommand should perform interpolation on the value
|
|
|
|
"""
|
2013-10-30 20:14:52 -04:00
|
|
|
config = paramiko.util.parse_ssh_config(StringIO("""
|
2012-11-05 20:44:25 -05:00
|
|
|
Host specific
|
|
|
|
Port 37
|
|
|
|
ProxyCommand host %h port %p lol
|
|
|
|
|
|
|
|
Host portonly
|
|
|
|
Port 155
|
2013-02-28 06:36:03 -05:00
|
|
|
|
|
|
|
Host *
|
|
|
|
Port 25
|
|
|
|
ProxyCommand host %h port %p
|
2012-11-05 20:44:25 -05:00
|
|
|
"""))
|
|
|
|
for host, val in (
|
|
|
|
('foo.com', "host foo.com port 25"),
|
|
|
|
('specific', "host specific port 37 lol"),
|
|
|
|
('portonly', "host portonly port 155"),
|
|
|
|
):
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(
|
2012-11-05 20:44:25 -05:00
|
|
|
host_config(host, config)['proxycommand'],
|
|
|
|
val
|
|
|
|
)
|
2012-10-16 07:53:06 -04:00
|
|
|
|
|
|
|
def test_11_host_config_test_negation(self):
|
|
|
|
test_config_file = """
|
|
|
|
Host www13.* !*.example.com
|
|
|
|
Port 22
|
|
|
|
|
|
|
|
Host *.example.com !www13.*
|
|
|
|
Port 2222
|
|
|
|
|
|
|
|
Host www13.*
|
|
|
|
Port 8080
|
|
|
|
|
|
|
|
Host *
|
|
|
|
Port 3333
|
|
|
|
"""
|
2013-10-30 20:14:52 -04:00
|
|
|
f = StringIO(test_config_file)
|
2012-10-16 07:53:06 -04:00
|
|
|
config = paramiko.util.parse_ssh_config(f)
|
|
|
|
host = 'www13.example.com'
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(
|
2012-10-16 07:53:06 -04:00
|
|
|
paramiko.util.lookup_ssh_host_config(host, config),
|
|
|
|
{'hostname': host, 'port': '8080'}
|
|
|
|
)
|
2012-10-16 11:02:04 -04:00
|
|
|
|
|
|
|
def test_12_host_config_test_proxycommand(self):
|
2012-10-16 10:38:09 -04:00
|
|
|
test_config_file = """
|
|
|
|
Host proxy-with-equal-divisor-and-space
|
|
|
|
ProxyCommand = foo=bar
|
|
|
|
|
|
|
|
Host proxy-with-equal-divisor-and-no-space
|
|
|
|
ProxyCommand=foo=bar
|
|
|
|
|
|
|
|
Host proxy-without-equal-divisor
|
|
|
|
ProxyCommand foo=bar:%h-%p
|
|
|
|
"""
|
|
|
|
for host, values in {
|
|
|
|
'proxy-with-equal-divisor-and-space' :{'hostname': 'proxy-with-equal-divisor-and-space',
|
|
|
|
'proxycommand': 'foo=bar'},
|
|
|
|
'proxy-with-equal-divisor-and-no-space':{'hostname': 'proxy-with-equal-divisor-and-no-space',
|
|
|
|
'proxycommand': 'foo=bar'},
|
|
|
|
'proxy-without-equal-divisor' :{'hostname': 'proxy-without-equal-divisor',
|
|
|
|
'proxycommand':
|
|
|
|
'foo=bar:proxy-without-equal-divisor-22'}
|
|
|
|
}.items():
|
|
|
|
|
2013-10-30 20:14:52 -04:00
|
|
|
f = StringIO(test_config_file)
|
2012-10-16 10:38:09 -04:00
|
|
|
config = paramiko.util.parse_ssh_config(f)
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(
|
2012-10-16 10:38:09 -04:00
|
|
|
paramiko.util.lookup_ssh_host_config(host, config),
|
|
|
|
values
|
|
|
|
)
|
2012-11-20 06:42:29 -05:00
|
|
|
|
|
|
|
def test_11_host_config_test_identityfile(self):
|
|
|
|
test_config_file = """
|
|
|
|
|
|
|
|
IdentityFile id_dsa0
|
|
|
|
|
|
|
|
Host *
|
|
|
|
IdentityFile id_dsa1
|
|
|
|
|
|
|
|
Host dsa2
|
|
|
|
IdentityFile id_dsa2
|
|
|
|
|
|
|
|
Host dsa2*
|
|
|
|
IdentityFile id_dsa22
|
|
|
|
"""
|
|
|
|
for host, values in {
|
|
|
|
'foo' :{'hostname': 'foo',
|
|
|
|
'identityfile': ['id_dsa0', 'id_dsa1']},
|
|
|
|
'dsa2' :{'hostname': 'dsa2',
|
|
|
|
'identityfile': ['id_dsa0', 'id_dsa1', 'id_dsa2', 'id_dsa22']},
|
|
|
|
'dsa22' :{'hostname': 'dsa22',
|
|
|
|
'identityfile': ['id_dsa0', 'id_dsa1', 'id_dsa22']}
|
|
|
|
}.items():
|
|
|
|
|
2013-10-30 20:14:52 -04:00
|
|
|
f = StringIO(test_config_file)
|
2012-11-20 06:42:29 -05:00
|
|
|
config = paramiko.util.parse_ssh_config(f)
|
2013-11-02 23:18:18 -04:00
|
|
|
self.assertEqual(
|
2012-11-20 06:42:29 -05:00
|
|
|
paramiko.util.lookup_ssh_host_config(host, config),
|
|
|
|
values
|
|
|
|
)
|
2013-09-27 19:08:59 -04:00
|
|
|
|
|
|
|
def test_12_config_addressfamily_and_lazy_fqdn(self):
|
|
|
|
"""
|
|
|
|
Ensure the code path honoring non-'all' AddressFamily doesn't asplode
|
|
|
|
"""
|
|
|
|
test_config = """
|
|
|
|
AddressFamily inet
|
|
|
|
IdentityFile something_%l_using_fqdn
|
|
|
|
"""
|
2013-10-30 20:14:52 -04:00
|
|
|
config = paramiko.util.parse_ssh_config(StringIO(test_config))
|
2013-09-27 19:08:59 -04:00
|
|
|
assert config.lookup('meh') # will die during lookup() if bug regresses
|