# Python code coverage for Lib/test/test_hmac.py

# | count | content |
---|---|---|

1 | n/a | import functools |

2 | n/a | import hmac |

3 | n/a | import hashlib |

4 | n/a | import unittest |

5 | n/a | import warnings |

6 | n/a | |

7 | n/a | |

8 | n/a | def ignore_warning(func): |

9 | n/a | @functools.wraps(func) |

10 | n/a | def wrapper(*args, **kwargs): |

11 | n/a | with warnings.catch_warnings(): |

12 | n/a | warnings.filterwarnings("ignore", |

13 | n/a | category=PendingDeprecationWarning) |

14 | n/a | return func(*args, **kwargs) |

15 | n/a | return wrapper |

16 | n/a | |

17 | n/a | |

18 | n/a | class TestVectorsTestCase(unittest.TestCase): |

19 | n/a | |

20 | n/a | def test_md5_vectors(self): |

21 | n/a | # Test the HMAC module against test vectors from the RFC. |

22 | n/a | |

23 | n/a | def md5test(key, data, digest): |

24 | n/a | h = hmac.HMAC(key, data, digestmod=hashlib.md5) |

25 | n/a | self.assertEqual(h.hexdigest().upper(), digest.upper()) |

26 | n/a | self.assertEqual(h.name, "hmac-md5") |

27 | n/a | self.assertEqual(h.digest_size, 16) |

28 | n/a | self.assertEqual(h.block_size, 64) |

29 | n/a | |

30 | n/a | h = hmac.HMAC(key, data, digestmod='md5') |

31 | n/a | self.assertEqual(h.hexdigest().upper(), digest.upper()) |

32 | n/a | self.assertEqual(h.name, "hmac-md5") |

33 | n/a | self.assertEqual(h.digest_size, 16) |

34 | n/a | self.assertEqual(h.block_size, 64) |

35 | n/a | |

36 | n/a | |

37 | n/a | md5test(b"\x0b" * 16, |

38 | n/a | b"Hi There", |

39 | n/a | "9294727A3638BB1C13F48EF8158BFC9D") |

40 | n/a | |

41 | n/a | md5test(b"Jefe", |

42 | n/a | b"what do ya want for nothing?", |

43 | n/a | "750c783e6ab0b503eaa86e310a5db738") |

44 | n/a | |

45 | n/a | md5test(b"\xaa" * 16, |

46 | n/a | b"\xdd" * 50, |

47 | n/a | "56be34521d144c88dbb8c733f0e8b3f6") |

48 | n/a | |

49 | n/a | md5test(bytes(range(1, 26)), |

50 | n/a | b"\xcd" * 50, |

51 | n/a | "697eaf0aca3a3aea3a75164746ffaa79") |

52 | n/a | |

53 | n/a | md5test(b"\x0C" * 16, |

54 | n/a | b"Test With Truncation", |

55 | n/a | "56461ef2342edc00f9bab995690efd4c") |

56 | n/a | |

57 | n/a | md5test(b"\xaa" * 80, |

58 | n/a | b"Test Using Larger Than Block-Size Key - Hash Key First", |

59 | n/a | "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") |

60 | n/a | |

61 | n/a | md5test(b"\xaa" * 80, |

62 | n/a | (b"Test Using Larger Than Block-Size Key " |

63 | n/a | b"and Larger Than One Block-Size Data"), |

64 | n/a | "6f630fad67cda0ee1fb1f562db3aa53e") |

65 | n/a | |

66 | n/a | def test_sha_vectors(self): |

67 | n/a | def shatest(key, data, digest): |

68 | n/a | h = hmac.HMAC(key, data, digestmod=hashlib.sha1) |

69 | n/a | self.assertEqual(h.hexdigest().upper(), digest.upper()) |

70 | n/a | self.assertEqual(h.name, "hmac-sha1") |

71 | n/a | self.assertEqual(h.digest_size, 20) |

72 | n/a | self.assertEqual(h.block_size, 64) |

73 | n/a | |

74 | n/a | h = hmac.HMAC(key, data, digestmod='sha1') |

75 | n/a | self.assertEqual(h.hexdigest().upper(), digest.upper()) |

76 | n/a | self.assertEqual(h.name, "hmac-sha1") |

77 | n/a | self.assertEqual(h.digest_size, 20) |

78 | n/a | self.assertEqual(h.block_size, 64) |

79 | n/a | |

80 | n/a | |

