diff options
Diffstat (limited to 'tests/test_algorithms.py')
| -rw-r--r-- | tests/test_algorithms.py | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index 3ff7788..f6a2635 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -18,6 +18,7 @@ try: Ed25519Algorithm, ) from .keys import load_rsa_pub_key, load_ec_pub_key_p_521 + has_crypto = True except ImportError: has_crypto = False @@ -194,47 +195,55 @@ class TestAlgorithms: result = algo.verify(message, pub_key, sig) assert not result - @pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library') + @pytest.mark.skipif( + not has_crypto, reason="Not supported without cryptography library" + ) def test_ec_jwk_public_and_private_keys_should_parse_and_verify(self): tests = { - 'P-256': ECAlgorithm.SHA256, - 'P-384': ECAlgorithm.SHA384, - 'P-521': ECAlgorithm.SHA512 + "P-256": ECAlgorithm.SHA256, + "P-384": ECAlgorithm.SHA384, + "P-521": ECAlgorithm.SHA512, } for (curve, hash) in tests.items(): algo = ECAlgorithm(hash) - with open(key_path('jwk_ec_pub_{}.json'.format(curve)), 'r') as keyfile: + with open( + key_path("jwk_ec_pub_{}.json".format(curve)), "r" + ) as keyfile: pub_key = algo.from_jwk(keyfile.read()) - with open(key_path('jwk_ec_key_{}.json'.format(curve)), 'r') as keyfile: + with open( + key_path("jwk_ec_key_{}.json".format(curve)), "r" + ) as keyfile: priv_key = algo.from_jwk(keyfile.read()) - signature = algo.sign(force_bytes('Hello World!'), priv_key) - assert algo.verify(force_bytes('Hello World!'), pub_key, signature) + signature = algo.sign(force_bytes("Hello World!"), priv_key) + assert algo.verify(force_bytes("Hello World!"), pub_key, signature) - @pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library') + @pytest.mark.skipif( + not has_crypto, reason="Not supported without cryptography library" + ) def test_ec_jwk_fails_on_invalid_json(self): algo = ECAlgorithm(ECAlgorithm.SHA512) valid_points = { - 'P-256': { - 'x': 'PTTjIY84aLtaZCxLTrG_d8I0G6YKCV7lg8M4xkKfwQ4=', - 'y': 'ank6KA34vv24HZLXlChVs85NEGlpg2sbqNmR_BcgyJU=' + "P-256": { + "x": "PTTjIY84aLtaZCxLTrG_d8I0G6YKCV7lg8M4xkKfwQ4=", + "y": "ank6KA34vv24HZLXlChVs85NEGlpg2sbqNmR_BcgyJU=", + }, + "P-384": { + "x": "IDC-5s6FERlbC4Nc_4JhKW8sd51AhixtMdNUtPxhRFP323QY6cwWeIA3leyZhz-J", + "y": "eovmN9ocANS8IJxDAGSuC1FehTq5ZFLJU7XSPg36zHpv4H2byKGEcCBiwT4sFJsy", }, - 'P-384': { - 'x': 'IDC-5s6FERlbC4Nc_4JhKW8sd51AhixtMdNUtPxhRFP323QY6cwWeIA3leyZhz-J', - 'y': 'eovmN9ocANS8IJxDAGSuC1FehTq5ZFLJU7XSPg36zHpv4H2byKGEcCBiwT4sFJsy' + "P-521": { + "x": "AHKZLLOsCOzz5cY97ewNUajB957y-C-U88c3v13nmGZx6sYl_oJXu9A5RkTKqjqvjyekWF-7ytDyRXYgCF5cj0Kt", + "y": "AdymlHvOiLxXkEhayXQnNCvDX4h9htZaCJN34kfmC6pV5OhQHiraVySsUdaQkAgDPrwQrJmbnX9cwlGfP-HqHZR1", }, - 'P-521': { - 'x': 'AHKZLLOsCOzz5cY97ewNUajB957y-C-U88c3v13nmGZx6sYl_oJXu9A5RkTKqjqvjyekWF-7ytDyRXYgCF5cj0Kt', - 'y': 'AdymlHvOiLxXkEhayXQnNCvDX4h9htZaCJN34kfmC6pV5OhQHiraVySsUdaQkAgDPrwQrJmbnX9cwlGfP-HqHZR1' - } } # Invalid JSON with pytest.raises(InvalidKeyError): - algo.from_jwk('<this isn\'t json>') + algo.from_jwk("<this isn't json>") # Bad key type with pytest.raises(InvalidKeyError): @@ -254,10 +263,12 @@ class TestAlgorithms: # EC coordinates not equally long with pytest.raises(InvalidKeyError): - algo.from_jwk('{"kty": "EC", "x": "dGVzdHRlc3Q=", "y": "dGVzdA=="}') + algo.from_jwk( + '{"kty": "EC", "x": "dGVzdHRlc3Q=", "y": "dGVzdA=="}' + ) # EC coordinates length invalid - for curve in ('P-256', 'P-384', 'P-521'): + for curve in ("P-256", "P-384", "P-521"): with pytest.raises(InvalidKeyError): algo.from_jwk( '{{"kty": "EC", "crv": "{}", "x": "dGVzdA==", ' @@ -269,10 +280,12 @@ class TestAlgorithms: with pytest.raises(InvalidKeyError): algo.from_jwk( '{{"kty": "EC", "crv": "{}", "x": "{}", "y": "{}", ' - '"d": "dGVzdA=="}}'.format(curve, point['x'], point['y']) + '"d": "dGVzdA=="}}'.format(curve, point["x"], point["y"]) ) - @pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library') + @pytest.mark.skipif( + not has_crypto, reason="Not supported without cryptography library" + ) def test_rsa_jwk_public_and_private_keys_should_parse_and_verify(self): algo = RSAAlgorithm(RSAAlgorithm.SHA256) |
