|
10 | 10 | from binascii import unhexlify
|
11 | 11 | import hashlib
|
12 | 12 | import importlib
|
| 13 | +import io |
13 | 14 | import itertools
|
14 | 15 | import os
|
15 | 16 | import sys
|
|
20 | 21 | from test import support
|
21 | 22 | from test.support import _4G, bigmemtest
|
22 | 23 | from test.support.import_helper import import_fresh_module
|
| 24 | +from test.support import os_helper |
23 | 25 | from test.support import threading_helper
|
24 | 26 | from test.support import warnings_helper
|
25 | 27 | from http.client import HTTPException
|
@@ -102,8 +104,7 @@ class HashLibTestCase(unittest.TestCase):
|
102 | 104 | 'sha384', 'SHA384', 'sha512', 'SHA512',
|
103 | 105 | 'blake2b', 'blake2s',
|
104 | 106 | 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
|
105 |
| - 'shake_128', 'shake_256' |
106 |
| - ) |
| 107 | + 'shake_128', 'shake_256') |
107 | 108 |
|
108 | 109 | shakes = {'shake_128', 'shake_256'}
|
109 | 110 |
|
@@ -384,6 +385,36 @@ def check(self, name, data, hexdigest, shake=False, **kwargs):
|
384 | 385 | if not shake:
|
385 | 386 | self.assertEqual(len(digest), m.digest_size)
|
386 | 387 |
|
| 388 | + if not shake and kwargs.get("key") is None: |
| 389 | + # skip shake and blake2 extended parameter tests |
| 390 | + self.check_file_digest(name, data, hexdigest) |
| 391 | + |
| 392 | + def check_file_digest(self, name, data, hexdigest): |
| 393 | + hexdigest = hexdigest.lower() |
| 394 | + try: |
| 395 | + hashlib.new(name) |
| 396 | + except ValueError: |
| 397 | + # skip, algorithm is blocked by security policy. |
| 398 | + return |
| 399 | + digests = [name] |
| 400 | + digests.extend(self.constructors_to_test[name]) |
| 401 | + |
| 402 | + with open(os_helper.TESTFN, "wb") as f: |
| 403 | + f.write(data) |
| 404 | + |
| 405 | + try: |
| 406 | + for digest in digests: |
| 407 | + buf = io.BytesIO(data) |
| 408 | + buf.seek(0) |
| 409 | + self.assertEqual( |
| 410 | + hashlib.file_digest(buf, digest).hexdigest(), hexdigest |
| 411 | + ) |
| 412 | + with open(os_helper.TESTFN, "rb") as f: |
| 413 | + digestobj = hashlib.file_digest(f, digest) |
| 414 | + self.assertEqual(digestobj.hexdigest(), hexdigest) |
| 415 | + finally: |
| 416 | + os.unlink(os_helper.TESTFN) |
| 417 | + |
387 | 418 | def check_no_unicode(self, algorithm_name):
|
388 | 419 | # Unicode objects are not allowed as input.
|
389 | 420 | constructors = self.constructors_to_test[algorithm_name]
|
@@ -898,6 +929,7 @@ def test_gil(self):
|
898 | 929 | )
|
899 | 930 |
|
900 | 931 | @threading_helper.reap_threads
|
| 932 | + @threading_helper.requires_working_threading() |
901 | 933 | def test_threaded_hashing(self):
|
902 | 934 | # Updating the same hash object from several threads at once
|
903 | 935 | # using data chunk sizes containing the same byte sequences.
|
@@ -1142,6 +1174,35 @@ def test_normalized_name(self):
|
1142 | 1174 | self.assertNotIn("blake2b512", hashlib.algorithms_available)
|
1143 | 1175 | self.assertNotIn("sha3-512", hashlib.algorithms_available)
|
1144 | 1176 |
|
| 1177 | + # TODO: RUSTPYTHON |
| 1178 | + @unittest.expectedFailure |
| 1179 | + def test_file_digest(self): |
| 1180 | + data = b'a' * 65536 |
| 1181 | + d1 = hashlib.sha256() |
| 1182 | + self.addCleanup(os.unlink, os_helper.TESTFN) |
| 1183 | + with open(os_helper.TESTFN, "wb") as f: |
| 1184 | + for _ in range(10): |
| 1185 | + d1.update(data) |
| 1186 | + f.write(data) |
| 1187 | + |
| 1188 | + with open(os_helper.TESTFN, "rb") as f: |
| 1189 | + d2 = hashlib.file_digest(f, hashlib.sha256) |
| 1190 | + |
| 1191 | + self.assertEqual(d1.hexdigest(), d2.hexdigest()) |
| 1192 | + self.assertEqual(d1.name, d2.name) |
| 1193 | + self.assertIs(type(d1), type(d2)) |
| 1194 | + |
| 1195 | + with self.assertRaises(ValueError): |
| 1196 | + hashlib.file_digest(None, "sha256") |
| 1197 | + |
| 1198 | + with self.assertRaises(ValueError): |
| 1199 | + with open(os_helper.TESTFN, "r") as f: |
| 1200 | + hashlib.file_digest(f, "sha256") |
| 1201 | + |
| 1202 | + with self.assertRaises(ValueError): |
| 1203 | + with open(os_helper.TESTFN, "wb") as f: |
| 1204 | + hashlib.file_digest(f, "sha256") |
| 1205 | + |
1145 | 1206 |
|
1146 | 1207 | if __name__ == "__main__":
|
1147 | 1208 | unittest.main()
|
0 commit comments