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