Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Static Private Attributes
test.test_hashlib.HashLibTestCase Class Reference

List of all members.

Public Member Functions

def __init__
def test_hash_array
def test_algorithms_guaranteed
def test_algorithms_available
def test_unknown_hash
def test_get_builtin_constructor
def test_hexdigest
def test_large_update
def check
def check_no_unicode
def test_no_unicode
def test_case_md5_0
def test_case_md5_1
def test_case_md5_2
def test_case_md5_huge
def test_case_md5_uintmax
def test_case_sha1_0
def test_case_sha1_1
def test_case_sha1_2
def test_case_sha1_3
def test_case_sha224_0
def test_case_sha224_1
def test_case_sha224_2
def test_case_sha224_3
def test_case_sha256_0
def test_case_sha256_1
def test_case_sha256_2
def test_case_sha256_3
def test_case_sha384_0
def test_case_sha384_1
def test_case_sha384_2
def test_case_sha384_3
def test_case_sha512_0
def test_case_sha512_1
def test_case_sha512_2
def test_case_sha512_3
def test_gil
def test_threaded_hashing

Public Attributes

 constructors_to_test

Static Public Attributes

tuple supported_hash_names

Private Member Functions

def _conditional_import_module

Static Private Attributes

 _warn_on_extension_import = COMPILED_WITH_PYDEBUG

Detailed Description

Definition at line 35 of file test_hashlib.py.


Constructor & Destructor Documentation

def test.test_hashlib.HashLibTestCase.__init__ (   self,
  args,
  kwargs 
)

Definition at line 51 of file test_hashlib.py.

00051 
00052     def __init__(self, *args, **kwargs):
00053         algorithms = set()
00054         for algorithm in self.supported_hash_names:
00055             algorithms.add(algorithm.lower())
00056         self.constructors_to_test = {}
00057         for algorithm in algorithms:
00058             self.constructors_to_test[algorithm] = set()
00059 
00060         # For each algorithm, test the direct constructor and the use
00061         # of hashlib.new given the algorithm name.
00062         for algorithm, constructors in self.constructors_to_test.items():
00063             constructors.add(getattr(hashlib, algorithm))
00064             def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm):
00065                 if data is None:
00066                     return hashlib.new(_alg)
00067                 return hashlib.new(_alg, data)
00068             constructors.add(_test_algorithm_via_hashlib_new)
00069 
00070         _hashlib = self._conditional_import_module('_hashlib')
00071         if _hashlib:
00072             # These two algorithms should always be present when this module
00073             # is compiled.  If not, something was compiled wrong.
00074             assert hasattr(_hashlib, 'openssl_md5')
00075             assert hasattr(_hashlib, 'openssl_sha1')
00076             for algorithm, constructors in self.constructors_to_test.items():
00077                 constructor = getattr(_hashlib, 'openssl_'+algorithm, None)
00078                 if constructor:
00079                     constructors.add(constructor)
00080 
00081         _md5 = self._conditional_import_module('_md5')
00082         if _md5:
00083             self.constructors_to_test['md5'].add(_md5.md5)
00084         _sha1 = self._conditional_import_module('_sha1')
00085         if _sha1:
00086             self.constructors_to_test['sha1'].add(_sha1.sha1)
00087         _sha256 = self._conditional_import_module('_sha256')
00088         if _sha256:
00089             self.constructors_to_test['sha224'].add(_sha256.sha224)
00090             self.constructors_to_test['sha256'].add(_sha256.sha256)
00091         _sha512 = self._conditional_import_module('_sha512')
00092         if _sha512:
00093             self.constructors_to_test['sha384'].add(_sha512.sha384)
00094             self.constructors_to_test['sha512'].add(_sha512.sha512)
00095 
00096         super(HashLibTestCase, self).__init__(*args, **kwargs)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def test.test_hashlib.HashLibTestCase._conditional_import_module (   self,
  module_name 
) [private]
Import a module and return a reference to it or None on failure.

Definition at line 42 of file test_hashlib.py.

00042 
00043     def _conditional_import_module(self, module_name):
00044         """Import a module and return a reference to it or None on failure."""
00045         try:
00046             exec('import '+module_name)
00047         except ImportError as error:
00048             if self._warn_on_extension_import:
00049                 warnings.warn('Did a C extension fail to compile? %s' % error)
00050         return locals().get(module_name)

