Skip to content

Commit

Permalink
Use the subtests fixture correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
facutuesca committed Feb 26, 2024
1 parent ca593a9 commit 45b10e2
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions tests/hazmat/primitives/test_ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,36 +543,37 @@ def test_deterministic_nonce(self, backend, subtests):
load_rfc6979_vectors,
)

for vector in vectors:
input = bytes(vector["input"], "utf-8")
output = bytes.fromhex(vector["output"])
key = bytes("\n".join(vector["key"]), "utf-8")
if "digest_sign" in vector:
algorithm = vector["digest_sign"]
hash_algorithm = supported_hash_algorithms[algorithm]
algorithm = ec.ECDSA(
hash_algorithm,
deterministic_signing=vector["deterministic_nonce"],
)
private_key = serialization.load_pem_private_key(
key, password=None
)
assert isinstance(private_key, EllipticCurvePrivateKey)
signature = private_key.sign(input, algorithm)
assert signature == output
else:
assert "digest_verify" in vector
algorithm = vector["digest_verify"]
assert algorithm in supported_hash_algorithms
hash_algorithm = supported_hash_algorithms[algorithm]
algorithm = ec.ECDSA(hash_algorithm)
public_key = serialization.load_pem_public_key(key)
assert isinstance(public_key, EllipticCurvePublicKey)
if vector["verify_error"]:
with pytest.raises(exceptions.InvalidSignature):
public_key.verify(output, input, algorithm)
with subtests.test():

This comment has been minimized.

Copy link
@alex

alex Feb 26, 2024

Member

Invert the order, you want the with subtests.test() inside teh loop, not outisde

This comment has been minimized.

Copy link
@facutuesca

facutuesca Feb 26, 2024

Author Contributor

🤦
fixed

This comment has been minimized.

Copy link
@facutuesca

facutuesca Feb 26, 2024

Author Contributor

I pushed the commit with the fix, but for some reason the PR is not updating
trail-of-forks@94c92df

for vector in vectors:
input = bytes(vector["input"], "utf-8")
output = bytes.fromhex(vector["output"])
key = bytes("\n".join(vector["key"]), "utf-8")
if "digest_sign" in vector:
algorithm = vector["digest_sign"]
hash_algorithm = supported_hash_algorithms[algorithm]
algorithm = ec.ECDSA(
hash_algorithm,
deterministic_signing=vector["deterministic_nonce"],
)
private_key = serialization.load_pem_private_key(
key, password=None
)
assert isinstance(private_key, EllipticCurvePrivateKey)
signature = private_key.sign(input, algorithm)
assert signature == output
else:
public_key.verify(output, input, algorithm)
assert "digest_verify" in vector
algorithm = vector["digest_verify"]
assert algorithm in supported_hash_algorithms
hash_algorithm = supported_hash_algorithms[algorithm]
algorithm = ec.ECDSA(hash_algorithm)
public_key = serialization.load_pem_public_key(key)
assert isinstance(public_key, EllipticCurvePublicKey)
if vector["verify_error"]:
with pytest.raises(exceptions.InvalidSignature):
public_key.verify(output, input, algorithm)
else:
public_key.verify(output, input, algorithm)

def test_sign(self, backend):
_skip_curve_unsupported(backend, ec.SECP256R1())
Expand Down

0 comments on commit 45b10e2

Please sign in to comment.