13428620b2b5baeddf3949caf9de468038dbac21aBenjamin Peterson# coding: utf-8
23428620b2b5baeddf3949caf9de468038dbac21aBenjamin Peterson
3f166994b8346708a867df823def3ab12e02d43e9Guido van Rossumimport hmac
4e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smithimport hashlib
5f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingimport unittest
6e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smithimport warnings
704f357cffef6d764f2f0ff2671dabde75ec250d1Barry Warsawfrom test import test_support
8f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
9f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass TestVectorsTestCase(unittest.TestCase):
107e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum
11893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton    def test_md5_vectors(self):
127e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Test the HMAC module against test vectors from the RFC.
13f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
14f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        def md5test(key, data, digest):
15f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            h = hmac.HMAC(key, data)
16893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton            self.assertEqual(h.hexdigest().upper(), digest.upper())
17f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
18f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        md5test(chr(0x0b) * 16,
19f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling                "Hi There",
20f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling                "9294727A3638BB1C13F48EF8158BFC9D")
21f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
22f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        md5test("Jefe",
23f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling                "what do ya want for nothing?",
24f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling                "750c783e6ab0b503eaa86e310a5db738")
25f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
26f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        md5test(chr(0xAA)*16,
27f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling                chr(0xDD)*50,
28f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling                "56be34521d144c88dbb8c733f0e8b3f6")
29f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
30893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        md5test("".join([chr(i) for i in range(1, 26)]),
31893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                chr(0xCD) * 50,
32893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "697eaf0aca3a3aea3a75164746ffaa79")
33893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
34893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        md5test(chr(0x0C) * 16,
35893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "Test With Truncation",
36893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "56461ef2342edc00f9bab995690efd4c")
37893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
38893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        md5test(chr(0xAA) * 80,
39893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "Test Using Larger Than Block-Size Key - Hash Key First",
40893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
41893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
42893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        md5test(chr(0xAA) * 80,
43893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                ("Test Using Larger Than Block-Size Key "
44893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                 "and Larger Than One Block-Size Data"),
45893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "6f630fad67cda0ee1fb1f562db3aa53e")
46893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
47893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton    def test_sha_vectors(self):
48893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        def shatest(key, data, digest):
49e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
50893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton            self.assertEqual(h.hexdigest().upper(), digest.upper())
51893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
52893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        shatest(chr(0x0b) * 20,
53893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "Hi There",
54893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "b617318655057264e28bc0b6fb378c8ef146be00")
55893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
56893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        shatest("Jefe",
57893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "what do ya want for nothing?",
58893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
59893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
60893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        shatest(chr(0xAA)*20,
61893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                chr(0xDD)*50,
62893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
63893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
64893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        shatest("".join([chr(i) for i in range(1, 26)]),
65893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                chr(0xCD) * 50,
66893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
67893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
68893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        shatest(chr(0x0C) * 20,
69893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "Test With Truncation",
70893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
71893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
72893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        shatest(chr(0xAA) * 80,
73893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "Test Using Larger Than Block-Size Key - Hash Key First",
74893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "aa4ae5e15272d00e95705637ce8a3b55ed402112")
75893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
76893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton        shatest(chr(0xAA) * 80,
77893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                ("Test Using Larger Than Block-Size Key "
78893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                 "and Larger Than One Block-Size Data"),
79893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton                "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
80893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
81e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith    def _rfc4231_test_cases(self, hashfunc):
82e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        def hmactest(key, data, hexdigests):
83e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            h = hmac.HMAC(key, data, digestmod=hashfunc)
84e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
85e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
86e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        # 4.2.  Test Case 1
87e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        hmactest(key = '\x0b'*20,
88e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 data = 'Hi There',
89e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 hexdigests = {
90e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha224: '896fb1128abbdf196832107cd49df33f'
91e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '47b4b1169912ba4f53684b22',
92e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b'
93e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '881dc200c9833da726e9376c2e32cff7',
94e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha384: 'afd03944d84895626b0825f4ab46907f'
95e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '15f9dadbe4101ec682aa034c7cebc59c'
96e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'faea9ea9076ede7f4af152e8b2fa9cb6',
97e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0'
98e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '2379f4e2ce4ec2787ad0b30545e17cde'
99e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'daa833b7d6b8a702038b274eaea3f4e4'
100e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'be9d914eeb61f1702e696c203a126854',
101e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 })
102e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
103e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        # 4.3.  Test Case 2
104e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        hmactest(key = 'Jefe',
105e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 data = 'what do ya want for nothing?',
106e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 hexdigests = {
107e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f'
108e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '8bbea2a39e6148008fd05e44',
109e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha256: '5bdcc146bf60754e6a042426089575c7'
110e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '5a003f089d2739839dec58b964ec3843',
111e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b'
112e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '9c7ef464f5a01b47e42ec3736322445e'
113e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '8e2240ca5e69e2c78b3239ecfab21649',
114e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3'
115e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '87bd64222e831fd610270cd7ea250554'
116e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '9758bf75c05a994a6d034f65f8f0e6fd'
117e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'caeab1a34d4a6b4b636e070a38bce737',
118e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 })
119e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
120e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        # 4.4.  Test Case 3
121e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        hmactest(key = '\xaa'*20,
122e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 data = '\xdd'*50,
123e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 hexdigests = {
124e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264'
125e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '9365b0c1f65d69d1ec8333ea',
126e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha256: '773ea91e36800e46854db8ebd09181a7'
127e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '2959098b3ef8c122d9635514ced565fe',
128e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f'
129e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '0aa635d947ac9febe83ef4e55966144b'
130e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '2a5ab39dc13814b94e3ab6e101a34f27',
131e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9'
132e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'b1b5dbdd8ee81a3655f83e33b2279d39'
133e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'bf3e848279a722c806b485a47e67c807'
134e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'b946a337bee8942674278859e13292fb',
135e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 })
136e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
137e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        # 4.5.  Test Case 4
138e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        hmactest(key = ''.join([chr(x) for x in xrange(0x01, 0x19+1)]),
139e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 data = '\xcd'*50,
140e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 hexdigests = {
141e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha224: '6c11506874013cac6a2abc1bb382627c'
142e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'ec6a90d86efc012de7afec5a',
143e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha256: '82558a389a443c0ea4cc819899f2083a'
144e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '85f0faa3e578f8077a2e3ff46729665b',
145e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7'
146e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '7a9981480850009cc5577c6e1f573b4e'
147e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '6801dd23c4a7d679ccf8a386c674cffb',
148e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7'
149e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'e576d97ff94b872de76f8050361ee3db'
150e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'a91ca5c11aa25eb4d679275cc5788063'
151e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'a5f19741120c4f2de2adebeb10a298dd',
152e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 })
153e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
154e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        # 4.7.  Test Case 6
155e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        hmactest(key = '\xaa'*131,
156e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 data = 'Test Using Larger Than Block-Siz'
157e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                        'e Key - Hash Key First',
158e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 hexdigests = {
159e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2'
160e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'd499f112f2d2b7273fa6870e',
161e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f'
162e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '8e0bc6213728c5140546040f0ee37f54',
163e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4'
164e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '4f9ef1012a2b588f3cd11f05033ac4c6'
165e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '0c2ef6ab4030fe8296248df163f44952',
166e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4'
167e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '9b46d1f41b4aeec1121b013783f8f352'
168e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '6b56d037e05f2598bd0fd2215d6a1e52'
169e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '95e64f73f63f0aec8b915a985d786598',
170e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 })
171e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
172e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        # 4.8.  Test Case 7
173e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        hmactest(key = '\xaa'*131,
174e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 data = 'This is a test using a larger th'
175e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                        'an block-size key and a larger t'
176e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                        'han block-size data. The key nee'
177e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                        'ds to be hashed before being use'
178e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                        'd by the HMAC algorithm.',
179e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 hexdigests = {
180e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd'
181e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '946770db9c2b95c9f6f565d1',
182e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944'
183e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'bfdc63644f0713938a7f51535c3a35e2',
184e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha384: '6617178e941f020d351e2f254e8fd32c'
185e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '602420feb0b8fb9adccebb82461e99c5'
186e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'a678cc31e799176d3860e6110c46523e',
187e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                   hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd'
188e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'debd71f8867289865df5a32d20cdc944'
189e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   'b6022cac3c4982b10d5eeb55c3e4de15'
190e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                                   '134676fb6de0446065c97440fa8c6a58',
191e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                 })
192e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
193e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith    def test_sha224_rfc4231(self):
194e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        self._rfc4231_test_cases(hashlib.sha224)
195e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
196e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith    def test_sha256_rfc4231(self):
197e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        self._rfc4231_test_cases(hashlib.sha256)
198e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
199e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith    def test_sha384_rfc4231(self):
200e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        self._rfc4231_test_cases(hashlib.sha384)
201e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
202e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith    def test_sha512_rfc4231(self):
203e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        self._rfc4231_test_cases(hashlib.sha512)
204e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
205e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith    def test_legacy_block_size_warnings(self):
206e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith        class MockCrazyHash(object):
207e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            """Ain't no block_size attribute here."""
208e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            def __init__(self, *args):
209e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                self._x = hashlib.sha1(*args)
210e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                self.digest_size = self._x.digest_size
211e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            def update(self, v):
212e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                self._x.update(v)
213e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            def digest(self):
214e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                return self._x.digest()
215e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
216672237dc6ca1498eabac08554bcbc5bd0fd9ddaaBrett Cannon        with warnings.catch_warnings():
21748581c5f08d368942840f99687fce7f10758fa7cBrett Cannon            warnings.simplefilter('error', RuntimeWarning)
2186257a7bbb2660ae75c44f2e71d7ac2ce73900f74Florent Xicluna            with self.assertRaises(RuntimeWarning):
219e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                hmac.HMAC('a', 'b', digestmod=MockCrazyHash)
220e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                self.fail('Expected warning about missing block_size')
221e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
222e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            MockCrazyHash.block_size = 1
2236257a7bbb2660ae75c44f2e71d7ac2ce73900f74Florent Xicluna            with self.assertRaises(RuntimeWarning):
224e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                hmac.HMAC('a', 'b', digestmod=MockCrazyHash)
225e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith                self.fail('Expected warning about small block_size')
226e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
227e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith
228893801efb6add1145f620ae70d5bef51c440821fJeremy Hylton
229f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass ConstructorTestCase(unittest.TestCase):
2307e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum
231f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    def test_normal(self):
2327e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Standard constructor call.
233f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        failed = 0
234f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        try:
235f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            h = hmac.HMAC("key")
236f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        except:
237f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            self.fail("Standard constructor call raised exception.")
238f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
239f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    def test_withtext(self):
2407e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Constructor call with text.
241f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        try:
242f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            h = hmac.HMAC("key", "hash this!")
243f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        except:
244f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            self.fail("Constructor call with text argument raised exception.")
245f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
246f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    def test_withmodule(self):
2477e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Constructor call with text and digest module.
248f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        try:
249e1ac4f19301aa0a4d28fc25b91c71c477c37cb21Gregory P. Smith            h = hmac.HMAC("key", "", hashlib.sha1)
250f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        except:
251c2aa09ad8055ba3e03da297608552d04b42e9aacBrett Cannon            self.fail("Constructor call with hashlib.sha1 raised exception.")
25288768483235beb5de12addee3d73d4e252f6f7cbTim Peters
253f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass SanityTestCase(unittest.TestCase):
2547e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum
255f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    def test_default_is_md5(self):
2567e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Testing if HMAC defaults to MD5 algorithm.
257f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        # NOTE: this whitebox test depends on the hmac class internals
258f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        h = hmac.HMAC("key")
2595c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(h.digest_cons == hashlib.md5)
260f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
261f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    def test_exercise_all_methods(self):
2627e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Exercising all methods once.
263f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        # This must not raise any exceptions
264f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        try:
265f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            h = hmac.HMAC("my secret key")
266f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            h.update("compute the hash of this text!")
267f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            dig = h.digest()
268f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            dig = h.hexdigest()
269f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            h2 = h.copy()
270f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        except:
27128bb572ab4e72abfd918343f1d6114630daa4564Neal Norwitz            self.fail("Exception raised during normal usage of HMAC class.")
272f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
273f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingclass CopyTestCase(unittest.TestCase):
2747e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum
275f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    def test_attributes(self):
2767e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Testing if attributes are of same type.
277f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        h1 = hmac.HMAC("key")
278f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        h2 = h1.copy()
2795c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(h1.digest_cons == h2.digest_cons,
280f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith            "digest constructors don't match.")
2815c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(type(h1.inner) == type(h2.inner),
282f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            "Types of inner don't match.")
2835c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(type(h1.outer) == type(h2.outer),
284f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            "Types of outer don't match.")
285f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
286f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    def test_realcopy(self):
2877e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Testing if the copy method created a real copy.
288f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        h1 = hmac.HMAC("key")
289f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        h2 = h1.copy()
290f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        # Using id() in case somebody has overridden __cmp__.
2915c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
2925c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(id(h1.inner) != id(h2.inner),
293f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            "No real copy of the attribute 'inner'.")
2945c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(id(h1.outer) != id(h2.outer),
295f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            "No real copy of the attribute 'outer'.")
296f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
297f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    def test_equality(self):
2987e8fdba01cdc1abad79a7bf0d457e610d08eb7b8Guido van Rossum        # Testing if the copy has the same digests.
299f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        h1 = hmac.HMAC("key")
300f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        h1.update("some random text")
301f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling        h2 = h1.copy()
3025c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(h1.digest() == h2.digest(),
303f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            "Digest of copy doesn't match original digest.")
3045c8da86f3a515ce1a6d5f27fd15e3c5f4d8e931eBenjamin Peterson        self.assertTrue(h1.hexdigest() == h2.hexdigest(),
305f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling            "Hexdigest of copy doesn't match original hexdigest.")
306f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
307629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
308629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Petersonclass CompareDigestTestCase(unittest.TestCase):
309629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
310629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson    def test_compare_digest(self):
311629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing input type exception handling
312629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = 100, 200
313629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
314629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = 100, b"foobar"
315629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
316629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"foobar", 200
317629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
318629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = u"foobar", b"foobar"
319629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
320629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"foobar", u"foobar"
321629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
322629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
323629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing bytes of different lengths
324629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"foobar", b"foo"
325629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
326629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"\xde\xad\xbe\xef", b"\xde\xad"
327629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
328629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
329629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing bytes of same lengths, different values
330629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"foobar", b"foobaz"
331629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
332629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea"
333629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
334629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
335629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing bytes of same lengths, same values
336629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"foobar", b"foobar"
337629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
338629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef"
339629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
340629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
341629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing bytearrays of same lengths, same values
342629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = bytearray(b"foobar"), bytearray(b"foobar")
343629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
344629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
345629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing bytearrays of diffeent lengths
346629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = bytearray(b"foobar"), bytearray(b"foo")
347629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
348629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
349629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing bytearrays of same lengths, different values
350629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = bytearray(b"foobar"), bytearray(b"foobaz")
351629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
352629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
353629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing byte and bytearray of same lengths, same values
354629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = bytearray(b"foobar"), b"foobar"
355629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
356629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(b, a))
357629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
358629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing byte bytearray of diffeent lengths
359629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = bytearray(b"foobar"), b"foo"
360629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
361629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(b, a))
362629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
363629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing byte and bytearray of same lengths, different values
364629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = bytearray(b"foobar"), b"foobaz"
365629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
366629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(b, a))
367629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
368629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing str of same lengths
369629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = "foobar", "foobar"
370629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
371629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
372629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing str of diffeent lengths
373629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = "foo", "foobar"
374629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
375629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
376629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing bytes of same lengths, different values
377629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = "foobar", "foobaz"
378629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
379629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
380629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        # Testing error cases
381629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = u"foobar", b"foobar"
382629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
383629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"foobar", u"foobar"
384629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
385629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = b"foobar", 1
386629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
387629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = 100, 200
388629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertRaises(TypeError, hmac.compare_digest, a, b)
389629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = "fooä", "fooä"
390629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
391629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
392a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner        with test_support.check_py3k_warnings():
393a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner            # subclasses are supported by ignore __eq__
394a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner            class mystr(str):
395a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner                def __eq__(self, other):
396a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner                    return False
397629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
398629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = mystr("foobar"), mystr("foobar")
399629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
400629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = mystr("foobar"), "foobar"
401629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
402629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = mystr("foobar"), mystr("foobaz")
403629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
404629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
405a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner        with test_support.check_py3k_warnings():
406a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner            class mybytes(bytes):
407a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner                def __eq__(self, other):
408a3acea3e07dba03a22dc35ca17ad83baa82e3729Victor Stinner                    return False
409629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
410629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = mybytes(b"foobar"), mybytes(b"foobar")
411629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
412629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = mybytes(b"foobar"), b"foobar"
413629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertTrue(hmac.compare_digest(a, b))
414629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        a, b = mybytes(b"foobar"), mybytes(b"foobaz")
415629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        self.assertFalse(hmac.compare_digest(a, b))
416629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
417629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson
418f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingdef test_main():
41921d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald    test_support.run_unittest(
42021d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald        TestVectorsTestCase,
42121d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald        ConstructorTestCase,
42221d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald        SanityTestCase,
423629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        CopyTestCase,
424629026aecc6bbb7cf8df25f7699eaf97d8e45701Benjamin Peterson        CompareDigestTestCase,
42521d3a32b99c5763444c34c189ef653ac9745f3c4Walter Dörwald    )
426f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling
427f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchlingif __name__ == "__main__":
428f792bba98f081b1d05b32ce3a9085ef135b75b96Andrew M. Kuchling    test_main()
429