Here is the call graph for this function:

def test.test_hashlib.HashLibTestCase.check (   self,
  name,
  data,
  digest 
)

Definition at line 161 of file test_hashlib.py.

00161 
00162     def check(self, name, data, digest):
00163         constructors = self.constructors_to_test[name]
00164         # 2 is for hashlib.name(...) and hashlib.new(name, ...)
00165         self.assertGreaterEqual(len(constructors), 2)
00166         for hash_object_constructor in constructors:
00167             computed = hash_object_constructor(data).hexdigest()
00168             self.assertEqual(
00169                     computed, digest,
00170                     "Hash algorithm %s constructed using %s returned hexdigest"
00171                     " %r for %d byte input data that should have hashed to %r."
00172                     % (name, hash_object_constructor,
00173                        computed, len(data), digest))

Here is the call graph for this function:

def test.test_hashlib.HashLibTestCase.check_no_unicode (   self,
  algorithm_name 
)

Definition at line 174 of file test_hashlib.py.

00174 
00175     def check_no_unicode(self, algorithm_name):
00176         # Unicode objects are not allowed as input.
00177         constructors = self.constructors_to_test[algorithm_name]
00178         for hash_object_constructor in constructors:
00179             self.assertRaises(TypeError, hash_object_constructor, 'spam')

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 109 of file test_hashlib.py.

00109 
00110     def test_algorithms_available(self):
00111         self.assertTrue(set(hashlib.algorithms_guaranteed).
00112                             issubset(hashlib.algorithms_available))

Here is the call graph for this function:

Definition at line 104 of file test_hashlib.py.

00104 
00105     def test_algorithms_guaranteed(self):
00106         self.assertEqual(hashlib.algorithms_guaranteed,
00107             set(_algo for _algo in self.supported_hash_names
00108                   if _algo.islower()))

Here is the call graph for this function:

Definition at line 188 of file test_hashlib.py.

00188 
00189     def test_case_md5_0(self):
00190         self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e')

Here is the call graph for this function:

Definition at line 191 of file test_hashlib.py.

00191 
00192     def test_case_md5_1(self):
00193         self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72')

Here is the call graph for this function:

Definition at line 194 of file test_hashlib.py.

00194 
00195     def test_case_md5_2(self):
00196         self.check('md5',
00197                    b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
00198                    'd174ab98d277d9f5a5611c2c9f419d9f')

Here is the call graph for this function:

Definition at line 200 of file test_hashlib.py.

00200 
00201     def test_case_md5_huge(self, size):
00202         if size == _4G + 5:
00203             try:
00204                 self.check('md5', b'A'*size, 'c9af2dff37468ce5dfee8f2cfc0a9c6d')
00205             except OverflowError:
00206                 pass # 32-bit arch

Here is the call graph for this function:

Definition at line 208 of file test_hashlib.py.

00208 
00209     def test_case_md5_uintmax(self, size):
00210         if size == _4G - 1:
00211             try:
00212                 self.check('md5', b'A'*size, '28138d306ff1b8281f1a9067e1a1a2b3')
00213             except OverflowError:
00214                 pass # 32-bit arch

Here is the call graph for this function:

Definition at line 219 of file test_hashlib.py.

00219 
00220     def test_case_sha1_0(self):
00221         self.check('sha1', b"",
00222                    "da39a3ee5e6b4b0d3255bfef95601890afd80709")

Here is the call graph for this function:

Definition at line 223 of file test_hashlib.py.

00223 
00224     def test_case_sha1_1(self):
00225         self.check('sha1', b"abc",
00226                    "a9993e364706816aba3e25717850c26c9cd0d89d")

Here is the call graph for this function:

Definition at line 227 of file test_hashlib.py.

00227 
00228     def test_case_sha1_2(self):
00229         self.check('sha1',
00230                    b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
00231                    "84983e441c3bd26ebaae4aa1f95129e5e54670f1")

Here is the call graph for this function:

Definition at line 232 of file test_hashlib.py.

00232 
00233     def test_case_sha1_3(self):
00234         self.check('sha1', b"a" * 1000000,
00235                    "34aa973cd4c4daa4f61eeb2bdbad27316534016f")
00236 

Here is the call graph for this function:

Definition at line 241 of file test_hashlib.py.

