"""Tests for Hash, SHA1Hash, Hash384, Hash512 — fixed-length hash containers.""" import pytest from i2p_crypto.hash_data import Hash, SHA1Hash, Hash384, Hash512 class TestHash: def test_default(self): h = Hash() assert h.data == b"\x00" * 32 assert len(bytes(h)) == 32 def test_from_data(self): data = b"\x01" * 32 h = Hash(data) assert h.data == data def test_wrong_length(self): with pytest.raises(ValueError, match="32 bytes"): Hash(b"\x00" * 31) def test_create(self): data = b"\xff" * 64 h = Hash.create(data, offset=16) assert h.data == b"\xff" * 32 def test_equality(self): a = Hash(b"\x01" * 32) b = Hash(b"\x01" * 32) assert a == b def test_inequality(self): a = Hash(b"\x01" * 32) b = Hash(b"\x02" * 32) assert a != b def test_not_equal_other_type(self): h = Hash() assert h != "not a hash" def test_hash_as_dict_key(self): a = Hash(b"\x01" * 32) b = Hash(b"\x01" * 32) d = {a: "found"} assert d[b] == "found" def test_repr(self): h = Hash(b"\x00" * 32) assert "Hash(" in repr(h) def test_bytes(self): data = b"\xab" * 32 h = Hash(data) assert bytes(h) == data def test_fake_hash(self): assert Hash.FAKE_HASH.data == b"\x00" * 32 class TestSHA1Hash: def test_default(self): h = SHA1Hash() assert len(h.data) == 20 def test_from_data(self): data = b"\x01" * 20 h = SHA1Hash(data) assert h.data == data def test_wrong_length(self): with pytest.raises(ValueError, match="20 bytes"): SHA1Hash(b"\x00" * 19) def test_equality(self): a = SHA1Hash(b"\x01" * 20) b = SHA1Hash(b"\x01" * 20) assert a == b def test_inequality(self): a = SHA1Hash(b"\x01" * 20) b = SHA1Hash(b"\x02" * 20) assert a != b def test_not_equal_other_type(self): assert SHA1Hash() != Hash() def test_hash_dict_key(self): a = SHA1Hash(b"\x01" * 20) d = {a: "yes"} assert d[a] == "yes" def test_repr(self): assert "SHA1Hash(" in repr(SHA1Hash()) def test_bytes(self): data = b"\xcd" * 20 assert bytes(SHA1Hash(data)) == data class TestHash384: def test_default(self): h = Hash384() assert len(h.data) == 48 def test_from_data(self): data = b"\x01" * 48 h = Hash384(data) assert h.data == data def test_wrong_length(self): with pytest.raises(ValueError, match="48 bytes"): Hash384(b"\x00" * 47) def test_equality(self): a = Hash384(b"\x01" * 48) b = Hash384(b"\x01" * 48) assert a == b def test_inequality(self): assert Hash384(b"\x01" * 48) != Hash384(b"\x02" * 48) def test_not_equal_other_type(self): assert Hash384() != Hash() def test_hash_dict_key(self): d = {Hash384(): "val"} assert len(d) == 1 def test_bytes(self): data = b"\xef" * 48 assert bytes(Hash384(data)) == data class TestHash512: def test_default(self): h = Hash512() assert len(h.data) == 64 def test_from_data(self): data = b"\x01" * 64 h = Hash512(data) assert h.data == data def test_wrong_length(self): with pytest.raises(ValueError, match="64 bytes"): Hash512(b"\x00" * 63) def test_equality(self): a = Hash512(b"\x01" * 64) b = Hash512(b"\x01" * 64) assert a == b def test_inequality(self): assert Hash512(b"\x01" * 64) != Hash512(b"\x02" * 64) def test_not_equal_other_type(self): assert Hash512() != Hash() def test_bytes(self): data = b"\xab" * 64 assert bytes(Hash512(data)) == data