Debian Codegraph @

test/test_checksums.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4
5 from test import NativeResourceTest
6 from awscrt import checksums
7 import unittest
8 import sys
9
10
11 class TestChecksums(NativeResourceTest):
12
13 def test_crc32_zeros_one_shot(self):
14 output = checksums.crc32(bytes(32))
15 expected = 0x190A55AD
16 self.assertEqual(expected, output)
17
18 def test_crc32_zeros_iterated(self):
19 output = 0
20 for i in range(32):
21 output = checksums.crc32(bytes(1), output)
22 expected = 0x190A55AD
23 self.assertEqual(expected, output)
24
25 def test_crc32_values_one_shot(self):
26 output = checksums.crc32(''.join(chr(i) for i in range(32)))
27 expected = 0x91267E8A
28 self.assertEqual(expected, output)
29
30 def test_crc32_values_iterated(self):
31 output = 0
32 for i in range(32):
33 output = checksums.crc32(chr(i), output)
34 expected = 0x91267E8A
35 self.assertEqual(expected, output)
36
37 def test_crc32_large_buffer(self):
38 # stress test gil optimization for 32 bit architecture which cannot handle huge buffer
39 large_buffer = bytes(25 * 2**20)
40 val = checksums.crc32(large_buffer)
41 self.assertEqual(0x72103906, val)
42
43 def test_crc32_huge_buffer(self):
44 # stress the internal logic that handles buffers larger than C's INT_MAX
45 if sys.platform.startswith('freebsd'):
46 # Skip this test for freebsd, as it simply crashes instead of raising exception in this case
47 raise unittest.SkipTest('Skip this test for freebsd')
48 try:
49 INT_MAX = 2**32 - 1
50 huge_buffer = bytes(INT_MAX + 5)
51 except (MemoryError, OverflowError):
52 raise unittest.SkipTest('Machine cant allocate giant buffer for giant buffer test')
53 val = checksums.crc32(huge_buffer)
54 self.assertEqual(0xc622f71d, val)
55
56 def test_crc32c_zeros_one_shot(self):
57 output = checksums.crc32c(bytes(32))
58 expected = 0x8A9136AA
59 self.assertEqual(expected, output)
60
61 def test_crc32c_zeros_iterated(self):
62 output = 0
63 for i in range(32):
64 output = checksums.crc32c(bytes(1), output)
65 expected = 0x8A9136AA
66 self.assertEqual(expected, output)
67
68 def test_crc32c_values_one_shot(self):
69 output = checksums.crc32c(''.join(chr(i) for i in range(32)))
70 expected = 0x46DD794E
71 self.assertEqual(expected, output)
72
73 def test_crc32c_values_iterated(self):
74 output = 0
75 for i in range(32):
76 output = checksums.crc32c(chr(i), output)
77 expected = 0x46DD794E
78 self.assertEqual(expected, output)
79
80 def test_crc32c_large_buffer(self):
81 # stress test gil optimization for 32 bit architecture which cannot handle huge buffer
82 large_buffer = bytes(25 * 2**20)
83 val = checksums.crc32c(large_buffer)
84 self.assertEqual(0xfb5b991d, val)
85
86 def test_crc32c_huge_buffer(self):
87 if sys.platform.startswith('freebsd'):
88 # Skip this test for freebsd, as it simply crashes instead of raising exception in this case
89 raise unittest.SkipTest('Skip this test for freebsd')
90 try:
91 INT_MAX = 2**32 - 1
92 huge_buffer = bytes(INT_MAX + 5)
93 except BaseException:
94 raise unittest.SkipTest('Machine cant allocate giant buffer for giant buffer test')
95 val = checksums.crc32c(huge_buffer)
96 self.assertEqual(0x572a7c8a, val)
97
98 def test_crc64nvme_zeros_one_shot(self):
99 output = checksums.crc64nvme(bytes(32))
100 expected = 0xcf3473434d4ecf3b
101 self.assertEqual(expected, output)
102
103 def test_crc64nvme_zeros_iterated(self):
104 output = 0
105 for i in range(32):
106 output = checksums.crc64nvme(bytes(1), output)
107 expected = 0xcf3473434d4ecf3b
108 self.assertEqual(expected, output)
109
110 def test_crc64nvme_values_one_shot(self):
111 output = checksums.crc64nvme(''.join(chr(i) for i in range(32)))
112 expected = 0xb9d9d4a8492cbd7f
113 self.assertEqual(expected, output)
114
115 def test_crc64nvme_values_iterated(self):
116 output = 0
117 for i in range(32):
118 output = checksums.crc64nvme(chr(i), output)
119 expected = 0xb9d9d4a8492cbd7f
120 self.assertEqual(expected, output)
121
122 def test_crc64nvme_large_buffer(self):
123 # stress test gil optimization for 32 bit architecture which cannot handle huge buffer
124 large_buffer = bytes(25 * 2**20)
125 val = checksums.crc64nvme(large_buffer)
126 self.assertEqual(0x5b6f5045463ca45e, val)
127
128 def test_crc64nvme_huge_buffer(self):
129 if sys.platform.startswith('freebsd'):
130 # Skip this test for freebsd, as it simply crashes instead of raising exception in this case
131 raise unittest.SkipTest('Skip this test for freebsd')
132 try:
133 INT_MAX = 2**32 - 1
134 huge_buffer = bytes(INT_MAX + 5)
135 except BaseException:
136 raise unittest.SkipTest('Machine cant allocate giant buffer for giant buffer test')
137 val = checksums.crc64nvme(huge_buffer)
138 self.assertEqual(0x2645c28052b1fbb0, val)
139
140 def _test_combine_helper(self, checksum_fn, combine_fn):
141 """Helper method to test checksum combine functions with various scenarios."""
142
143 # Test 1: Basic combine of two blocks
144 data1 = b"Hello, "
145 data2 = b"World!"
146
147 crc1 = checksum_fn(data1)
148 crc2 = checksum_fn(data2)
149 combined = combine_fn(crc1, crc2, len(data2))
150 expected = checksum_fn(data1 + data2)
151
152 self.assertEqual(expected, combined)
153
154 # Test 2: Empty second block
155 data1 = b"Hello, World!"
156 data2 = b""
157
158 crc1 = checksum_fn(data1)
159 crc2 = checksum_fn(data2)
160 combined = combine_fn(crc1, crc2, len(data2))
161
162 self.assertEqual(crc1, combined)
163
164 # Test 3: Multiple blocks
165 data1 = b"The quick "
166 data2 = b"brown fox "
167 data3 = b"jumps over the lazy dog"
168
169 crc1 = checksum_fn(data1)
170 crc2 = checksum_fn(data2)
171 crc3 = checksum_fn(data3)
172
173 combined_12 = combine_fn(crc1, crc2, len(data2))
174 combined_123 = combine_fn(combined_12, crc3, len(data3))
175 expected = checksum_fn(data1 + data2 + data3)
176
177 self.assertEqual(expected, combined_123)
178
179 # Test 4: Large blocks
180 data1 = bytes(1024)
181 data2 = bytes(range(256)) * 4
182
183 crc1 = checksum_fn(data1)
184 crc2 = checksum_fn(data2)
185 combined = combine_fn(crc1, crc2, len(data2))
186 expected = checksum_fn(data1 + data2)
187
188 self.assertEqual(expected, combined)
189
190 def test_crc32_combine(self):
191 """Test CRC32 combine function."""
192 self._test_combine_helper(checksums.crc32, checksums.combine_crc32)
193
194 def test_crc32c_combine(self):
195 """Test CRC32C combine function."""
196 self._test_combine_helper(checksums.crc32c, checksums.combine_crc32c)
197
198 def test_crc64nvme_combine(self):
199 """Test CRC64-NVME combine function."""
200 self._test_combine_helper(checksums.crc64nvme, checksums.combine_crc64nvme)
201
202 def test_combine_invalid_inputs(self):
203 """Test that combine functions raise ValueError for invalid inputs."""
204 # Test invalid values (should fail for all algorithms)
205 for combine_fn in [checksums.combine_crc32, checksums.combine_crc32c, checksums.combine_crc64nvme]:
206 with self.assertRaises(ValueError) as context:
207 combine_fn(-1, 0, 0)
208 self.assertIn("not a valid unsigned", str(context.exception))
209
210 with self.assertRaises(ValueError) as context:
211 combine_fn(0, 0, -1)
212 self.assertIn("not a valid unsigned", str(context.exception))
213
214 # Test that valid inputs don't raise exceptions
215 for combine_fn in [checksums.combine_crc32, checksums.combine_crc32c, checksums.combine_crc64nvme]:
216 # This should not raise any exception
217 result = combine_fn(0, 0, 0)
218 # Result should be an integer
219 self.assertIsInstance(result, int)
220
221 def test_xxhash64_piping(self):
222 """Test xxhash64 piping from native side"""
223 data = b"Hello world"
224
225 out = checksums.XXHash.compute_xxhash64(data)
226
227 expected = bytes([0xc5, 0x00, 0xb0, 0xc9, 0x12, 0xb3, 0x76, 0xd8])
228
229 self.assertEqual(out, expected)
230
231 hash = checksums.XXHash.new_xxhash64()
232 hash.update(data)
233 out2 = hash.finalize()
234 self.assertEqual(out2, expected)
235
236 def test_xxhash3_64_piping(self):
237 """Test xxhash3_64 piping from native side"""
238 data = b"Hello world"
239
240 out = checksums.XXHash.compute_xxhash3_64(data)
241
242 expected = bytes([0xb6, 0xac, 0xb9, 0xd8, 0x4a, 0x38, 0xff, 0x74])
243
244 self.assertEqual(out, expected)
245
246 hash = checksums.XXHash.new_xxhash3_64()
247 hash.update(data)
248 out2 = hash.finalize()
249 self.assertEqual(out2, expected)
250
251 def test_xxhash3_128_piping(self):
252 """Test xxhash3_128 piping from native side"""
253 data = b"Hello world"
254
255 out = checksums.XXHash.compute_xxhash3_128(data)
256
257 expected = bytes([0x73, 0x51, 0xf8, 0x98, 0x12, 0xf9, 0x73, 0x82,
258 0xb9, 0x1d, 0x05, 0xb3, 0x1e, 0x04, 0xdd, 0x7f])
259
260 self.assertEqual(out, expected)
261
262 hash = checksums.XXHash.new_xxhash3_128()
263 hash.update(data)
264 out2 = hash.finalize()
265 self.assertEqual(out2, expected)
266
267
268 if __name__ == '__main__':
269 unittest.main()