Update version number and release date.
[python/dscho.git] / Lib / test / test_hmac.py
blobf36db4ea6367c51894659d3da076b4f4f4fbb300
1 import hmac
2 import unittest
3 from test import test_support
5 class TestVectorsTestCase(unittest.TestCase):
7 def test_vectors(self):
8 # Test the HMAC module against test vectors from the RFC.
10 def md5test(key, data, digest):
11 h = hmac.HMAC(key, data)
12 self.failUnless(h.hexdigest().upper() == digest.upper())
14 md5test(chr(0x0b) * 16,
15 "Hi There",
16 "9294727A3638BB1C13F48EF8158BFC9D")
18 md5test("Jefe",
19 "what do ya want for nothing?",
20 "750c783e6ab0b503eaa86e310a5db738")
22 md5test(chr(0xAA)*16,
23 chr(0xDD)*50,
24 "56be34521d144c88dbb8c733f0e8b3f6")
26 class ConstructorTestCase(unittest.TestCase):
28 def test_normal(self):
29 # Standard constructor call.
30 failed = 0
31 try:
32 h = hmac.HMAC("key")
33 except:
34 self.fail("Standard constructor call raised exception.")
36 def test_withtext(self):
37 # Constructor call with text.
38 try:
39 h = hmac.HMAC("key", "hash this!")
40 except:
41 self.fail("Constructor call with text argument raised exception.")
43 def test_withmodule(self):
44 # Constructor call with text and digest module.
45 import sha
46 try:
47 h = hmac.HMAC("key", "", sha)
48 except:
49 self.fail("Constructor call with sha module raised exception.")
51 class SanityTestCase(unittest.TestCase):
53 def test_default_is_md5(self):
54 # Testing if HMAC defaults to MD5 algorithm.
55 import md5
56 h = hmac.HMAC("key")
57 self.failUnless(h.digestmod == md5)
59 def test_exercise_all_methods(self):
60 # Exercising all methods once.
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:
69 self.fail("Exception raised during normal usage of HMAC class.")
71 class CopyTestCase(unittest.TestCase):
73 def test_attributes(self):
74 # Testing if attributes are of same type.
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.")
84 def test_realcopy(self):
85 # Testing if the copy method created a real copy.
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'.")
95 def test_equality(self):
96 # Testing if the copy has the same digests.
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.")
105 def test_main():
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)
113 if __name__ == "__main__":
114 test_main()