Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | # Test hashlib module |
| 2 | # |
| 3 | # $Id$ |
| 4 | # |
Georg Brandl | 8cdc9bc | 2010-01-01 13:07:05 +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 | |
| 9 | import hashlib |
Gregory P. Smith | d02eeda | 2009-05-04 00:16:49 +0000 | [diff] [blame] | 10 | import StringIO |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 11 | import sys |
Gregory P. Smith | d02eeda | 2009-05-04 00:16:49 +0000 | [diff] [blame] | 12 | try: |
| 13 | import threading |
| 14 | except ImportError: |
| 15 | threading = None |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 16 | import unittest |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 17 | import warnings |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 18 | from test import test_support |
Benjamin Peterson | 8c2b7dc | 2008-09-18 01:22:16 +0000 | [diff] [blame] | 19 | from test.test_support import _4G, precisionbigmemtest |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 20 | |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 21 | # Were we compiled --with-pydebug or with #define Py_DEBUG? |
| 22 | COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount') |
| 23 | |
| 24 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 25 | def hexstr(s): |
| 26 | import string |
| 27 | h = string.hexdigits |
| 28 | r = '' |
| 29 | for c in s: |
| 30 | i = ord(c) |
| 31 | r = r + h[(i >> 4) & 0xF] + h[i & 0xF] |
| 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 | 6dcdcde | 2010-01-03 00:19:04 +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, 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: |
| 82 | self.constructors_to_test['md5'].add(_md5.new) |
| 83 | _sha = self._conditional_import_module('_sha') |
| 84 | if _sha: |
| 85 | self.constructors_to_test['sha1'].add(_sha.new) |
| 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 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 97 | def test_unknown_hash(self): |
| 98 | try: |
| 99 | hashlib.new('spam spam spam spam spam') |
| 100 | except ValueError: |
| 101 | pass |
| 102 | else: |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 103 | self.assertTrue(0 == "hashlib didn't reject bogus hash name") |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 104 | |
| 105 | def test_hexdigest(self): |
| 106 | for name in self.supported_hash_names: |
| 107 | h = hashlib.new(name) |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 108 | self.assertTrue(hexstr(h.digest()) == h.hexdigest()) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 109 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 110 | def test_large_update(self): |
| 111 | aas = 'a' * 128 |
| 112 | bees = 'b' * 127 |
| 113 | cees = 'c' * 126 |
Gregory P. Smith | ea38826 | 2009-02-13 03:00:00 +0000 | [diff] [blame] | 114 | abcs = aas + bees + cees |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 115 | |
| 116 | for name in self.supported_hash_names: |
| 117 | m1 = hashlib.new(name) |
| 118 | m1.update(aas) |
| 119 | m1.update(bees) |
| 120 | m1.update(cees) |
| 121 | |
| 122 | m2 = hashlib.new(name) |
Gregory P. Smith | ea38826 | 2009-02-13 03:00:00 +0000 | [diff] [blame] | 123 | m2.update(abcs) |
| 124 | self.assertEqual(m1.digest(), m2.digest(), name+' update problem.') |
| 125 | |
| 126 | m3 = hashlib.new(name, abcs) |
| 127 | self.assertEqual(m1.digest(), m3.digest(), name+' new problem.') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 128 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 129 | def check(self, name, data, digest): |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 130 | constructors = self.constructors_to_test[name] |
| 131 | # 2 is for hashlib.name(...) and hashlib.new(name, ...) |
| 132 | self.assertGreaterEqual(len(constructors), 2) |
| 133 | for hash_object_constructor in constructors: |
| 134 | computed = hash_object_constructor(data).hexdigest() |
| 135 | self.assertEqual( |
| 136 | computed, digest, |
| 137 | "Hash algorithm %s constructed using %s returned hexdigest" |
| 138 | " %r for %d byte input data that should have hashed to %r." |
| 139 | % (name, hash_object_constructor, |
| 140 | computed, len(data), digest)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 141 | |
Gregory P. Smith | 443ec68 | 2010-01-02 22:28:48 +0000 | [diff] [blame] | 142 | def check_unicode(self, algorithm_name): |
Gregory P. Smith | ea38826 | 2009-02-13 03:00:00 +0000 | [diff] [blame] | 143 | # Unicode objects are not allowed as input. |
Gregory P. Smith | 443ec68 | 2010-01-02 22:28:48 +0000 | [diff] [blame] | 144 | expected = hashlib.new(algorithm_name, str(u'spam')).hexdigest() |
Gregory P. Smith | 6dcdcde | 2010-01-03 00:19:04 +0000 | [diff] [blame] | 145 | self.check(algorithm_name, u'spam', expected) |
Gregory P. Smith | ea38826 | 2009-02-13 03:00:00 +0000 | [diff] [blame] | 146 | |
Gregory P. Smith | 443ec68 | 2010-01-02 22:28:48 +0000 | [diff] [blame] | 147 | def test_unicode(self): |
| 148 | # In python 2.x unicode is auto-encoded to the system default encoding |
| 149 | # when passed to hashlib functions. |
| 150 | self.check_unicode('md5') |
| 151 | self.check_unicode('sha1') |
| 152 | self.check_unicode('sha224') |
| 153 | self.check_unicode('sha256') |
| 154 | self.check_unicode('sha384') |
| 155 | self.check_unicode('sha512') |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 156 | |
| 157 | def test_case_md5_0(self): |
| 158 | self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e') |
| 159 | |
| 160 | def test_case_md5_1(self): |
| 161 | self.check('md5', 'abc', '900150983cd24fb0d6963f7d28e17f72') |
| 162 | |
| 163 | def test_case_md5_2(self): |
| 164 | self.check('md5', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', |
| 165 | 'd174ab98d277d9f5a5611c2c9f419d9f') |
| 166 | |
Benjamin Peterson | 8c2b7dc | 2008-09-18 01:22:16 +0000 | [diff] [blame] | 167 | @precisionbigmemtest(size=_4G + 5, memuse=1) |
| 168 | def test_case_md5_huge(self, size): |
| 169 | if size == _4G + 5: |
| 170 | try: |
| 171 | self.check('md5', 'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d') |
| 172 | except OverflowError: |
| 173 | pass # 32-bit arch |
| 174 | |
| 175 | @precisionbigmemtest(size=_4G - 1, memuse=1) |
| 176 | def test_case_md5_uintmax(self, size): |
| 177 | if size == _4G - 1: |
| 178 | try: |
| 179 | self.check('md5', 'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3') |
| 180 | except OverflowError: |
| 181 | pass # 32-bit arch |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 182 | |
| 183 | # use the three examples from Federal Information Processing Standards |
| 184 | # Publication 180-1, Secure Hash Standard, 1995 April 17 |
| 185 | # http://www.itl.nist.gov/div897/pubs/fip180-1.htm |
| 186 | |
| 187 | def test_case_sha1_0(self): |
| 188 | self.check('sha1', "", |
| 189 | "da39a3ee5e6b4b0d3255bfef95601890afd80709") |
| 190 | |
| 191 | def test_case_sha1_1(self): |
| 192 | self.check('sha1', "abc", |
| 193 | "a9993e364706816aba3e25717850c26c9cd0d89d") |
| 194 | |
| 195 | def test_case_sha1_2(self): |
| 196 | self.check('sha1', "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 197 | "84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
| 198 | |
| 199 | def test_case_sha1_3(self): |
| 200 | self.check('sha1', "a" * 1000000, |
| 201 | "34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
| 202 | |
| 203 | |
| 204 | # use the examples from Federal Information Processing Standards |
| 205 | # Publication 180-2, Secure Hash Standard, 2002 August 1 |
| 206 | # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf |
| 207 | |
| 208 | def test_case_sha224_0(self): |
| 209 | self.check('sha224', "", |
| 210 | "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f") |
| 211 | |
| 212 | def test_case_sha224_1(self): |
| 213 | self.check('sha224', "abc", |
| 214 | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7") |
| 215 | |
| 216 | def test_case_sha224_2(self): |
| 217 | self.check('sha224', |
| 218 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 219 | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525") |
| 220 | |
| 221 | def test_case_sha224_3(self): |
| 222 | self.check('sha224', "a" * 1000000, |
| 223 | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67") |
| 224 | |
| 225 | |
| 226 | def test_case_sha256_0(self): |
| 227 | self.check('sha256', "", |
| 228 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") |
| 229 | |
| 230 | def test_case_sha256_1(self): |
| 231 | self.check('sha256', "abc", |
| 232 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") |
| 233 | |
| 234 | def test_case_sha256_2(self): |
| 235 | self.check('sha256', |
| 236 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 237 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1") |
| 238 | |
| 239 | def test_case_sha256_3(self): |
| 240 | self.check('sha256', "a" * 1000000, |
| 241 | "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0") |
| 242 | |
| 243 | |
| 244 | def test_case_sha384_0(self): |
| 245 | self.check('sha384', "", |
| 246 | "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+ |
| 247 | "274edebfe76f65fbd51ad2f14898b95b") |
| 248 | |
| 249 | def test_case_sha384_1(self): |
| 250 | self.check('sha384', "abc", |
| 251 | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+ |
| 252 | "8086072ba1e7cc2358baeca134c825a7") |
| 253 | |
| 254 | def test_case_sha384_2(self): |
| 255 | self.check('sha384', |
| 256 | "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 257 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
| 258 | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+ |
| 259 | "fcc7c71a557e2db966c3e9fa91746039") |
| 260 | |
| 261 | def test_case_sha384_3(self): |
| 262 | self.check('sha384', "a" * 1000000, |
| 263 | "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+ |
| 264 | "07b8b3dc38ecc4ebae97ddd87f3d8985") |
| 265 | |
| 266 | |
| 267 | def test_case_sha512_0(self): |
| 268 | self.check('sha512', "", |
| 269 | "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+ |
| 270 | "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") |
| 271 | |
| 272 | def test_case_sha512_1(self): |
| 273 | self.check('sha512', "abc", |
| 274 | "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+ |
| 275 | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f") |
| 276 | |
| 277 | def test_case_sha512_2(self): |
| 278 | self.check('sha512', |
| 279 | "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ |
| 280 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", |
| 281 | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ |
| 282 | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") |
| 283 | |
| 284 | def test_case_sha512_3(self): |
| 285 | self.check('sha512', "a" * 1000000, |
| 286 | "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ |
| 287 | "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") |
| 288 | |
Gregory P. Smith | d02eeda | 2009-05-04 00:16:49 +0000 | [diff] [blame] | 289 | def test_threaded_hashing(self): |
| 290 | if not threading: |
| 291 | raise unittest.SkipTest('No threading module.') |
| 292 | |
| 293 | # Updating the same hash object from several threads at once |
| 294 | # using data chunk sizes containing the same byte sequences. |
| 295 | # |
| 296 | # If the internal locks are working to prevent multiple |
| 297 | # updates on the same object from running at once, the resulting |
| 298 | # hash will be the same as doing it single threaded upfront. |
| 299 | hasher = hashlib.sha1() |
| 300 | num_threads = 5 |
| 301 | smallest_data = 'swineflu' |
| 302 | data = smallest_data*200000 |
| 303 | expected_hash = hashlib.sha1(data*num_threads).hexdigest() |
| 304 | |
| 305 | def hash_in_chunks(chunk_size, event): |
| 306 | index = 0 |
| 307 | while index < len(data): |
| 308 | hasher.update(data[index:index+chunk_size]) |
| 309 | index += chunk_size |
| 310 | event.set() |
| 311 | |
| 312 | events = [] |
| 313 | for threadnum in xrange(num_threads): |
| 314 | chunk_size = len(data) // (10**threadnum) |
| 315 | assert chunk_size > 0 |
| 316 | assert chunk_size % len(smallest_data) == 0 |
| 317 | event = threading.Event() |
| 318 | events.append(event) |
| 319 | threading.Thread(target=hash_in_chunks, |
| 320 | args=(chunk_size, event)).start() |
| 321 | |
| 322 | for event in events: |
| 323 | event.wait() |
| 324 | |
| 325 | self.assertEqual(expected_hash, hasher.hexdigest()) |
| 326 | |
Benjamin Peterson | 0db3cd6 | 2009-05-14 22:40:34 +0000 | [diff] [blame] | 327 | @test_support.reap_threads |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 328 | def test_main(): |
Benjamin Peterson | 0db3cd6 | 2009-05-14 22:40:34 +0000 | [diff] [blame] | 329 | test_support.run_unittest(HashLibTestCase) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 330 | |
| 331 | if __name__ == "__main__": |
| 332 | test_main() |