4 from test
import test_support
6 class TestVectorsTestCase(unittest
.TestCase
):
8 def test_md5_vectors(self
):
9 # Test the HMAC module against test vectors from the RFC.
11 def md5test(key
, data
, digest
):
12 h
= hmac
.HMAC(key
, data
)
13 self
.assertEqual(h
.hexdigest().upper(), digest
.upper())
15 md5test(chr(0x0b) * 16,
17 "9294727A3638BB1C13F48EF8158BFC9D")
20 "what do ya want for nothing?",
21 "750c783e6ab0b503eaa86e310a5db738")
25 "56be34521d144c88dbb8c733f0e8b3f6")
27 md5test("".join([chr(i
) for i
in range(1, 26)]),
29 "697eaf0aca3a3aea3a75164746ffaa79")
31 md5test(chr(0x0C) * 16,
32 "Test With Truncation",
33 "56461ef2342edc00f9bab995690efd4c")
35 md5test(chr(0xAA) * 80,
36 "Test Using Larger Than Block-Size Key - Hash Key First",
37 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
39 md5test(chr(0xAA) * 80,
40 ("Test Using Larger Than Block-Size Key "
41 "and Larger Than One Block-Size Data"),
42 "6f630fad67cda0ee1fb1f562db3aa53e")
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())
49 shatest(chr(0x0b) * 20,
51 "b617318655057264e28bc0b6fb378c8ef146be00")
54 "what do ya want for nothing?",
55 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
59 "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
61 shatest("".join([chr(i
) for i
in range(1, 26)]),
63 "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
65 shatest(chr(0x0C) * 20,
66 "Test With Truncation",
67 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
69 shatest(chr(0xAA) * 80,
70 "Test Using Larger Than Block-Size Key - Hash Key First",
71 "aa4ae5e15272d00e95705637ce8a3b55ed402112")
73 shatest(chr(0xAA) * 80,
74 ("Test Using Larger Than Block-Size Key "
75 "and Larger Than One Block-Size Data"),
76 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
79 class ConstructorTestCase(unittest
.TestCase
):
81 def test_normal(self
):
82 # Standard constructor call.
87 self
.fail("Standard constructor call raised exception.")
89 def test_withtext(self
):
90 # Constructor call with text.
92 h
= hmac
.HMAC("key", "hash this!")
94 self
.fail("Constructor call with text argument raised exception.")
96 def test_withmodule(self
):
97 # Constructor call with text and digest module.
100 h
= hmac
.HMAC("key", "", sha
)
102 self
.fail("Constructor call with sha module raised exception.")
104 class SanityTestCase(unittest
.TestCase
):
106 def test_default_is_md5(self
):
107 # Testing if HMAC defaults to MD5 algorithm.
110 self
.failUnless(h
.digestmod
== md5
)
112 def test_exercise_all_methods(self
):
113 # Exercising all methods once.
114 # This must not raise any exceptions
116 h
= hmac
.HMAC("my secret key")
117 h
.update("compute the hash of this text!")
122 self
.fail("Exception raised during normal usage of HMAC class.")
124 class CopyTestCase(unittest
.TestCase
):
126 def test_attributes(self
):
127 # Testing if attributes are of same type.
128 h1
= hmac
.HMAC("key")
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.")
137 def test_realcopy(self
):
138 # Testing if the copy method created a real copy.
139 h1
= hmac
.HMAC("key")
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'.")
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")
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.")
159 test_support
.run_unittest(
166 if __name__
== "__main__":