Skip to content

Commit 0565277

Browse files
committed
add TSAN tests for BLAKE2
1 parent 374bfef commit 0565277

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

Lib/test/test_hashlib.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import tempfile
1919
import threading
2020
import unittest
21+
from functools import partial
22+
from operator import attrgetter
2123
from test import support
2224
from test.support import _4G, bigmemtest
2325
from test.support import hashlib_helper
@@ -56,14 +58,12 @@ def get_fips_mode():
5658
import _blake2
5759
except ImportError:
5860
_blake2 = None
59-
6061
requires_blake2 = unittest.skipUnless(_blake2, 'requires _blake2')
6162

6263
try:
6364
import _sha3
6465
except ImportError:
6566
_sha3 = None
66-
6767
requires_sha3 = unittest.skipUnless(_sha3, 'requires _sha3')
6868

6969

@@ -1418,5 +1418,42 @@ def scrypt(password=b"password", /, **kwargs):
14181418
self.assertRaises(numeric_exc_types, scrypt, dklen=MAX_DKLEN + 1)
14191419

14201420

1421+
@threading_helper.requires_working_threading()
1422+
class TestTSAN(unittest.TestCase):
1423+
1424+
@threading_helper.reap_threads
1425+
def check_attribute(self, write, read, expected, nthreads=8):
1426+
ready = threading.Event()
1427+
barrier = threading.Barrier(nthreads)
1428+
1429+
def writer():
1430+
barrier.wait()
1431+
while not ready.is_set():
1432+
write()
1433+
1434+
def reader():
1435+
barrier.wait()
1436+
while not ready.is_set():
1437+
self.assertEqual(read(), expected)
1438+
1439+
targets = [writer if i % 2 else reader for i in range(nthreads)]
1440+
workers = [threading.Thread(target=target) for target in targets]
1441+
with threading_helper.start_threads(workers, unlock=ready.set):
1442+
pass
1443+
1444+
def check_HACL_attribute(self, module, version, attrname):
1445+
blob = b"A" * 65536
1446+
obj = getattr(module, version)()
1447+
update = partial(obj.update, blob)
1448+
read = attrgetter(attrname)
1449+
self.check_attribute(update, partial(read, obj), read(obj))
1450+
1451+
@requires_blake2
1452+
@support.subTests("version", ["blake2s", "blake2b"])
1453+
@support.subTests("attrname", ["block_size", "digest_size"])
1454+
def test_HACL_blake2_attributes(self, version, attrname):
1455+
self.check_HACL_attribute(module, version, attrname)
1456+
1457+
14211458
if __name__ == "__main__":
14221459
unittest.main()

0 commit comments

Comments
 (0)