diff -r f6199284ef3a Lib/test/test_hmac.py --- a/Lib/test/test_hmac.py Wed Mar 14 00:40:57 2012 +0100 +++ b/Lib/test/test_hmac.py Tue Mar 13 20:35:43 2012 -0700 @@ -248,6 +248,44 @@ 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) + assert len(w) == 1 + assert "No block_size" in 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) + assert len(w) == 1 + assert "block_size of 15 seems too small" in 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 +294,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