Lines Matching refs:self
114 def _conditional_import_module(self, module_name):
119 if self._warn_on_extension_import and module_name in builtin_hashes:
123 def __init__(self, *args, **kwargs):
125 for algorithm in self.supported_hash_names:
128 _blake2 = self._conditional_import_module('_blake2')
132 self.constructors_to_test = {}
136 self.constructors_to_test[algorithm] = set()
140 for algorithm, constructors in self.constructors_to_test.items():
148 _hashlib = self._conditional_import_module('_hashlib')
149 self._hashlib = _hashlib
153 self.assertTrue(hasattr(_hashlib, 'openssl_md5'))
154 self.assertTrue(hasattr(_hashlib, 'openssl_sha1'))
155 for algorithm, constructors in self.constructors_to_test.items():
168 self.constructors_to_test[name].add(constructor)
170 _md5 = self._conditional_import_module('_md5')
173 _sha1 = self._conditional_import_module('_sha1')
176 _sha256 = self._conditional_import_module('_sha256')
180 _sha512 = self._conditional_import_module('_sha512')
189 _sha3 = self._conditional_import_module('_sha3')
198 super(HashLibTestCase, self).__init__(*args, **kwargs)
201 def hash_constructors(self):
202 constructors = self.constructors_to_test.values()
206 def is_fips_mode(self):
209 def test_hash_array(self):
211 for cons in self.hash_constructors:
213 if c.name in self.shakes:
218 def test_algorithms_guaranteed(self):
219 self.assertEqual(hashlib.algorithms_guaranteed,
220 set(_algo for _algo in self.supported_hash_names
223 def test_algorithms_available(self):
224 self.assertTrue(set(hashlib.algorithms_guaranteed).
227 self.assertNotIn("undefined", hashlib.algorithms_available)
231 def test_usedforsecurity_true(self):
233 if self.is_fips_mode:
234 self.skipTest("skip in FIPS mode")
235 for cons in self.hash_constructors:
240 if self._hashlib is not None:
241 self._hashlib.new("md5", usedforsecurity=True)
242 self._hashlib.openssl_md5(usedforsecurity=True)
244 def test_usedforsecurity_false(self):
246 for cons in self.hash_constructors:
251 if self._hashlib is not None:
252 self._hashlib.new("md5", usedforsecurity=False)
253 self._hashlib.openssl_md5(usedforsecurity=False)
255 def test_unknown_hash(self):
256 self.assertRaises(ValueError, hashlib.new, 'spam spam spam spam spam')
257 self.assertRaises(TypeError, hashlib.new, 1)
259 def test_new_upper_to_lower(self):
260 self.assertEqual(hashlib.new("SHA256").name, "sha256")
262 def test_get_builtin_constructor(self):
267 self.assertRaises(ValueError, get_builtin_constructor, 'test')
271 self.skipTest("_md5 module not available")
277 self.assertRaises(ValueError, get_builtin_constructor, 'md5')
283 self.assertRaises(TypeError, get_builtin_constructor, 3)
285 self.assertIs(constructor, _md5.md5)
286 self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5'])
288 def test_hexdigest(self):
289 for cons in self.hash_constructors:
291 if h.name in self.shakes:
292 self.assertIsInstance(h.digest(16), bytes)
293 self.assertEqual(hexstr(h.digest(16)), h.hexdigest(16))
295 self.assertIsInstance(h.digest(), bytes)
296 self.assertEqual(hexstr(h.digest()), h.hexdigest())
298 def test_digest_length_overflow(self):
301 for cons in self.hash_constructors:
303 if h.name not in self.shakes:
309 self.assertRaises(ValueError, digest, -10)
311 with self.assertRaises((ValueError, OverflowError)):
314 def test_name_attribute(self):
315 for cons in self.hash_constructors:
317 self.assertIsInstance(h.name, str)
318 if h.name in self.supported_hash_names:
319 self.assertIn(h.name, self.supported_hash_names)
321 self.assertNotIn(h.name, self.supported_hash_names)
322 self.assertEqual(
327 def test_large_update(self):
333 for cons in self.hash_constructors:
339 if m1.name in self.shakes:
346 self.assertEqual(m1.digest(*args), m2.digest(*args))
349 self.assertEqual(m1.digest(*args), m3.digest(*args))
356 self.assertEqual(m1.digest(*args), m4_copy.digest(*args))
357 self.assertEqual(m4.digest(*args), m4_digest)
359 def check(self, name, data, hexdigest, shake=False, **kwargs):
362 constructors = self.constructors_to_test[name]
364 self.assertGreaterEqual(len(constructors), 2)
368 self.assertEqual(
376 self.assertEqual(computed, digest)
378 self.assertEqual(len(digest), m.digest_size)
382 self.check_file_digest(name, data, hexdigest)
384 def check_file_digest(self, name, data, hexdigest):
392 digests.extend(self.constructors_to_test[name])
401 self.assertEqual(
406 self.assertEqual(digestobj.hexdigest(), hexdigest)
410 def check_no_unicode(self, algorithm_name):
412 constructors = self.constructors_to_test[algorithm_name]
414 self.assertRaises(TypeError, hash_object_constructor, 'spam')
416 def test_no_unicode(self):
417 self.check_no_unicode('md5')
418 self.check_no_unicode('sha1')
419 self.check_no_unicode('sha224')
420 self.check_no_unicode('sha256')
421 self.check_no_unicode('sha384')
422 self.check_no_unicode('sha512')
425 def test_no_unicode_blake2(self):
426 self.check_no_unicode('blake2b')
427 self.check_no_unicode('blake2s')
430 def test_no_unicode_sha3(self):
431 self.check_no_unicode('sha3_224')
432 self.check_no_unicode('sha3_256')
433 self.check_no_unicode('sha3_384')
434 self.check_no_unicode('sha3_512')
435 self.check_no_unicode('shake_128')
436 self.check_no_unicode('shake_256')
438 def check_blocksize_name(self, name, block_size=0, digest_size=0,
440 constructors = self.constructors_to_test[name]
443 self.assertEqual(m.block_size, block_size)
444 self.assertEqual(m.digest_size, digest_size)
446 self.assertEqual(len(m.digest(digest_length)),
448 self.assertEqual(len(m.hexdigest(digest_length)),
451 self.assertEqual(len(m.digest()), digest_size)
452 self.assertEqual(len(m.hexdigest()), 2*digest_size)
453 self.assertEqual(m.name, name)
455 self.assertIn(name.split("_")[0], repr(m))
457 def test_blocksize_name(self):
458 self.check_blocksize_name('md5', 64, 16)
459 self.check_blocksize_name('sha1', 64, 20)
460 self.check_blocksize_name('sha224', 64, 28)
461 self.check_blocksize_name('sha256', 64, 32)
462 self.check_blocksize_name('sha384', 128, 48)
463 self.check_blocksize_name('sha512', 128, 64)
466 def test_blocksize_name_sha3(self):
467 self.check_blocksize_name('sha3_224', 144, 28)
468 self.check_blocksize_name('sha3_256', 136, 32)
469 self.check_blocksize_name('sha3_384', 104, 48)
470 self.check_blocksize_name('sha3_512', 72, 64)
471 self.check_blocksize_name('shake_128', 168, 0, 32)
472 self.check_blocksize_name('shake_256', 136, 0, 64)
474 def check_sha3(self, name, capacity, rate, suffix):
475 constructors = self.constructors_to_test[name]
481 self.assertEqual(capacity + rate, 1600)
482 self.assertEqual(m._capacity_bits, capacity)
483 self.assertEqual(m._rate_bits, rate)
484 self.assertEqual(m._suffix, suffix)
487 def test_extra_sha3(self):
488 self.check_sha3('sha3_224', 448, 1152, b'\x06')
489 self.check_sha3('sha3_256', 512, 1088, b'\x06')
490 self.check_sha3('sha3_384', 768, 832, b'\x06')
491 self.check_sha3('sha3_512', 1024, 576, b'\x06')
492 self.check_sha3('shake_128', 256, 1344, b'\x1f')
493 self.check_sha3('shake_256', 512, 1088, b'\x1f')
496 def test_blocksize_name_blake2(self):
497 self.check_blocksize_name('blake2b', 128, 64)
498 self.check_blocksize_name('blake2s', 64, 32)
500 def test_case_md5_0(self):
501 self.check(
506 def test_case_md5_1(self):
507 self.check(
512 def test_case_md5_2(self):
513 self.check(
522 def test_case_md5_huge(self, size):
523 self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
527 def test_case_md5_uintmax(self, size):
528 self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
534 def test_case_sha1_0(self):
535 self.check('sha1', b"",
538 def test_case_sha1_1(self):
539 self.check('sha1', b"abc",
542 def test_case_sha1_2(self):
543 self.check('sha1',
547 def test_case_sha1_3(self):
548 self.check('sha1', b"a" * 1000000,
556 def test_case_sha224_0(self):
557 self.check('sha224', b"",
560 def test_case_sha224_1(self):
561 self.check('sha224', b"abc",
564 def test_case_sha224_2(self):
565 self.check('sha224',
569 def test_case_sha224_3(self):
570 self.check('sha224', b"a" * 1000000,
574 def test_case_sha256_0(self):
575 self.check('sha256', b"",
578 def test_case_sha256_1(self):
579 self.check('sha256', b"abc",
582 def test_case_sha256_2(self):
583 self.check('sha256',
587 def test_case_sha256_3(self):
588 self.check('sha256', b"a" * 1000000,
592 def test_case_sha384_0(self):
593 self.check('sha384', b"",
597 def test_case_sha384_1(self):
598 self.check('sha384', b"abc",
602 def test_case_sha384_2(self):
603 self.check('sha384',
609 def test_case_sha384_3(self):
610 self.check('sha384', b"a" * 1000000,
615 def test_case_sha512_0(self):
616 self.check('sha512', b"",
620 def test_case_sha512_1(self):
621 self.check('sha512', b"abc",
625 def test_case_sha512_2(self):
626 self.check('sha512',
632 def test_case_sha512_3(self):
633 self.check('sha512', b"a" * 1000000,
637 def check_blake2(self, constructor, salt_size, person_size, key_size,
639 self.assertEqual(constructor.SALT_SIZE, salt_size)
643 self.assertRaises(ValueError, constructor, salt=salt)
645 self.assertEqual(constructor.PERSON_SIZE, person_size)
649 self.assertRaises(ValueError, constructor, person=person)
651 self.assertEqual(constructor.MAX_DIGEST_SIZE, digest_size)
654 self.assertRaises(ValueError, constructor, digest_size=-1)
655 self.assertRaises(ValueError, constructor, digest_size=0)
656 self.assertRaises(ValueError, constructor, digest_size=digest_size+1)
658 self.assertEqual(constructor.MAX_KEY_SIZE, key_size)
662 self.assertRaises(ValueError, constructor, key=key)
663 self.assertEqual(constructor().hexdigest(),
668 self.assertRaises(ValueError, constructor, fanout=-1)
669 self.assertRaises(ValueError, constructor, fanout=256)
673 self.assertRaises(ValueError, constructor, depth=-1)
674 self.assertRaises(ValueError, constructor, depth=0)
675 self.assertRaises(ValueError, constructor, depth=256)
679 self.assertRaises(ValueError, constructor, node_depth=-1)
680 self.assertRaises(ValueError, constructor, node_depth=256)
684 self.assertRaises(ValueError, constructor, inner_size=-1)
685 self.assertRaises(ValueError, constructor, inner_size=digest_size+1)
689 self.assertRaises(ValueError, constructor, leaf_size=-1)
690 self.assertRaises(OverflowError, constructor, leaf_size=1<<32)
694 self.assertRaises(ValueError, constructor, node_offset=-1)
695 self.assertRaises(OverflowError, constructor, node_offset=max_offset+1)
697 self.assertRaises(TypeError, constructor, data=b'')
698 self.assertRaises(TypeError, constructor, string=b'')
699 self.assertRaises(TypeError, constructor, '')
716 def blake2_rfc7693(self, constructor, md_len, in_len):
739 def test_blake2b(self):
740 self.check_blake2(hashlib.blake2b, 16, 16, 64, 64, (1<<64)-1)
743 self.assertEqual(
744 self.blake2_rfc7693(hashlib.blake2b, b2b_md_len, b2b_in_len),
748 def test_case_blake2b_0(self):
749 self.check('blake2b', b"",
754 def test_case_blake2b_1(self):
755 self.check('blake2b', b"abc",
760 def test_case_blake2b_all_parameters(self):
763 self.check('blake2b', b"foo",
778 def test_blake2b_vectors(self):
781 self.check('blake2b', msg, md, key=key)
784 def test_blake2s(self):
785 self.check_blake2(hashlib.blake2s, 8, 8, 32, 32, (1<<48)-1)
788 self.assertEqual(
789 self.blake2_rfc7693(hashlib.blake2s, b2s_md_len, b2s_in_len),
793 def test_case_blake2s_0(self):
794 self.check('blake2s', b"",
798 def test_case_blake2s_1(self):
799 self.check('blake2s', b"abc",
803 def test_case_blake2s_all_parameters(self):
806 self.check('blake2s', b"foo",
821 def test_blake2s_vectors(self):
824 self.check('blake2s', msg, md, key=key)
827 def test_case_sha3_224_0(self):
828 self.check('sha3_224', b"",
832 def test_case_sha3_224_vector(self):
834 self.check('sha3_224', msg, md)
837 def test_case_sha3_256_0(self):
838 self.check('sha3_256', b"",
842 def test_case_sha3_256_vector(self):
844 self.check('sha3_256', msg, md)
847 def test_case_sha3_384_0(self):
848 self.check('sha3_384', b"",
853 def test_case_sha3_384_vector(self):
855 self.check('sha3_384', msg, md)
858 def test_case_sha3_512_0(self):
859 self.check('sha3_512', b"",
864 def test_case_sha3_512_vector(self):
866 self.check('sha3_512', msg, md)
868 def test_case_shake_128_0(self):
869 self.check('shake_128', b"",
872 self.check('shake_128', b"", "7f9c", True)
874 def test_case_shake128_vector(self):
876 self.check('shake_128', msg, md, True)
878 def test_case_shake_256_0(self):
879 self.check('shake_256', b"",
882 self.check('shake_256', b"", "46b9", True)
884 def test_case_shake256_vector(self):
886 self.check('shake_256', msg, md, True)
888 def test_gil(self):
893 for cons in self.hash_constructors:
906 self.assertEqual(
912 self.assertEqual(
919 def test_threaded_hashing(self):
941 self.assertGreater(chunk_size, 0)
942 self.assertEqual(chunk_size % len(smallest_data), 0)
952 self.assertEqual(expected_hash, hasher.hexdigest())
954 def test_get_fips_mode(self):
955 fips_mode = self.is_fips_mode
957 self.assertIsInstance(fips_mode, int)
960 def test_disallow_instantiation(self):
961 for algorithm, constructors in self.constructors_to_test.items():
972 with self.subTest(constructor=constructor):
973 support.check_disallow_instantiation(self, type(h))
976 def test_hash_disallow_instantiation(self):
978 support.check_disallow_instantiation(self, HASH)
979 support.check_disallow_instantiation(self, HASHXOF)
981 def test_readonly_types(self):
982 for algorithm, constructors in self.constructors_to_test.items():
990 with self.subTest(hash_type=hash_type):
991 with self.assertRaisesRegex(TypeError, "immutable type"):
1052 def _test_pbkdf2_hmac(self, pbkdf2, supported):
1053 for digest_name, results in self.pbkdf2_results.items():
1056 for i, vector in enumerate(self.pbkdf2_test_vectors):
1062 self.assertEqual(out, expected,
1066 self.assertEqual(out, expected)
1069 self.assertEqual(out, expected)
1072 self.assertEqual(out, expected,
1075 with self.assertRaisesRegex(ValueError, '.*unsupported.*'):
1079 self.assertRaises(
1082 self.assertRaises(
1085 self.assertRaises(
1088 self.assertRaises(
1091 self.assertRaises(
1094 self.assertRaises(
1099 self.assertEqual(out, self.pbkdf2_results['sha1'][0][0])
1102 def test_pbkdf2_hmac_py(self):
1104 self._test_pbkdf2_hmac(
1110 def test_pbkdf2_hmac_c(self):
1111 self._test_pbkdf2_hmac(openssl_hashlib.pbkdf2_hmac, openssl_md_meth_names)
1116 def test_scrypt(self):
1117 for password, salt, n, r, p, expected in self.scrypt_test_vectors:
1119 self.assertEqual(result, expected)
1124 with self.assertRaises(TypeError):
1126 with self.assertRaises(TypeError):
1129 with self.assertRaises(TypeError):
1131 with self.assertRaises(TypeError):
1133 with self.assertRaises(TypeError):
1136 with self.assertRaises((ValueError, OverflowError, TypeError)):
1139 with self.assertRaises((ValueError, OverflowError, TypeError)):
1142 with self.assertRaises((ValueError, OverflowError, TypeError)):
1145 with self.assertRaises((ValueError, OverflowError, TypeError)):
1149 with self.assertRaises((ValueError, OverflowError, TypeError)):
1153 def test_normalized_name(self):
1154 self.assertNotIn("blake2b512", hashlib.algorithms_available)
1155 self.assertNotIn("sha3-512", hashlib.algorithms_available)
1157 def test_file_digest(self):
1160 self.addCleanup(os.unlink, os_helper.TESTFN)
1169 self.assertEqual(d1.hexdigest(), d2.hexdigest())
1170 self.assertEqual(d1.name, d2.name)
1171 self.assertIs(type(d1), type(d2))
1173 with self.assertRaises(ValueError):
1176 with self.assertRaises(ValueError):
1180 with self.assertRaises(ValueError):