Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / lib / python2.4 / test / test_hmac.py
CommitLineData
920dae64
AT
1import hmac
2import sha
3import unittest
4from test import test_support
5
6class TestVectorsTestCase(unittest.TestCase):
7
8 def test_md5_vectors(self):
9 # Test the HMAC module against test vectors from the RFC.
10
11 def md5test(key, data, digest):
12 h = hmac.HMAC(key, data)
13 self.assertEqual(h.hexdigest().upper(), digest.upper())
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
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
79class ConstructorTestCase(unittest.TestCase):
80
81 def test_normal(self):
82 # Standard constructor call.
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):
90 # Constructor call with text.
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):
97 # Constructor call with text and digest module.
98 import sha
99 try:
100 h = hmac.HMAC("key", "", sha)
101 except:
102 self.fail("Constructor call with sha module raised exception.")
103
104class SanityTestCase(unittest.TestCase):
105
106 def test_default_is_md5(self):
107 # Testing if HMAC defaults to MD5 algorithm.
108 import md5
109 h = hmac.HMAC("key")
110 self.failUnless(h.digestmod == md5)
111
112 def test_exercise_all_methods(self):
113 # Exercising all methods once.
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:
122 self.fail("Exception raised during normal usage of HMAC class.")
123
124class CopyTestCase(unittest.TestCase):
125
126 def test_attributes(self):
127 # Testing if attributes are of same type.
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):
138 # Testing if the copy method created a real copy.
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):
149 # Testing if the copy has the same digests.
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
158def test_main():
159 test_support.run_unittest(
160 TestVectorsTestCase,
161 ConstructorTestCase,
162 SanityTestCase,
163 CopyTestCase
164 )
165
166if __name__ == "__main__":
167 test_main()