Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | # Test hashlib module |
| 2 | # |
| 3 | # $Id$ |
| 4 | # |
Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 5 | # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 6 | # Licensed to PSF under a Contributor Agreement. |
| 7 | # |
| 8 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 9 | import array |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 10 | import hashlib |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 11 | import itertools |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 12 | import sys |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 13 | try: |
| 14 | import threading |
| 15 | except ImportError: |
| 16 | threading = None |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 17 | import unittest |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 18 | import warnings |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 19 | from test import support |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 20 | from test.support import _4G, precisionbigmemtest |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 21 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 22 | # Were we compiled --with-pydebug or with #define Py_DEBUG? |
| 23 | COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') |
| 24 | |
| 25 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 26 | def hexstr(s): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 27 | assert isinstance(s, bytes), repr(s) |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 28 | h = "0123456789abcdef" |
| 29 | r = '' |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 30 | for i in s: |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 31 | r += h[(i >> 4) & 0xF] + h[i & 0xF] |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 32 | return r |
| 33 | |
| 34 | |
| 35 | class HashLibTestCase(unittest.TestCase): |
| 36 | supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', |
| 37 | 'sha224', 'SHA224', 'sha256', 'SHA256', |
| 38 | 'sha384', 'SHA384', 'sha512', 'SHA512' ) |
| 39 | |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 40 | _warn_on_extension_import = COMPILED_WITH_PYDEBUG |
| 41 | |
| 42 | def _conditional_import_module(self, module_name): |
| 43 | """Import a module and return a reference to it or None on failure.""" |
| 44 | try: |
| 45 | exec('import '+module_name) |
| 46 | except ImportError as error: |
| 47 | if self._warn_on_extension_import: |
| 48 | warnings.warn('Did a C extension fail to compile? %s' % error) |
| 49 | return locals().get(module_name) |
| 50 | |
| 51 | def __init__(self, *args, **kwargs): |
| 52 | algorithms = set() |
| 53 | for algorithm in self.supported_hash_names: |
| 54 | algorithms.add(algorithm.lower()) |
| 55 | self.constructors_to_test = {} |
| 56 | for algorithm in algorithms: |
| 57 | self.constructors_to_test[algorithm] = set() |
| 58 | |
| 59 | # For each algorithm, test the direct constructor and the use |
| 60 | # of hashlib.new given the algorithm name. |
| 61 | for algorithm, constructors in self.constructors_to_test.items(): |
| 62 | constructors.add(getattr(hashlib, algorithm)) |
| 63 | def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm): |
| 64 | if data is None: |
| 65 | return hashlib.new(_alg) |
| 66 | return hashlib.new(_alg, data) |
| 67 | constructors.add(_test_algorithm_via_hashlib_new) |
| 68 | |
| 69 | _hashlib = self._conditional_import_module('_hashlib') |
| 70 | if _hashlib: |
| 71 | # These two algorithms should always be present when this module |
| 72 | # is compiled. If not, something was compiled wrong. |
| 73 | assert hasattr(_hashlib, 'openssl_md5') |
| 74 | assert hasattr(_hashlib, 'openssl_sha1') |
| 75 | for algorithm, constructors in self.constructors_to_test.items(): |
| 76 | constructor = getattr(_hashlib, 'openssl_'+algorithm, None) |
| 77 | if constructor: |
| 78 | constructors.add(constructor) |
| 79 | |
| 80 | _md5 = self._conditional_import_module('_md5') |
| 81 | if _md5: |
Gregory P. Smith | b04ded4 | 2010-01-03 00:38:10 +0000 | [diff] [blame] | 82 | self.constructors_to_test['md5'].add(_md5.md5) |
| 83 | _sha1 = self._conditional_import_module('_sha1') |
| 84 | if _sha1: |
| 85 | self.constructors_to_test['sha1'].add(_sha1.sha1) |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 86 | _sha256 = self._conditional_import_module('_sha256') |
| 87 | if _sha256: |
| 88 | self.constructors_to_test['sha224'].add(_sha256.sha224) |
| 89 | self.constructors_to_test['sha256'].add(_sha256.sha256) |
| 90 | _sha512 = self._conditional_import_module('_sha512') |
| 91 | if _sha512: |
| 92 | self.constructors_to_test['sha384'].add(_sha512.sha384) |
| 93 | self.constructors_to_test['sha512'].add(_sha512.sha512) |
| 94 | |
| 95 | super(HashLibTestCase, self).__init__(*args, **kwargs) |
| 96 | |
Benjamin Peterson | a28e702 | 2010-01-09 18:53:06 +0000 | [diff] [blame] | 97 | def test_hash_array(self): |
| 98 | a = array.array("b", range(10)) |
| 99 | constructors = self.constructors_to_test.values() |
| 100 | for cons in itertools.chain.from_iterable(constructors): |
| 101 | c = cons(a) |
| 102 | c.hexdigest() |
| 103 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 104 | def test_algorithms_guaranteed(self): |
| 105 | self.assertEqual(hashlib.algorithms_guaranteed, |
Gregory P. Smith | 86508cc | 2010-03-01 02:05:26 +0000 | [diff] [blame] | 106 | tuple(_algo for _algo in self.supported_hash_names |
| 107 | if _algo.islower())) |
| 108 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 109 | def test_algorithms_available(self): |
| 110 | self.assertTrue(set(hashlib.algorithms_guaranteed). |
| 111 | issubset(hashlib.algorithms_available)) |
| 112 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 113 | def test_unknown_hash(self): |
| 114 | try: |
| 115 | hashlib.new('spam spam spam spam spam') |
| 116 | except ValueError: |
| 117 | pass |
| 118 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 119 | self.assertTrue(0 == "hashlib didn't reject bogus hash name") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 120 | |
| 121 | def test_hexdigest(self): |
| 122 | for name in self.supported_hash_names: |
| 123 | h = hashlib.new(name) |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 124 | assert isinstance(h.digest(), bytes), name |
| 125 | self.assertEqual(hexstr(h.digest()), h.hexdigest()) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 126 | |
| 127 | |
| 128 | def test_large_update(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 129 | aas = b'a' * 128 |
| 130 | bees = b'b' * 127 |
| 131 | cees = b'c' * 126 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 132 | |
| 133 | for name in self.supported_hash_names: |
| 134 | m1 = hashlib.new(name) |
| 135 | m1.update(aas) |
| 136 | m1.update(bees) |
| 137 | m1.update(cees) |
| 138 | |
| 139 | m2 = hashlib.new(name) |
| 140 | m2.update(aas + bees + cees) |
| 141 | self.assertEqual(m1.digest(), m2.digest()) |
| 142 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 143 | def check(self, name, data, digest): |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 144 | constructors = self.constructors_to_test[name] |
| 145 | # 2 is for hashlib.name(...) and hashlib.new(name, ...) |
| 146 | self.assertGreaterEqual(len(constructors), 2) |
| 147 | for hash_object_constructor in constructors: |
| 148 | computed = hash_object_constructor(data).hexdigest() |
| 149 | self.assertEqual( |
| 150 | computed, digest, |
| 151 | "Hash algorithm %s constructed using %s returned hexdigest" |
| 152 | " %r for %d byte input data that should have hashed to %r." |
| 153 | % (name, hash_object_constructor, |
| 154 | computed, len(data), digest)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 155 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 156 | def check_no_unicode(self, algorithm_name): |
| 157 | # Unicode objects are not allowed as input. |
Gregory P. Smith | cd54e54 | 2010-01-03 00:29:15 +0000 | [diff] [blame] | 158 | constructors = self.constructors_to_test[algorithm_name] |
| 159 | for hash_object_constructor in constructors: |
| 160 | self.assertRaises(TypeError, hash_object_constructor, 'spam') |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 161 | |
| 162 | def test_no_unicode(self): |
| 163 | self.check_no_unicode('md5') |
| 164 | self.check_no_unicode('sha1') |
| 165 | self.check_no_unicode('sha224') |
| 166 | self.check_no_unicode('sha256') |
| 167 | self.check_no_unicode('sha384') |
| 168 | self.check_no_unicode('sha512') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 169 | |
| 170 | def test_case_md5_0(self): |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 171 | self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 172 | |
| 173 | def test_case_md5_1(self): |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 174 | self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 175 | |
| 176 | def test_case_md5_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 177 | self.check('md5', |
| 178 | b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 179 | 'd174ab98d277d9f5a5611c2c9f419d9f') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 180 | |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 181 | @precisionbigmemtest(size=_4G + 5, memuse=1) |
| 182 | def test_case_md5_huge(self, size): |
| 183 | if size == _4G + 5: |
| 184 | try: |
Antoine Pitrou | d7ae299 | 2010-11-08 20:36:57 +0000 | [diff] [blame] | 185 | self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 186 | except OverflowError: |
| 187 | pass # 32-bit arch |
| 188 | |
| 189 | @precisionbigmemtest(size=_4G - 1, memuse=1) |
| 190 | def test_case_md5_uintmax(self, size): |
| 191 | if size == _4G - 1: |
| 192 | try: |
Antoine Pitrou | d7ae299 | 2010-11-08 20:36:57 +0000 | [diff] [blame] | 193 | self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3') |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 194 | except OverflowError: |
| 195 | pass # 32-bit arch |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 196 | |
| 197 | # use the three examples from Federal Information Processing Standards |
| 198 | # Publication 180-1, Secure Hash Standard, 1995 April 17 |
| 199 | # http://www.itl.nist.gov/div897/pubs/fip180-1.htm |
| 200 | |
| 201 | def test_case_sha1_0(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 202 | self.check('sha1', b"", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 203 | "da39a3ee5e6b4b0d3255bfef95601890afd80709") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 204 | |
| 205 | def test_case_sha1_1(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 206 | self.check('sha1', b"abc", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 207 | "a9993e364706816aba3e25717850c26c9cd0d89d") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 208 | |
| 209 | def test_case_sha1_2(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 210 | self.check('sha1', |
| 211 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 212 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 213 | |
| 214 | def test_case_sha1_3(self): |
Guido van Rossum | 5ed033b | 2007-07-09 14:29:40 +0000 | [diff] [blame] | 215 | self.check('sha1', b"a" * 1000000, |
Guido van Rossum | 558ca84 | 2007-07-10 20:31:05 +0000 | [diff] [blame] | 216 | "34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | # use the examples from Federal Information Processing Standards |
| 220 | # Publication 180-2, Secure Hash Standard, 2002 August 1 |
| 221 | # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 222 | |
| 223 | def test_case_sha224_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 224 | self.check('sha224', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 225 | "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f") |
| 226 | |
| 227 | def test_case_sha224_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 228 | self.check('sha224', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 229 | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7") |
| 230 | |
| 231 | def test_case_sha224_2(self): |
| 232 | self.check('sha224', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 233 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 234 | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525") |
| 235 | |
| 236 | def test_case_sha224_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 237 | self.check('sha224', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 238 | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67") |
| 239 | |
| 240 | |
| 241 | def test_case_sha256_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 242 | self.check('sha256', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 243 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") |
| 244 | |
| 245 | def test_case_sha256_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 246 | self.check('sha256', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 247 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") |
| 248 | |
| 249 | def test_case_sha256_2(self): |
| 250 | self.check('sha256', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 251 | b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 252 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1") |
| 253 | |
| 254 | def test_case_sha256_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 255 | self.check('sha256', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 256 | "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0") |
| 257 | |
| 258 | |
| 259 | def test_case_sha384_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 260 | self.check('sha384', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 261 | "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+ |
| 262 | "274edebfe76f65fbd51ad2f14898b95b") |
| 263 | |
| 264 | def test_case_sha384_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 265 | self.check('sha384', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 266 | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+ |
| 267 | "8086072ba1e7cc2358baeca134c825a7") |
| 268 | |
| 269 | def test_case_sha384_2(self): |
| 270 | self.check('sha384', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 271 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 272 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 273 | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+ |
| 274 | "fcc7c71a557e2db966c3e9fa91746039") |
| 275 | |
| 276 | def test_case_sha384_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 277 | self.check('sha384', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 278 | "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+ |
| 279 | "07b8b3dc38ecc4ebae97ddd87f3d8985") |
| 280 | |
| 281 | |
| 282 | def test_case_sha512_0(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 283 | self.check('sha512', b"", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 284 | "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+ |
| 285 | "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") |
| 286 | |
| 287 | def test_case_sha512_1(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 288 | self.check('sha512', b"abc", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 289 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+ |
| 290 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f") |
| 291 | |
| 292 | def test_case_sha512_2(self): |
| 293 | self.check('sha512', |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 294 | b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 295 | b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 296 | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ |
| 297 | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") |
| 298 | |
| 299 | def test_case_sha512_3(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 300 | self.check('sha512', b"a" * 1000000, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 301 | "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ |
| 302 | "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") |
| 303 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 304 | def test_gil(self): |
| 305 | # Check things work fine with an input larger than the size required |
| 306 | # for multithreaded operation (which is hardwired to 2048). |
| 307 | gil_minsize = 2048 |
| 308 | |
| 309 | m = hashlib.md5() |
| 310 | m.update(b'1') |
| 311 | m.update(b'#' * gil_minsize) |
| 312 | m.update(b'1') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 313 | self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21') |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 314 | |
| 315 | m = hashlib.md5(b'x' * gil_minsize) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 316 | self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 317 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 318 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 319 | @support.reap_threads |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 320 | def test_threaded_hashing(self): |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 321 | # Updating the same hash object from several threads at once |
| 322 | # using data chunk sizes containing the same byte sequences. |
| 323 | # |
| 324 | # If the internal locks are working to prevent multiple |
| 325 | # updates on the same object from running at once, the resulting |
| 326 | # hash will be the same as doing it single threaded upfront. |
| 327 | hasher = hashlib.sha1() |
| 328 | num_threads = 5 |
| 329 | smallest_data = b'swineflu' |
| 330 | data = smallest_data*200000 |
| 331 | expected_hash = hashlib.sha1(data*num_threads).hexdigest() |
| 332 | |
| 333 | def hash_in_chunks(chunk_size, event): |
| 334 | index = 0 |
| 335 | while index < len(data): |
| 336 | hasher.update(data[index:index+chunk_size]) |
| 337 | index += chunk_size |
| 338 | event.set() |
| 339 | |
| 340 | events = [] |
| 341 | for threadnum in range(num_threads): |
| 342 | chunk_size = len(data) // (10**threadnum) |
| 343 | assert chunk_size > 0 |
| 344 | assert chunk_size % len(smallest_data) == 0 |
| 345 | event = threading.Event() |
| 346 | events.append(event) |
| 347 | threading.Thread(target=hash_in_chunks, |
| 348 | args=(chunk_size, event)).start() |
| 349 | |
| 350 | for event in events: |
| 351 | event.wait() |
| 352 | |
| 353 | self.assertEqual(expected_hash, hasher.hexdigest()) |
| 354 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 355 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 356 | support.run_unittest(HashLibTestCase) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 357 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 358 | if __name__ == "__main__": |
| 359 | test_main() |