test_hmac.py 5.49 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1
import hmac
2
from hashlib import sha1
3
import unittest
4
from test import test_support
5 6

class TestVectorsTestCase(unittest.TestCase):
7

Jeremy Hylton's avatar
Jeremy Hylton committed
8
    def test_md5_vectors(self):
9
        # Test the HMAC module against test vectors from the RFC.
10 11 12

        def md5test(key, data, digest):
            h = hmac.HMAC(key, data)
Jeremy Hylton's avatar
Jeremy Hylton committed
13
            self.assertEqual(h.hexdigest().upper(), digest.upper())
14

15 16
        md5test(b"\x0b" * 16,
                b"Hi There",
17 18
                "9294727A3638BB1C13F48EF8158BFC9D")

19 20
        md5test(b"Jefe",
                b"what do ya want for nothing?",
21 22
                "750c783e6ab0b503eaa86e310a5db738")

23 24
        md5test(b"\xaa" * 16,
                b"\xdd" * 50,
25 26
                "56be34521d144c88dbb8c733f0e8b3f6")

27
        md5test(bytes(range(1, 26)),
28
                b"\xcd" * 50,
Jeremy Hylton's avatar
Jeremy Hylton committed
29 30
                "697eaf0aca3a3aea3a75164746ffaa79")

31 32
        md5test(b"\x0C" * 16,
                b"Test With Truncation",
Jeremy Hylton's avatar
Jeremy Hylton committed
33 34
                "56461ef2342edc00f9bab995690efd4c")

35
        md5test(b"\xaa" * 80,
36
                b"Test Using Larger Than Block-Size Key - Hash Key First",
Jeremy Hylton's avatar
Jeremy Hylton committed
37 38
                "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")

39
        md5test(b"\xaa" * 80,
40 41
                (b"Test Using Larger Than Block-Size Key "
                 b"and Larger Than One Block-Size Data"),
Jeremy Hylton's avatar
Jeremy Hylton committed
42 43 44 45
                "6f630fad67cda0ee1fb1f562db3aa53e")

    def test_sha_vectors(self):
        def shatest(key, data, digest):
46
            h = hmac.HMAC(key, data, digestmod=sha1)
Jeremy Hylton's avatar
Jeremy Hylton committed
47 48
            self.assertEqual(h.hexdigest().upper(), digest.upper())

49 50
        shatest(b"\x0b" * 20,
                b"Hi There",
Jeremy Hylton's avatar
Jeremy Hylton committed
51 52
                "b617318655057264e28bc0b6fb378c8ef146be00")

53 54
        shatest(b"Jefe",
                b"what do ya want for nothing?",
Jeremy Hylton's avatar
Jeremy Hylton committed
55 56
                "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")

57 58
        shatest(b"\xAA" * 20,
                b"\xDD" * 50,
Jeremy Hylton's avatar
Jeremy Hylton committed
59 60
                "125d7342b9ac11cd91a39af48aa17b4f63f175d3")

61 62
        shatest(bytes(range(1, 26)),
                b"\xCD" * 50,
Jeremy Hylton's avatar
Jeremy Hylton committed
63 64
                "4c9007f4026250c6bc8414f9bf50c86c2d7235da")

65 66
        shatest(b"\x0C" * 20,
                b"Test With Truncation",
Jeremy Hylton's avatar
Jeremy Hylton committed
67 68
                "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")

69 70
        shatest(b"\xAA" * 80,
                b"Test Using Larger Than Block-Size Key - Hash Key First",
Jeremy Hylton's avatar
Jeremy Hylton committed
71 72
                "aa4ae5e15272d00e95705637ce8a3b55ed402112")

73 74 75
        shatest(b"\xAA" * 80,
                (b"Test Using Larger Than Block-Size Key "
                 b"and Larger Than One Block-Size Data"),
Jeremy Hylton's avatar
Jeremy Hylton committed
76 77 78
                "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")


79
class ConstructorTestCase(unittest.TestCase):
80

81
    def test_normal(self):
82
        # Standard constructor call.
83 84
        failed = 0
        try:
85
            h = hmac.HMAC(b"key")
86 87 88 89
        except:
            self.fail("Standard constructor call raised exception.")

    def test_withtext(self):
90
        # Constructor call with text.
91
        try:
92
            h = hmac.HMAC(b"key", b"hash this!")
93 94 95 96
        except:
            self.fail("Constructor call with text argument raised exception.")

    def test_withmodule(self):
97
        # Constructor call with text and digest module.
98
        from hashlib import sha1
99
        try:
100
            h = hmac.HMAC(b"key", b"", sha1)
101
        except:
102
            self.fail("Constructor call with hashlib.sha1 raised exception.")
Tim Peters's avatar
Tim Peters committed
103

104
class SanityTestCase(unittest.TestCase):
105

106
    def test_default_is_md5(self):
107
        # Testing if HMAC defaults to MD5 algorithm.
Gregory P. Smith's avatar
Gregory P. Smith committed
108 109
        # NOTE: this whitebox test depends on the hmac class internals
        import hashlib
110
        h = hmac.HMAC(b"key")
111
        self.assertEqual(h.digest_cons, hashlib.md5)
112 113

    def test_exercise_all_methods(self):
114
        # Exercising all methods once.
115 116
        # This must not raise any exceptions
        try:
117 118
            h = hmac.HMAC(b"my secret key")
            h.update(b"compute the hash of this text!")
119 120 121 122
            dig = h.digest()
            dig = h.hexdigest()
            h2 = h.copy()
        except:
Neal Norwitz's avatar
Neal Norwitz committed
123
            self.fail("Exception raised during normal usage of HMAC class.")
124 125

class CopyTestCase(unittest.TestCase):
126

127
    def test_attributes(self):
128
        # Testing if attributes are of same type.
129
        h1 = hmac.HMAC(b"key")
130
        h2 = h1.copy()
Gregory P. Smith's avatar
Gregory P. Smith committed
131 132
        self.failUnless(h1.digest_cons == h2.digest_cons,
            "digest constructors don't match.")
133
        self.assertEqual(type(h1.inner), type(h2.inner),
134
            "Types of inner don't match.")
135
        self.assertEqual(type(h1.outer), type(h2.outer),
136 137 138
            "Types of outer don't match.")

    def test_realcopy(self):
139
        # Testing if the copy method created a real copy.
140
        h1 = hmac.HMAC(b"key")
141 142 143 144 145 146 147 148 149
        h2 = h1.copy()
        # Using id() in case somebody has overridden __cmp__.
        self.failUnless(id(h1) != id(h2), "No real copy of the HMAC instance.")
        self.failUnless(id(h1.inner) != id(h2.inner),
            "No real copy of the attribute 'inner'.")
        self.failUnless(id(h1.outer) != id(h2.outer),
            "No real copy of the attribute 'outer'.")

    def test_equality(self):
150
        # Testing if the copy has the same digests.
151 152
        h1 = hmac.HMAC(b"key")
        h1.update(b"some random text")
153
        h2 = h1.copy()
154
        self.assertEqual(h1.digest(), h2.digest(),
155
            "Digest of copy doesn't match original digest.")
156
        self.assertEqual(h1.hexdigest(), h2.hexdigest(),
157 158 159
            "Hexdigest of copy doesn't match original hexdigest.")

def test_main():
160 161 162 163 164 165
    test_support.run_unittest(
        TestVectorsTestCase,
        ConstructorTestCase,
        SanityTestCase,
        CopyTestCase
    )
166 167 168

if __name__ == "__main__":
    test_main()