81 | n/a | shatest(b"\x0b" * 20, |

82 | n/a | b"Hi There", |

83 | n/a | "b617318655057264e28bc0b6fb378c8ef146be00") |

84 | n/a | |

85 | n/a | shatest(b"Jefe", |

86 | n/a | b"what do ya want for nothing?", |

87 | n/a | "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") |

88 | n/a | |

89 | n/a | shatest(b"\xAA" * 20, |

90 | n/a | b"\xDD" * 50, |

91 | n/a | "125d7342b9ac11cd91a39af48aa17b4f63f175d3") |

92 | n/a | |

93 | n/a | shatest(bytes(range(1, 26)), |

94 | n/a | b"\xCD" * 50, |

95 | n/a | "4c9007f4026250c6bc8414f9bf50c86c2d7235da") |

96 | n/a | |

97 | n/a | shatest(b"\x0C" * 20, |

98 | n/a | b"Test With Truncation", |

99 | n/a | "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") |

100 | n/a | |

101 | n/a | shatest(b"\xAA" * 80, |

102 | n/a | b"Test Using Larger Than Block-Size Key - Hash Key First", |

103 | n/a | "aa4ae5e15272d00e95705637ce8a3b55ed402112") |

104 | n/a | |

105 | n/a | shatest(b"\xAA" * 80, |

106 | n/a | (b"Test Using Larger Than Block-Size Key " |

107 | n/a | b"and Larger Than One Block-Size Data"), |

108 | n/a | "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") |

109 | n/a | |

110 | n/a | def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size): |

111 | n/a | def hmactest(key, data, hexdigests): |

112 | n/a | hmac_name = "hmac-" + hash_name |

113 | n/a | h = hmac.HMAC(key, data, digestmod=hashfunc) |

114 | n/a | self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc]) |

115 | n/a | self.assertEqual(h.name, hmac_name) |

116 | n/a | self.assertEqual(h.digest_size, digest_size) |

117 | n/a | self.assertEqual(h.block_size, block_size) |

118 | n/a | |

119 | n/a | h = hmac.HMAC(key, data, digestmod=hash_name) |

120 | n/a | self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc]) |

121 | n/a | self.assertEqual(h.name, hmac_name) |

122 | n/a | self.assertEqual(h.digest_size, digest_size) |

123 | n/a | self.assertEqual(h.block_size, block_size) |

124 | n/a | |

125 | n/a | |

126 | n/a | # 4.2. Test Case 1 |

127 | n/a | hmactest(key = b'\x0b'*20, |

128 | n/a | data = b'Hi There', |

129 | n/a | hexdigests = { |

130 | n/a | hashlib.sha224: '896fb1128abbdf196832107cd49df33f' |

131 | n/a | '47b4b1169912ba4f53684b22', |

132 | n/a | hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b' |

133 | n/a | '881dc200c9833da726e9376c2e32cff7', |

134 | n/a | hashlib.sha384: 'afd03944d84895626b0825f4ab46907f' |

135 | n/a | '15f9dadbe4101ec682aa034c7cebc59c' |

136 | n/a | 'faea9ea9076ede7f4af152e8b2fa9cb6', |

137 | n/a | hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0' |

138 | n/a | '2379f4e2ce4ec2787ad0b30545e17cde' |

139 | n/a | 'daa833b7d6b8a702038b274eaea3f4e4' |

140 | n/a | 'be9d914eeb61f1702e696c203a126854', |

141 | n/a | }) |

142 | n/a | |

143 | n/a | # 4.3. Test Case 2 |

144 | n/a | hmactest(key = b'Jefe', |

145 | n/a | data = b'what do ya want for nothing?', |

146 | n/a | hexdigests = { |

147 | n/a | hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f' |

148 | n/a | '8bbea2a39e6148008fd05e44', |

149 | n/a | hashlib.sha256: '5bdcc146bf60754e6a042426089575c7' |

150 | n/a | '5a003f089d2739839dec58b964ec3843', |

151 | n/a | hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b' |

152 | n/a | '9c7ef464f5a01b47e42ec3736322445e' |

153 | n/a | '8e2240ca5e69e2c78b3239ecfab21649', |

154 | n/a | hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3' |

155 | n/a | '87bd64222e831fd610270cd7ea250554' |

156 | n/a | '9758bf75c05a994a6d034f65f8f0e6fd' |

157 | n/a | 'caeab1a34d4a6b4b636e070a38bce737', |

158 | n/a | }) |

