Skip to content

Commit 5cd60d1

Browse files
committed
improve tests
1 parent 55b2afa commit 5cd60d1

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

Lib/test/test_hashlib.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,28 +1027,30 @@ def test_sha256_gil(self):
10271027
@threading_helper.reap_threads
10281028
@threading_helper.requires_working_threading()
10291029
def test_threaded_hashing(self):
1030-
for constructor in self.hash_constructors:
1031-
if constructor().name not in self.shakes:
1032-
with self.subTest(constructor=constructor):
1033-
self.do_test_threaded_hashing(constructor)
1030+
for algorithm, constructors in self.constructors_to_test.items():
1031+
is_shake = algorithm in self.shakes
1032+
for constructor in constructors:
1033+
with self.subTest(constructor=constructor, is_shake=is_shake):
1034+
self.do_test_threaded_hashing(constructor, is_shake)
10341035

1035-
def do_test_threaded_hashing(self, constructor):
1036+
def do_test_threaded_hashing(self, constructor, is_shake):
10361037
# Updating the same hash object from several threads at once
10371038
# using data chunk sizes containing the same byte sequences.
10381039
#
10391040
# If the internal locks are working to prevent multiple
10401041
# updates on the same object from running at once, the resulting
10411042
# hash will be the same as doing it single threaded upfront.
1042-
hasher = constructor()
10431043
num_threads = 5
1044-
smallest_data = b'swineflu'
1044+
smallest_data = os.urandom(16)
10451045
data = smallest_data * 200000
1046-
expected_hash = constructor(data*num_threads).hexdigest()
1046+
1047+
h1 = constructor()
1048+
h2 = constructor(data * num_threads)
10471049

10481050
def hash_in_chunks(chunk_size):
10491051
index = 0
10501052
while index < len(data):
1051-
hasher.update(data[index:index + chunk_size])
1053+
h1.update(data[index:index + chunk_size])
10521054
index += chunk_size
10531055

10541056
threads = []
@@ -1065,7 +1067,10 @@ def hash_in_chunks(chunk_size):
10651067
for thread in threads:
10661068
thread.join()
10671069

1068-
self.assertEqual(expected_hash, hasher.hexdigest())
1070+
if is_shake:
1071+
self.assertEqual(h1.hexdigest(16), h2.hexdigest(16))
1072+
else:
1073+
self.assertEqual(h1.hexdigest(), h2.hexdigest())
10691074

10701075
def test_get_fips_mode(self):
10711076
fips_mode = self.is_fips_mode

0 commit comments

Comments
 (0)