annotate Lib/test/test_hashlib.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents bf39434dd506
children c97cfe04880f 0a1d7be10946
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1 # Test hashlib module
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
2 #
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
3 # $Id$
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
4 #
58219
aadfc31b3f14 Merged revisions 77389 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 55593
diff changeset
5 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6 # Licensed to PSF under a Contributor Agreement.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
7 #
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
8
58222
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
9 import array
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
10 import hashlib
58222
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
11 import itertools
58107
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
12 import sys
53727
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
13 try:
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
14 import threading
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
15 except ImportError:
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
16 threading = None
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
17 import unittest
58107
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
18 import warnings
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 43297
diff changeset
19 from test import support
72642
bf39434dd506 Start fixing test_bigmem:
Antoine Pitrou <solipsis@pitrou.net>
parents: 70122
diff changeset
20 from test.support import _4G, bigmemtest
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
21
58107
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
22 # Were we compiled --with-pydebug or with #define Py_DEBUG?
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
23 COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
24
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
25
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
26 def hexstr(s):
42166
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
27 assert isinstance(s, bytes), repr(s)
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
28 h = "0123456789abcdef"
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
29 r = ''
42166
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
30 for i in s:
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
31 r += h[(i >> 4) & 0xF] + h[i & 0xF]
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
32 return r
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
33
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
34
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
35 class HashLibTestCase(unittest.TestCase):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
36 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
37 'sha224', 'SHA224', 'sha256', 'SHA256',
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
38 'sha384', 'SHA384', 'sha512', 'SHA512' )
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
39
58107
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
40 _warn_on_extension_import = COMPILED_WITH_PYDEBUG
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
41
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
42 def _conditional_import_module(self, module_name):
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
43 """Import a module and return a reference to it or None on failure."""
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
44 try:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
45 exec('import '+module_name)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
46 except ImportError as error:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
47 if self._warn_on_extension_import:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
48 warnings.warn('Did a C extension fail to compile? %s' % error)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
49 return locals().get(module_name)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
50
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
51 def __init__(self, *args, **kwargs):
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
52 algorithms = set()
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
53 for algorithm in self.supported_hash_names:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
54 algorithms.add(algorithm.lower())
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
55 self.constructors_to_test = {}
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
56 for algorithm in algorithms:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
57 self.constructors_to_test[algorithm] = set()
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
58
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
59 # For each algorithm, test the direct constructor and the use
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
60 # of hashlib.new given the algorithm name.
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
61 for algorithm, constructors in self.constructors_to_test.items():
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
62 constructors.add(getattr(hashlib, algorithm))
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
63 def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm):
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
64 if data is None:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
65 return hashlib.new(_alg)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
66 return hashlib.new(_alg, data)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
67 constructors.add(_test_algorithm_via_hashlib_new)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
68
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
69 _hashlib = self._conditional_import_module('_hashlib')
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
70 if _hashlib:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
71 # These two algorithms should always be present when this module
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
72 # is compiled. If not, something was compiled wrong.
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
73 assert hasattr(_hashlib, 'openssl_md5')
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
74 assert hasattr(_hashlib, 'openssl_sha1')
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
75 for algorithm, constructors in self.constructors_to_test.items():
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
76 constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
77 if constructor:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
78 constructors.add(constructor)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
79
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
80 _md5 = self._conditional_import_module('_md5')
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
81 if _md5:
58108
3856c764e1f5 Also fixes test_hashlib for the different extension module names in py3k.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58107
diff changeset
82 self.constructors_to_test['md5'].add(_md5.md5)
3856c764e1f5 Also fixes test_hashlib for the different extension module names in py3k.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58107
diff changeset
83 _sha1 = self._conditional_import_module('_sha1')
3856c764e1f5 Also fixes test_hashlib for the different extension module names in py3k.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58107
diff changeset
84 if _sha1:
3856c764e1f5 Also fixes test_hashlib for the different extension module names in py3k.
Gregory P. Smith <greg@mad-scientist.com>
parents: 58107
diff changeset
85 self.constructors_to_test['sha1'].add(_sha1.sha1)
58107
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
86 _sha256 = self._conditional_import_module('_sha256')
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
87 if _sha256:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
88 self.constructors_to_test['sha224'].add(_sha256.sha224)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
89 self.constructors_to_test['sha256'].add(_sha256.sha256)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
90 _sha512 = self._conditional_import_module('_sha512')
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
91 if _sha512:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
92 self.constructors_to_test['sha384'].add(_sha512.sha384)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
93 self.constructors_to_test['sha512'].add(_sha512.sha512)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
94
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
95 super(HashLibTestCase, self).__init__(*args, **kwargs)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
96
58222
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
97 def test_hash_array(self):
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
98 a = array.array("b", range(10))
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
99 constructors = self.constructors_to_test.values()
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
100 for cons in itertools.chain.from_iterable(constructors):
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
101 c = cons(a)
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
102 c.hexdigest()
f2d54096dc8c Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58218
diff changeset
103
64488
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 61073
diff changeset
104 def test_algorithms_guaranteed(self):
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 61073
diff changeset
105 self.assertEqual(hashlib.algorithms_guaranteed,
67733
6f40b4709e9a Make the type consistent for hashlib algorithm constants. (Reviewed by Benjamin).
Raymond Hettinger <python@rcn.com>
parents: 66310
diff changeset
106 set(_algo for _algo in self.supported_hash_names
59212
9b4e0cd7817a Merged revisions 78528 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58222
diff changeset
107 if _algo.islower()))
9b4e0cd7817a Merged revisions 78528 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58222
diff changeset
108
64488
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 61073
diff changeset
109 def test_algorithms_available(self):
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 61073
diff changeset
110 self.assertTrue(set(hashlib.algorithms_guaranteed).
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 61073
diff changeset
111 issubset(hashlib.algorithms_available))
49441e88ccc1 hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents: 61073
diff changeset
112
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
113 def test_unknown_hash(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
114 try:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
115 hashlib.new('spam spam spam spam spam')
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
116 except ValueError:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
117 pass
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
118 else:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54501
diff changeset
119 self.assertTrue(0 == "hashlib didn't reject bogus hash name")
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
120
70122
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
121 def test_get_builtin_constructor(self):
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
122 get_builtin_constructor = hashlib.__dict__[
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
123 '__get_builtin_constructor']
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
124 self.assertRaises(ValueError, get_builtin_constructor, 'test')
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
125 try:
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
126 import _md5
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
127 except ImportError:
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
128 pass
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
129 # This forces an ImportError for "import _md5" statements
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
130 sys.modules['_md5'] = None
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
131 try:
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
132 self.assertRaises(ValueError, get_builtin_constructor, 'md5')
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
133 finally:
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
134 if '_md5' in locals():
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
135 sys.modules['_md5'] = _md5
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
136 else:
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
137 del sys.modules['_md5']
ad9249f35914 Fixes Issue #12059: Properly handle missing hash functions even when
Gregory P. Smith <greg@krypto.org>
parents: 68257
diff changeset
138
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
139 def test_hexdigest(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
140 for name in self.supported_hash_names:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
141 h = hashlib.new(name)
42166
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
142 assert isinstance(h.digest(), bytes), name
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
143 self.assertEqual(hexstr(h.digest()), h.hexdigest())
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
144
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
145
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
146 def test_large_update(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
147 aas = b'a' * 128
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
148 bees = b'b' * 127
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
149 cees = b'c' * 126
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
150
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
151 for name in self.supported_hash_names:
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
152 m1 = hashlib.new(name)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
153 m1.update(aas)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
154 m1.update(bees)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
155 m1.update(cees)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
156
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
157 m2 = hashlib.new(name)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
158 m2.update(aas + bees + cees)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
159 self.assertEqual(m1.digest(), m2.digest())
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
160
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
161 def check(self, name, data, digest):
58107
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
162 constructors = self.constructors_to_test[name]
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
163 # 2 is for hashlib.name(...) and hashlib.new(name, ...)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
164 self.assertGreaterEqual(len(constructors), 2)
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
165 for hash_object_constructor in constructors:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
166 computed = hash_object_constructor(data).hexdigest()
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
167 self.assertEqual(
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
168 computed, digest,
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
169 "Hash algorithm %s constructed using %s returned hexdigest"
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
170 " %r for %d byte input data that should have hashed to %r."
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
171 % (name, hash_object_constructor,
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
172 computed, len(data), digest))
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
173
51543
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
174 def check_no_unicode(self, algorithm_name):
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
175 # Unicode objects are not allowed as input.
58107
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
176 constructors = self.constructors_to_test[algorithm_name]
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
177 for hash_object_constructor in constructors:
2a2399534591 Merged revisions 77257 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 55593
diff changeset
178 self.assertRaises(TypeError, hash_object_constructor, 'spam')
51543
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
179
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
180 def test_no_unicode(self):
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
181 self.check_no_unicode('md5')
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
182 self.check_no_unicode('sha1')
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
183 self.check_no_unicode('sha224')
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
184 self.check_no_unicode('sha256')
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
185 self.check_no_unicode('sha384')
2ac02daef2ee Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents: 50636
diff changeset
186 self.check_no_unicode('sha512')
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
187
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
188 def test_case_md5_0(self):
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
189 self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e')
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
190
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
191 def test_case_md5_1(self):
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
192 self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72')
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
193
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
194 def test_case_md5_2(self):
42166
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
195 self.check('md5',
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
196 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
197 'd174ab98d277d9f5a5611c2c9f419d9f')
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
198
72642
bf39434dd506 Start fixing test_bigmem:
Antoine Pitrou <solipsis@pitrou.net>
parents: 70122
diff changeset
199 @bigmemtest(size=_4G + 5, memuse=1)
49292
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
200 def test_case_md5_huge(self, size):
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
201 if size == _4G + 5:
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
202 try:
66118
65364c44e295 Merged revisions 86338 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58219
diff changeset
203 self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
49292
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
204 except OverflowError:
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
205 pass # 32-bit arch
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
206
72642
bf39434dd506 Start fixing test_bigmem:
Antoine Pitrou <solipsis@pitrou.net>
parents: 70122
diff changeset
207 @bigmemtest(size=_4G - 1, memuse=1)
49292
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
208 def test_case_md5_uintmax(self, size):
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
209 if size == _4G - 1:
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
210 try:
66118
65364c44e295 Merged revisions 86338 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 58219
diff changeset
211 self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
49292
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
212 except OverflowError:
a00626f2d705 Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47201
diff changeset
213 pass # 32-bit arch
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
214
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
215 # use the three examples from Federal Information Processing Standards
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
216 # Publication 180-1, Secure Hash Standard, 1995 April 17
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
217 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
218
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
219 def test_case_sha1_0(self):
42166
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
220 self.check('sha1', b"",
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
221 "da39a3ee5e6b4b0d3255bfef95601890afd80709")
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
222
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
223 def test_case_sha1_1(self):
42166
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
224 self.check('sha1', b"abc",
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
225 "a9993e364706816aba3e25717850c26c9cd0d89d")
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
226
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
227 def test_case_sha1_2(self):
42166
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
228 self.check('sha1',
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
229 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
230 "84983e441c3bd26ebaae4aa1f95129e5e54670f1")
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
231
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
232 def test_case_sha1_3(self):
42166
78203298b42b Change hashlib to return bytes from digest() instead of str8.
Guido van Rossum <guido@python.org>
parents: 35375
diff changeset
233 self.check('sha1', b"a" * 1000000,
42181
3641a20630f3 Fix test_hashlib regression caused by my fixing hexdigest() to return
Guido van Rossum <guido@python.org>
parents: 42166
diff changeset
234 "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
235
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
236
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
237 # use the examples from Federal Information Processing Standards
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
238 # Publication 180-2, Secure Hash Standard, 2002 August 1
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
239 # http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
240
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
241 def test_case_sha224_0(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
242 self.check('sha224', b"",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
243 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
244
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
245 def test_case_sha224_1(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
246 self.check('sha224', b"abc",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
247 "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
248
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
249 def test_case_sha224_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
250 self.check('sha224',
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
251 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
252 "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
253
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
254 def test_case_sha224_3(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
255 self.check('sha224', b"a" * 1000000,
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
256 "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
257
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
258
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
259 def test_case_sha256_0(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
260 self.check('sha256', b"",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
261 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
262
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
263 def test_case_sha256_1(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
264 self.check('sha256', b"abc",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
265 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
266
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
267 def test_case_sha256_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
268 self.check('sha256',
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
269 b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
270 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
271
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
272 def test_case_sha256_3(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
273 self.check('sha256', b"a" * 1000000,
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
274 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
275
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
276
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
277 def test_case_sha384_0(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
278 self.check('sha384', b"",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
279 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
280 "274edebfe76f65fbd51ad2f14898b95b")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
281
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
282 def test_case_sha384_1(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
283 self.check('sha384', b"abc",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
284 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
285 "8086072ba1e7cc2358baeca134c825a7")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
286
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
287 def test_case_sha384_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
288 self.check('sha384',
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
289 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
290 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
291 "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
292 "fcc7c71a557e2db966c3e9fa91746039")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
293
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
294 def test_case_sha384_3(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
295 self.check('sha384', b"a" * 1000000,
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
296 "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
297 "07b8b3dc38ecc4ebae97ddd87f3d8985")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
298
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
299
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
300 def test_case_sha512_0(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
301 self.check('sha512', b"",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
302 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
303 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
304
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
305 def test_case_sha512_1(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
306 self.check('sha512', b"abc",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
307 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
308 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
309
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
310 def test_case_sha512_2(self):
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
311 self.check('sha512',
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
312 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
313 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
314 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
315 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
316
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
317 def test_case_sha512_3(self):
42918
9220931e4741 More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents: 42181
diff changeset
318 self.check('sha512', b"a" * 1000000,
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
319 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
320 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
321
50636
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
322 def test_gil(self):
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
323 # Check things work fine with an input larger than the size required
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
324 # for multithreaded operation (which is hardwired to 2048).
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
325 gil_minsize = 2048
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
326
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
327 m = hashlib.md5()
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
328 m.update(b'1')
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
329 m.update(b'#' * gil_minsize)
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
330 m.update(b'1')
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66118
diff changeset
331 self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21')
50636
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
332
f9f5d9047a05 Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 49292
diff changeset
333 m = hashlib.md5(b'x' * gil_minsize)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66118
diff changeset
334 self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958')
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
335
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59615
diff changeset
336 @unittest.skipUnless(threading, 'Threading required for this test.')
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59615
diff changeset
337 @support.reap_threads
53727
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
338 def test_threaded_hashing(self):
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
339 # Updating the same hash object from several threads at once
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
340 # using data chunk sizes containing the same byte sequences.
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
341 #
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
342 # If the internal locks are working to prevent multiple
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
343 # updates on the same object from running at once, the resulting
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
344 # hash will be the same as doing it single threaded upfront.
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
345 hasher = hashlib.sha1()
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
346 num_threads = 5
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
347 smallest_data = b'swineflu'
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
348 data = smallest_data*200000
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
349 expected_hash = hashlib.sha1(data*num_threads).hexdigest()
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
350
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
351 def hash_in_chunks(chunk_size, event):
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
352 index = 0
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
353 while index < len(data):
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
354 hasher.update(data[index:index+chunk_size])
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
355 index += chunk_size
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
356 event.set()
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
357
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
358 events = []
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
359 for threadnum in range(num_threads):
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
360 chunk_size = len(data) // (10**threadnum)
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
361 assert chunk_size > 0
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
362 assert chunk_size % len(smallest_data) == 0
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
363 event = threading.Event()
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
364 events.append(event)
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
365 threading.Thread(target=hash_in_chunks,
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
366 args=(chunk_size, event)).start()
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
367
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
368 for event in events:
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
369 event.wait()
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
370
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
371 self.assertEqual(expected_hash, hasher.hexdigest())
4cd437dface8 Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents: 51543
diff changeset
372
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
373 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 43297
diff changeset
374 support.run_unittest(HashLibTestCase)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
376 if __name__ == "__main__":
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
377 test_main()