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