A Python port of the Invisible Internet Project (I2P)
1"""Tests for Hash, SHA1Hash, Hash384, Hash512 — fixed-length hash containers."""
2
3import pytest
4
5from i2p_crypto.hash_data import Hash, SHA1Hash, Hash384, Hash512
6
7
8class TestHash:
9 def test_default(self):
10 h = Hash()
11 assert h.data == b"\x00" * 32
12 assert len(bytes(h)) == 32
13
14 def test_from_data(self):
15 data = b"\x01" * 32
16 h = Hash(data)
17 assert h.data == data
18
19 def test_wrong_length(self):
20 with pytest.raises(ValueError, match="32 bytes"):
21 Hash(b"\x00" * 31)
22
23 def test_create(self):
24 data = b"\xff" * 64
25 h = Hash.create(data, offset=16)
26 assert h.data == b"\xff" * 32
27
28 def test_equality(self):
29 a = Hash(b"\x01" * 32)
30 b = Hash(b"\x01" * 32)
31 assert a == b
32
33 def test_inequality(self):
34 a = Hash(b"\x01" * 32)
35 b = Hash(b"\x02" * 32)
36 assert a != b
37
38 def test_not_equal_other_type(self):
39 h = Hash()
40 assert h != "not a hash"
41
42 def test_hash_as_dict_key(self):
43 a = Hash(b"\x01" * 32)
44 b = Hash(b"\x01" * 32)
45 d = {a: "found"}
46 assert d[b] == "found"
47
48 def test_repr(self):
49 h = Hash(b"\x00" * 32)
50 assert "Hash(" in repr(h)
51
52 def test_bytes(self):
53 data = b"\xab" * 32
54 h = Hash(data)
55 assert bytes(h) == data
56
57 def test_fake_hash(self):
58 assert Hash.FAKE_HASH.data == b"\x00" * 32
59
60
61class TestSHA1Hash:
62 def test_default(self):
63 h = SHA1Hash()
64 assert len(h.data) == 20
65
66 def test_from_data(self):
67 data = b"\x01" * 20
68 h = SHA1Hash(data)
69 assert h.data == data
70
71 def test_wrong_length(self):
72 with pytest.raises(ValueError, match="20 bytes"):
73 SHA1Hash(b"\x00" * 19)
74
75 def test_equality(self):
76 a = SHA1Hash(b"\x01" * 20)
77 b = SHA1Hash(b"\x01" * 20)
78 assert a == b
79
80 def test_inequality(self):
81 a = SHA1Hash(b"\x01" * 20)
82 b = SHA1Hash(b"\x02" * 20)
83 assert a != b
84
85 def test_not_equal_other_type(self):
86 assert SHA1Hash() != Hash()
87
88 def test_hash_dict_key(self):
89 a = SHA1Hash(b"\x01" * 20)
90 d = {a: "yes"}
91 assert d[a] == "yes"
92
93 def test_repr(self):
94 assert "SHA1Hash(" in repr(SHA1Hash())
95
96 def test_bytes(self):
97 data = b"\xcd" * 20
98 assert bytes(SHA1Hash(data)) == data
99
100
101class TestHash384:
102 def test_default(self):
103 h = Hash384()
104 assert len(h.data) == 48
105
106 def test_from_data(self):
107 data = b"\x01" * 48
108 h = Hash384(data)
109 assert h.data == data
110
111 def test_wrong_length(self):
112 with pytest.raises(ValueError, match="48 bytes"):
113 Hash384(b"\x00" * 47)
114
115 def test_equality(self):
116 a = Hash384(b"\x01" * 48)
117 b = Hash384(b"\x01" * 48)
118 assert a == b
119
120 def test_inequality(self):
121 assert Hash384(b"\x01" * 48) != Hash384(b"\x02" * 48)
122
123 def test_not_equal_other_type(self):
124 assert Hash384() != Hash()
125
126 def test_hash_dict_key(self):
127 d = {Hash384(): "val"}
128 assert len(d) == 1
129
130 def test_bytes(self):
131 data = b"\xef" * 48
132 assert bytes(Hash384(data)) == data
133
134
135class TestHash512:
136 def test_default(self):
137 h = Hash512()
138 assert len(h.data) == 64
139
140 def test_from_data(self):
141 data = b"\x01" * 64
142 h = Hash512(data)
143 assert h.data == data
144
145 def test_wrong_length(self):
146 with pytest.raises(ValueError, match="64 bytes"):
147 Hash512(b"\x00" * 63)
148
149 def test_equality(self):
150 a = Hash512(b"\x01" * 64)
151 b = Hash512(b"\x01" * 64)
152 assert a == b
153
154 def test_inequality(self):
155 assert Hash512(b"\x01" * 64) != Hash512(b"\x02" * 64)
156
157 def test_not_equal_other_type(self):
158 assert Hash512() != Hash()
159
160 def test_bytes(self):
161 data = b"\xab" * 64
162 assert bytes(Hash512(data)) == data