|
12 | 12 | import itertools
|
13 | 13 | import logging
|
14 | 14 | import os
|
| 15 | +import re |
15 | 16 | import sys
|
16 | 17 | import sysconfig
|
17 | 18 | import tempfile
|
@@ -140,11 +141,10 @@ def __init__(self, *args, **kwargs):
|
140 | 141 | # of hashlib.new given the algorithm name.
|
141 | 142 | for algorithm, constructors in self.constructors_to_test.items():
|
142 | 143 | constructors.add(getattr(hashlib, algorithm))
|
143 |
| - def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm, **kwargs): |
144 |
| - if data is None: |
145 |
| - return hashlib.new(_alg, **kwargs) |
146 |
| - return hashlib.new(_alg, data, **kwargs) |
147 |
| - constructors.add(_test_algorithm_via_hashlib_new) |
| 144 | + def c(*args, __algorithm_name=algorithm, **kwargs): |
| 145 | + return hashlib.new(__algorithm_name, *args, **kwargs) |
| 146 | + c.__name__ = f'do_test_algorithm_via_hashlib_new_{algorithm}' |
| 147 | + constructors.add(c) |
148 | 148 |
|
149 | 149 | _hashlib = self._conditional_import_module('_hashlib')
|
150 | 150 | self._hashlib = _hashlib
|
@@ -249,6 +249,56 @@ def test_usedforsecurity_false(self):
|
249 | 249 | self._hashlib.new("md5", usedforsecurity=False)
|
250 | 250 | self._hashlib.openssl_md5(usedforsecurity=False)
|
251 | 251 |
|
| 252 | + def test_clinic_signature(self): |
| 253 | + for constructor in self.hash_constructors: |
| 254 | + with self.subTest(constructor.__name__): |
| 255 | + constructor(b'') |
| 256 | + constructor(data=b'') |
| 257 | + constructor(string=b'') # should be deprecated in the future |
| 258 | + |
| 259 | + def test_clinic_signature_errors(self): |
| 260 | + nomsg = b'' |
| 261 | + mymsg = b'msg' |
| 262 | + conflicting_call = re.escape( |
| 263 | + "'data' and 'string' are mutually exclusive " |
| 264 | + "and support for 'string' keyword parameter " |
| 265 | + "is slated for removal in a future version." |
| 266 | + ) |
| 267 | + duplicated_param = re.escape("given by name ('data') and position") |
| 268 | + unexpected_param = re.escape("got an unexpected keyword argument '_'") |
| 269 | + for args, kwds, errmsg in [ |
| 270 | + # Reject duplicated arguments before unknown keyword arguments. |
| 271 | + ((nomsg,), dict(data=nomsg, _=nomsg), duplicated_param), |
| 272 | + ((mymsg,), dict(data=nomsg, _=nomsg), duplicated_param), |
| 273 | + # Reject duplicated arguments before conflicting ones. |
| 274 | + *itertools.product( |
| 275 | + [[nomsg], [mymsg]], |
| 276 | + [dict(data=nomsg), dict(data=nomsg, string=nomsg)], |
| 277 | + [duplicated_param] |
| 278 | + ), |
| 279 | + # Reject unknown keyword arguments before conflicting ones. |
| 280 | + *itertools.product( |
| 281 | + [()], |
| 282 | + [ |
| 283 | + dict(_=None), |
| 284 | + dict(data=nomsg, _=None), |
| 285 | + dict(string=nomsg, _=None), |
| 286 | + dict(string=nomsg, data=nomsg, _=None), |
| 287 | + ], |
| 288 | + [unexpected_param] |
| 289 | + ), |
| 290 | + ((nomsg,), dict(_=None), unexpected_param), |
| 291 | + ((mymsg,), dict(_=None), unexpected_param), |
| 292 | + # Reject conflicting arguments. |
| 293 | + [(nomsg,), dict(string=nomsg), conflicting_call], |
| 294 | + [(mymsg,), dict(string=nomsg), conflicting_call], |
| 295 | + [(), dict(data=nomsg, string=nomsg), conflicting_call], |
| 296 | + ]: |
| 297 | + for constructor in self.hash_constructors: |
| 298 | + with self.subTest(constructor.__name__, args=args, kwds=kwds): |
| 299 | + with self.assertRaisesRegex(TypeError, errmsg): |
| 300 | + constructor(*args, **kwds) |
| 301 | + |
252 | 302 | def test_unknown_hash(self):
|
253 | 303 | self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
|
254 | 304 | self.assertRaises(TypeError, hashlib.new, 1)
|
@@ -718,8 +768,6 @@ def check_blake2(self, constructor, salt_size, person_size, key_size,
|
718 | 768 | self.assertRaises(ValueError, constructor, node_offset=-1)
|
719 | 769 | self.assertRaises(OverflowError, constructor, node_offset=max_offset+1)
|
720 | 770 |
|
721 |
| - self.assertRaises(TypeError, constructor, data=b'') |
722 |
| - self.assertRaises(TypeError, constructor, string=b'') |
723 | 771 | self.assertRaises(TypeError, constructor, '')
|
724 | 772 |
|
725 | 773 | constructor(
|
|
0 commit comments