159 | n/a | |

160 | n/a | # 4.4. Test Case 3 |

161 | n/a | hmactest(key = b'\xaa'*20, |

162 | n/a | data = b'\xdd'*50, |

163 | n/a | hexdigests = { |

164 | n/a | hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264' |

165 | n/a | '9365b0c1f65d69d1ec8333ea', |

166 | n/a | hashlib.sha256: '773ea91e36800e46854db8ebd09181a7' |

167 | n/a | '2959098b3ef8c122d9635514ced565fe', |

168 | n/a | hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f' |

169 | n/a | '0aa635d947ac9febe83ef4e55966144b' |

170 | n/a | '2a5ab39dc13814b94e3ab6e101a34f27', |

171 | n/a | hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9' |

172 | n/a | 'b1b5dbdd8ee81a3655f83e33b2279d39' |

173 | n/a | 'bf3e848279a722c806b485a47e67c807' |

174 | n/a | 'b946a337bee8942674278859e13292fb', |

175 | n/a | }) |

176 | n/a | |

177 | n/a | # 4.5. Test Case 4 |

178 | n/a | hmactest(key = bytes(x for x in range(0x01, 0x19+1)), |

179 | n/a | data = b'\xcd'*50, |

180 | n/a | hexdigests = { |

181 | n/a | hashlib.sha224: '6c11506874013cac6a2abc1bb382627c' |

182 | n/a | 'ec6a90d86efc012de7afec5a', |

183 | n/a | hashlib.sha256: '82558a389a443c0ea4cc819899f2083a' |

184 | n/a | '85f0faa3e578f8077a2e3ff46729665b', |

185 | n/a | hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7' |

186 | n/a | '7a9981480850009cc5577c6e1f573b4e' |

187 | n/a | '6801dd23c4a7d679ccf8a386c674cffb', |

188 | n/a | hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7' |

189 | n/a | 'e576d97ff94b872de76f8050361ee3db' |

190 | n/a | 'a91ca5c11aa25eb4d679275cc5788063' |

191 | n/a | 'a5f19741120c4f2de2adebeb10a298dd', |

192 | n/a | }) |

193 | n/a | |

194 | n/a | # 4.7. Test Case 6 |

195 | n/a | hmactest(key = b'\xaa'*131, |

196 | n/a | data = b'Test Using Larger Than Block-Siz' |

197 | n/a | b'e Key - Hash Key First', |

198 | n/a | hexdigests = { |

199 | n/a | hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2' |

200 | n/a | 'd499f112f2d2b7273fa6870e', |

201 | n/a | hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f' |

202 | n/a | '8e0bc6213728c5140546040f0ee37f54', |

203 | n/a | hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4' |

204 | n/a | '4f9ef1012a2b588f3cd11f05033ac4c6' |

205 | n/a | '0c2ef6ab4030fe8296248df163f44952', |

206 | n/a | hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4' |

207 | n/a | '9b46d1f41b4aeec1121b013783f8f352' |

208 | n/a | '6b56d037e05f2598bd0fd2215d6a1e52' |

209 | n/a | '95e64f73f63f0aec8b915a985d786598', |

210 | n/a | }) |

211 | n/a | |

212 | n/a | # 4.8. Test Case 7 |

213 | n/a | hmactest(key = b'\xaa'*131, |

214 | n/a | data = b'This is a test using a larger th' |

215 | n/a | b'an block-size key and a larger t' |

216 | n/a | b'han block-size data. The key nee' |

217 | n/a | b'ds to be hashed before being use' |

218 | n/a | b'd by the HMAC algorithm.', |

219 | n/a | hexdigests = { |

220 | n/a | hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd' |

221 | n/a | '946770db9c2b95c9f6f565d1', |

222 | n/a | hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944' |

223 | n/a | 'bfdc63644f0713938a7f51535c3a35e2', |

224 | n/a | hashlib.sha384: '6617178e941f020d351e2f254e8fd32c' |

225 | n/a | '602420feb0b8fb9adccebb82461e99c5' |

226 | n/a | 'a678cc31e799176d3860e6110c46523e', |

227 | n/a | hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd' |

228 | n/a | 'debd71f8867289865df5a32d20cdc944' |

229 | n/a | 'b6022cac3c4982b10d5eeb55c3e4de15' |

230 | n/a | '134676fb6de0446065c97440fa8c6a58', |

231 | n/a | }) |

