2005-08-21 15:45:59 -03:00
|
|
|
# Test hashlib module
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
#
|
2010-01-09 14:45:30 -04:00
|
|
|
# Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
|
2005-08-21 15:45:59 -03:00
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
|
|
|
#
|
|
|
|
|
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77185 | andrew.kuchling | 2009-12-31 10:17:05 -0600 (Thu, 31 Dec 2009) | 1 line
Add some items
........
r77186 | benjamin.peterson | 2009-12-31 10:28:24 -0600 (Thu, 31 Dec 2009) | 1 line
update expat comment
........
r77187 | andrew.kuchling | 2009-12-31 10:38:53 -0600 (Thu, 31 Dec 2009) | 1 line
Add various items
........
r77188 | benjamin.peterson | 2009-12-31 10:49:37 -0600 (Thu, 31 Dec 2009) | 1 line
add another advancement
........
r77262 | andrew.kuchling | 2010-01-02 19:15:21 -0600 (Sat, 02 Jan 2010) | 1 line
Add a few items
........
r77313 | benjamin.peterson | 2010-01-04 18:04:19 -0600 (Mon, 04 Jan 2010) | 1 line
add a test about hashing array.array
........
r77317 | georg.brandl | 2010-01-05 12:14:52 -0600 (Tue, 05 Jan 2010) | 1 line
Add Stefan.
........
r77331 | georg.brandl | 2010-01-06 11:43:06 -0600 (Wed, 06 Jan 2010) | 1 line
Small fixes to test_cmd: fix signature of do_shell, remove duplicate import, add option to run the custom Cmd class.
........
r77332 | georg.brandl | 2010-01-06 12:02:16 -0600 (Wed, 06 Jan 2010) | 7 lines
#5991: let completion for the "help" command include help topics.
This also simplifies the Cmd.get_names() method implementation; it was written
at a time where dir() didn't consider base class attributes.
........
r77333 | georg.brandl | 2010-01-06 12:26:08 -0600 (Wed, 06 Jan 2010) | 1 line
#5950: document that zip files with comments are unsupported in zipimport.
........
r77337 | r.david.murray | 2010-01-06 21:09:08 -0600 (Wed, 06 Jan 2010) | 3 lines
Add -W to the 'basics', 'opt', and 'all' test runs so that we get verbose
information if a failure happens.
........
r77338 | r.david.murray | 2010-01-06 22:04:28 -0600 (Wed, 06 Jan 2010) | 2 lines
Fix inadvertent checkin of debug line.
........
2010-01-09 14:53:06 -04:00
|
|
|
import array
|
2005-08-21 15:45:59 -03:00
|
|
|
import hashlib
|
2009-05-03 21:45:33 -03:00
|
|
|
from io import StringIO
|
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77185 | andrew.kuchling | 2009-12-31 10:17:05 -0600 (Thu, 31 Dec 2009) | 1 line
Add some items
........
r77186 | benjamin.peterson | 2009-12-31 10:28:24 -0600 (Thu, 31 Dec 2009) | 1 line
update expat comment
........
r77187 | andrew.kuchling | 2009-12-31 10:38:53 -0600 (Thu, 31 Dec 2009) | 1 line
Add various items
........
r77188 | benjamin.peterson | 2009-12-31 10:49:37 -0600 (Thu, 31 Dec 2009) | 1 line
add another advancement
........
r77262 | andrew.kuchling | 2010-01-02 19:15:21 -0600 (Sat, 02 Jan 2010) | 1 line
Add a few items
........
r77313 | benjamin.peterson | 2010-01-04 18:04:19 -0600 (Mon, 04 Jan 2010) | 1 line
add a test about hashing array.array
........
r77317 | georg.brandl | 2010-01-05 12:14:52 -0600 (Tue, 05 Jan 2010) | 1 line
Add Stefan.
........
r77331 | georg.brandl | 2010-01-06 11:43:06 -0600 (Wed, 06 Jan 2010) | 1 line
Small fixes to test_cmd: fix signature of do_shell, remove duplicate import, add option to run the custom Cmd class.
........
r77332 | georg.brandl | 2010-01-06 12:02:16 -0600 (Wed, 06 Jan 2010) | 7 lines
#5991: let completion for the "help" command include help topics.
This also simplifies the Cmd.get_names() method implementation; it was written
at a time where dir() didn't consider base class attributes.
........
r77333 | georg.brandl | 2010-01-06 12:26:08 -0600 (Wed, 06 Jan 2010) | 1 line
#5950: document that zip files with comments are unsupported in zipimport.
........
r77337 | r.david.murray | 2010-01-06 21:09:08 -0600 (Wed, 06 Jan 2010) | 3 lines
Add -W to the 'basics', 'opt', and 'all' test runs so that we get verbose
information if a failure happens.
........
r77338 | r.david.murray | 2010-01-06 22:04:28 -0600 (Wed, 06 Jan 2010) | 2 lines
Fix inadvertent checkin of debug line.
........
2010-01-09 14:53:06 -04:00
|
|
|
import itertools
|
2010-01-02 20:29:15 -04:00
|
|
|
import sys
|
2009-05-03 21:45:33 -03:00
|
|
|
try:
|
|
|
|
import threading
|
|
|
|
except ImportError:
|
|
|
|
threading = None
|
2005-08-21 15:45:59 -03:00
|
|
|
import unittest
|
2010-01-02 20:29:15 -04:00
|
|
|
import warnings
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support
|
2008-09-24 19:53:33 -03:00
|
|
|
from test.support import _4G, precisionbigmemtest
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2010-01-02 20:29:15 -04:00
|
|
|
# Were we compiled --with-pydebug or with #define Py_DEBUG?
|
|
|
|
COMPILED_WITH_PYDEBUG = hasattr(sys, 'gettotalrefcount')
|
|
|
|
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
def hexstr(s):
|
2007-07-09 11:29:40 -03:00
|
|
|
assert isinstance(s, bytes), repr(s)
|
2007-07-10 17:31:05 -03:00
|
|
|
h = "0123456789abcdef"
|
|
|
|
r = ''
|
2007-07-09 11:29:40 -03:00
|
|
|
for i in s:
|
2007-07-10 17:31:05 -03:00
|
|
|
r += h[(i >> 4) & 0xF] + h[i & 0xF]
|
2005-08-21 15:45:59 -03:00
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
class HashLibTestCase(unittest.TestCase):
|
|
|
|
supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
|
|
|
|
'sha224', 'SHA224', 'sha256', 'SHA256',
|
|
|
|
'sha384', 'SHA384', 'sha512', 'SHA512' )
|
|
|
|
|
2010-01-02 20:29:15 -04:00
|
|
|
_warn_on_extension_import = COMPILED_WITH_PYDEBUG
|
|
|
|
|
|
|
|
def _conditional_import_module(self, module_name):
|
|
|
|
"""Import a module and return a reference to it or None on failure."""
|
|
|
|
try:
|
|
|
|
exec('import '+module_name)
|
|
|
|
except ImportError as error:
|
|
|
|
if self._warn_on_extension_import:
|
|
|
|
warnings.warn('Did a C extension fail to compile? %s' % error)
|
|
|
|
return locals().get(module_name)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
algorithms = set()
|
|
|
|
for algorithm in self.supported_hash_names:
|
|
|
|
algorithms.add(algorithm.lower())
|
|
|
|
self.constructors_to_test = {}
|
|
|
|
for algorithm in algorithms:
|
|
|
|
self.constructors_to_test[algorithm] = set()
|
|
|
|
|
|
|
|
# For each algorithm, test the direct constructor and the use
|
|
|
|
# of hashlib.new given the algorithm name.
|
|
|
|
for algorithm, constructors in self.constructors_to_test.items():
|
|
|
|
constructors.add(getattr(hashlib, algorithm))
|
|
|
|
def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm):
|
|
|
|
if data is None:
|
|
|
|
return hashlib.new(_alg)
|
|
|
|
return hashlib.new(_alg, data)
|
|
|
|
constructors.add(_test_algorithm_via_hashlib_new)
|
|
|
|
|
|
|
|
_hashlib = self._conditional_import_module('_hashlib')
|
|
|
|
if _hashlib:
|
|
|
|
# These two algorithms should always be present when this module
|
|
|
|
# is compiled. If not, something was compiled wrong.
|
|
|
|
assert hasattr(_hashlib, 'openssl_md5')
|
|
|
|
assert hasattr(_hashlib, 'openssl_sha1')
|
|
|
|
for algorithm, constructors in self.constructors_to_test.items():
|
|
|
|
constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
|
|
|
|
if constructor:
|
|
|
|
constructors.add(constructor)
|
|
|
|
|
|
|
|
_md5 = self._conditional_import_module('_md5')
|
|
|
|
if _md5:
|
2010-01-02 20:38:10 -04:00
|
|
|
self.constructors_to_test['md5'].add(_md5.md5)
|
|
|
|
_sha1 = self._conditional_import_module('_sha1')
|
|
|
|
if _sha1:
|
|
|
|
self.constructors_to_test['sha1'].add(_sha1.sha1)
|
2010-01-02 20:29:15 -04:00
|
|
|
_sha256 = self._conditional_import_module('_sha256')
|
|
|
|
if _sha256:
|
|
|
|
self.constructors_to_test['sha224'].add(_sha256.sha224)
|
|
|
|
self.constructors_to_test['sha256'].add(_sha256.sha256)
|
|
|
|
_sha512 = self._conditional_import_module('_sha512')
|
|
|
|
if _sha512:
|
|
|
|
self.constructors_to_test['sha384'].add(_sha512.sha384)
|
|
|
|
self.constructors_to_test['sha512'].add(_sha512.sha512)
|
|
|
|
|
|
|
|
super(HashLibTestCase, self).__init__(*args, **kwargs)
|
|
|
|
|
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r77185 | andrew.kuchling | 2009-12-31 10:17:05 -0600 (Thu, 31 Dec 2009) | 1 line
Add some items
........
r77186 | benjamin.peterson | 2009-12-31 10:28:24 -0600 (Thu, 31 Dec 2009) | 1 line
update expat comment
........
r77187 | andrew.kuchling | 2009-12-31 10:38:53 -0600 (Thu, 31 Dec 2009) | 1 line
Add various items
........
r77188 | benjamin.peterson | 2009-12-31 10:49:37 -0600 (Thu, 31 Dec 2009) | 1 line
add another advancement
........
r77262 | andrew.kuchling | 2010-01-02 19:15:21 -0600 (Sat, 02 Jan 2010) | 1 line
Add a few items
........
r77313 | benjamin.peterson | 2010-01-04 18:04:19 -0600 (Mon, 04 Jan 2010) | 1 line
add a test about hashing array.array
........
r77317 | georg.brandl | 2010-01-05 12:14:52 -0600 (Tue, 05 Jan 2010) | 1 line
Add Stefan.
........
r77331 | georg.brandl | 2010-01-06 11:43:06 -0600 (Wed, 06 Jan 2010) | 1 line
Small fixes to test_cmd: fix signature of do_shell, remove duplicate import, add option to run the custom Cmd class.
........
r77332 | georg.brandl | 2010-01-06 12:02:16 -0600 (Wed, 06 Jan 2010) | 7 lines
#5991: let completion for the "help" command include help topics.
This also simplifies the Cmd.get_names() method implementation; it was written
at a time where dir() didn't consider base class attributes.
........
r77333 | georg.brandl | 2010-01-06 12:26:08 -0600 (Wed, 06 Jan 2010) | 1 line
#5950: document that zip files with comments are unsupported in zipimport.
........
r77337 | r.david.murray | 2010-01-06 21:09:08 -0600 (Wed, 06 Jan 2010) | 3 lines
Add -W to the 'basics', 'opt', and 'all' test runs so that we get verbose
information if a failure happens.
........
r77338 | r.david.murray | 2010-01-06 22:04:28 -0600 (Wed, 06 Jan 2010) | 2 lines
Fix inadvertent checkin of debug line.
........
2010-01-09 14:53:06 -04:00
|
|
|
def test_hash_array(self):
|
|
|
|
a = array.array("b", range(10))
|
|
|
|
constructors = self.constructors_to_test.values()
|
|
|
|
for cons in itertools.chain.from_iterable(constructors):
|
|
|
|
c = cons(a)
|
|
|
|
c.hexdigest()
|
|
|
|
|
2010-02-28 22:05:26 -04:00
|
|
|
def test_algorithms_attribute(self):
|
|
|
|
self.assertEqual(hashlib.algorithms,
|
|
|
|
tuple(_algo for _algo in self.supported_hash_names
|
|
|
|
if _algo.islower()))
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
def test_unknown_hash(self):
|
|
|
|
try:
|
|
|
|
hashlib.new('spam spam spam spam spam')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(0 == "hashlib didn't reject bogus hash name")
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
def test_hexdigest(self):
|
|
|
|
for name in self.supported_hash_names:
|
|
|
|
h = hashlib.new(name)
|
2007-07-09 11:29:40 -03:00
|
|
|
assert isinstance(h.digest(), bytes), name
|
|
|
|
self.assertEqual(hexstr(h.digest()), h.hexdigest())
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
|
|
|
|
def test_large_update(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
aas = b'a' * 128
|
|
|
|
bees = b'b' * 127
|
|
|
|
cees = b'c' * 126
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
for name in self.supported_hash_names:
|
|
|
|
m1 = hashlib.new(name)
|
|
|
|
m1.update(aas)
|
|
|
|
m1.update(bees)
|
|
|
|
m1.update(cees)
|
|
|
|
|
|
|
|
m2 = hashlib.new(name)
|
|
|
|
m2.update(aas + bees + cees)
|
|
|
|
self.assertEqual(m1.digest(), m2.digest())
|
|
|
|
|
|
|
|
def check(self, name, data, digest):
|
2010-01-02 20:29:15 -04:00
|
|
|
constructors = self.constructors_to_test[name]
|
|
|
|
# 2 is for hashlib.name(...) and hashlib.new(name, ...)
|
|
|
|
self.assertGreaterEqual(len(constructors), 2)
|
|
|
|
for hash_object_constructor in constructors:
|
|
|
|
computed = hash_object_constructor(data).hexdigest()
|
|
|
|
self.assertEqual(
|
|
|
|
computed, digest,
|
|
|
|
"Hash algorithm %s constructed using %s returned hexdigest"
|
|
|
|
" %r for %d byte input data that should have hashed to %r."
|
|
|
|
% (name, hash_object_constructor,
|
|
|
|
computed, len(data), digest))
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2009-02-12 03:35:29 -04:00
|
|
|
def check_no_unicode(self, algorithm_name):
|
|
|
|
# Unicode objects are not allowed as input.
|
2010-01-02 20:29:15 -04:00
|
|
|
constructors = self.constructors_to_test[algorithm_name]
|
|
|
|
for hash_object_constructor in constructors:
|
|
|
|
self.assertRaises(TypeError, hash_object_constructor, 'spam')
|
2009-02-12 03:35:29 -04:00
|
|
|
|
|
|
|
def test_no_unicode(self):
|
|
|
|
self.check_no_unicode('md5')
|
|
|
|
self.check_no_unicode('sha1')
|
|
|
|
self.check_no_unicode('sha224')
|
|
|
|
self.check_no_unicode('sha256')
|
|
|
|
self.check_no_unicode('sha384')
|
|
|
|
self.check_no_unicode('sha512')
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
def test_case_md5_0(self):
|
2007-07-10 17:31:05 -03:00
|
|
|
self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e')
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
def test_case_md5_1(self):
|
2007-07-10 17:31:05 -03:00
|
|
|
self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72')
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
def test_case_md5_2(self):
|
2007-07-09 11:29:40 -03:00
|
|
|
self.check('md5',
|
|
|
|
b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
|
2007-07-10 17:31:05 -03:00
|
|
|
'd174ab98d277d9f5a5611c2c9f419d9f')
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2008-09-24 19:53:33 -03:00
|
|
|
@precisionbigmemtest(size=_4G + 5, memuse=1)
|
|
|
|
def test_case_md5_huge(self, size):
|
|
|
|
if size == _4G + 5:
|
|
|
|
try:
|
|
|
|
self.check('md5', 'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
|
|
|
|
except OverflowError:
|
|
|
|
pass # 32-bit arch
|
|
|
|
|
|
|
|
@precisionbigmemtest(size=_4G - 1, memuse=1)
|
|
|
|
def test_case_md5_uintmax(self, size):
|
|
|
|
if size == _4G - 1:
|
|
|
|
try:
|
|
|
|
self.check('md5', 'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
|
|
|
|
except OverflowError:
|
|
|
|
pass # 32-bit arch
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
# use the three examples from Federal Information Processing Standards
|
|
|
|
# Publication 180-1, Secure Hash Standard, 1995 April 17
|
|
|
|
# http://www.itl.nist.gov/div897/pubs/fip180-1.htm
|
|
|
|
|
|
|
|
def test_case_sha1_0(self):
|
2007-07-09 11:29:40 -03:00
|
|
|
self.check('sha1', b"",
|
2007-07-10 17:31:05 -03:00
|
|
|
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
def test_case_sha1_1(self):
|
2007-07-09 11:29:40 -03:00
|
|
|
self.check('sha1', b"abc",
|
2007-07-10 17:31:05 -03:00
|
|
|
"a9993e364706816aba3e25717850c26c9cd0d89d")
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
def test_case_sha1_2(self):
|
2007-07-09 11:29:40 -03:00
|
|
|
self.check('sha1',
|
|
|
|
b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
2007-07-10 17:31:05 -03:00
|
|
|
"84983e441c3bd26ebaae4aa1f95129e5e54670f1")
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
def test_case_sha1_3(self):
|
2007-07-09 11:29:40 -03:00
|
|
|
self.check('sha1', b"a" * 1000000,
|
2007-07-10 17:31:05 -03:00
|
|
|
"34aa973cd4c4daa4f61eeb2bdbad27316534016f")
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
|
|
|
|
# use the examples from Federal Information Processing Standards
|
|
|
|
# Publication 180-2, Secure Hash Standard, 2002 August 1
|
|
|
|
# http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
|
|
|
|
|
|
|
def test_case_sha224_0(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha224', b"",
|
2005-08-21 15:45:59 -03:00
|
|
|
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")
|
|
|
|
|
|
|
|
def test_case_sha224_1(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha224', b"abc",
|
2005-08-21 15:45:59 -03:00
|
|
|
"23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")
|
|
|
|
|
|
|
|
def test_case_sha224_2(self):
|
|
|
|
self.check('sha224',
|
2007-08-27 20:09:25 -03:00
|
|
|
b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
2005-08-21 15:45:59 -03:00
|
|
|
"75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525")
|
|
|
|
|
|
|
|
def test_case_sha224_3(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha224', b"a" * 1000000,
|
2005-08-21 15:45:59 -03:00
|
|
|
"20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67")
|
|
|
|
|
|
|
|
|
|
|
|
def test_case_sha256_0(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha256', b"",
|
2005-08-21 15:45:59 -03:00
|
|
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
|
|
|
|
|
|
|
|
def test_case_sha256_1(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha256', b"abc",
|
2005-08-21 15:45:59 -03:00
|
|
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
|
|
|
|
|
|
|
|
def test_case_sha256_2(self):
|
|
|
|
self.check('sha256',
|
2007-08-27 20:09:25 -03:00
|
|
|
b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
2005-08-21 15:45:59 -03:00
|
|
|
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1")
|
|
|
|
|
|
|
|
def test_case_sha256_3(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha256', b"a" * 1000000,
|
2005-08-21 15:45:59 -03:00
|
|
|
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0")
|
|
|
|
|
|
|
|
|
|
|
|
def test_case_sha384_0(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha384', b"",
|
2005-08-21 15:45:59 -03:00
|
|
|
"38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+
|
|
|
|
"274edebfe76f65fbd51ad2f14898b95b")
|
|
|
|
|
|
|
|
def test_case_sha384_1(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha384', b"abc",
|
2005-08-21 15:45:59 -03:00
|
|
|
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+
|
|
|
|
"8086072ba1e7cc2358baeca134c825a7")
|
|
|
|
|
|
|
|
def test_case_sha384_2(self):
|
|
|
|
self.check('sha384',
|
2007-08-27 20:09:25 -03:00
|
|
|
b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
|
|
|
|
b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
2005-08-21 15:45:59 -03:00
|
|
|
"09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+
|
|
|
|
"fcc7c71a557e2db966c3e9fa91746039")
|
|
|
|
|
|
|
|
def test_case_sha384_3(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha384', b"a" * 1000000,
|
2005-08-21 15:45:59 -03:00
|
|
|
"9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+
|
|
|
|
"07b8b3dc38ecc4ebae97ddd87f3d8985")
|
|
|
|
|
|
|
|
|
|
|
|
def test_case_sha512_0(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha512', b"",
|
2005-08-21 15:45:59 -03:00
|
|
|
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+
|
|
|
|
"47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
|
|
|
|
|
|
|
|
def test_case_sha512_1(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha512', b"abc",
|
2005-08-21 15:45:59 -03:00
|
|
|
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+
|
|
|
|
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")
|
|
|
|
|
|
|
|
def test_case_sha512_2(self):
|
|
|
|
self.check('sha512',
|
2007-08-27 20:09:25 -03:00
|
|
|
b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
|
|
|
|
b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
2005-08-21 15:45:59 -03:00
|
|
|
"8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
|
|
|
|
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
|
|
|
|
|
|
|
|
def test_case_sha512_3(self):
|
2007-08-27 20:09:25 -03:00
|
|
|
self.check('sha512', b"a" * 1000000,
|
2005-08-21 15:45:59 -03:00
|
|
|
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
|
|
|
|
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
|
|
|
|
|
2009-01-08 17:17:16 -04:00
|
|
|
def test_gil(self):
|
|
|
|
# Check things work fine with an input larger than the size required
|
|
|
|
# for multithreaded operation (which is hardwired to 2048).
|
|
|
|
gil_minsize = 2048
|
|
|
|
|
|
|
|
m = hashlib.md5()
|
|
|
|
m.update(b'1')
|
|
|
|
m.update(b'#' * gil_minsize)
|
|
|
|
m.update(b'1')
|
|
|
|
self.assertEquals(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21')
|
|
|
|
|
|
|
|
m = hashlib.md5(b'x' * gil_minsize)
|
|
|
|
self.assertEquals(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958')
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2009-05-03 21:45:33 -03:00
|
|
|
def test_threaded_hashing(self):
|
|
|
|
if not threading:
|
|
|
|
raise unittest.SkipTest('No threading module.')
|
|
|
|
|
|
|
|
# Updating the same hash object from several threads at once
|
|
|
|
# using data chunk sizes containing the same byte sequences.
|
|
|
|
#
|
|
|
|
# If the internal locks are working to prevent multiple
|
|
|
|
# updates on the same object from running at once, the resulting
|
|
|
|
# hash will be the same as doing it single threaded upfront.
|
|
|
|
hasher = hashlib.sha1()
|
|
|
|
num_threads = 5
|
|
|
|
smallest_data = b'swineflu'
|
|
|
|
data = smallest_data*200000
|
|
|
|
expected_hash = hashlib.sha1(data*num_threads).hexdigest()
|
|
|
|
|
|
|
|
def hash_in_chunks(chunk_size, event):
|
|
|
|
index = 0
|
|
|
|
while index < len(data):
|
|
|
|
hasher.update(data[index:index+chunk_size])
|
|
|
|
index += chunk_size
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
events = []
|
|
|
|
for threadnum in range(num_threads):
|
|
|
|
chunk_size = len(data) // (10**threadnum)
|
|
|
|
assert chunk_size > 0
|
|
|
|
assert chunk_size % len(smallest_data) == 0
|
|
|
|
event = threading.Event()
|
|
|
|
events.append(event)
|
|
|
|
threading.Thread(target=hash_in_chunks,
|
|
|
|
args=(chunk_size, event)).start()
|
|
|
|
|
|
|
|
for event in events:
|
|
|
|
event.wait()
|
|
|
|
|
|
|
|
self.assertEqual(expected_hash, hasher.hexdigest())
|
|
|
|
|
Merged revisions 72506,72525-72526,72551,72558,72616,72654-72655,72689,72745,72750,72802,72812,72822,72824,72826-72827,72833,72876,72890,72923,72946,73026,73042,73045,73047,73065,73068-73069 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72506 | vinay.sajip | 2009-05-09 07:07:17 -0500 (Sat, 09 May 2009) | 1 line
Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
........
r72525 | benjamin.peterson | 2009-05-09 20:38:02 -0500 (Sat, 09 May 2009) | 1 line
close file explicitly
........
r72526 | benjamin.peterson | 2009-05-09 21:29:00 -0500 (Sat, 09 May 2009) | 1 line
make sure files are closed using the with statement
........
r72551 | benjamin.peterson | 2009-05-10 09:16:47 -0500 (Sun, 10 May 2009) | 1 line
use isinstance
........
r72558 | benjamin.peterson | 2009-05-10 18:52:09 -0500 (Sun, 10 May 2009) | 1 line
sys.setdefaultencoding() strikes me as a bad example
........
r72616 | benjamin.peterson | 2009-05-13 19:33:10 -0500 (Wed, 13 May 2009) | 1 line
importlib.import_module is better these days
........
r72654 | benjamin.peterson | 2009-05-14 17:37:49 -0500 (Thu, 14 May 2009) | 1 line
prevent refleaks from threads
........
r72655 | benjamin.peterson | 2009-05-14 17:40:34 -0500 (Thu, 14 May 2009) | 1 line
a useful decorator for cleaning up threads
........
r72689 | benjamin.peterson | 2009-05-16 13:44:34 -0500 (Sat, 16 May 2009) | 1 line
use skipTest()
........
r72745 | benjamin.peterson | 2009-05-17 09:16:29 -0500 (Sun, 17 May 2009) | 1 line
ignore .rst files in sphinx its self
........
r72750 | benjamin.peterson | 2009-05-17 11:59:27 -0500 (Sun, 17 May 2009) | 1 line
chop off slash
........
r72802 | georg.brandl | 2009-05-20 13:35:27 -0500 (Wed, 20 May 2009) | 1 line
#6051: refer to email examples for better way to construct email messages.
........
r72812 | michael.foord | 2009-05-21 17:57:02 -0500 (Thu, 21 May 2009) | 1 line
Rename TestCase._result to _resultForDoCleanups to avoid potential clashes in TestCase subclasses. Issue 6072.
........
r72822 | georg.brandl | 2009-05-22 04:33:25 -0500 (Fri, 22 May 2009) | 1 line
#6084: fix example.
........
r72824 | georg.brandl | 2009-05-22 04:43:17 -0500 (Fri, 22 May 2009) | 1 line
Fix references to file-related functions and methods (os.* vs file.*).
........
r72826 | georg.brandl | 2009-05-22 04:49:42 -0500 (Fri, 22 May 2009) | 1 line
Fix confusing wording.
........
r72827 | georg.brandl | 2009-05-22 04:50:30 -0500 (Fri, 22 May 2009) | 1 line
s/use/call/
........
r72833 | georg.brandl | 2009-05-22 12:00:17 -0500 (Fri, 22 May 2009) | 1 line
#6078: _warnings is a builtin module and has no standard init_warnings function.
........
r72876 | benjamin.peterson | 2009-05-23 15:59:09 -0500 (Sat, 23 May 2009) | 1 line
remove mention of old ctypes version
........
r72890 | gregory.p.smith | 2009-05-24 13:00:13 -0500 (Sun, 24 May 2009) | 2 lines
add a versionadded tag for set_tunnel
........
r72923 | michael.foord | 2009-05-25 15:36:56 -0500 (Mon, 25 May 2009) | 1 line
Make assertSequenceEqual error messages less cryptic, particularly for nested sequences.
........
r72946 | ronald.oussoren | 2009-05-26 13:44:48 -0500 (Tue, 26 May 2009) | 2 lines
Fixes issue 6110
........
r73026 | r.david.murray | 2009-05-29 14:30:27 -0500 (Fri, 29 May 2009) | 3 lines
Issue 6141: document that the first item of args is still the
command name even when executable is specified.
........
r73042 | benjamin.peterson | 2009-05-29 22:10:52 -0500 (Fri, 29 May 2009) | 1 line
no fdatasync on macos
........
r73045 | georg.brandl | 2009-05-30 02:26:04 -0500 (Sat, 30 May 2009) | 1 line
#6146: fix markup bug.
........
r73047 | georg.brandl | 2009-05-30 05:33:23 -0500 (Sat, 30 May 2009) | 1 line
Fix some more small markup problems.
........
r73065 | antoine.pitrou | 2009-05-30 16:39:25 -0500 (Sat, 30 May 2009) | 3 lines
The test for #5330 wasn't correct.
........
r73068 | antoine.pitrou | 2009-05-30 16:45:40 -0500 (Sat, 30 May 2009) | 3 lines
Update ACKS
........
r73069 | benjamin.peterson | 2009-05-30 19:42:42 -0500 (Sat, 30 May 2009) | 1 line
fix signature
........
2009-06-01 19:42:33 -03:00
|
|
|
@support.reap_threads
|
2005-08-21 15:45:59 -03:00
|
|
|
def test_main():
|
2008-05-20 18:35:26 -03:00
|
|
|
support.run_unittest(HashLibTestCase)
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|