diff -r c05d4d1f21d4 Lib/test/test_hmac.py --- a/Lib/test/test_hmac.py Fri Mar 16 21:08:47 2012 -0700 +++ b/Lib/test/test_hmac.py Fri Mar 16 21:13:35 2012 -0700 @@ -248,6 +248,45 @@ except: self.fail("Constructor call with hashlib.sha1 raised exception.") + def test_withnoncallable_digestmod(self): + try: + # Use hmac itself as the digestmod since it conforms to PEP 247 + h = hmac.HMAC(b"key", b"", hmac) + except: + self.fail("Constructor call with digestmod=hmac raised exception.") + + def test_no_block_size(self): + # hmac follows PEP 247 but doesn't have a block_size attribute + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + h = hmac.HMAC(b"key", b"", hmac) + self.assertEqual(1, len(w)) + self.assertIn("No block_size", str(w[-1].message)) + + def test_small_block_size(self): + # hmac follows PEP 247 but doesn't have a block_size attribute, + # so we need to mock its new() method to fake one for testing. + old_new = hmac.new + def mock_new(d=""): + h = old_new(d) + h.block_size = 15 + return h + hmac.new = mock_new + + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + h = hmac.HMAC(b"key", b"", hmac) + self.assertEqual(1, len(w)) + self.assertIn( + "block_size of 15 seems too small", str(w[-1].message)) + + hmac.new = old_new + + def test_with_invalid_msg(self): + # Constructor call with non-bytes message + with self.assertRaises(TypeError): + h = hmac.HMAC(u"invalidkey") + class SanityTestCase(unittest.TestCase): def test_default_is_md5(self): @@ -256,6 +295,12 @@ h = hmac.HMAC(b"key") self.assertEqual(h.digest_cons, hashlib.md5) + def test_update_error_handling(self): + # Test update method with non-bytes message + h = hmac.HMAC(b"key") + with self.assertRaises(TypeError): + h.update(u"invalidmsg") + def test_exercise_all_methods(self): # Exercising all methods once. # This must not raise any exceptions