232 | n/a | |

233 | n/a | def test_sha224_rfc4231(self): |

234 | n/a | self._rfc4231_test_cases(hashlib.sha224, 'sha224', 28, 64) |

235 | n/a | |

236 | n/a | def test_sha256_rfc4231(self): |

237 | n/a | self._rfc4231_test_cases(hashlib.sha256, 'sha256', 32, 64) |

238 | n/a | |

239 | n/a | def test_sha384_rfc4231(self): |

240 | n/a | self._rfc4231_test_cases(hashlib.sha384, 'sha384', 48, 128) |

241 | n/a | |

242 | n/a | def test_sha512_rfc4231(self): |

243 | n/a | self._rfc4231_test_cases(hashlib.sha512, 'sha512', 64, 128) |

244 | n/a | |

245 | n/a | def test_legacy_block_size_warnings(self): |

246 | n/a | class MockCrazyHash(object): |

247 | n/a | """Ain't no block_size attribute here.""" |

248 | n/a | def __init__(self, *args): |

249 | n/a | self._x = hashlib.sha1(*args) |

250 | n/a | self.digest_size = self._x.digest_size |

251 | n/a | def update(self, v): |

252 | n/a | self._x.update(v) |

253 | n/a | def digest(self): |

254 | n/a | return self._x.digest() |

255 | n/a | |

256 | n/a | with warnings.catch_warnings(): |

257 | n/a | warnings.simplefilter('error', RuntimeWarning) |

258 | n/a | with self.assertRaises(RuntimeWarning): |

259 | n/a | hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) |

260 | n/a | self.fail('Expected warning about missing block_size') |

261 | n/a | |

262 | n/a | MockCrazyHash.block_size = 1 |

263 | n/a | with self.assertRaises(RuntimeWarning): |

264 | n/a | hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash) |

265 | n/a | self.fail('Expected warning about small block_size') |

266 | n/a | |

267 | n/a | def test_with_digestmod_warning(self): |

268 | n/a | with self.assertWarns(PendingDeprecationWarning): |

269 | n/a | key = b"\x0b" * 16 |

270 | n/a | data = b"Hi There" |

271 | n/a | digest = "9294727A3638BB1C13F48EF8158BFC9D" |

272 | n/a | h = hmac.HMAC(key, data) |

273 | n/a | self.assertEqual(h.hexdigest().upper(), digest) |

274 | n/a | |

275 | n/a | |

276 | n/a | class ConstructorTestCase(unittest.TestCase): |

277 | n/a | |

278 | n/a | @ignore_warning |

279 | n/a | def test_normal(self): |

280 | n/a | # Standard constructor call. |

281 | n/a | failed = 0 |

282 | n/a | try: |

283 | n/a | h = hmac.HMAC(b"key") |

284 | n/a | except Exception: |

285 | n/a | self.fail("Standard constructor call raised exception.") |

286 | n/a | |

287 | n/a | @ignore_warning |

288 | n/a | def test_with_str_key(self): |

289 | n/a | # Pass a key of type str, which is an error, because it expects a key |

290 | n/a | # of type bytes |

291 | n/a | with self.assertRaises(TypeError): |

292 | n/a | h = hmac.HMAC("key") |

293 | n/a | |

294 | n/a | @ignore_warning |

295 | n/a | def test_dot_new_with_str_key(self): |

296 | n/a | # Pass a key of type str, which is an error, because it expects a key |

297 | n/a | # of type bytes |

298 | n/a | with self.assertRaises(TypeError): |

299 | n/a | h = hmac.new("key") |

300 | n/a | |

301 | n/a | @ignore_warning |

302 | n/a | def test_withtext(self): |

303 | n/a | # Constructor call with text. |

304 | n/a | try: |

305 | n/a | h = hmac.HMAC(b"key", b"hash this!") |

306 | n/a | except Exception: |

307 | n/a | self.fail("Constructor call with text argument raised exception.") |

308 | n/a | self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') |

309 | n/a | |

310 | n/a | def test_with_bytearray(self): |

311 | n/a | try: |

312 | n/a | h = hmac.HMAC(bytearray(b"key"), bytearray(b"hash this!"), |

313 | n/a | digestmod="md5") |

314 | n/a | except Exception: |

315 | n/a | self.fail("Constructor call with bytearray arguments raised exception.") |

316 | n/a | self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') |

317 | n/a | |

