55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import pytest
|
|
|
|
from algorithms import Algorithm, get_algorithm
|
|
|
|
|
|
def test_algorithm_has_required_methods():
|
|
"""Verify Algorithm protocol defines required methods."""
|
|
algo = get_algorithm("pbkdf2")
|
|
assert hasattr(algo, "hash")
|
|
assert hasattr(algo, "verify")
|
|
assert hasattr(algo, "identifier")
|
|
|
|
|
|
def test_get_algorithm_returns_pbkdf2():
|
|
"""Verify default algorithm is PBKDF2."""
|
|
algo = get_algorithm("pbkdf2")
|
|
assert algo.identifier == "pbkdf2"
|
|
|
|
|
|
def test_get_algorithm_unknown_raises_error():
|
|
"""Verify unknown algorithm raises ValueError."""
|
|
with pytest.raises(ValueError, match="Unknown algorithm"):
|
|
get_algorithm("unknown")
|
|
|
|
|
|
def test_pbkdf2_algorithm_hash_round_trip():
|
|
"""Verify PBKDF2 algorithm can hash and verify passwords."""
|
|
algo = get_algorithm("pbkdf2")
|
|
salt, hashed = algo.hash("test password")
|
|
assert algo.verify("test password", salt, hashed)
|
|
assert not algo.verify("wrong password", salt, hashed)
|
|
|
|
|
|
def test_pbkdf2_algorithm_respects_iterations():
|
|
"""Verify PBKDF2 algorithm respects custom iterations."""
|
|
algo = get_algorithm("pbkdf2")
|
|
salt1, hash1 = algo.hash("test", iterations=100000)
|
|
salt2, hash2 = algo.hash("test", iterations=200000)
|
|
# Different iterations should produce different hashes even with same password
|
|
assert hash1 != hash2
|
|
|
|
|
|
def test_argon2_algorithm_hash_round_trip():
|
|
"""Verify Argon2 algorithm can hash and verify passwords."""
|
|
algo = get_algorithm("argon2")
|
|
salt, hashed = algo.hash("test password")
|
|
assert algo.verify("test password", salt, hashed)
|
|
assert not algo.verify("wrong password", salt, hashed)
|
|
|
|
|
|
def test_argon2_algorithm_identifier():
|
|
"""Verify Argon2 algorithm has correct identifier."""
|
|
algo = get_algorithm("argon2")
|
|
assert algo.identifier == "argon2"
|