1import binascii 2import functools 3import hmac 4import hashlib 5import unittest 6import unittest.mock 7import warnings 8 9from test.support import hashlib_helper, check_disallow_instantiation 10 11from _operator import _compare_digest as operator_compare_digest 12 13try: 14 import _hashlib as _hashopenssl 15 from _hashlib import HMAC as C_HMAC 16 from _hashlib import hmac_new as c_hmac_new 17 from _hashlib import compare_digest as openssl_compare_digest 18except ImportError: 19 _hashopenssl = None 20 C_HMAC = None 21 c_hmac_new = None 22 openssl_compare_digest = None 23 24try: 25 import _sha256 as sha256_module 26except ImportError: 27 sha256_module = None 28 29 30def ignore_warning(func): 31 @functools.wraps(func) 32 def wrapper(*args, **kwargs): 33 with warnings.catch_warnings(): 34 warnings.filterwarnings("ignore", 35 category=DeprecationWarning) 36 return func(*args, **kwargs) 37 return wrapper 38 39 40class TestVectorsTestCase(unittest.TestCase): 41 42 def assert_hmac_internals( 43 self, h, digest, hashname, digest_size, block_size 44 ): 45 self.assertEqual(h.hexdigest().upper(), digest.upper()) 46 self.assertEqual(h.digest(), binascii.unhexlify(digest)) 47 self.assertEqual(h.name, f"hmac-{hashname}") 48 self.assertEqual(h.digest_size, digest_size) 49 self.assertEqual(h.block_size, block_size) 50 51 def assert_hmac( 52 self, key, data, digest, hashfunc, hashname, digest_size, block_size 53 ): 54 h = hmac.HMAC(key, data, digestmod=hashfunc) 55 self.assert_hmac_internals( 56 h, digest, hashname, digest_size, block_size 57 ) 58 59 h = hmac.HMAC(key, data, digestmod=hashname) 60 self.assert_hmac_internals( 61 h, digest, hashname, digest_size, block_size 62 ) 63 64 h = hmac.HMAC(key, digestmod=hashname) 65 h2 = h.copy() 66 h2.update(b"test update") 67 h.update(data) 68 self.assertEqual(h.hexdigest().upper(), digest.upper()) 69 70 h = hmac.new(key, data, digestmod=hashname) 71 self.assert_hmac_internals( 72 h, digest, hashname, digest_size, block_size 73 ) 74 75 h = hmac.new(key, None, digestmod=hashname) 76 h.update(data) 77 self.assertEqual(h.hexdigest().upper(), digest.upper()) 78 79 h = hmac.new(key, digestmod=hashname) 80 h.update(data) 81 self.assertEqual(h.hexdigest().upper(), digest.upper()) 82 83 h = hmac.new(key, data, digestmod=hashfunc) 84 self.assertEqual(h.hexdigest().upper(), digest.upper()) 85 86 self.assertEqual( 87 hmac.digest(key, data, digest=hashname), 88 binascii.unhexlify(digest) 89 ) 90 self.assertEqual( 91 hmac.digest(key, data, digest=hashfunc), 92 binascii.unhexlify(digest) 93 ) 94 95 h = hmac.HMAC.__new__(hmac.HMAC) 96 h._init_old(key, data, digestmod=hashname) 97 self.assert_hmac_internals( 98 h, digest, hashname, digest_size, block_size 99 ) 100 101 if c_hmac_new is not None: 102 h = c_hmac_new(key, data, digestmod=hashname) 103 self.assert_hmac_internals( 104 h, digest, hashname, digest_size, block_size 105 ) 106 107 h = c_hmac_new(key, digestmod=hashname) 108 h2 = h.copy() 109 h2.update(b"test update") 110 h.update(data) 111 self.assertEqual(h.hexdigest().upper(), digest.upper()) 112 113 func = getattr(_hashopenssl, f"openssl_{hashname}") 114 h = c_hmac_new(key, data, digestmod=func) 115 self.assert_hmac_internals( 116 h, digest, hashname, digest_size, block_size 117 ) 118 119 h = hmac.HMAC.__new__(hmac.HMAC) 120 h._init_hmac(key, data, digestmod=hashname) 121 self.assert_hmac_internals( 122 h, digest, hashname, digest_size, block_size 123 ) 124 125 @hashlib_helper.requires_hashdigest('md5', openssl=True) 126 def test_md5_vectors(self): 127 # Test the HMAC module against test vectors from the RFC. 128 129 def md5test(key, data, digest): 130 self.assert_hmac( 131 key, data, digest, 132 hashfunc=hashlib.md5, 133 hashname="md5", 134 digest_size=16, 135 block_size=64 136 ) 137 138 md5test(b"\x0b" * 16, 139 b"Hi There", 140 "9294727A3638BB1C13F48EF8158BFC9D") 141 142 md5test(b"Jefe", 143 b"what do ya want for nothing?", 144 "750c783e6ab0b503eaa86e310a5db738") 145 146 md5test(b"\xaa" * 16, 147 b"\xdd" * 50, 148 "56be34521d144c88dbb8c733f0e8b3f6") 149 150 md5test(bytes(range(1, 26)), 151 b"\xcd" * 50, 152 "697eaf0aca3a3aea3a75164746ffaa79") 153 154 md5test(b"\x0C" * 16, 155 b"Test With Truncation", 156 "56461ef2342edc00f9bab995690efd4c") 157 158 md5test(b"\xaa" * 80, 159 b"Test Using Larger Than Block-Size Key - Hash Key First", 160 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") 161 162 md5test(b"\xaa" * 80, 163 (b"Test Using Larger Than Block-Size Key " 164 b"and Larger Than One Block-Size Data"), 165 "6f630fad67cda0ee1fb1f562db3aa53e") 166 167 @hashlib_helper.requires_hashdigest('sha1', openssl=True) 168 def test_sha_vectors(self): 169 def shatest(key, data, digest): 170 self.assert_hmac( 171 key, data, digest, 172 hashfunc=hashlib.sha1, 173 hashname="sha1", 174 digest_size=20, 175 block_size=64 176 ) 177 178 shatest(b"\x0b" * 20, 179 b"Hi There", 180 "b617318655057264e28bc0b6fb378c8ef146be00") 181 182 shatest(b"Jefe", 183 b"what do ya want for nothing?", 184 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") 185 186 shatest(b"\xAA" * 20, 187 b"\xDD" * 50, 188 "125d7342b9ac11cd91a39af48aa17b4f63f175d3") 189 190 shatest(bytes(range(1, 26)), 191 b"\xCD" * 50, 192 "4c9007f4026250c6bc8414f9bf50c86c2d7235da") 193 194 shatest(b"\x0C" * 20, 195 b"Test With Truncation", 196 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") 197 198 shatest(b"\xAA" * 80, 199 b"Test Using Larger Than Block-Size Key - Hash Key First", 200 "aa4ae5e15272d00e95705637ce8a3b55ed402112") 201 202 shatest(b"\xAA" * 80, 203 (b"Test Using Larger Than Block-Size Key " 204 b"and Larger Than One Block-Size Data"), 205 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") 206 207 def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size): 208 def hmactest(key, data, hexdigests): 209 digest = hexdigests[hashfunc] 210 211 self.assert_hmac( 212 key, data, digest, 213 hashfunc=hashfunc, 214 hashname=hash_name, 215 digest_size=digest_size, 216 block_size=block_size 217 ) 218 219 # 4.2. Test Case 1 220 hmactest(key = b'\x0b'*20, 221 data = b'Hi There', 222 hexdigests = { 223 hashlib.sha224: '896fb1128abbdf196832107cd49df33f' 224 '47b4b1169912ba4f53684b22', 225 hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b' 226 '881dc200c9833da726e9376c2e32cff7', 227 hashlib.sha384: 'afd03944d84895626b0825f4ab46907f' 228 '15f9dadbe4101ec682aa034c7cebc59c' 229 'faea9ea9076ede7f4af152e8b2fa9cb6', 230 hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0' 231 '2379f4e2ce4ec2787ad0b30545e17cde' 232 'daa833b7d6b8a702038b274eaea3f4e4' 233 'be9d914eeb61f1702e696c203a126854', 234 }) 235 236 # 4.3. Test Case 2 237 hmactest(key = b'Jefe', 238 data = b'what do ya want for nothing?', 239 hexdigests = { 240 hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f' 241 '8bbea2a39e6148008fd05e44', 242 hashlib.sha256: '5bdcc146bf60754e6a042426089575c7' 243 '5a003f089d2739839dec58b964ec3843', 244 hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b' 245 '9c7ef464f5a01b47e42ec3736322445e' 246 '8e2240ca5e69e2c78b3239ecfab21649', 247 hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3' 248 '87bd64222e831fd610270cd7ea250554' 249 '9758bf75c05a994a6d034f65f8f0e6fd' 250 'caeab1a34d4a6b4b636e070a38bce737', 251 }) 252 253 # 4.4. Test Case 3 254 hmactest(key = b'\xaa'*20, 255 data = b'\xdd'*50, 256 hexdigests = { 257 hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264' 258 '9365b0c1f65d69d1ec8333ea', 259 hashlib.sha256: '773ea91e36800e46854db8ebd09181a7' 260 '2959098b3ef8c122d9635514ced565fe', 261 hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f' 262 '0aa635d947ac9febe83ef4e55966144b' 263 '2a5ab39dc13814b94e3ab6e101a34f27', 264 hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9' 265 'b1b5dbdd8ee81a3655f83e33b2279d39' 266 'bf3e848279a722c806b485a47e67c807' 267 'b946a337bee8942674278859e13292fb', 268 }) 269 270 # 4.5. Test Case 4 271 hmactest(key = bytes(x for x in range(0x01, 0x19+1)), 272 data = b'\xcd'*50, 273 hexdigests = { 274 hashlib.sha224: '6c11506874013cac6a2abc1bb382627c' 275 'ec6a90d86efc012de7afec5a', 276 hashlib.sha256: '82558a389a443c0ea4cc819899f2083a' 277 '85f0faa3e578f8077a2e3ff46729665b', 278 hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7' 279 '7a9981480850009cc5577c6e1f573b4e' 280 '6801dd23c4a7d679ccf8a386c674cffb', 281 hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7' 282 'e576d97ff94b872de76f8050361ee3db' 283 'a91ca5c11aa25eb4d679275cc5788063' 284 'a5f19741120c4f2de2adebeb10a298dd', 285 }) 286 287 # 4.7. Test Case 6 288 hmactest(key = b'\xaa'*131, 289 data = b'Test Using Larger Than Block-Siz' 290 b'e Key - Hash Key First', 291 hexdigests = { 292 hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2' 293 'd499f112f2d2b7273fa6870e', 294 hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f' 295 '8e0bc6213728c5140546040f0ee37f54', 296 hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4' 297 '4f9ef1012a2b588f3cd11f05033ac4c6' 298 '0c2ef6ab4030fe8296248df163f44952', 299 hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4' 300 '9b46d1f41b4aeec1121b013783f8f352' 301 '6b56d037e05f2598bd0fd2215d6a1e52' 302 '95e64f73f63f0aec8b915a985d786598', 303 }) 304 305 # 4.8. Test Case 7 306 hmactest(key = b'\xaa'*131, 307 data = b'This is a test using a larger th' 308 b'an block-size key and a larger t' 309 b'han block-size data. The key nee' 310 b'ds to be hashed before being use' 311 b'd by the HMAC algorithm.', 312 hexdigests = { 313 hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd' 314 '946770db9c2b95c9f6f565d1', 315 hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944' 316 'bfdc63644f0713938a7f51535c3a35e2', 317 hashlib.sha384: '6617178e941f020d351e2f254e8fd32c' 318 '602420feb0b8fb9adccebb82461e99c5' 319 'a678cc31e799176d3860e6110c46523e', 320 hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd' 321 'debd71f8867289865df5a32d20cdc944' 322 'b6022cac3c4982b10d5eeb55c3e4de15' 323 '134676fb6de0446065c97440fa8c6a58', 324 }) 325 326 @hashlib_helper.requires_hashdigest('sha224', openssl=True) 327 def test_sha224_rfc4231(self): 328 self._rfc4231_test_cases(hashlib.sha224, 'sha224', 28, 64) 329 330 @hashlib_helper.requires_hashdigest('sha256', openssl=True) 331 def test_sha256_rfc4231(self): 332 self._rfc4231_test_cases(hashlib.sha256, 'sha256', 32, 64) 333 334 @hashlib_helper.requires_hashdigest('sha384', openssl=True) 335 def test_sha384_rfc4231(self): 336 self._rfc4231_test_cases(hashlib.sha384, 'sha384', 48, 128) 337 338 @hashlib_helper.requires_hashdigest('sha512', openssl=True) 339 def test_sha512_rfc4231(self): 340 self._rfc4231_test_cases(hashlib.sha512, 'sha512', 64, 128) 341 342 @hashlib_helper.requires_hashdigest('sha256') 343 def test_legacy_block_size_warnings(self): 344 class MockCrazyHash(object): 345 """Ain't no block_size attribute here.""" 346 def __init__(self, *args): 347 self._x = hashlib.sha256(*args) 348 self.digest_size = self._x.digest_size 349 def update(self, v): 350 self._x.update(v) 351 def digest(self): 352 return self._x.digest() 353 354 with warnings.catch_warnings(): 355 warnings.simplefilter('error', RuntimeWarning) 356 with self.assertRaises(RuntimeWarning): 357 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) 358 self.fail('Expected warning about missing block_size') 359 360 MockCrazyHash.block_size = 1 361 with self.assertRaises(RuntimeWarning): 362 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) 363 self.fail('Expected warning about small block_size') 364 365 def test_with_digestmod_no_default(self): 366 """The digestmod parameter is required as of Python 3.8.""" 367 with self.assertRaisesRegex(TypeError, r'required.*digestmod'): 368 key = b"\x0b" * 16 369 data = b"Hi There" 370 hmac.HMAC(key, data, digestmod=None) 371 with self.assertRaisesRegex(TypeError, r'required.*digestmod'): 372 hmac.new(key, data) 373 with self.assertRaisesRegex(TypeError, r'required.*digestmod'): 374 hmac.HMAC(key, msg=data, digestmod='') 375 376 377class ConstructorTestCase(unittest.TestCase): 378 379 expected = ( 380 "6c845b47f52b3b47f6590c502db7825aad757bf4fadc8fa972f7cd2e76a5bdeb" 381 ) 382 383 @hashlib_helper.requires_hashdigest('sha256') 384 def test_normal(self): 385 # Standard constructor call. 386 try: 387 hmac.HMAC(b"key", digestmod='sha256') 388 except Exception: 389 self.fail("Standard constructor call raised exception.") 390 391 @hashlib_helper.requires_hashdigest('sha256') 392 def test_with_str_key(self): 393 # Pass a key of type str, which is an error, because it expects a key 394 # of type bytes 395 with self.assertRaises(TypeError): 396 h = hmac.HMAC("key", digestmod='sha256') 397 398 @hashlib_helper.requires_hashdigest('sha256') 399 def test_dot_new_with_str_key(self): 400 # Pass a key of type str, which is an error, because it expects a key 401 # of type bytes 402 with self.assertRaises(TypeError): 403 h = hmac.new("key", digestmod='sha256') 404 405 @hashlib_helper.requires_hashdigest('sha256') 406 def test_withtext(self): 407 # Constructor call with text. 408 try: 409 h = hmac.HMAC(b"key", b"hash this!", digestmod='sha256') 410 except Exception: 411 self.fail("Constructor call with text argument raised exception.") 412 self.assertEqual(h.hexdigest(), self.expected) 413 414 @hashlib_helper.requires_hashdigest('sha256') 415 def test_with_bytearray(self): 416 try: 417 h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!"), 418 digestmod="sha256") 419 except Exception: 420 self.fail("Constructor call with bytearray arguments raised exception.") 421 self.assertEqual(h.hexdigest(), self.expected) 422 423 @hashlib_helper.requires_hashdigest('sha256') 424 def test_with_memoryview_msg(self): 425 try: 426 h = hmac.HMAC(b"key", memoryview(b"hash this!"), digestmod="sha256") 427 except Exception: 428 self.fail("Constructor call with memoryview msg raised exception.") 429 self.assertEqual(h.hexdigest(), self.expected) 430 431 @hashlib_helper.requires_hashdigest('sha256') 432 def test_withmodule(self): 433 # Constructor call with text and digest module. 434 try: 435 h = hmac.HMAC(b"key", b"", hashlib.sha256) 436 except Exception: 437 self.fail("Constructor call with hashlib.sha256 raised exception.") 438 439 @unittest.skipUnless(C_HMAC is not None, 'need _hashlib') 440 def test_internal_types(self): 441 # internal types like _hashlib.C_HMAC are not constructable 442 check_disallow_instantiation(self, C_HMAC) 443 with self.assertRaisesRegex(TypeError, "immutable type"): 444 C_HMAC.value = None 445 446 @unittest.skipUnless(sha256_module is not None, 'need _sha256') 447 def test_with_sha256_module(self): 448 h = hmac.HMAC(b"key", b"hash this!", digestmod=sha256_module.sha256) 449 self.assertEqual(h.hexdigest(), self.expected) 450 self.assertEqual(h.name, "hmac-sha256") 451 452 digest = hmac.digest(b"key", b"hash this!", sha256_module.sha256) 453 self.assertEqual(digest, binascii.unhexlify(self.expected)) 454 455 456class SanityTestCase(unittest.TestCase): 457 458 @hashlib_helper.requires_hashdigest('sha256') 459 def test_exercise_all_methods(self): 460 # Exercising all methods once. 461 # This must not raise any exceptions 462 try: 463 h = hmac.HMAC(b"my secret key", digestmod="sha256") 464 h.update(b"compute the hash of this text!") 465 h.digest() 466 h.hexdigest() 467 h.copy() 468 except Exception: 469 self.fail("Exception raised during normal usage of HMAC class.") 470 471 472class CopyTestCase(unittest.TestCase): 473 474 @hashlib_helper.requires_hashdigest('sha256') 475 def test_attributes_old(self): 476 # Testing if attributes are of same type. 477 h1 = hmac.HMAC.__new__(hmac.HMAC) 478 h1._init_old(b"key", b"msg", digestmod="sha256") 479 h2 = h1.copy() 480 self.assertEqual(type(h1._inner), type(h2._inner), 481 "Types of inner don't match.") 482 self.assertEqual(type(h1._outer), type(h2._outer), 483 "Types of outer don't match.") 484 485 @hashlib_helper.requires_hashdigest('sha256') 486 def test_realcopy_old(self): 487 # Testing if the copy method created a real copy. 488 h1 = hmac.HMAC.__new__(hmac.HMAC) 489 h1._init_old(b"key", b"msg", digestmod="sha256") 490 h2 = h1.copy() 491 # Using id() in case somebody has overridden __eq__/__ne__. 492 self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.") 493 self.assertTrue(id(h1._inner) != id(h2._inner), 494 "No real copy of the attribute 'inner'.") 495 self.assertTrue(id(h1._outer) != id(h2._outer), 496 "No real copy of the attribute 'outer'.") 497 self.assertIs(h1._hmac, None) 498 499 @unittest.skipIf(_hashopenssl is None, "test requires _hashopenssl") 500 @hashlib_helper.requires_hashdigest('sha256') 501 def test_realcopy_hmac(self): 502 h1 = hmac.HMAC.__new__(hmac.HMAC) 503 h1._init_hmac(b"key", b"msg", digestmod="sha256") 504 h2 = h1.copy() 505 self.assertTrue(id(h1._hmac) != id(h2._hmac)) 506 507 @hashlib_helper.requires_hashdigest('sha256') 508 def test_equality(self): 509 # Testing if the copy has the same digests. 510 h1 = hmac.HMAC(b"key", digestmod="sha256") 511 h1.update(b"some random text") 512 h2 = h1.copy() 513 self.assertEqual(h1.digest(), h2.digest(), 514 "Digest of copy doesn't match original digest.") 515 self.assertEqual(h1.hexdigest(), h2.hexdigest(), 516 "Hexdigest of copy doesn't match original hexdigest.") 517 518 @hashlib_helper.requires_hashdigest('sha256') 519 def test_equality_new(self): 520 # Testing if the copy has the same digests with hmac.new(). 521 h1 = hmac.new(b"key", digestmod="sha256") 522 h1.update(b"some random text") 523 h2 = h1.copy() 524 self.assertTrue( 525 id(h1) != id(h2), "No real copy of the HMAC instance." 526 ) 527 self.assertEqual(h1.digest(), h2.digest(), 528 "Digest of copy doesn't match original digest.") 529 self.assertEqual(h1.hexdigest(), h2.hexdigest(), 530 "Hexdigest of copy doesn't match original hexdigest.") 531 532 533class CompareDigestTestCase(unittest.TestCase): 534 535 def test_hmac_compare_digest(self): 536 self._test_compare_digest(hmac.compare_digest) 537 if openssl_compare_digest is not None: 538 self.assertIs(hmac.compare_digest, openssl_compare_digest) 539 else: 540 self.assertIs(hmac.compare_digest, operator_compare_digest) 541 542 def test_operator_compare_digest(self): 543 self._test_compare_digest(operator_compare_digest) 544 545 @unittest.skipIf(openssl_compare_digest is None, "test requires _hashlib") 546 def test_openssl_compare_digest(self): 547 self._test_compare_digest(openssl_compare_digest) 548 549 def _test_compare_digest(self, compare_digest): 550 # Testing input type exception handling 551 a, b = 100, 200 552 self.assertRaises(TypeError, compare_digest, a, b) 553 a, b = 100, b"foobar" 554 self.assertRaises(TypeError, compare_digest, a, b) 555 a, b = b"foobar", 200 556 self.assertRaises(TypeError, compare_digest, a, b) 557 a, b = "foobar", b"foobar" 558 self.assertRaises(TypeError, compare_digest, a, b) 559 a, b = b"foobar", "foobar" 560 self.assertRaises(TypeError, compare_digest, a, b) 561 562 # Testing bytes of different lengths 563 a, b = b"foobar", b"foo" 564 self.assertFalse(compare_digest(a, b)) 565 a, b = b"\xde\xad\xbe\xef", b"\xde\xad" 566 self.assertFalse(compare_digest(a, b)) 567 568 # Testing bytes of same lengths, different values 569 a, b = b"foobar", b"foobaz" 570 self.assertFalse(compare_digest(a, b)) 571 a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea" 572 self.assertFalse(compare_digest(a, b)) 573 574 # Testing bytes of same lengths, same values 575 a, b = b"foobar", b"foobar" 576 self.assertTrue(compare_digest(a, b)) 577 a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef" 578 self.assertTrue(compare_digest(a, b)) 579 580 # Testing bytearrays of same lengths, same values 581 a, b = bytearray(b"foobar"), bytearray(b"foobar") 582 self.assertTrue(compare_digest(a, b)) 583 584 # Testing bytearrays of different lengths 585 a, b = bytearray(b"foobar"), bytearray(b"foo") 586 self.assertFalse(compare_digest(a, b)) 587 588 # Testing bytearrays of same lengths, different values 589 a, b = bytearray(b"foobar"), bytearray(b"foobaz") 590 self.assertFalse(compare_digest(a, b)) 591 592 # Testing byte and bytearray of same lengths, same values 593 a, b = bytearray(b"foobar"), b"foobar" 594 self.assertTrue(compare_digest(a, b)) 595 self.assertTrue(compare_digest(b, a)) 596 597 # Testing byte bytearray of different lengths 598 a, b = bytearray(b"foobar"), b"foo" 599 self.assertFalse(compare_digest(a, b)) 600 self.assertFalse(compare_digest(b, a)) 601 602 # Testing byte and bytearray of same lengths, different values 603 a, b = bytearray(b"foobar"), b"foobaz" 604 self.assertFalse(compare_digest(a, b)) 605 self.assertFalse(compare_digest(b, a)) 606 607 # Testing str of same lengths 608 a, b = "foobar", "foobar" 609 self.assertTrue(compare_digest(a, b)) 610 611 # Testing str of different lengths 612 a, b = "foo", "foobar" 613 self.assertFalse(compare_digest(a, b)) 614 615 # Testing bytes of same lengths, different values 616 a, b = "foobar", "foobaz" 617 self.assertFalse(compare_digest(a, b)) 618 619 # Testing error cases 620 a, b = "foobar", b"foobar" 621 self.assertRaises(TypeError, compare_digest, a, b) 622 a, b = b"foobar", "foobar" 623 self.assertRaises(TypeError, compare_digest, a, b) 624 a, b = b"foobar", 1 625 self.assertRaises(TypeError, compare_digest, a, b) 626 a, b = 100, 200 627 self.assertRaises(TypeError, compare_digest, a, b) 628 a, b = "fooä", "fooä" 629 self.assertRaises(TypeError, compare_digest, a, b) 630 631 # subclasses are supported by ignore __eq__ 632 class mystr(str): 633 def __eq__(self, other): 634 return False 635 636 a, b = mystr("foobar"), mystr("foobar") 637 self.assertTrue(compare_digest(a, b)) 638 a, b = mystr("foobar"), "foobar" 639 self.assertTrue(compare_digest(a, b)) 640 a, b = mystr("foobar"), mystr("foobaz") 641 self.assertFalse(compare_digest(a, b)) 642 643 class mybytes(bytes): 644 def __eq__(self, other): 645 return False 646 647 a, b = mybytes(b"foobar"), mybytes(b"foobar") 648 self.assertTrue(compare_digest(a, b)) 649 a, b = mybytes(b"foobar"), b"foobar" 650 self.assertTrue(compare_digest(a, b)) 651 a, b = mybytes(b"foobar"), mybytes(b"foobaz") 652 self.assertFalse(compare_digest(a, b)) 653 654 655if __name__ == "__main__": 656 unittest.main() 657