318 | n/a | def test_with_memoryview_msg(self): |

319 | n/a | try: |

320 | n/a | h = hmac.HMAC(b"key", memoryview(b"hash this!"), digestmod="md5") |

321 | n/a | except Exception: |

322 | n/a | self.fail("Constructor call with memoryview msg raised exception.") |

323 | n/a | self.assertEqual(h.hexdigest(), '34325b639da4cfd95735b381e28cb864') |

324 | n/a | |

325 | n/a | def test_withmodule(self): |

326 | n/a | # Constructor call with text and digest module. |

327 | n/a | try: |

328 | n/a | h = hmac.HMAC(b"key", b"", hashlib.sha1) |

329 | n/a | except Exception: |

330 | n/a | self.fail("Constructor call with hashlib.sha1 raised exception.") |

331 | n/a | |

332 | n/a | class SanityTestCase(unittest.TestCase): |

333 | n/a | |

334 | n/a | @ignore_warning |

335 | n/a | def test_default_is_md5(self): |

336 | n/a | # Testing if HMAC defaults to MD5 algorithm. |

337 | n/a | # NOTE: this whitebox test depends on the hmac class internals |

338 | n/a | h = hmac.HMAC(b"key") |

339 | n/a | self.assertEqual(h.digest_cons, hashlib.md5) |

340 | n/a | |

341 | n/a | def test_exercise_all_methods(self): |

342 | n/a | # Exercising all methods once. |

343 | n/a | # This must not raise any exceptions |

344 | n/a | try: |

345 | n/a | h = hmac.HMAC(b"my secret key", digestmod="md5") |

346 | n/a | h.update(b"compute the hash of this text!") |

347 | n/a | dig = h.digest() |

348 | n/a | dig = h.hexdigest() |

349 | n/a | h2 = h.copy() |

350 | n/a | except Exception: |

351 | n/a | self.fail("Exception raised during normal usage of HMAC class.") |

352 | n/a | |

353 | n/a | class CopyTestCase(unittest.TestCase): |

354 | n/a | |

355 | n/a | def test_attributes(self): |

356 | n/a | # Testing if attributes are of same type. |

357 | n/a | h1 = hmac.HMAC(b"key", digestmod="md5") |

358 | n/a | h2 = h1.copy() |

359 | n/a | self.assertTrue(h1.digest_cons == h2.digest_cons, |

360 | n/a | "digest constructors don't match.") |

361 | n/a | self.assertEqual(type(h1.inner), type(h2.inner), |

362 | n/a | "Types of inner don't match.") |

363 | n/a | self.assertEqual(type(h1.outer), type(h2.outer), |

364 | n/a | "Types of outer don't match.") |

365 | n/a | |

366 | n/a | def test_realcopy(self): |

367 | n/a | # Testing if the copy method created a real copy. |

368 | n/a | h1 = hmac.HMAC(b"key", digestmod="md5") |

369 | n/a | h2 = h1.copy() |

370 | n/a | # Using id() in case somebody has overridden __eq__/__ne__. |

371 | n/a | self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.") |

372 | n/a | self.assertTrue(id(h1.inner) != id(h2.inner), |

373 | n/a | "No real copy of the attribute 'inner'.") |

374 | n/a | self.assertTrue(id(h1.outer) != id(h2.outer), |

375 | n/a | "No real copy of the attribute 'outer'.") |

376 | n/a | |

377 | n/a | def test_equality(self): |

378 | n/a | # Testing if the copy has the same digests. |

379 | n/a | h1 = hmac.HMAC(b"key", digestmod="md5") |

380 | n/a | h1.update(b"some random text") |

381 | n/a | h2 = h1.copy() |

382 | n/a | self.assertEqual(h1.digest(), h2.digest(), |

383 | n/a | "Digest of copy doesn't match original digest.") |

384 | n/a | self.assertEqual(h1.hexdigest(), h2.hexdigest(), |

385 | n/a | "Hexdigest of copy doesn't match original hexdigest.") |

386 | n/a | |

387 | n/a | class CompareDigestTestCase(unittest.TestCase): |

388 | n/a | |

389 | n/a | def test_compare_digest(self): |

390 | n/a | # Testing input type exception handling |

391 | n/a | a, b = 100, 200 |

392 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

393 | n/a | a, b = 100, b"foobar" |

394 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

395 | n/a | a, b = b"foobar", 200 |

396 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

397 | n/a | a, b = "foobar", b"foobar" |

