Guido van Rossum | f166994 | 2001-09-11 15:54:16 +0000 | [diff] [blame] | 1 | import hmac |
Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 2 | import sha |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 3 | import unittest |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 4 | from test import test_support |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 5 | |
| 6 | class TestVectorsTestCase(unittest.TestCase): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 7 | |
Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 8 | def test_md5_vectors(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 9 | # Test the HMAC module against test vectors from the RFC. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 10 | |
| 11 | def md5test(key, data, digest): |
| 12 | h = hmac.HMAC(key, data) |
Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 13 | self.assertEqual(h.hexdigest().upper(), digest.upper()) |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 14 | |
| 15 | md5test(chr(0x0b) * 16, |
| 16 | "Hi There", |
| 17 | "9294727A3638BB1C13F48EF8158BFC9D") |
| 18 | |
| 19 | md5test("Jefe", |
| 20 | "what do ya want for nothing?", |
| 21 | "750c783e6ab0b503eaa86e310a5db738") |
| 22 | |
| 23 | md5test(chr(0xAA)*16, |
| 24 | chr(0xDD)*50, |
| 25 | "56be34521d144c88dbb8c733f0e8b3f6") |
| 26 | |
Jeremy Hylton | 893801e | 2003-05-27 16:16:41 +0000 | [diff] [blame] | 27 | md5test("".join([chr(i) for i in range(1, 26)]), |
| 28 | chr(0xCD) * 50, |
| 29 | "697eaf0aca3a3aea3a75164746ffaa79") |
| 30 | |
| 31 | md5test(chr(0x0C) * 16, |
| 32 | "Test With Truncation", |
| 33 | "56461ef2342edc00f9bab995690efd4c") |
| 34 | |
| 35 | md5test(chr(0xAA) * 80, |
| 36 | "Test Using Larger Than Block-Size Key - Hash Key First", |
| 37 | "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") |
| 38 | |
| 39 | md5test(chr(0xAA) * 80, |
| 40 | ("Test Using Larger Than Block-Size Key " |
| 41 | "and Larger Than One Block-Size Data"), |
| 42 | "6f630fad67cda0ee1fb1f562db3aa53e") |
| 43 | |
| 44 | def test_sha_vectors(self): |
| 45 | def shatest(key, data, digest): |
| 46 | h = hmac.HMAC(key, data, digestmod=sha) |
| 47 | self.assertEqual(h.hexdigest().upper(), digest.upper()) |
| 48 | |
| 49 | shatest(chr(0x0b) * 20, |
| 50 | "Hi There", |
| 51 | "b617318655057264e28bc0b6fb378c8ef146be00") |
| 52 | |
| 53 | shatest("Jefe", |
| 54 | "what do ya want for nothing?", |
| 55 | "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") |
| 56 | |
| 57 | shatest(chr(0xAA)*20, |
| 58 | chr(0xDD)*50, |
| 59 | "125d7342b9ac11cd91a39af48aa17b4f63f175d3") |
| 60 | |
| 61 | shatest("".join([chr(i) for i in range(1, 26)]), |
| 62 | chr(0xCD) * 50, |
| 63 | "4c9007f4026250c6bc8414f9bf50c86c2d7235da") |
| 64 | |
| 65 | shatest(chr(0x0C) * 20, |
| 66 | "Test With Truncation", |
| 67 | "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") |
| 68 | |
| 69 | shatest(chr(0xAA) * 80, |
| 70 | "Test Using Larger Than Block-Size Key - Hash Key First", |
| 71 | "aa4ae5e15272d00e95705637ce8a3b55ed402112") |
| 72 | |
| 73 | shatest(chr(0xAA) * 80, |
| 74 | ("Test Using Larger Than Block-Size Key " |
| 75 | "and Larger Than One Block-Size Data"), |
| 76 | "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") |
| 77 | |
| 78 | |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 79 | class ConstructorTestCase(unittest.TestCase): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 80 | |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 81 | def test_normal(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 82 | # Standard constructor call. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 83 | failed = 0 |
| 84 | try: |
| 85 | h = hmac.HMAC("key") |
| 86 | except: |
| 87 | self.fail("Standard constructor call raised exception.") |
| 88 | |
| 89 | def test_withtext(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 90 | # Constructor call with text. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 91 | try: |
| 92 | h = hmac.HMAC("key", "hash this!") |
| 93 | except: |
| 94 | self.fail("Constructor call with text argument raised exception.") |
| 95 | |
| 96 | def test_withmodule(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 97 | # Constructor call with text and digest module. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 98 | import sha |
| 99 | try: |
| 100 | h = hmac.HMAC("key", "", sha) |
| 101 | except: |
| 102 | self.fail("Constructor call with sha module raised exception.") |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 103 | |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 104 | class SanityTestCase(unittest.TestCase): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 105 | |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 106 | def test_default_is_md5(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 107 | # Testing if HMAC defaults to MD5 algorithm. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 108 | import md5 |
| 109 | h = hmac.HMAC("key") |
| 110 | self.failUnless(h.digestmod == md5) |
| 111 | |
| 112 | def test_exercise_all_methods(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 113 | # Exercising all methods once. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 114 | # This must not raise any exceptions |
| 115 | try: |
| 116 | h = hmac.HMAC("my secret key") |
| 117 | h.update("compute the hash of this text!") |
| 118 | dig = h.digest() |
| 119 | dig = h.hexdigest() |
| 120 | h2 = h.copy() |
| 121 | except: |
Neal Norwitz | 28bb572 | 2002-04-01 19:00:50 +0000 | [diff] [blame] | 122 | self.fail("Exception raised during normal usage of HMAC class.") |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 123 | |
| 124 | class CopyTestCase(unittest.TestCase): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 125 | |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 126 | def test_attributes(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 127 | # Testing if attributes are of same type. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 128 | h1 = hmac.HMAC("key") |
| 129 | h2 = h1.copy() |
| 130 | self.failUnless(h1.digestmod == h2.digestmod, |
| 131 | "Modules don't match.") |
| 132 | self.failUnless(type(h1.inner) == type(h2.inner), |
| 133 | "Types of inner don't match.") |
| 134 | self.failUnless(type(h1.outer) == type(h2.outer), |
| 135 | "Types of outer don't match.") |
| 136 | |
| 137 | def test_realcopy(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 138 | # Testing if the copy method created a real copy. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 139 | h1 = hmac.HMAC("key") |
| 140 | h2 = h1.copy() |
| 141 | # Using id() in case somebody has overridden __cmp__. |
| 142 | self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.") |
| 143 | self.failUnless(id(h1.inner) != id(h2.inner), |
| 144 | "No real copy of the attribute 'inner'.") |
| 145 | self.failUnless(id(h1.outer) != id(h2.outer), |
| 146 | "No real copy of the attribute 'outer'.") |
| 147 | |
| 148 | def test_equality(self): |
Guido van Rossum | 7e8fdba | 2002-08-22 19:38:14 +0000 | [diff] [blame] | 149 | # Testing if the copy has the same digests. |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 150 | h1 = hmac.HMAC("key") |
| 151 | h1.update("some random text") |
| 152 | h2 = h1.copy() |
| 153 | self.failUnless(h1.digest() == h2.digest(), |
| 154 | "Digest of copy doesn't match original digest.") |
| 155 | self.failUnless(h1.hexdigest() == h2.hexdigest(), |
| 156 | "Hexdigest of copy doesn't match original hexdigest.") |
| 157 | |
| 158 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 159 | test_support.run_unittest( |
| 160 | TestVectorsTestCase, |
| 161 | ConstructorTestCase, |
| 162 | SanityTestCase, |
| 163 | CopyTestCase |
| 164 | ) |
Andrew M. Kuchling | f792bba | 2001-11-02 21:49:59 +0000 | [diff] [blame] | 165 | |
| 166 | if __name__ == "__main__": |
| 167 | test_main() |