From 9c49dc721e25992c824035088d5a054715bd0d87 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Sun, 28 Nov 2021 14:56:29 -0500 Subject: Tests proving #1257 --- tests/test_pkey.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'tests/test_pkey.py') diff --git a/tests/test_pkey.py b/tests/test_pkey.py index 9b144d22..94b2492b 100644 --- a/tests/test_pkey.py +++ b/tests/test_pkey.py @@ -26,9 +26,18 @@ import os from binascii import hexlify from hashlib import md5 -from paramiko import RSAKey, DSSKey, ECDSAKey, Ed25519Key, Message, util +from paramiko import ( + RSAKey, + DSSKey, + ECDSAKey, + Ed25519Key, + Message, + util, + SSHException, +) from paramiko.py3compat import StringIO, byte_chr, b, bytes, PY2 +from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateNumbers from mock import patch import pytest @@ -163,6 +172,16 @@ class KeyTest(unittest.TestCase): key2 = RSAKey.from_private_key(s) self.assertEqual(key, key2) + def test_load_rsa_transmutes_crypto_exceptions(self): + # TODO: nix unittest for pytest + for exception in (TypeError("onoz"), UnsupportedAlgorithm("oops")): + with patch( + "paramiko.rsakey.serialization.load_der_private_key" + ) as loader: + loader.side_effect = exception + with pytest.raises(SSHException, match=str(exception)): + RSAKey.from_private_key_file(_support("test_rsa.key")) + def test_load_rsa_password(self): key = RSAKey.from_private_key_file( _support("test_rsa_password.key"), "television" @@ -369,6 +388,17 @@ class KeyTest(unittest.TestCase): self.assertEqual(PUB_ECDSA_384.split()[1], key.get_base64()) self.assertEqual(384, key.get_bits()) + def test_load_ecdsa_transmutes_crypto_exceptions(self): + path = _support("test_ecdsa_256.key") + # TODO: nix unittest for pytest + for exception in (TypeError("onoz"), UnsupportedAlgorithm("oops")): + with patch( + "paramiko.ecdsakey.serialization.load_der_private_key" + ) as loader: + loader.side_effect = exception + with pytest.raises(SSHException, match=str(exception)): + ECDSAKey.from_private_key_file(path) + def test_compare_ecdsa_384(self): # verify that the private & public keys compare equal key = ECDSAKey.from_private_key_file(_support("test_ecdsa_384.key")) -- cgit v1.2.3