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