diff options
Diffstat (limited to 'tests/test_algorithms.py')
-rw-r--r-- | tests/test_algorithms.py | 89 |
1 files changed, 61 insertions, 28 deletions
diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index 7a90376..1a39552 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -1,6 +1,6 @@ import base64 import json -from typing import cast +from typing import Any, cast import pytest @@ -91,11 +91,15 @@ class TestAlgorithms: signature = algo.sign(b"Hello World!", key) assert algo.verify(b"Hello World!", key, signature) - def test_hmac_to_jwk_returns_correct_values(self): + @pytest.mark.parametrize("as_dict", (False, True)) + def test_hmac_to_jwk_returns_correct_values(self, as_dict): algo = HMACAlgorithm(HMACAlgorithm.SHA256) - key = algo.to_jwk("secret") + key: Any = algo.to_jwk("secret", as_dict=as_dict) - assert json.loads(key) == {"kty": "oct", "k": "c2VjcmV0"} + if not as_dict: + key = json.loads(key) + + assert key == {"kty": "oct", "k": "c2VjcmV0"} def test_hmac_from_jwk_should_raise_exception_if_not_hmac_key(self): algo = HMACAlgorithm(HMACAlgorithm.SHA256) @@ -261,13 +265,17 @@ class TestAlgorithms: assert parsed_key.public_numbers() == orig_key.public_numbers() @crypto_required - def test_ec_to_jwk_returns_correct_values_for_public_key(self): + @pytest.mark.parametrize("as_dict", (False, True)) + def test_ec_to_jwk_returns_correct_values_for_public_key(self, as_dict): algo = ECAlgorithm(ECAlgorithm.SHA256) with open(key_path("testkey_ec.pub")) as keyfile: pub_key = algo.prepare_key(keyfile.read()) - key = algo.to_jwk(pub_key) + key: Any = algo.to_jwk(pub_key, as_dict=as_dict) + + if not as_dict: + key = json.loads(key) expected = { "kty": "EC", @@ -276,16 +284,20 @@ class TestAlgorithms: "y": "t2G02kbWiOqimYfQAfnARdp2CTycsJPhwA8rn1Cn0SQ", } - assert json.loads(key) == expected + assert key == expected @crypto_required - def test_ec_to_jwk_returns_correct_values_for_private_key(self): + @pytest.mark.parametrize("as_dict", (False, True)) + def test_ec_to_jwk_returns_correct_values_for_private_key(self, as_dict): algo = ECAlgorithm(ECAlgorithm.SHA256) with open(key_path("testkey_ec.priv")) as keyfile: priv_key = algo.prepare_key(keyfile.read()) - key = algo.to_jwk(priv_key) + key: Any = algo.to_jwk(priv_key, as_dict=as_dict) + + if not as_dict: + key = json.loads(key) expected = { "kty": "EC", @@ -295,17 +307,18 @@ class TestAlgorithms: "d": "2nninfu2jMHDwAbn9oERUhRADS6duQaJEadybLaa0YQ", } - assert json.loads(key) == expected + assert key == expected @crypto_required def test_ec_to_jwk_raises_exception_on_invalid_key(self): algo = ECAlgorithm(ECAlgorithm.SHA256) with pytest.raises(InvalidKeyError): - algo.to_jwk({"not": "a valid key"}) # type: ignore[arg-type] + algo.to_jwk({"not": "a valid key"}) # type: ignore[call-overload] @crypto_required - def test_ec_to_jwk_with_valid_curves(self): + @pytest.mark.parametrize("as_dict", (False, True)) + def test_ec_to_jwk_with_valid_curves(self, as_dict): tests = { "P-256": ECAlgorithm.SHA256, "P-384": ECAlgorithm.SHA384, @@ -317,11 +330,21 @@ class TestAlgorithms: with open(key_path(f"jwk_ec_pub_{curve}.json")) as keyfile: pub_key = algo.from_jwk(keyfile.read()) - assert json.loads(algo.to_jwk(pub_key))["crv"] == curve + jwk: Any = algo.to_jwk(pub_key, as_dict=as_dict) + + if not as_dict: + jwk = json.loads(jwk) + + assert jwk["crv"] == curve with open(key_path(f"jwk_ec_key_{curve}.json")) as keyfile: priv_key = algo.from_jwk(keyfile.read()) - assert json.loads(algo.to_jwk(priv_key))["crv"] == curve + jwk = algo.to_jwk(priv_key, as_dict=as_dict) + + if not as_dict: + jwk = json.loads(jwk) + + assert jwk["crv"] == curve @crypto_required def test_ec_to_jwk_with_invalid_curve(self): @@ -440,13 +463,17 @@ class TestAlgorithms: algo.from_jwk('{"kty": "RSA"}') @crypto_required - def test_rsa_to_jwk_returns_correct_values_for_public_key(self): + @pytest.mark.parametrize("as_dict", (False, True)) + def test_rsa_to_jwk_returns_correct_values_for_public_key(self, as_dict): algo = RSAAlgorithm(RSAAlgorithm.SHA256) with open(key_path("testkey_rsa.pub")) as keyfile: pub_key = algo.prepare_key(keyfile.read()) - key = algo.to_jwk(pub_key) + key: Any = algo.to_jwk(pub_key, as_dict=as_dict) + + if not as_dict: + key = json.loads(key) expected = { "e": "AQAB", @@ -461,16 +488,20 @@ class TestAlgorithms: "sNruF3ogJWNq1Lyn_ijPQnkPLpZHyhvuiycYcI3DiQ" ), } - assert json.loads(key) == expected + assert key == expected @crypto_required - def test_rsa_to_jwk_returns_correct_values_for_private_key(self): + @pytest.mark.parametrize("as_dict", (False, True)) + def test_rsa_to_jwk_returns_correct_values_for_private_key(self, as_dict): algo = RSAAlgorithm(RSAAlgorithm.SHA256) with open(key_path("testkey_rsa.priv")) as keyfile: priv_key = algo.prepare_key(keyfile.read()) - key = algo.to_jwk(priv_key) + key: Any = algo.to_jwk(priv_key, as_dict=as_dict) + + if not as_dict: + key = json.loads(key) expected = { "key_ops": ["sign"], @@ -518,14 +549,14 @@ class TestAlgorithms: "AuKhin-kc4mh9ssDXRQZwlMymZP0QtaxUDw_nlfVrUCZgO7L1_ZsUTk" ), } - assert json.loads(key) == expected + assert key == expected @crypto_required def test_rsa_to_jwk_raises_exception_on_invalid_key(self): algo = RSAAlgorithm(RSAAlgorithm.SHA256) with pytest.raises(InvalidKeyError): - algo.to_jwk({"not": "a valid key"}) # type: ignore[arg-type] + algo.to_jwk({"not": "a valid key"}) # type: ignore[call-overload] @crypto_required def test_rsa_from_jwk_raises_exception_on_invalid_key(self): @@ -923,7 +954,8 @@ class TestOKPAlgorithms: with pytest.raises(InvalidKeyError): algo.from_jwk(v) - def test_okp_ed25519_to_jwk_works_with_from_jwk(self): + @pytest.mark.parametrize("as_dict", (False, True)) + def test_okp_ed25519_to_jwk_works_with_from_jwk(self, as_dict): algo = OKPAlgorithm() with open(key_path("jwk_okp_key_Ed25519.json")) as keyfile: @@ -932,9 +964,9 @@ class TestOKPAlgorithms: with open(key_path("jwk_okp_pub_Ed25519.json")) as keyfile: pub_key_1 = cast(Ed25519PublicKey, algo.from_jwk(keyfile.read())) - pub = algo.to_jwk(pub_key_1) + pub = algo.to_jwk(pub_key_1, as_dict=as_dict) pub_key_2 = algo.from_jwk(pub) - pri = algo.to_jwk(priv_key_1) + pri = algo.to_jwk(priv_key_1, as_dict=as_dict) priv_key_2 = cast(Ed25519PrivateKey, algo.from_jwk(pri)) signature_1 = algo.sign(b"Hello World!", priv_key_1) @@ -946,7 +978,7 @@ class TestOKPAlgorithms: algo = OKPAlgorithm() with pytest.raises(InvalidKeyError): - algo.to_jwk({"not": "a valid key"}) # type: ignore[arg-type] + algo.to_jwk({"not": "a valid key"}) # type: ignore[call-overload] def test_okp_ed448_jwk_private_key_should_parse_and_verify(self): algo = OKPAlgorithm() @@ -1032,7 +1064,8 @@ class TestOKPAlgorithms: with pytest.raises(InvalidKeyError): algo.from_jwk(v) - def test_okp_ed448_to_jwk_works_with_from_jwk(self): + @pytest.mark.parametrize("as_dict", (False, True)) + def test_okp_ed448_to_jwk_works_with_from_jwk(self, as_dict): algo = OKPAlgorithm() with open(key_path("jwk_okp_key_Ed448.json")) as keyfile: @@ -1041,9 +1074,9 @@ class TestOKPAlgorithms: with open(key_path("jwk_okp_pub_Ed448.json")) as keyfile: pub_key_1 = cast(Ed448PublicKey, algo.from_jwk(keyfile.read())) - pub = algo.to_jwk(pub_key_1) + pub = algo.to_jwk(pub_key_1, as_dict=as_dict) pub_key_2 = algo.from_jwk(pub) - pri = algo.to_jwk(priv_key_1) + pri = algo.to_jwk(priv_key_1, as_dict=as_dict) priv_key_2 = cast(Ed448PrivateKey, algo.from_jwk(pri)) signature_1 = algo.sign(b"Hello World!", priv_key_1) |