Skip to content

Commit 1967754

Browse files
committed
Adding get_pem_for_key and normalize_pem methods to normalize PEM formatting of keys in tests/algorithms/test_EC.py and updating tests/algorithms/test_EC_compat.py to use these methods
Test failures were occurring due to differences of line lengths generated by the `cryptography` vs `ecdsa` PIP libraries for PEM formatting of cryptographic keys. This method removes newlines from the bodies of PEM-formated keys so that test comparisons will not fail on differentiated line lengths between PEM formattings.
1 parent c533ed6 commit 1967754

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

tests/algorithms/test_EC.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import re
23

34
from jose.backends import ECKey
45
from jose.constants import ALGORITHMS
@@ -48,6 +49,31 @@
4849
b"\xfeMO\x04\xb2[\x86A\xbd\xc6hu\x953X\x1e"
4950
)
5051

52+
# Define the regex pattern to capture the header, body, and footer of the PEM file
53+
PEM_REGEX = re.compile(r"(-----BEGIN [A-Z ]+-----)(.*?)(-----END [A-Z ]+-----)", re.DOTALL)
54+
WHITE_SPACE_REGEX = re.compile(r"\s+")
55+
56+
57+
def get_pem_for_key(key):
58+
return key.to_pem().strip().decode("utf-8")
59+
60+
61+
def normalize_pem(key_pem_str):
62+
# Search for the PEM sections
63+
pem_match = PEM_REGEX.search(key_pem_str)
64+
if not pem_match:
65+
raise ValueError("The provided string does not contain a valid PEM formatted data.")
66+
67+
header = pem_match.group(1)
68+
body = pem_match.group(2)
69+
footer = pem_match.group(3)
70+
71+
# Remove all newlines and spaces from the body
72+
clean_body = WHITE_SPACE_REGEX.sub("", body)
73+
74+
# Reassemble the PEM string
75+
return f"{header}\n{clean_body}\n{footer}"
76+
5177

5278
def _backend_exception_types():
5379
"""Build the backend exception types based on available backends."""
@@ -104,7 +130,7 @@ def test_key_from_pem(self):
104130
def test_to_pem(self):
105131
key = ECKey(private_key, ALGORITHMS.ES256)
106132
assert not key.is_public()
107-
assert key.to_pem().strip() == private_key.strip().encode("utf-8")
133+
assert normalize_pem(get_pem_for_key(key)) == normalize_pem(private_key.strip())
108134

109135
public_pem = key.public_key().to_pem()
110136
assert ECKey(public_pem, ALGORITHMS.ES256).is_public()

tests/algorithms/test_EC_compat.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ECDSAECKey = CryptographyECKey = None
88
from jose.constants import ALGORITHMS
99

10-
from .test_EC import private_key
10+
from .test_EC import get_pem_for_key, normalize_pem, private_key
1111

1212

1313
@pytest.mark.backend_compatibility
@@ -37,35 +37,35 @@ def test_public_key_to_pem(self, BackendFrom, BackendTo):
3737
key = BackendFrom(private_key, ALGORITHMS.ES256)
3838
key2 = BackendTo(private_key, ALGORITHMS.ES256)
3939

40-
assert key.public_key().to_pem().strip() == key2.public_key().to_pem().strip()
40+
assert normalize_pem(get_pem_for_key(key.public_key())) == normalize_pem(get_pem_for_key(key2.public_key()))
4141

4242
@pytest.mark.parametrize("BackendFrom", [ECDSAECKey, CryptographyECKey])
4343
@pytest.mark.parametrize("BackendTo", [ECDSAECKey, CryptographyECKey])
4444
def test_private_key_to_pem(self, BackendFrom, BackendTo):
4545
key = BackendFrom(private_key, ALGORITHMS.ES256)
4646
key2 = BackendTo(private_key, ALGORITHMS.ES256)
4747

48-
assert key.to_pem().strip() == key2.to_pem().strip()
48+
assert normalize_pem(get_pem_for_key(key)) == normalize_pem(get_pem_for_key(key2))
4949

5050
@pytest.mark.parametrize("BackendFrom", [ECDSAECKey, CryptographyECKey])
5151
@pytest.mark.parametrize("BackendTo", [ECDSAECKey, CryptographyECKey])
5252
def test_public_key_load_cycle(self, BackendFrom, BackendTo):
5353
key = BackendFrom(private_key, ALGORITHMS.ES256)
5454
pubkey = key.public_key()
5555

56-
pub_pem_source = pubkey.to_pem().strip()
56+
pub_pem_source = normalize_pem(get_pem_for_key(pubkey))
5757

5858
pub_target = BackendTo(pub_pem_source, ALGORITHMS.ES256)
5959

60-
assert pub_pem_source == pub_target.to_pem().strip()
60+
assert pub_pem_source == normalize_pem(get_pem_for_key(pub_target))
6161

6262
@pytest.mark.parametrize("BackendFrom", [ECDSAECKey, CryptographyECKey])
6363
@pytest.mark.parametrize("BackendTo", [ECDSAECKey, CryptographyECKey])
6464
def test_private_key_load_cycle(self, BackendFrom, BackendTo):
6565
key = BackendFrom(private_key, ALGORITHMS.ES256)
6666

67-
pem_source = key.to_pem().strip()
67+
pem_source = normalize_pem(get_pem_for_key(key))
6868

6969
target = BackendTo(pem_source, ALGORITHMS.ES256)
7070

71-
assert pem_source == target.to_pem().strip()
71+
assert pem_source == normalize_pem(get_pem_for_key(target))

0 commit comments

Comments
 (0)