398 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

399 | n/a | a, b = b"foobar", "foobar" |

400 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

401 | n/a | |

402 | n/a | # Testing bytes of different lengths |

403 | n/a | a, b = b"foobar", b"foo" |

404 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

405 | n/a | a, b = b"\xde\xad\xbe\xef", b"\xde\xad" |

406 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

407 | n/a | |

408 | n/a | # Testing bytes of same lengths, different values |

409 | n/a | a, b = b"foobar", b"foobaz" |

410 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

411 | n/a | a, b = b"\xde\xad\xbe\xef", b"\xab\xad\x1d\xea" |

412 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

413 | n/a | |

414 | n/a | # Testing bytes of same lengths, same values |

415 | n/a | a, b = b"foobar", b"foobar" |

416 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

417 | n/a | a, b = b"\xde\xad\xbe\xef", b"\xde\xad\xbe\xef" |

418 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

419 | n/a | |

420 | n/a | # Testing bytearrays of same lengths, same values |

421 | n/a | a, b = bytearray(b"foobar"), bytearray(b"foobar") |

422 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

423 | n/a | |

424 | n/a | # Testing bytearrays of diffeent lengths |

425 | n/a | a, b = bytearray(b"foobar"), bytearray(b"foo") |

426 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

427 | n/a | |

428 | n/a | # Testing bytearrays of same lengths, different values |

429 | n/a | a, b = bytearray(b"foobar"), bytearray(b"foobaz") |

430 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

431 | n/a | |

432 | n/a | # Testing byte and bytearray of same lengths, same values |

433 | n/a | a, b = bytearray(b"foobar"), b"foobar" |

434 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

435 | n/a | self.assertTrue(hmac.compare_digest(b, a)) |

436 | n/a | |

437 | n/a | # Testing byte bytearray of diffeent lengths |

438 | n/a | a, b = bytearray(b"foobar"), b"foo" |

439 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

440 | n/a | self.assertFalse(hmac.compare_digest(b, a)) |

441 | n/a | |

442 | n/a | # Testing byte and bytearray of same lengths, different values |

443 | n/a | a, b = bytearray(b"foobar"), b"foobaz" |

444 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

445 | n/a | self.assertFalse(hmac.compare_digest(b, a)) |

446 | n/a | |

447 | n/a | # Testing str of same lengths |

448 | n/a | a, b = "foobar", "foobar" |

449 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

450 | n/a | |

451 | n/a | # Testing str of diffeent lengths |

452 | n/a | a, b = "foo", "foobar" |

453 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

454 | n/a | |

455 | n/a | # Testing bytes of same lengths, different values |

456 | n/a | a, b = "foobar", "foobaz" |

457 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

458 | n/a | |

459 | n/a | # Testing error cases |

460 | n/a | a, b = "foobar", b"foobar" |

461 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

462 | n/a | a, b = b"foobar", "foobar" |

463 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

464 | n/a | a, b = b"foobar", 1 |

465 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

466 | n/a | a, b = 100, 200 |

467 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

468 | n/a | a, b = "fooÃ¤", "fooÃ¤" |

469 | n/a | self.assertRaises(TypeError, hmac.compare_digest, a, b) |

470 | n/a | |

471 | n/a | # subclasses are supported by ignore __eq__ |

472 | n/a | class mystr(str): |

473 | n/a | def __eq__(self, other): |

474 | n/a | return False |

475 | n/a | |

476 | n/a | a, b = mystr("foobar"), mystr("foobar") |

477 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

478 | n/a | a, b = mystr("foobar"), "foobar" |

479 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

480 | n/a | a, b = mystr("foobar"), mystr("foobaz") |

481 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

482 | n/a | |

483 | n/a | class mybytes(bytes): |

484 | n/a | def __eq__(self, other): |

485 | n/a | return False |

486 | n/a | |

487 | n/a | a, b = mybytes(b"foobar"), mybytes(b"foobar") |

488 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

489 | n/a | a, b = mybytes(b"foobar"), b"foobar" |

490 | n/a | self.assertTrue(hmac.compare_digest(a, b)) |

491 | n/a | a, b = mybytes(b"foobar"), mybytes(b"foobaz") |

492 | n/a | self.assertFalse(hmac.compare_digest(a, b)) |

493 | n/a | |

494 | n/a | |

495 | n/a | if __name__ == "__main__": |

496 | n/a | unittest.main() |