Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(14641)

Side by Side Diff: Lib/test/test_hashlib.py

Issue 16113: Add SHA-3 (Keccak) support
Patch Set: Created 6 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Test hashlib module 1 # Test hashlib module
2 # 2 #
3 # $Id$ 3 # $Id$
4 # 4 #
5 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) 5 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
6 # Licensed to PSF under a Contributor Agreement. 6 # Licensed to PSF under a Contributor Agreement.
7 # 7 #
8 8
9 import array 9 import array
10 import hashlib 10 import hashlib
(...skipping 18 matching lines...) Expand all
29 h = "0123456789abcdef" 29 h = "0123456789abcdef"
30 r = '' 30 r = ''
31 for i in s: 31 for i in s:
32 r += h[(i >> 4) & 0xF] + h[i & 0xF] 32 r += h[(i >> 4) & 0xF] + h[i & 0xF]
33 return r 33 return r
34 34
35 35
36 class HashLibTestCase(unittest.TestCase): 36 class HashLibTestCase(unittest.TestCase):
37 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1', 37 supported_hash_names = ( 'md5', 'MD5', 'sha1', 'SHA1',
38 'sha224', 'SHA224', 'sha256', 'SHA256', 38 'sha224', 'SHA224', 'sha256', 'SHA256',
39 'sha384', 'SHA384', 'sha512', 'SHA512' ) 39 'sha384', 'SHA384', 'sha512', 'SHA512',
40 'sha3_224', 'sha3_256', 'sha3_384',
41 'sha3_512', 'SHA3_224', 'SHA3_256',
42 'SHA3_384', 'SHA3_512' )
40 43
41 # Issue #14693: fallback modules are always compiled under POSIX 44 # Issue #14693: fallback modules are always compiled under POSIX
42 _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG 45 _warn_on_extension_import = os.name == 'posix' or COMPILED_WITH_PYDEBUG
43 46
44 def _conditional_import_module(self, module_name): 47 def _conditional_import_module(self, module_name):
45 """Import a module and return a reference to it or None on failure.""" 48 """Import a module and return a reference to it or None on failure."""
46 try: 49 try:
47 exec('import '+module_name) 50 exec('import '+module_name)
48 except ImportError as error: 51 except ImportError as error:
49 if self._warn_on_extension_import: 52 if self._warn_on_extension_import:
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 if _sha1: 89 if _sha1:
87 self.constructors_to_test['sha1'].add(_sha1.sha1) 90 self.constructors_to_test['sha1'].add(_sha1.sha1)
88 _sha256 = self._conditional_import_module('_sha256') 91 _sha256 = self._conditional_import_module('_sha256')
89 if _sha256: 92 if _sha256:
90 self.constructors_to_test['sha224'].add(_sha256.sha224) 93 self.constructors_to_test['sha224'].add(_sha256.sha224)
91 self.constructors_to_test['sha256'].add(_sha256.sha256) 94 self.constructors_to_test['sha256'].add(_sha256.sha256)
92 _sha512 = self._conditional_import_module('_sha512') 95 _sha512 = self._conditional_import_module('_sha512')
93 if _sha512: 96 if _sha512:
94 self.constructors_to_test['sha384'].add(_sha512.sha384) 97 self.constructors_to_test['sha384'].add(_sha512.sha384)
95 self.constructors_to_test['sha512'].add(_sha512.sha512) 98 self.constructors_to_test['sha512'].add(_sha512.sha512)
99 _sha3 = self._conditional_import_module('_sha3')
100 if _sha3:
101 self.constructors_to_test['sha3_224'].add(_sha3.sha3_224)
102 self.constructors_to_test['sha3_256'].add(_sha3.sha3_256)
103 self.constructors_to_test['sha3_384'].add(_sha3.sha3_384)
104 self.constructors_to_test['sha3_512'].add(_sha3.sha3_512)
96 105
97 super(HashLibTestCase, self).__init__(*args, **kwargs) 106 super(HashLibTestCase, self).__init__(*args, **kwargs)
98 107
99 def test_hash_array(self): 108 def test_hash_array(self):
100 a = array.array("b", range(10)) 109 a = array.array("b", range(10))
101 constructors = self.constructors_to_test.values() 110 constructors = self.constructors_to_test.values()
102 for cons in itertools.chain.from_iterable(constructors): 111 for cons in itertools.chain.from_iterable(constructors):
103 c = cons(a) 112 c = cons(a)
104 c.hexdigest() 113 c.hexdigest()
105 114
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 m1 = hashlib.new(name) 160 m1 = hashlib.new(name)
152 m1.update(aas) 161 m1.update(aas)
153 m1.update(bees) 162 m1.update(bees)
154 m1.update(cees) 163 m1.update(cees)
155 164
156 m2 = hashlib.new(name) 165 m2 = hashlib.new(name)
157 m2.update(aas + bees + cees) 166 m2.update(aas + bees + cees)
158 self.assertEqual(m1.digest(), m2.digest()) 167 self.assertEqual(m1.digest(), m2.digest())
159 168
160 def check(self, name, data, digest): 169 def check(self, name, data, digest):
170 digest = digest.lower()
161 constructors = self.constructors_to_test[name] 171 constructors = self.constructors_to_test[name]
162 # 2 is for hashlib.name(...) and hashlib.new(name, ...) 172 # 2 is for hashlib.name(...) and hashlib.new(name, ...)
163 self.assertGreaterEqual(len(constructors), 2) 173 self.assertGreaterEqual(len(constructors), 2)
164 for hash_object_constructor in constructors: 174 for hash_object_constructor in constructors:
165 computed = hash_object_constructor(data).hexdigest() 175 computed = hash_object_constructor(data).hexdigest()
166 self.assertEqual( 176 self.assertEqual(
167 computed, digest, 177 computed, digest,
168 "Hash algorithm %s constructed using %s returned hexdigest" 178 "Hash algorithm %s constructed using %s returned hexdigest"
169 " %r for %d byte input data that should have hashed to %r." 179 " %r for %d byte input data that should have hashed to %r."
170 % (name, hash_object_constructor, 180 % (name, hash_object_constructor,
171 computed, len(data), digest)) 181 computed, len(data), digest))
172 182
173 def check_no_unicode(self, algorithm_name): 183 def check_no_unicode(self, algorithm_name):
174 # Unicode objects are not allowed as input. 184 # Unicode objects are not allowed as input.
175 constructors = self.constructors_to_test[algorithm_name] 185 constructors = self.constructors_to_test[algorithm_name]
176 for hash_object_constructor in constructors: 186 for hash_object_constructor in constructors:
177 self.assertRaises(TypeError, hash_object_constructor, 'spam') 187 self.assertRaises(TypeError, hash_object_constructor, 'spam')
178 188
179 def test_no_unicode(self): 189 def test_no_unicode(self):
180 self.check_no_unicode('md5') 190 self.check_no_unicode('md5')
181 self.check_no_unicode('sha1') 191 self.check_no_unicode('sha1')
182 self.check_no_unicode('sha224') 192 self.check_no_unicode('sha224')
183 self.check_no_unicode('sha256') 193 self.check_no_unicode('sha256')
184 self.check_no_unicode('sha384') 194 self.check_no_unicode('sha384')
185 self.check_no_unicode('sha512') 195 self.check_no_unicode('sha512')
196 self.check_no_unicode('sha3_224')
197 self.check_no_unicode('sha3_256')
198 self.check_no_unicode('sha3_384')
199 self.check_no_unicode('sha3_512')
186 200
187 def test_case_md5_0(self): 201 def test_case_md5_0(self):
188 self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e') 202 self.check('md5', b'', 'd41d8cd98f00b204e9800998ecf8427e')
189 203
190 def test_case_md5_1(self): 204 def test_case_md5_1(self):
191 self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72') 205 self.check('md5', b'abc', '900150983cd24fb0d6963f7d28e17f72')
192 206
193 def test_case_md5_2(self): 207 def test_case_md5_2(self):
194 self.check('md5', 208 self.check('md5',
195 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 789', 209 b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456 789',
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 self.check('sha512', 324 self.check('sha512',
311 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+ 325 b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"+
312 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 326 b"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
313 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+ 327 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"+
314 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909") 328 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909")
315 329
316 def test_case_sha512_3(self): 330 def test_case_sha512_3(self):
317 self.check('sha512', b"a" * 1000000, 331 self.check('sha512', b"a" * 1000000,
318 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+ 332 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
319 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b") 333 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
334
335 def test_case_sha3_224_0(self):
336 self.check('sha3_224', b"",
337 "F71837502BA8E10837BDD8D365ADB85591895602FC552B48B7390ABD")
338
339 def test_case_sha3_256_0(self):
340 self.check('sha3_256', b"",
341 "C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470")
342
343 def test_case_sha3_384_0(self):
344 self.check('sha3_384', b"",
345 "2C23146A63A29ACF99E73B88F8C24EAA7DC60AA771780CCC006AFBFA8FE2479B"+
346 "2DD2B21362337441AC12B515911957FF")
347
348 def test_case_sha3_512_0(self):
349 self.check('sha3_512', b"",
350 "0EAB42DE4C3CEB9235FC91ACFFE746B29C29A8C366B7C60E4E67C466F36A4304C"+
351 "00FA9CAF9D87976BA469BCBE06713B435F091EF2769FB160CDAB33D3670680E")
320 352
gregory.p.smith 2012/10/05 08:41:13 add a # TODO comment in here asking for more test
christian.heimes 2012/10/05 11:16:27 The Keccak team has released a long list of test v
321 def test_gil(self): 353 def test_gil(self):
322 # Check things work fine with an input larger than the size required 354 # Check things work fine with an input larger than the size required
323 # for multithreaded operation (which is hardwired to 2048). 355 # for multithreaded operation (which is hardwired to 2048).
324 gil_minsize = 2048 356 gil_minsize = 2048
325 357
326 m = hashlib.md5() 358 m = hashlib.md5()
327 m.update(b'1') 359 m.update(b'1')
328 m.update(b'#' * gil_minsize) 360 m.update(b'#' * gil_minsize)
329 m.update(b'1') 361 m.update(b'1')
330 self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21') 362 self.assertEqual(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21')
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 for event in events: 399 for event in events:
368 event.wait() 400 event.wait()
369 401
370 self.assertEqual(expected_hash, hasher.hexdigest()) 402 self.assertEqual(expected_hash, hasher.hexdigest())
371 403
372 def test_main(): 404 def test_main():
373 support.run_unittest(HashLibTestCase) 405 support.run_unittest(HashLibTestCase)
374 406
375 if __name__ == "__main__": 407 if __name__ == "__main__":
376 test_main() 408 test_main()
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+