| 1 |
# Copyright (C) 2003-2009 Robey Pointer <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of paramiko. |
| 4 |
# |
| 5 |
# Paramiko is free software; you can redistribute it and/or modify it under the |
| 6 |
# terms of the GNU Lesser General Public License as published by the Free |
| 7 |
# Software Foundation; either version 2.1 of the License, or (at your option) |
| 8 |
# any later version. |
| 9 |
# |
| 10 |
# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 |
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 13 |
# details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public License |
| 16 |
# along with Paramiko; if not, write to the Free Software Foundation, Inc., |
| 17 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
|
| 19 |
""" |
| 20 |
Some unit tests for utility functions. |
| 21 |
""" |
| 22 |
|
| 23 |
from binascii import hexlify |
| 24 |
import os |
| 25 |
from hashlib import sha1 |
| 26 |
import unittest |
| 27 |
from importlib import metadata |
| 28 |
|
| 29 |
import paramiko |
| 30 |
import paramiko.util |
| 31 |
from paramiko.util import safe_string |
| 32 |
|
| 33 |
|
| 34 |
test_hosts_file = """\ |
| 35 |
secure.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA1PD6U2/TVxET6lkpKhOk5r\ |
| 36 |
9q/kAYG6sP9f5zuUYP8i7FOFp/6ncCEbbtg/lB+A3iidyxoSWl+9jtoyyDOOVX4UIDV9G11Ml8om3\ |
| 37 |
D+jrpI9cycZHqilK0HmxDeCuxbwyMuaCygU9gS2qoRvNLWZk70OpIKSSpBo0Wl3/XUmz9uhc= |
| 38 |
happy.example.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA8bP1ZA7DCZDB9J0s50l31M\ |
| 39 |
BGQ3GQ/Fc7SX6gkpXkwcZryoi4kNFhHu5LvHcZPdxXV1D+uTMfGS1eyd2Yz/DoNWXNAl8TI0cAsW\ |
| 40 |
5ymME3bQ4J/k1IKxCtz/bAlAqFgKoc+EolMziDYqWIATtW0rYTJvzGAzTmMj80/QpsFH+Pc2M= |
| 41 |
""" |
| 42 |
|
| 43 |
|
| 44 |
class UtilTest(unittest.TestCase): |
| 45 |
def test_imports(self): |
| 46 |
""" |
| 47 |
Verify that all the classes can be imported from paramiko. |
| 48 |
""" |
| 49 |
for name in ( |
| 50 |
"Agent", |
| 51 |
"AgentKey", |
| 52 |
"AuthenticationException", |
| 53 |
"AuthFailure", |
| 54 |
"AuthHandler", |
| 55 |
"AuthResult", |
| 56 |
"AuthSource", |
| 57 |
"AuthStrategy", |
| 58 |
"AutoAddPolicy", |
| 59 |
"BadAuthenticationType", |
| 60 |
"BufferedFile", |
| 61 |
"Channel", |
| 62 |
"ChannelException", |
| 63 |
"ConfigParseError", |
| 64 |
"CouldNotCanonicalize", |
| 65 |
"ECDSAKey", |
| 66 |
"Ed25519Key", |
| 67 |
"HostKeys", |
| 68 |
"InMemoryPrivateKey", |
| 69 |
"Message", |
| 70 |
"MissingHostKeyPolicy", |
| 71 |
"NoneAuth", |
| 72 |
"OnDiskPrivateKey", |
| 73 |
"Password", |
| 74 |
"PasswordRequiredException", |
| 75 |
"PrivateKey", |
| 76 |
"RSAKey", |
| 77 |
"RejectPolicy", |
| 78 |
"SFTP", |
| 79 |
"SFTPAttributes", |
| 80 |
"SFTPClient", |
| 81 |
"SFTPError", |
| 82 |
"SFTPFile", |
| 83 |
"SFTPHandle", |
| 84 |
"SFTPServer", |
| 85 |
"SFTPServerInterface", |
| 86 |
"SSHClient", |
| 87 |
"SSHConfig", |
| 88 |
"SSHConfigDict", |
| 89 |
"SSHException", |
| 90 |
"SecurityOptions", |
| 91 |
"ServerInterface", |
| 92 |
"SourceResult", |
| 93 |
"SubsystemHandler", |
| 94 |
"Transport", |
| 95 |
"WarningPolicy", |
| 96 |
"util", |
| 97 |
): |
| 98 |
assert name in dir(paramiko) |
| 99 |
|
| 100 |
def test_version_available_in_main_module(self): |
| 101 |
assert paramiko.__version__ == metadata.version("paramiko") |
| 102 |
|
| 103 |
def test_generate_key_bytes(self): |
| 104 |
key_bytes = paramiko.util.generate_key_bytes( |
| 105 |
sha1, b"ABCDEFGH", "This is my secret passphrase.", 64 |
| 106 |
) |
| 107 |
hexy = "".join([f"{byte:02x}" for byte in key_bytes]) |
| 108 |
hexpected = "9110e2f6793b69363e58173e9436b13a5a4b339005741d5c680e505f57d871347b4239f14fb5c46e857d5e100424873ba849ac699cea98d729e57b3e84378e8b" # noqa |
| 109 |
assert hexy == hexpected |
| 110 |
|
| 111 |
def test_host_keys(self): |
| 112 |
with open("hostfile.temp", "w") as f: |
| 113 |
f.write(test_hosts_file) |
| 114 |
try: |
| 115 |
hostdict = paramiko.util.load_host_keys("hostfile.temp") |
| 116 |
assert 2 == len(hostdict) |
| 117 |
assert 1 == len(list(hostdict.values())[0]) |
| 118 |
assert 1 == len(list(hostdict.values())[1]) |
| 119 |
fp = hexlify( |
| 120 |
hostdict["secure.example.com"]["ssh-rsa"].get_fingerprint() |
| 121 |
).upper() |
| 122 |
assert b"E6684DB30E109B67B70FF1DC5C7F1363" == fp |
| 123 |
finally: |
| 124 |
os.unlink("hostfile.temp") |
| 125 |
|
| 126 |
def test_clamp_value(self): |
| 127 |
assert 32768 == paramiko.util.clamp_value(32767, 32768, 32769) |
| 128 |
assert 32767 == paramiko.util.clamp_value(32767, 32765, 32769) |
| 129 |
assert 32769 == paramiko.util.clamp_value(32767, 32770, 32769) |
| 130 |
|
| 131 |
def test_safe_string(self): |
| 132 |
vanilla = b"vanilla" |
| 133 |
has_bytes = b"has \7\3 bytes" |
| 134 |
safe_vanilla = safe_string(vanilla) |
| 135 |
safe_has_bytes = safe_string(has_bytes) |
| 136 |
expected_bytes = b"has %07%03 bytes" |
| 137 |
err = "{!r} != {!r}" |
| 138 |
msg = err.format(safe_vanilla, vanilla) |
| 139 |
assert safe_vanilla == vanilla, msg |
| 140 |
msg = err.format(safe_has_bytes, expected_bytes) |
| 141 |
assert safe_has_bytes == expected_bytes, msg |