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,
16 "9294727A3638BB1C13F48EF8158BFC9D")
19 "what do ya want for nothing?",
20 "750c783e6ab0b503eaa86e310a5db738")
24 "56be34521d144c88dbb8c733f0e8b3f6")
26 class ConstructorTestCase(unittest
.TestCase
):
28 def test_normal(self
):
29 # Standard constructor call.
34 self
.fail("Standard constructor call raised exception.")
36 def test_withtext(self
):
37 # Constructor call with text.
39 h
= hmac
.HMAC("key", "hash this!")
41 self
.fail("Constructor call with text argument raised exception.")
43 def test_withmodule(self
):
44 # Constructor call with text and digest module.
47 h
= hmac
.HMAC("key", "", sha
)
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.
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
63 h
= hmac
.HMAC("my secret key")
64 h
.update("compute the hash of this text!")
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.
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.
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.
98 h1
.update("some random text")
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.")
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__":