PyXR

c:\python24\lib \ test \ test_hmac.py



0001 import hmac
0002 import sha
0003 import unittest
0004 from test import test_support
0005 
0006 class TestVectorsTestCase(unittest.TestCase):
0007 
0008     def test_md5_vectors(self):
0009         # Test the HMAC module against test vectors from the RFC.
0010 
0011         def md5test(key, data, digest):
0012             h = hmac.HMAC(key, data)
0013             self.assertEqual(h.hexdigest().upper(), digest.upper())
0014 
0015         md5test(chr(0x0b) * 16,
0016                 "Hi There",
0017                 "9294727A3638BB1C13F48EF8158BFC9D")
0018 
0019         md5test("Jefe",
0020                 "what do ya want for nothing?",
0021                 "750c783e6ab0b503eaa86e310a5db738")
0022 
0023         md5test(chr(0xAA)*16,
0024                 chr(0xDD)*50,
0025                 "56be34521d144c88dbb8c733f0e8b3f6")
0026 
0027         md5test("".join([chr(i) for i in range(1, 26)]),
0028                 chr(0xCD) * 50,
0029                 "697eaf0aca3a3aea3a75164746ffaa79")
0030 
0031         md5test(chr(0x0C) * 16,
0032                 "Test With Truncation",
0033                 "56461ef2342edc00f9bab995690efd4c")
0034 
0035         md5test(chr(0xAA) * 80,
0036                 "Test Using Larger Than Block-Size Key - Hash Key First",
0037                 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
0038 
0039         md5test(chr(0xAA) * 80,
0040                 ("Test Using Larger Than Block-Size Key "
0041                  "and Larger Than One Block-Size Data"),
0042                 "6f630fad67cda0ee1fb1f562db3aa53e")
0043 
0044     def test_sha_vectors(self):
0045         def shatest(key, data, digest):
0046             h = hmac.HMAC(key, data, digestmod=sha)
0047             self.assertEqual(h.hexdigest().upper(), digest.upper())
0048 
0049         shatest(chr(0x0b) * 20,
0050                 "Hi There",
0051                 "b617318655057264e28bc0b6fb378c8ef146be00")
0052 
0053         shatest("Jefe",
0054                 "what do ya want for nothing?",
0055                 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
0056 
0057         shatest(chr(0xAA)*20,
0058                 chr(0xDD)*50,
0059                 "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
0060 
0061         shatest("".join([chr(i) for i in range(1, 26)]),
0062                 chr(0xCD) * 50,
0063                 "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
0064 
0065         shatest(chr(0x0C) * 20,
0066                 "Test With Truncation",
0067                 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
0068 
0069         shatest(chr(0xAA) * 80,
0070                 "Test Using Larger Than Block-Size Key - Hash Key First",
0071                 "aa4ae5e15272d00e95705637ce8a3b55ed402112")
0072 
0073         shatest(chr(0xAA) * 80,
0074                 ("Test Using Larger Than Block-Size Key "
0075                  "and Larger Than One Block-Size Data"),
0076                 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
0077 
0078 
0079 class ConstructorTestCase(unittest.TestCase):
0080 
0081     def test_normal(self):
0082         # Standard constructor call.
0083         failed = 0
0084         try:
0085             h = hmac.HMAC("key")
0086         except:
0087             self.fail("Standard constructor call raised exception.")
0088 
0089     def test_withtext(self):
0090         # Constructor call with text.
0091         try:
0092             h = hmac.HMAC("key", "hash this!")
0093         except:
0094             self.fail("Constructor call with text argument raised exception.")
0095 
0096     def test_withmodule(self):
0097         # Constructor call with text and digest module.
0098         import sha
0099         try:
0100             h = hmac.HMAC("key", "", sha)
0101         except:
0102             self.fail("Constructor call with sha module raised exception.")
0103 
0104 class SanityTestCase(unittest.TestCase):
0105 
0106     def test_default_is_md5(self):
0107         # Testing if HMAC defaults to MD5 algorithm.
0108         import md5
0109         h = hmac.HMAC("key")
0110         self.failUnless(h.digestmod == md5)
0111 
0112     def test_exercise_all_methods(self):
0113         # Exercising all methods once.
0114         # This must not raise any exceptions
0115         try:
0116             h = hmac.HMAC("my secret key")
0117             h.update("compute the hash of this text!")
0118             dig = h.digest()
0119             dig = h.hexdigest()
0120             h2 = h.copy()
0121         except:
0122             self.fail("Exception raised during normal usage of HMAC class.")
0123 
0124 class CopyTestCase(unittest.TestCase):
0125 
0126     def test_attributes(self):
0127         # Testing if attributes are of same type.
0128         h1 = hmac.HMAC("key")
0129         h2 = h1.copy()
0130         self.failUnless(h1.digestmod == h2.digestmod,
0131             "Modules don't match.")
0132         self.failUnless(type(h1.inner) == type(h2.inner),
0133             "Types of inner don't match.")
0134         self.failUnless(type(h1.outer) == type(h2.outer),
0135             "Types of outer don't match.")
0136 
0137     def test_realcopy(self):
0138         # Testing if the copy method created a real copy.
0139         h1 = hmac.HMAC("key")
0140         h2 = h1.copy()
0141         # Using id() in case somebody has overridden __cmp__.
0142         self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.")
0143         self.failUnless(id(h1.inner) != id(h2.inner),
0144             "No real copy of the attribute 'inner'.")
0145         self.failUnless(id(h1.outer) != id(h2.outer),
0146             "No real copy of the attribute 'outer'.")
0147 
0148     def test_equality(self):
0149         # Testing if the copy has the same digests.
0150         h1 = hmac.HMAC("key")
0151         h1.update("some random text")
0152         h2 = h1.copy()
0153         self.failUnless(h1.digest() == h2.digest(),
0154             "Digest of copy doesn't match original digest.")
0155         self.failUnless(h1.hexdigest() == h2.hexdigest(),
0156             "Hexdigest of copy doesn't match original hexdigest.")
0157 
0158 def test_main():
0159     test_support.run_unittest(
0160         TestVectorsTestCase,
0161         ConstructorTestCase,
0162         SanityTestCase,
0163         CopyTestCase
0164     )
0165 
0166 if __name__ == "__main__":
0167     test_main()
0168 

Generated by PyXR 0.9.4
SourceForge.net Logo