diff options
author | Jeff Forcier <jeff@bitprophet.org> | 2023-04-27 21:08:30 -0400 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2023-05-05 12:27:20 -0400 |
commit | 45621fe96644e4e15b421ef665be841ab324564e (patch) | |
tree | d48fb4fb93b74013d5695eb591f27f13484c8f66 /tests | |
parent | 162213fa1a4551bd955134c97ca5276a5f29e907 (diff) |
Enhance PKey.from_path and test it better
Diffstat (limited to 'tests')
-rw-r--r-- | tests/_support/ed448.key | 4 | ||||
-rw-r--r-- | tests/conftest.py | 2 | ||||
-rw-r--r-- | tests/pkey.py | 39 | ||||
-rw-r--r-- | tests/test_client.py | 12 | ||||
-rw-r--r-- | tests/test_pkey.py | 6 |
5 files changed, 40 insertions, 23 deletions
diff --git a/tests/_support/ed448.key b/tests/_support/ed448.key new file mode 100644 index 00000000..887b51c6 --- /dev/null +++ b/tests/_support/ed448.key @@ -0,0 +1,4 @@ +-----BEGIN PRIVATE KEY----- +MEcCAQAwBQYDK2VxBDsEOcvcl9IoD0ktR5RWtW84NM7O2e4LmD2cWfRg7Wht/OA9 +POkmRW12VNvlP6BsXKir5yygumIjD91SQQ== +-----END PRIVATE KEY----- diff --git a/tests/conftest.py b/tests/conftest.py index beef87c2..f3dc2d61 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -136,7 +136,7 @@ for datum in key_data: @pytest.fixture(scope="session", params=key_data, ids=lambda x: x[0]) -def key(request): +def keys(request): """ Yield an object for each known type of key, with attributes: diff --git a/tests/pkey.py b/tests/pkey.py index b1cba825..9c8fe8fc 100644 --- a/tests/pkey.py +++ b/tests/pkey.py @@ -1,13 +1,38 @@ -from paramiko import PKey +from pytest import raises + +from cryptography.hazmat.primitives.asymmetric.ed448 import Ed448PrivateKey +from paramiko import PKey, UnknownKeyType, RSAKey + +from ._util import _support class PKey_: class from_type_string: - def loads_from_type_and_bytes(self, key): - obj = PKey.from_type_string(key.full_type, key.pkey.asbytes()) - assert obj == key.pkey + def loads_from_type_and_bytes(self, keys): + obj = PKey.from_type_string(keys.full_type, keys.pkey.asbytes()) + assert obj == keys.pkey class from_path: - def loads_from_file_path(self, key): - obj = PKey.from_path(key.path) - assert obj == key.pkey + def loads_from_Path(self, keys): + obj = PKey.from_path(keys.path) + assert obj == keys.pkey + + def loads_from_str(self): + key = PKey.from_path(str(_support("rsa.key"))) + assert isinstance(key, RSAKey) + + def raises_UnknownKeyType_for_unknown_types(self): + # I.e. a real, becomes a useful object via cryptography.io, key + # class that we do NOT support. Chose Ed448 randomly as OpenSSH + # doesn't seem to support it either, going by ssh-keygen... + keypath = _support("ed448.key") + with raises(UnknownKeyType) as exc: + PKey.from_path(keypath) + assert issubclass(exc.value.key_type, Ed448PrivateKey) + with open(keypath, "rb") as fd: + assert exc.value.key_bytes == fd.read() + + def leaves_cryptography_exceptions_untouched(self): + # a Python file is not a private key! + with raises(ValueError): + PKey.from_path(__file__) diff --git a/tests/test_client.py b/tests/test_client.py index 564cda00..ea7396d9 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -194,9 +194,7 @@ class ClientTest(unittest.TestCase): run_kwargs[key] = kwargs.pop(key, None) # Server setup threading.Thread(target=self._run, kwargs=run_kwargs).start() - host_key = paramiko.RSAKey.from_private_key_file( - _support("rsa.key") - ) + host_key = paramiko.RSAKey.from_private_key_file(_support("rsa.key")) public_host_key = paramiko.RSAKey(data=host_key.asbytes()) # Client setup @@ -415,9 +413,7 @@ class SSHClientTest(ClientTest): """ warnings.filterwarnings("ignore", "tempnam.*") - host_key = paramiko.RSAKey.from_private_key_file( - _support("rsa.key") - ) + host_key = paramiko.RSAKey.from_private_key_file(_support("rsa.key")) public_host_key = paramiko.RSAKey(data=host_key.asbytes()) fd, localname = mkstemp() os.close(fd) @@ -517,9 +513,7 @@ class SSHClientTest(ClientTest): """ # Start the thread with a 1 second wait. threading.Thread(target=self._run, kwargs={"delay": 1}).start() - host_key = paramiko.RSAKey.from_private_key_file( - _support("rsa.key") - ) + host_key = paramiko.RSAKey.from_private_key_file(_support("rsa.key")) public_host_key = paramiko.RSAKey(data=host_key.asbytes()) self.tc = SSHClient() diff --git a/tests/test_pkey.py b/tests/test_pkey.py index c5b20f91..47e19945 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -138,12 +138,6 @@ TEST_KEY_BYTESTR = "\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x01#\x00\x00\x00\x00ӏV\ class KeyTest(unittest.TestCase): - def setUp(self): - pass - - def tearDown(self): - pass - def assert_keyfile_is_encrypted(self, keyfile): """ A quick check that filename looks like an encrypted key. |