00241 
00242     def test_case_sha224_0(self):
00243         self.check('sha224', b"",
00244           "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")

Here is the call graph for this function:

Definition at line 245 of file test_hashlib.py.

00245 
00246     def test_case_sha224_1(self):
00247         self.check('sha224', b"abc",
00248           "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7")

Here is the call graph for this function:

Definition at line 249 of file test_hashlib.py.

00249 
00250     def test_case_sha224_2(self):
00251         self.check('sha224',
00252           b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
00253           "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525")

Here is the call graph for this function:

Definition at line 254 of file test_hashlib.py.

00254 
00255     def test_case_sha224_3(self):
00256         self.check('sha224', b"a" * 1000000,
00257           "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67")
00258 

Here is the call graph for this function:

Definition at line 259 of file test_hashlib.py.

00259 
00260     def test_case_sha256_0(self):
00261         self.check('sha256', b"",
00262           "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")

Here is the call graph for this function:

Definition at line 263 of file test_hashlib.py.

00263 
00264     def test_case_sha256_1(self):
00265         self.check('sha256', b"abc",
00266           "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")

Here is the call graph for this function:

Definition at line 267 of file test_hashlib.py.

00267 
00268     def test_case_sha256_2(self):
00269         self.check('sha256',
00270           b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
00271           "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1")

Here is the call graph for this function:

Definition at line 272 of file test_hashlib.py.

00272 
00273     def test_case_sha256_3(self):
00274         self.check('sha256', b"a" * 1000000,
00275           "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0")
00276 

Here is the call graph for this function:

Definition at line 277 of file test_hashlib.py.

00277 
00278     def test_case_sha384_0(self):
00279         self.check('sha384', b"",
00280           "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da"+
00281           "274edebfe76f65fbd51ad2f14898b95b")

Here is the call graph for this function:

Definition at line 282 of file test_hashlib.py.

00282 
00283     def test_case_sha384_1(self):
00284         self.check('sha384', b"abc",
00285           "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"+
00286           "8086072ba1e7cc2358baeca134c825a7")

Here is the call graph for this function:

Definition at line 287 of file test_hashlib.py.

00287 
00288     def test_case_sha384_2(self):
00289         self.check('sha384',
00290                    b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
00291                    b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
00292           "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"+
00293           "fcc7c71a557e2db966c3e9fa91746039")

Here is the call graph for this function:

Definition at line 294 of file test_hashlib.py.

00294 
00295     def test_case_sha384_3(self):
00296         self.check('sha384', b"a" * 1000000,
00297           "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b"+
00298           "07b8b3dc38ecc4ebae97ddd87f3d8985")
00299 

Here is the call graph for this function:

Definition at line 300 of file test_hashlib.py.

00300 
00301     def test_case_sha512_0(self):
00302         self.check('sha512', b"",
00303           "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"+
00304           "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")

Here is the call graph for this function:

Definition at line 305 of file test_hashlib.py.

00305 
00306     def test_case_sha512_1(self):
00307         self.check('sha512', b"abc",
00308           "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"+
00309           "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f")

Here is the call graph for this function:

Definition at line 310 of file test_hashlib.py.

00310 
00311     def test_case_sha512_2(self):
00312         self.check('sha512',
00313                    b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
00314                    b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
00315           "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
00316           "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")

Here is the call graph for this function:

Definition at line 317 of file test_hashlib.py.

00317 
00318     def test_case_sha512_3(self):
00319         self.check('sha512', b"a" * 1000000,
00320           "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
00321           "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")

Here is the call graph for this function:

Definition at line 121 of file test_hashlib.py.

00121 
00122     def test_get_builtin_constructor(self):
00123         get_builtin_constructor = hashlib.__dict__[
00124                 '__get_builtin_constructor']
00125         self.assertRaises(ValueError, get_builtin_constructor, 'test')
00126         try:
00127             import _md5
00128         except ImportError:
00129             pass
00130         # This forces an ImportError for "import _md5" statements
00131         sys.modules['_md5'] = None
00132         try:
00133             self.assertRaises(ValueError, get_builtin_constructor, 'md5')
00134         finally:
00135             if '_md5' in locals():
00136                 sys.modules['_md5'] = _md5
00137             else:
00138                 del sys.modules['_md5']

Here is the call graph for this function:

Definition at line 322 of file test_hashlib.py.

