Skip to content

Commit 784197e

Browse files
committed
Update test_hashlib.py from Cpython v3.11.2
1 parent 6c6290d commit 784197e

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

Lib/test/test_hashlib.py

+63-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from binascii import unhexlify
1111
import hashlib
1212
import importlib
13+
import io
1314
import itertools
1415
import os
1516
import sys
@@ -20,6 +21,7 @@
2021
from test import support
2122
from test.support import _4G, bigmemtest
2223
from test.support.import_helper import import_fresh_module
24+
from test.support import os_helper
2325
from test.support import threading_helper
2426
from test.support import warnings_helper
2527
from http.client import HTTPException
@@ -102,8 +104,7 @@ class HashLibTestCase(unittest.TestCase):
102104
'sha384', 'SHA384', 'sha512', 'SHA512',
103105
'blake2b', 'blake2s',
104106
'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
105-
'shake_128', 'shake_256'
106-
)
107+
'shake_128', 'shake_256')
107108

108109
shakes = {'shake_128', 'shake_256'}
109110

@@ -384,6 +385,36 @@ def check(self, name, data, hexdigest, shake=False, **kwargs):
384385
if not shake:
385386
self.assertEqual(len(digest), m.digest_size)
386387

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+
387418
def check_no_unicode(self, algorithm_name):
388419
# Unicode objects are not allowed as input.
389420
constructors = self.constructors_to_test[algorithm_name]
@@ -898,6 +929,7 @@ def test_gil(self):
898929
)
899930

900931
@threading_helper.reap_threads
932+
@threading_helper.requires_working_threading()
901933
def test_threaded_hashing(self):
902934
# Updating the same hash object from several threads at once
903935
# using data chunk sizes containing the same byte sequences.
@@ -1142,6 +1174,35 @@ def test_normalized_name(self):
11421174
self.assertNotIn("blake2b512", hashlib.algorithms_available)
11431175
self.assertNotIn("sha3-512", hashlib.algorithms_available)
11441176

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+
11451206

11461207
if __name__ == "__main__":
11471208
unittest.main()

0 commit comments

Comments
 (0)