test_hmac.py revision f792bba98f081b1d05b32ce3a9085ef135b75b96
1f166994b8346708a867df823def3ab12e02d43e9Guido van Rossumimport hmac 2f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingimport unittest 3f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingimport test_support 4f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 5f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass TestVectorsTestCase(unittest.TestCase): 6f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_vectors(self): 7f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Test the HMAC module against test vectors from the RFC.""" 8f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 9f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def md5test(key, data, digest): 10f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC(key, data) 11f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(h.hexdigest().upper() == digest.upper()) 12f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 13f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling md5test(chr(0x0b) * 16, 14f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Hi There", 15f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "9294727A3638BB1C13F48EF8158BFC9D") 16f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 17f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling md5test("Jefe", 18f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "what do ya want for nothing?", 19f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "750c783e6ab0b503eaa86e310a5db738") 20f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 21f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling md5test(chr(0xAA)*16, 22f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling chr(0xDD)*50, 23f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "56be34521d144c88dbb8c733f0e8b3f6") 24f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 25f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass ConstructorTestCase(unittest.TestCase): 26f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_normal(self): 27f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Standard constructor call.""" 28f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling failed = 0 29f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling try: 30f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("key") 31f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling except: 32f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.fail("Standard constructor call raised exception.") 33f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 34f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_withtext(self): 35f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Constructor call with text.""" 36f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling try: 37f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("key", "hash this!") 38f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling except: 39f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.fail("Constructor call with text argument raised exception.") 40f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 41f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_withmodule(self): 42f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Constructor call with text and digest module.""" 43f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling import sha 44f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling try: 45f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("key", "", sha) 46f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling except: 47f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.fail("Constructor call with sha module raised exception.") 48f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 49f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass SanityTestCase(unittest.TestCase): 50f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_default_is_md5(self): 51f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Testing if HMAC defaults to MD5 algorithm.""" 52f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling import md5 53f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("key") 54f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(h.digestmod == md5) 55f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 56f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_exercise_all_methods(self): 57f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Exercising all methods once.""" 58f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling # This must not raise any exceptions 59f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling try: 60f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h = hmac.HMAC("my secret key") 61f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h.update("compute the hash of this text!") 62f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling dig = h.digest() 63f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling dig = h.hexdigest() 64f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h2 = h.copy() 65f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling except: 66f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling fail("Exception raised during normal usage of HMAC class.") 67f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 68f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass CopyTestCase(unittest.TestCase): 69f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_attributes(self): 70f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Testing if attributes are of same type.""" 71f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h1 = hmac.HMAC("key") 72f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h2 = h1.copy() 73f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(h1.digestmod == h2.digestmod, 74f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Modules don't match.") 75f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(type(h1.inner) == type(h2.inner), 76f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Types of inner don't match.") 77f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(type(h1.outer) == type(h2.outer), 78f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Types of outer don't match.") 79f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 80f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_realcopy(self): 81f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Testing if the copy method created a real copy.""" 82f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h1 = hmac.HMAC("key") 83f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h2 = h1.copy() 84f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling # Using id() in case somebody has overridden __cmp__. 85f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.") 86f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(id(h1.inner) != id(h2.inner), 87f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "No real copy of the attribute 'inner'.") 88f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(id(h1.outer) != id(h2.outer), 89f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "No real copy of the attribute 'outer'.") 90f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 91f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling def test_equality(self): 92f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling """Testing if the copy has the same digests.""" 93f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h1 = hmac.HMAC("key") 94f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h1.update("some random text") 95f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling h2 = h1.copy() 96f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(h1.digest() == h2.digest(), 97f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Digest of copy doesn't match original digest.") 98f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling self.failUnless(h1.hexdigest() == h2.hexdigest(), 99f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling "Hexdigest of copy doesn't match original hexdigest.") 100f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 101f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingdef test_main(): 102f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling test_support.run_unittest(TestVectorsTestCase) 103f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling test_support.run_unittest(ConstructorTestCase) 104f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling test_support.run_unittest(SanityTestCase) 105f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling test_support.run_unittest(CopyTestCase) 106f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 107f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingif __name__ == "__main__": 108f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling test_main() 109f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling 110