00322 
00323     def test_gil(self):
00324         # Check things work fine with an input larger than the size required
00325         # for multithreaded operation (which is hardwired to 2048).
00326         gil_minsize = 2048
00327 
00328         m = hashlib.md5()
00329         m.update(b'1')
00330         m.update(b'#' * gil_minsize)
00331         m.update(b'1')
00332         self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21')
00333 
00334         m = hashlib.md5(b'x' * gil_minsize)
00335         self.assertEqual(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958')

Here is the call graph for this function:

Definition at line 97 of file test_hashlib.py.

00097 
00098     def test_hash_array(self):
00099         a = array.array("b", range(10))
00100         constructors = self.constructors_to_test.values()
00101         for cons in itertools.chain.from_iterable(constructors):
00102             c = cons(a)
00103             c.hexdigest()

Definition at line 139 of file test_hashlib.py.

00139 
00140     def test_hexdigest(self):
00141         for name in self.supported_hash_names:
00142             h = hashlib.new(name)
00143             assert isinstance(h.digest(), bytes), name
00144             self.assertEqual(hexstr(h.digest()), h.hexdigest())
00145 

Here is the call graph for this function:

Definition at line 146 of file test_hashlib.py.

00146 
00147     def test_large_update(self):
00148         aas = b'a' * 128
00149         bees = b'b' * 127
00150         cees = b'c' * 126
00151 
00152         for name in self.supported_hash_names:
00153             m1 = hashlib.new(name)
00154             m1.update(aas)
00155             m1.update(bees)
00156             m1.update(cees)
00157 
00158             m2 = hashlib.new(name)
00159             m2.update(aas + bees + cees)
00160             self.assertEqual(m1.digest(), m2.digest())

Here is the call graph for this function:

Definition at line 180 of file test_hashlib.py.

00180 
00181     def test_no_unicode(self):
00182         self.check_no_unicode('md5')
00183         self.check_no_unicode('sha1')
00184         self.check_no_unicode('sha224')
00185         self.check_no_unicode('sha256')
00186         self.check_no_unicode('sha384')
00187         self.check_no_unicode('sha512')

Here is the call graph for this function:

Definition at line 338 of file test_hashlib.py.

00338 
00339     def test_threaded_hashing(self):
00340         # Updating the same hash object from several threads at once
00341         # using data chunk sizes containing the same byte sequences.
00342         #
00343         # If the internal locks are working to prevent multiple
00344         # updates on the same object from running at once, the resulting
00345         # hash will be the same as doing it single threaded upfront.
00346         hasher = hashlib.sha1()
00347         num_threads = 5
00348         smallest_data = b'swineflu'
00349         data = smallest_data*200000
00350         expected_hash = hashlib.sha1(data*num_threads).hexdigest()
00351 
00352         def hash_in_chunks(chunk_size, event):
00353             index = 0
00354             while index < len(data):
00355                 hasher.update(data[index:index+chunk_size])
00356                 index += chunk_size
00357             event.set()
00358 
00359         events = []
00360         for threadnum in range(num_threads):
00361             chunk_size = len(data) // (10**threadnum)
00362             assert chunk_size > 0
00363             assert chunk_size % len(smallest_data) == 0
00364             event = threading.Event()
00365             events.append(event)
00366             threading.Thread(target=hash_in_chunks,
00367                              args=(chunk_size, event)).start()
00368 
00369         for event in events:
00370             event.wait()
00371 
00372         self.assertEqual(expected_hash, hasher.hexdigest())

Here is the call graph for this function:

Definition at line 113 of file test_hashlib.py.

00113 
00114     def test_unknown_hash(self):
00115         try:
00116             hashlib.new('spam spam spam spam spam')
00117         except ValueError:
00118             pass
00119         else:
00120             self.assertTrue(0 == "hashlib didn't reject bogus hash name")

Here is the call graph for this function:


Member Data Documentation

Definition at line 40 of file test_hashlib.py.

Definition at line 55 of file test_hashlib.py.

Initial value:
( 'md5', 'MD5', 'sha1', 'SHA1',
                             'sha224', 'SHA224', 'sha256', 'SHA256',
                             'sha384', 'SHA384', 'sha512', 'SHA512' )

Definition at line 36 of file test_hashlib.py.


The documentation for this class was generated from the following file: