forked from Rust-related/RustPython
Update test_hashlib.py from Cpython v3.11.2
This commit is contained in:
committed by
Jeong YunWon
parent
94bdb6b97a
commit
8782bf8cb0
65
Lib/test/test_hashlib.py
vendored
65
Lib/test/test_hashlib.py
vendored
@@ -10,6 +10,7 @@ import array
|
||||
from binascii import unhexlify
|
||||
import hashlib
|
||||
import importlib
|
||||
import io
|
||||
import itertools
|
||||
import os
|
||||
import sys
|
||||
@@ -20,6 +21,7 @@ import warnings
|
||||
from test import support
|
||||
from test.support import _4G, bigmemtest
|
||||
from test.support.import_helper import import_fresh_module
|
||||
from test.support import os_helper
|
||||
from test.support import threading_helper
|
||||
from test.support import warnings_helper
|
||||
from http.client import HTTPException
|
||||
@@ -102,8 +104,7 @@ class HashLibTestCase(unittest.TestCase):
|
||||
'sha384', 'SHA384', 'sha512', 'SHA512',
|
||||
'blake2b', 'blake2s',
|
||||
'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
|
||||
'shake_128', 'shake_256'
|
||||
)
|
||||
'shake_128', 'shake_256')
|
||||
|
||||
shakes = {'shake_128', 'shake_256'}
|
||||
|
||||
@@ -384,6 +385,36 @@ class HashLibTestCase(unittest.TestCase):
|
||||
if not shake:
|
||||
self.assertEqual(len(digest), m.digest_size)
|
||||
|
||||
if not shake and kwargs.get("key") is None:
|
||||
# skip shake and blake2 extended parameter tests
|
||||
self.check_file_digest(name, data, hexdigest)
|
||||
|
||||
def check_file_digest(self, name, data, hexdigest):
|
||||
hexdigest = hexdigest.lower()
|
||||
try:
|
||||
hashlib.new(name)
|
||||
except ValueError:
|
||||
# skip, algorithm is blocked by security policy.
|
||||
return
|
||||
digests = [name]
|
||||
digests.extend(self.constructors_to_test[name])
|
||||
|
||||
with open(os_helper.TESTFN, "wb") as f:
|
||||
f.write(data)
|
||||
|
||||
try:
|
||||
for digest in digests:
|
||||
buf = io.BytesIO(data)
|
||||
buf.seek(0)
|
||||
self.assertEqual(
|
||||
hashlib.file_digest(buf, digest).hexdigest(), hexdigest
|
||||
)
|
||||
with open(os_helper.TESTFN, "rb") as f:
|
||||
digestobj = hashlib.file_digest(f, digest)
|
||||
self.assertEqual(digestobj.hexdigest(), hexdigest)
|
||||
finally:
|
||||
os.unlink(os_helper.TESTFN)
|
||||
|
||||
def check_no_unicode(self, algorithm_name):
|
||||
# Unicode objects are not allowed as input.
|
||||
constructors = self.constructors_to_test[algorithm_name]
|
||||
@@ -898,6 +929,7 @@ class HashLibTestCase(unittest.TestCase):
|
||||
)
|
||||
|
||||
@threading_helper.reap_threads
|
||||
@threading_helper.requires_working_threading()
|
||||
def test_threaded_hashing(self):
|
||||
# Updating the same hash object from several threads at once
|
||||
# using data chunk sizes containing the same byte sequences.
|
||||
@@ -1142,6 +1174,35 @@ class KDFTests(unittest.TestCase):
|
||||
self.assertNotIn("blake2b512", hashlib.algorithms_available)
|
||||
self.assertNotIn("sha3-512", hashlib.algorithms_available)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_file_digest(self):
|
||||
data = b'a' * 65536
|
||||
d1 = hashlib.sha256()
|
||||
self.addCleanup(os.unlink, os_helper.TESTFN)
|
||||
with open(os_helper.TESTFN, "wb") as f:
|
||||
for _ in range(10):
|
||||
d1.update(data)
|
||||
f.write(data)
|
||||
|
||||
with open(os_helper.TESTFN, "rb") as f:
|
||||
d2 = hashlib.file_digest(f, hashlib.sha256)
|
||||
|
||||
self.assertEqual(d1.hexdigest(), d2.hexdigest())
|
||||
self.assertEqual(d1.name, d2.name)
|
||||
self.assertIs(type(d1), type(d2))
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
hashlib.file_digest(None, "sha256")
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with open(os_helper.TESTFN, "r") as f:
|
||||
hashlib.file_digest(f, "sha256")
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with open(os_helper.TESTFN, "wb") as f:
|
||||
hashlib.file_digest(f, "sha256")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user