blob: 4ca7cec44ce840214bb474dc083407619f20e0e0 [file] [log] [blame]
Guido van Rossumf1669942001-09-11 15:54:16 +00001import hmac
Guido van Rossuma19f80c2007-11-06 20:51:31 +00002import hashlib
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00003import unittest
Guido van Rossuma19f80c2007-11-06 20:51:31 +00004import warnings
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +00006
7class TestVectorsTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +00008
Jeremy Hylton893801e2003-05-27 16:16:41 +00009 def test_md5_vectors(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +000010 # Test the HMAC module against test vectors from the RFC.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000011
12 def md5test(key, data, digest):
13 h = hmac.HMAC(key, data)
Jeremy Hylton893801e2003-05-27 16:16:41 +000014 self.assertEqual(h.hexdigest().upper(), digest.upper())
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000015
Guido van Rossum3f429082007-07-10 13:35:52 +000016 md5test(b"\x0b" * 16,
17 b"Hi There",
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000018 "9294727A3638BB1C13F48EF8158BFC9D")
19
Guido van Rossum3f429082007-07-10 13:35:52 +000020 md5test(b"Jefe",
21 b"what do ya want for nothing?",
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000022 "750c783e6ab0b503eaa86e310a5db738")
23
Guido van Rossum3f429082007-07-10 13:35:52 +000024 md5test(b"\xaa" * 16,
25 b"\xdd" * 50,
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +000026 "56be34521d144c88dbb8c733f0e8b3f6")
27
Guido van Rossum39478e82007-08-27 17:23:59 +000028 md5test(bytes(range(1, 26)),
Guido van Rossum3f429082007-07-10 13:35:52 +000029 b"\xcd" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +000030 "697eaf0aca3a3aea3a75164746ffaa79")
31
Guido van Rossum39478e82007-08-27 17:23:59 +000032 md5test(b"\x0C" * 16,
33 b"Test With Truncation",
Jeremy Hylton893801e2003-05-27 16:16:41 +000034 "56461ef2342edc00f9bab995690efd4c")
35
Guido van Rossum3f429082007-07-10 13:35:52 +000036 md5test(b"\xaa" * 80,
Guido van Rossum39478e82007-08-27 17:23:59 +000037 b"Test Using Larger Than Block-Size Key - Hash Key First",
Jeremy Hylton893801e2003-05-27 16:16:41 +000038 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd")
39
Guido van Rossum3f429082007-07-10 13:35:52 +000040 md5test(b"\xaa" * 80,
Guido van Rossum39478e82007-08-27 17:23:59 +000041 (b"Test Using Larger Than Block-Size Key "
42 b"and Larger Than One Block-Size Data"),
Jeremy Hylton893801e2003-05-27 16:16:41 +000043 "6f630fad67cda0ee1fb1f562db3aa53e")
44
45 def test_sha_vectors(self):
46 def shatest(key, data, digest):
Guido van Rossuma19f80c2007-11-06 20:51:31 +000047 h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
Jeremy Hylton893801e2003-05-27 16:16:41 +000048 self.assertEqual(h.hexdigest().upper(), digest.upper())
49
Guido van Rossum3f429082007-07-10 13:35:52 +000050 shatest(b"\x0b" * 20,
51 b"Hi There",
Jeremy Hylton893801e2003-05-27 16:16:41 +000052 "b617318655057264e28bc0b6fb378c8ef146be00")
53
Guido van Rossum3f429082007-07-10 13:35:52 +000054 shatest(b"Jefe",
55 b"what do ya want for nothing?",
Jeremy Hylton893801e2003-05-27 16:16:41 +000056 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79")
57
Guido van Rossum3f429082007-07-10 13:35:52 +000058 shatest(b"\xAA" * 20,
59 b"\xDD" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +000060 "125d7342b9ac11cd91a39af48aa17b4f63f175d3")
61
Guido van Rossum3f429082007-07-10 13:35:52 +000062 shatest(bytes(range(1, 26)),
63 b"\xCD" * 50,
Jeremy Hylton893801e2003-05-27 16:16:41 +000064 "4c9007f4026250c6bc8414f9bf50c86c2d7235da")
65
Guido van Rossum39478e82007-08-27 17:23:59 +000066 shatest(b"\x0C" * 20,
67 b"Test With Truncation",
Jeremy Hylton893801e2003-05-27 16:16:41 +000068 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04")
69
Guido van Rossum3f429082007-07-10 13:35:52 +000070 shatest(b"\xAA" * 80,
71 b"Test Using Larger Than Block-Size Key - Hash Key First",
Jeremy Hylton893801e2003-05-27 16:16:41 +000072 "aa4ae5e15272d00e95705637ce8a3b55ed402112")
73
Guido van Rossum3f429082007-07-10 13:35:52 +000074 shatest(b"\xAA" * 80,
75 (b"Test Using Larger Than Block-Size Key "
76 b"and Larger Than One Block-Size Data"),
Jeremy Hylton893801e2003-05-27 16:16:41 +000077 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
78
Guido van Rossuma19f80c2007-11-06 20:51:31 +000079 def _rfc4231_test_cases(self, hashfunc):
80 def hmactest(key, data, hexdigests):
81 h = hmac.HMAC(key, data, digestmod=hashfunc)
82 self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
83
84 # 4.2. Test Case 1
85 hmactest(key = b'\x0b'*20,
86 data = b'Hi There',
87 hexdigests = {
88 hashlib.sha224: '896fb1128abbdf196832107cd49df33f'
89 '47b4b1169912ba4f53684b22',
90 hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b'
91 '881dc200c9833da726e9376c2e32cff7',
92 hashlib.sha384: 'afd03944d84895626b0825f4ab46907f'
93 '15f9dadbe4101ec682aa034c7cebc59c'
94 'faea9ea9076ede7f4af152e8b2fa9cb6',
95 hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0'
96 '2379f4e2ce4ec2787ad0b30545e17cde'
97 'daa833b7d6b8a702038b274eaea3f4e4'
98 'be9d914eeb61f1702e696c203a126854',
99 })
100
101 # 4.3. Test Case 2
102 hmactest(key = b'Jefe',
103 data = b'what do ya want for nothing?',
104 hexdigests = {
105 hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f'
106 '8bbea2a39e6148008fd05e44',
107 hashlib.sha256: '5bdcc146bf60754e6a042426089575c7'
108 '5a003f089d2739839dec58b964ec3843',
109 hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b'
110 '9c7ef464f5a01b47e42ec3736322445e'
111 '8e2240ca5e69e2c78b3239ecfab21649',
112 hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3'
113 '87bd64222e831fd610270cd7ea250554'
114 '9758bf75c05a994a6d034f65f8f0e6fd'
115 'caeab1a34d4a6b4b636e070a38bce737',
116 })
117
118 # 4.4. Test Case 3
119 hmactest(key = b'\xaa'*20,
120 data = b'\xdd'*50,
121 hexdigests = {
122 hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264'
123 '9365b0c1f65d69d1ec8333ea',
124 hashlib.sha256: '773ea91e36800e46854db8ebd09181a7'
125 '2959098b3ef8c122d9635514ced565fe',
126 hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f'
127 '0aa635d947ac9febe83ef4e55966144b'
128 '2a5ab39dc13814b94e3ab6e101a34f27',
129 hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9'
130 'b1b5dbdd8ee81a3655f83e33b2279d39'
131 'bf3e848279a722c806b485a47e67c807'
132 'b946a337bee8942674278859e13292fb',
133 })
134
135 # 4.5. Test Case 4
136 hmactest(key = bytes(x for x in range(0x01, 0x19+1)),
137 data = b'\xcd'*50,
138 hexdigests = {
139 hashlib.sha224: '6c11506874013cac6a2abc1bb382627c'
140 'ec6a90d86efc012de7afec5a',
141 hashlib.sha256: '82558a389a443c0ea4cc819899f2083a'
142 '85f0faa3e578f8077a2e3ff46729665b',
143 hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7'
144 '7a9981480850009cc5577c6e1f573b4e'
145 '6801dd23c4a7d679ccf8a386c674cffb',
146 hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7'
147 'e576d97ff94b872de76f8050361ee3db'
148 'a91ca5c11aa25eb4d679275cc5788063'
149 'a5f19741120c4f2de2adebeb10a298dd',
150 })
151
152 # 4.7. Test Case 6
153 hmactest(key = b'\xaa'*131,
154 data = b'Test Using Larger Than Block-Siz'
155 b'e Key - Hash Key First',
156 hexdigests = {
157 hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2'
158 'd499f112f2d2b7273fa6870e',
159 hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f'
160 '8e0bc6213728c5140546040f0ee37f54',
161 hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4'
162 '4f9ef1012a2b588f3cd11f05033ac4c6'
163 '0c2ef6ab4030fe8296248df163f44952',
164 hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4'
165 '9b46d1f41b4aeec1121b013783f8f352'
166 '6b56d037e05f2598bd0fd2215d6a1e52'
167 '95e64f73f63f0aec8b915a985d786598',
168 })
169
170 # 4.8. Test Case 7
171 hmactest(key = b'\xaa'*131,
172 data = b'This is a test using a larger th'
173 b'an block-size key and a larger t'
174 b'han block-size data. The key nee'
175 b'ds to be hashed before being use'
176 b'd by the HMAC algorithm.',
177 hexdigests = {
178 hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd'
179 '946770db9c2b95c9f6f565d1',
180 hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944'
181 'bfdc63644f0713938a7f51535c3a35e2',
182 hashlib.sha384: '6617178e941f020d351e2f254e8fd32c'
183 '602420feb0b8fb9adccebb82461e99c5'
184 'a678cc31e799176d3860e6110c46523e',
185 hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd'
186 'debd71f8867289865df5a32d20cdc944'
187 'b6022cac3c4982b10d5eeb55c3e4de15'
188 '134676fb6de0446065c97440fa8c6a58',
189 })
190
191 def test_sha224_rfc4231(self):
192 self._rfc4231_test_cases(hashlib.sha224)
193
194 def test_sha256_rfc4231(self):
195 self._rfc4231_test_cases(hashlib.sha256)
196
197 def test_sha384_rfc4231(self):
198 self._rfc4231_test_cases(hashlib.sha384)
199
200 def test_sha512_rfc4231(self):
201 self._rfc4231_test_cases(hashlib.sha512)
202
203 def test_legacy_block_size_warnings(self):
204 class MockCrazyHash(object):
205 """Ain't no block_size attribute here."""
206 def __init__(self, *args):
207 self._x = hashlib.sha1(*args)
208 self.digest_size = self._x.digest_size
209 def update(self, v):
210 self._x.update(v)
211 def digest(self):
212 return self._x.digest()
213
Brett Cannon1cd02472008-09-09 01:52:27 +0000214 with warnings.catch_warnings():
Christian Heimese25f35e2008-03-20 10:49:03 +0000215 warnings.simplefilter('error', RuntimeWarning)
Florent Xicluna41fe6152010-04-02 18:52:12 +0000216 with self.assertRaises(RuntimeWarning):
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000217 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000218 self.fail('Expected warning about missing block_size')
219
220 MockCrazyHash.block_size = 1
Florent Xicluna41fe6152010-04-02 18:52:12 +0000221 with self.assertRaises(RuntimeWarning):
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000222 hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000223 self.fail('Expected warning about small block_size')
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000224
225
Jeremy Hylton893801e2003-05-27 16:16:41 +0000226
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000227class ConstructorTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000228
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000229 def test_normal(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000230 # Standard constructor call.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000231 failed = 0
232 try:
Guido van Rossum39478e82007-08-27 17:23:59 +0000233 h = hmac.HMAC(b"key")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000234 except:
235 self.fail("Standard constructor call raised exception.")
236
Antoine Pitrou24ef3e92012-06-30 17:27:56 +0200237 def test_with_str_key(self):
238 # Pass a key of type str, which is an error, because it expects a key
239 # of type bytes
240 with self.assertRaises(TypeError):
241 h = hmac.HMAC("key")
242
243 def test_dot_new_with_str_key(self):
244 # Pass a key of type str, which is an error, because it expects a key
245 # of type bytes
246 with self.assertRaises(TypeError):
247 h = hmac.new("key")
248
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000249 def test_withtext(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000250 # Constructor call with text.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000251 try:
Guido van Rossum39478e82007-08-27 17:23:59 +0000252 h = hmac.HMAC(b"key", b"hash this!")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000253 except:
254 self.fail("Constructor call with text argument raised exception.")
255
256 def test_withmodule(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000257 # Constructor call with text and digest module.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000258 try:
Guido van Rossuma19f80c2007-11-06 20:51:31 +0000259 h = hmac.HMAC(b"key", b"", hashlib.sha1)
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000260 except:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000261 self.fail("Constructor call with hashlib.sha1 raised exception.")
Tim Peters88768482001-11-13 21:51:26 +0000262
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000263class SanityTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000264
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000265 def test_default_is_md5(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000266 # Testing if HMAC defaults to MD5 algorithm.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000267 # NOTE: this whitebox test depends on the hmac class internals
Guido van Rossum39478e82007-08-27 17:23:59 +0000268 h = hmac.HMAC(b"key")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000269 self.assertEqual(h.digest_cons, hashlib.md5)
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000270
271 def test_exercise_all_methods(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000272 # Exercising all methods once.
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000273 # This must not raise any exceptions
274 try:
Guido van Rossum39478e82007-08-27 17:23:59 +0000275 h = hmac.HMAC(b"my secret key")
276 h.update(b"compute the hash of this text!")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000277 dig = h.digest()
278 dig = h.hexdigest()
279 h2 = h.copy()
280 except:
Neal Norwitz28bb5722002-04-01 19:00:50 +0000281 self.fail("Exception raised during normal usage of HMAC class.")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000282
283class CopyTestCase(unittest.TestCase):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000284
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000285 def test_attributes(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000286 # Testing if attributes are of same type.
Guido van Rossum39478e82007-08-27 17:23:59 +0000287 h1 = hmac.HMAC(b"key")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000288 h2 = h1.copy()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000289 self.assertTrue(h1.digest_cons == h2.digest_cons,
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000290 "digest constructors don't match.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000291 self.assertEqual(type(h1.inner), type(h2.inner),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000292 "Types of inner don't match.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000293 self.assertEqual(type(h1.outer), type(h2.outer),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000294 "Types of outer don't match.")
295
296 def test_realcopy(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000297 # Testing if the copy method created a real copy.
Guido van Rossum39478e82007-08-27 17:23:59 +0000298 h1 = hmac.HMAC(b"key")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000299 h2 = h1.copy()
Mark Dickinsona56c4672009-01-27 18:17:45 +0000300 # Using id() in case somebody has overridden __eq__/__ne__.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000301 self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.")
302 self.assertTrue(id(h1.inner) != id(h2.inner),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000303 "No real copy of the attribute 'inner'.")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000304 self.assertTrue(id(h1.outer) != id(h2.outer),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000305 "No real copy of the attribute 'outer'.")
306
307 def test_equality(self):
Guido van Rossum7e8fdba2002-08-22 19:38:14 +0000308 # Testing if the copy has the same digests.
Guido van Rossum39478e82007-08-27 17:23:59 +0000309 h1 = hmac.HMAC(b"key")
310 h1.update(b"some random text")
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000311 h2 = h1.copy()
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000312 self.assertEqual(h1.digest(), h2.digest(),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000313 "Digest of copy doesn't match original digest.")
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000314 self.assertEqual(h1.hexdigest(), h2.hexdigest(),
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000315 "Hexdigest of copy doesn't match original hexdigest.")
316
Nick Coghlan807770e2012-06-15 21:14:08 +1000317class CompareDigestTestCase(unittest.TestCase):
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200318
Christian Heimes6cea6552012-06-24 13:48:32 +0200319 def test_compare_digest(self):
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200320 # Testing input type exception handling
321 a, b = 100, 200
Nick Coghlan807770e2012-06-15 21:14:08 +1000322 self.assertRaises(TypeError, hmac.compare_digest, a, b)
323 a, b = 100, b"foobar"
324 self.assertRaises(TypeError, hmac.compare_digest, a, b)
325 a, b = b"foobar", 200
326 self.assertRaises(TypeError, hmac.compare_digest, a, b)
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200327 a, b = "foobar", b"foobar"
Nick Coghlan807770e2012-06-15 21:14:08 +1000328 self.assertRaises(TypeError, hmac.compare_digest, a, b)
329 a, b = b"foobar", "foobar"
330 self.assertRaises(TypeError, hmac.compare_digest, a, b)
Nick Coghlan807770e2012-06-15 21:14:08 +1000331
332 # Testing bytes of different lengths
333 a, b = b"foobar", b"foo"
334 self.assertFalse(hmac.compare_digest(a, b))
335 a, b = b"\xde\xad\xbe\xef", b"\xde\xad"
336 self.assertFalse(hmac.compare_digest(a, b))
337
338 # Testing bytes of same lengths, different values
339 a, b = b"foobar", b"foobaz"
340 self.assertFalse(hmac.compare_digest(a, b))
341 a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea"
342 self.assertFalse(hmac.compare_digest(a, b))
343
344 # Testing bytes of same lengths, same values
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200345 a, b = b"foobar", b"foobar"
Nick Coghlan807770e2012-06-15 21:14:08 +1000346 self.assertTrue(hmac.compare_digest(a, b))
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200347 a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef"
Nick Coghlan807770e2012-06-15 21:14:08 +1000348 self.assertTrue(hmac.compare_digest(a, b))
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200349
Christian Heimes6cea6552012-06-24 13:48:32 +0200350 # Testing bytearrays of same lengths, same values
351 a, b = bytearray(b"foobar"), bytearray(b"foobar")
352 self.assertTrue(hmac.compare_digest(a, b))
353
354 # Testing bytearrays of diffeent lengths
355 a, b = bytearray(b"foobar"), bytearray(b"foo")
356 self.assertFalse(hmac.compare_digest(a, b))
357
358 # Testing bytearrays of same lengths, different values
359 a, b = bytearray(b"foobar"), bytearray(b"foobaz")
360 self.assertFalse(hmac.compare_digest(a, b))
361
362 # Testing byte and bytearray of same lengths, same values
363 a, b = bytearray(b"foobar"), b"foobar"
364 self.assertTrue(hmac.compare_digest(a, b))
365 self.assertTrue(hmac.compare_digest(b, a))
366
367 # Testing byte bytearray of diffeent lengths
368 a, b = bytearray(b"foobar"), b"foo"
369 self.assertFalse(hmac.compare_digest(a, b))
370 self.assertFalse(hmac.compare_digest(b, a))
371
372 # Testing byte and bytearray of same lengths, different values
373 a, b = bytearray(b"foobar"), b"foobaz"
374 self.assertFalse(hmac.compare_digest(a, b))
375 self.assertFalse(hmac.compare_digest(b, a))
376
377 # Testing str of same lengths
378 a, b = "foobar", "foobar"
379 self.assertTrue(hmac.compare_digest(a, b))
380
381 # Testing str of diffeent lengths
382 a, b = "foo", "foobar"
383 self.assertFalse(hmac.compare_digest(a, b))
384
385 # Testing bytes of same lengths, different values
386 a, b = "foobar", "foobaz"
387 self.assertFalse(hmac.compare_digest(a, b))
388
389 # Testing error cases
390 a, b = "foobar", b"foobar"
391 self.assertRaises(TypeError, hmac.compare_digest, a, b)
392 a, b = b"foobar", "foobar"
393 self.assertRaises(TypeError, hmac.compare_digest, a, b)
394 a, b = b"foobar", 1
395 self.assertRaises(TypeError, hmac.compare_digest, a, b)
396 a, b = 100, 200
397 self.assertRaises(TypeError, hmac.compare_digest, a, b)
398 a, b = "fooä", "fooä"
399 self.assertRaises(TypeError, hmac.compare_digest, a, b)
400
401 # subclasses are supported by ignore __eq__
402 class mystr(str):
403 def __eq__(self, other):
404 return False
405
406 a, b = mystr("foobar"), mystr("foobar")
407 self.assertTrue(hmac.compare_digest(a, b))
408 a, b = mystr("foobar"), "foobar"
409 self.assertTrue(hmac.compare_digest(a, b))
410 a, b = mystr("foobar"), mystr("foobaz")
411 self.assertFalse(hmac.compare_digest(a, b))
412
413 class mybytes(bytes):
414 def __eq__(self, other):
415 return False
416
417 a, b = mybytes(b"foobar"), mybytes(b"foobar")
418 self.assertTrue(hmac.compare_digest(a, b))
419 a, b = mybytes(b"foobar"), b"foobar"
420 self.assertTrue(hmac.compare_digest(a, b))
421 a, b = mybytes(b"foobar"), mybytes(b"foobaz")
422 self.assertFalse(hmac.compare_digest(a, b))
423
424
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000425def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000426 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000427 TestVectorsTestCase,
428 ConstructorTestCase,
429 SanityTestCase,
Charles-François Natali7feb9f42012-05-13 19:53:07 +0200430 CopyTestCase,
Nick Coghlan807770e2012-06-15 21:14:08 +1000431 CompareDigestTestCase
Walter Dörwald21d3a322003-05-01 17:45:56 +0000432 )
Andrew M. Kuchlingf792bba2001-11-02 21:49:59 +0000433
434if __name__ == "__main__":
435 test_main()