def test_digestmod_from_ctor(md_algorithm): assert callable(md_algorithm) algorithm = _get_md_alg(md_algorithm) assert isinstance(algorithm(), hashlib.Hash)
class _TestCipherBase: @pytest.fixture def cipher(self): raise NotImplementedError @pytest.fixture def key(self, cipher): raise NotImplementedError @pytest.fixture def pub(self, cipher, key): return type(cipher).from_buffer(cipher.export_public_key()) def test_hash(self, cipher): assert isinstance(hash(cipher), int) @pytest.mark.usefixtures("key") def test_cmp_eq(self, cipher): assert cipher == cipher @pytest.mark.parametrize("format", ["DER", "PEM"]) @pytest.mark.usefixtures("key") def test_cmp_eq_prv(self, cipher, format): assert cipher == cipher.export_key(format) @pytest.mark.parametrize("format", ["DER", "PEM"]) def test_cmp_eq_pub(self, pub, format): assert pub == pub.export_public_key(format) @pytest.mark.parametrize("invalid", [b"", "", b"\1\2\3", "123"]) @pytest.mark.usefixtures("key") def test_cmp_neq(self, cipher, invalid): assert cipher != invalid def test_export_key_without_key(self, cipher): assert cipher.export_key("DER") == b"" assert cipher.export_key("PEM") == "" def test_export_public_key_without_key(self, cipher): assert cipher.export_public_key("DER") == b"" assert cipher.export_public_key("PEM") == "" @pytest.mark.usefixtures("key") def test_export_key_to_PEM(self, cipher): der = cipher.export_key("DER") other = type(cipher).from_DER(der) assert der != b"" assert cipher == other @pytest.mark.usefixtures("key") def test_export_key_to_DER(self, cipher): pem = cipher.export_key("PEM") other = type(cipher).from_PEM(pem) assert pem != "" assert cipher == other @pytest.mark.usefixtures("key") def test_export_public_key_to_DER(self, cipher): der = cipher.export_public_key("DER") other = type(cipher).from_DER(der) assert der != b"" assert other == cipher.export_public_key("DER") @pytest.mark.usefixtures("key") def test_export_public_key_to_PEM(self, cipher): pem = cipher.export_public_key("PEM") other = type(cipher).from_PEM(pem) assert pem != "" assert other == cipher.export_public_key("PEM") def test_generate(self, cipher, key): assert cipher.export_key() assert cipher.export_key() == key assert cipher.export_public_key() @pytest.mark.usefixtures("key") def test_type_accessor(self, cipher): assert cipher._type == _type_from_name(cipher.name) def test_key_size_accessor(self, cipher): assert cipher.key_size == 0 @pytest.mark.usefixtures("key") def test_key_size_accessor_with_key(self, cipher): assert cipher.key_size != 0 @pytest.mark.usefixtures("key") def test_check_pair(self, cipher): assert check_pair(cipher, cipher) is True @pytest.mark.parametrize( "digestmod", [_get_md_alg(name) for name in hashlib.algorithms_guaranteed], ids=lambda dm: dm().name, ) def test_sign_without_key_returns_none(self, cipher, digestmod, randbytes): message = randbytes(4096) assert cipher.sign(message, digestmod) is None @pytest.mark.usefixtures("key") @pytest.mark.parametrize( "digestmod", [_get_md_alg(name) for name in hashlib.algorithms_guaranteed], ids=lambda dm: dm().name, ) def test_sign_verify(self, cipher, digestmod, randbytes): msg = randbytes(4096) sig = cipher.sign(msg, digestmod) assert sig is not None assert cipher.verify(msg, sig, digestmod) is True assert cipher.verify(msg + b"\0", sig, digestmod) is False @pytest.mark.usefixtures("key") def test_import_public_key(self, cipher): pub = cipher.export_public_key() other = type(cipher).from_buffer(pub) assert not other.export_key() assert other.export_public_key() assert check_pair(cipher, other) is False # Test private half. assert check_pair(other, cipher) is True # Test public half. assert check_pair(other, other) is False assert cipher != other def test_import_private_key(self, cipher, key): other = type(cipher).from_buffer(key) assert other.export_key() assert other.export_public_key() assert check_pair(cipher, other) is True # Test private half. assert check_pair(other, cipher) is True # Test public half. assert check_pair(other, other) is True assert cipher == other
class _TestCipherBase(object): @pytest.fixture def key(self): raise NotImplementedError def test_key_accessors_without_key(self): assert not self.cipher.export_key() assert not self.cipher.export_public_key() def test_generate(self, key): assert self.cipher.export_key() assert self.cipher.export_key() == key assert self.cipher.export_public_key() @pytest.mark.usefixtures("key") def test_type_accessor(self): assert self.cipher._type == _type_from_name(self.cipher.name) def test_key_size_accessor(self): assert self.cipher.key_size == 0 @pytest.mark.usefixtures("key") def test_key_size_accessor_with_key(self): assert self.cipher.key_size != 0 @pytest.mark.usefixtures("key") def test_check_pair(self): assert check_pair(self.cipher, self.cipher) is True @pytest.mark.parametrize( "digestmod", [_get_md_alg(name) for name in _hash.algorithms_guaranteed], ids=lambda dm: dm().name) def test_sign_without_key_returns_none(self, digestmod, randbytes): message = randbytes(4096) assert self.cipher.sign(message, digestmod) is None @pytest.mark.usefixtures("key") @pytest.mark.parametrize( "digestmod", [_get_md_alg(name) for name in _hash.algorithms_guaranteed], ids=lambda dm: dm().name) def test_sign_verify(self, digestmod, randbytes): msg = randbytes(4096) sig = self.cipher.sign(msg, digestmod) assert sig is not None assert self.cipher.verify(msg, sig, digestmod) is True assert self.cipher.verify(msg + b"\0", sig, digestmod) is False @pytest.mark.usefixtures("key") def test_import_public_key(self): other = type(self.cipher)() pub = self.cipher.export_public_key() other.from_buffer(pub) assert not other.export_key() assert other.export_public_key() assert check_pair(self.cipher, other) is False # Test private half. assert check_pair(other, self.cipher) is True # Test public half. assert check_pair(other, other) is False assert self.cipher != other def test_import_private_key(self, key): other = type(self.cipher)() other.from_buffer(key) assert other.export_key() assert other.export_public_key() assert check_pair(self.cipher, other) is True # Test private half. assert check_pair(other, self.cipher) is True # Test public half. assert check_pair(other, other) is True assert self.cipher == other @pytest.mark.usefixtures("key") def test_export_to_PEM(self): other = type(self.cipher)() prv = self.cipher.export_key(format="PEM") other.from_PEM(prv) assert self.cipher == other