Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_hmac.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | b36cb4602e21 |
| children |
| rev | line source |
|---|---|
| 19863 | 1 import hmac |
|
43774
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
2 import hashlib |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
3 import unittest |
|
43773
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
4 import warnings |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
22738
diff
changeset
|
5 from test import test_support |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
6 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
7 class TestVectorsTestCase(unittest.TestCase): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
8 |
|
29278
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
9 def test_md5_vectors(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
10 # Test the HMAC module against test vectors from the RFC. |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
11 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
12 def md5test(key, data, digest): |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
13 h = hmac.HMAC(key, data) |
|
29278
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
14 self.assertEqual(h.hexdigest().upper(), digest.upper()) |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
15 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
16 md5test(chr(0x0b) * 16, |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
17 "Hi There", |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
18 "9294727A3638BB1C13F48EF8158BFC9D") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
19 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
20 md5test("Jefe", |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
21 "what do ya want for nothing?", |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
22 "750c783e6ab0b503eaa86e310a5db738") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
23 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
24 md5test(chr(0xAA)*16, |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
25 chr(0xDD)*50, |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
26 "56be34521d144c88dbb8c733f0e8b3f6") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
27 |
|
29278
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
28 md5test("".join([chr(i) for i in range(1, 26)]), |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
29 chr(0xCD) * 50, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
30 "697eaf0aca3a3aea3a75164746ffaa79") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
31 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
32 md5test(chr(0x0C) * 16, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
33 "Test With Truncation", |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
34 "56461ef2342edc00f9bab995690efd4c") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
35 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
36 md5test(chr(0xAA) * 80, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
37 "Test Using Larger Than Block-Size Key - Hash Key First", |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
38 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
39 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
40 md5test(chr(0xAA) * 80, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
41 ("Test Using Larger Than Block-Size Key " |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
42 "and Larger Than One Block-Size Data"), |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
43 "6f630fad67cda0ee1fb1f562db3aa53e") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
44 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
45 def test_sha_vectors(self): |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
46 def shatest(key, data, digest): |
|
43773
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
47 h = hmac.HMAC(key, data, digestmod=hashlib.sha1) |
|
29278
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
48 self.assertEqual(h.hexdigest().upper(), digest.upper()) |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
49 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
50 shatest(chr(0x0b) * 20, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
51 "Hi There", |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
52 "b617318655057264e28bc0b6fb378c8ef146be00") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
53 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
54 shatest("Jefe", |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
55 "what do ya want for nothing?", |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
56 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
57 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
58 shatest(chr(0xAA)*20, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
59 chr(0xDD)*50, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
60 "125d7342b9ac11cd91a39af48aa17b4f63f175d3") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
61 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
62 shatest("".join([chr(i) for i in range(1, 26)]), |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
63 chr(0xCD) * 50, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
64 "4c9007f4026250c6bc8414f9bf50c86c2d7235da") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
65 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
66 shatest(chr(0x0C) * 20, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
67 "Test With Truncation", |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
68 "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
69 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
70 shatest(chr(0xAA) * 80, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
71 "Test Using Larger Than Block-Size Key - Hash Key First", |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
72 "aa4ae5e15272d00e95705637ce8a3b55ed402112") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
73 |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
74 shatest(chr(0xAA) * 80, |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
75 ("Test Using Larger Than Block-Size Key " |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
76 "and Larger Than One Block-Size Data"), |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
77 "e8e99d0f45237d786d6bbaa7965c7808bbff1a91") |
|
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
78 |
|
43774
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
79 def _rfc4231_test_cases(self, hashfunc): |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
80 def hmactest(key, data, hexdigests): |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
81 h = hmac.HMAC(key, data, digestmod=hashfunc) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
82 self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc]) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
83 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
84 # 4.2. Test Case 1 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
85 hmactest(key = '\x0b'*20, |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
86 data = 'Hi There', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
87 hexdigests = { |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
88 hashlib.sha224: '896fb1128abbdf196832107cd49df33f' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
89 '47b4b1169912ba4f53684b22', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
90 hashlib.sha256: 'b0344c61d8db38535ca8afceaf0bf12b' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
91 '881dc200c9833da726e9376c2e32cff7', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
92 hashlib.sha384: 'afd03944d84895626b0825f4ab46907f' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
93 '15f9dadbe4101ec682aa034c7cebc59c' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
94 'faea9ea9076ede7f4af152e8b2fa9cb6', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
95 hashlib.sha512: '87aa7cdea5ef619d4ff0b4241a1d6cb0' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
96 '2379f4e2ce4ec2787ad0b30545e17cde' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
97 'daa833b7d6b8a702038b274eaea3f4e4' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
98 'be9d914eeb61f1702e696c203a126854', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
99 }) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
100 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
101 # 4.3. Test Case 2 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
102 hmactest(key = 'Jefe', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
103 data = 'what do ya want for nothing?', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
104 hexdigests = { |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
105 hashlib.sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
106 '8bbea2a39e6148008fd05e44', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
107 hashlib.sha256: '5bdcc146bf60754e6a042426089575c7' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
108 '5a003f089d2739839dec58b964ec3843', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
109 hashlib.sha384: 'af45d2e376484031617f78d2b58a6b1b' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
110 '9c7ef464f5a01b47e42ec3736322445e' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
111 '8e2240ca5e69e2c78b3239ecfab21649', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
112 hashlib.sha512: '164b7a7bfcf819e2e395fbe73b56e0a3' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
113 '87bd64222e831fd610270cd7ea250554' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
114 '9758bf75c05a994a6d034f65f8f0e6fd' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
115 'caeab1a34d4a6b4b636e070a38bce737', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
116 }) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
117 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
118 # 4.4. Test Case 3 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
119 hmactest(key = '\xaa'*20, |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
120 data = '\xdd'*50, |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
121 hexdigests = { |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
122 hashlib.sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad264' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
123 '9365b0c1f65d69d1ec8333ea', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
124 hashlib.sha256: '773ea91e36800e46854db8ebd09181a7' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
125 '2959098b3ef8c122d9635514ced565fe', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
126 hashlib.sha384: '88062608d3e6ad8a0aa2ace014c8a86f' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
127 '0aa635d947ac9febe83ef4e55966144b' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
128 '2a5ab39dc13814b94e3ab6e101a34f27', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
129 hashlib.sha512: 'fa73b0089d56a284efb0f0756c890be9' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
130 'b1b5dbdd8ee81a3655f83e33b2279d39' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
131 'bf3e848279a722c806b485a47e67c807' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
132 'b946a337bee8942674278859e13292fb', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
133 }) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
134 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
135 # 4.5. Test Case 4 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
136 hmactest(key = ''.join([chr(x) for x in xrange(0x01, 0x19+1)]), |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
137 data = '\xcd'*50, |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
138 hexdigests = { |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
139 hashlib.sha224: '6c11506874013cac6a2abc1bb382627c' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
140 'ec6a90d86efc012de7afec5a', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
141 hashlib.sha256: '82558a389a443c0ea4cc819899f2083a' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
142 '85f0faa3e578f8077a2e3ff46729665b', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
143 hashlib.sha384: '3e8a69b7783c25851933ab6290af6ca7' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
144 '7a9981480850009cc5577c6e1f573b4e' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
145 '6801dd23c4a7d679ccf8a386c674cffb', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
146 hashlib.sha512: 'b0ba465637458c6990e5a8c5f61d4af7' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
147 'e576d97ff94b872de76f8050361ee3db' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
148 'a91ca5c11aa25eb4d679275cc5788063' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
149 'a5f19741120c4f2de2adebeb10a298dd', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
150 }) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
151 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
152 # 4.7. Test Case 6 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
153 hmactest(key = '\xaa'*131, |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
154 data = 'Test Using Larger Than Block-Siz' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
155 'e Key - Hash Key First', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
156 hexdigests = { |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
157 hashlib.sha224: '95e9a0db962095adaebe9b2d6f0dbce2' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
158 'd499f112f2d2b7273fa6870e', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
159 hashlib.sha256: '60e431591ee0b67f0d8a26aacbf5b77f' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
160 '8e0bc6213728c5140546040f0ee37f54', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
161 hashlib.sha384: '4ece084485813e9088d2c63a041bc5b4' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
162 '4f9ef1012a2b588f3cd11f05033ac4c6' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
163 '0c2ef6ab4030fe8296248df163f44952', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
164 hashlib.sha512: '80b24263c7c1a3ebb71493c1dd7be8b4' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
165 '9b46d1f41b4aeec1121b013783f8f352' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
166 '6b56d037e05f2598bd0fd2215d6a1e52' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
167 '95e64f73f63f0aec8b915a985d786598', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
168 }) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
169 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
170 # 4.8. Test Case 7 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
171 hmactest(key = '\xaa'*131, |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
172 data = 'This is a test using a larger th' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
173 'an block-size key and a larger t' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
174 'han block-size data. The key nee' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
175 'ds to be hashed before being use' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
176 'd by the HMAC algorithm.', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
177 hexdigests = { |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
178 hashlib.sha224: '3a854166ac5d9f023f54d517d0b39dbd' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
179 '946770db9c2b95c9f6f565d1', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
180 hashlib.sha256: '9b09ffa71b942fcb27635fbcd5b0e944' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
181 'bfdc63644f0713938a7f51535c3a35e2', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
182 hashlib.sha384: '6617178e941f020d351e2f254e8fd32c' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
183 '602420feb0b8fb9adccebb82461e99c5' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
184 'a678cc31e799176d3860e6110c46523e', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
185 hashlib.sha512: 'e37b6a775dc87dbaa4dfa9f96e5e3ffd' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
186 'debd71f8867289865df5a32d20cdc944' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
187 'b6022cac3c4982b10d5eeb55c3e4de15' |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
188 '134676fb6de0446065c97440fa8c6a58', |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
189 }) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
190 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
191 def test_sha224_rfc4231(self): |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
192 self._rfc4231_test_cases(hashlib.sha224) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
193 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
194 def test_sha256_rfc4231(self): |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
195 self._rfc4231_test_cases(hashlib.sha256) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
196 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
197 def test_sha384_rfc4231(self): |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
198 self._rfc4231_test_cases(hashlib.sha384) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
199 |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
200 def test_sha512_rfc4231(self): |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
201 self._rfc4231_test_cases(hashlib.sha512) |
|
8d31a36f0caa
Backport r58868:
Gregory P. Smith <greg@mad-scientist.com>
parents:
35375
diff
changeset
|
202 |
|
43773
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
203 def test_legacy_block_size_warnings(self): |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
204 class MockCrazyHash(object): |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
205 """Ain't no block_size attribute here.""" |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
206 def __init__(self, *args): |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
207 self._x = hashlib.sha1(*args) |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
208 self.digest_size = self._x.digest_size |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
209 def update(self, v): |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
210 self._x.update(v) |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
211 def digest(self): |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
212 return self._x.digest() |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
213 |
|
49099
ff96a313cd1c
warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents:
45938
diff
changeset
|
214 with warnings.catch_warnings(): |
|
45938
6509ef8bfe9c
Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents:
43773
diff
changeset
|
215 warnings.simplefilter('error', RuntimeWarning) |
|
60116
3748bca4d869
Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
55002
diff
changeset
|
216 with self.assertRaises(RuntimeWarning): |
|
43773
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
217 hmac.HMAC('a', 'b', digestmod=MockCrazyHash) |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
218 self.fail('Expected warning about missing block_size') |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
219 |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
220 MockCrazyHash.block_size = 1 |
|
60116
3748bca4d869
Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
55002
diff
changeset
|
221 with self.assertRaises(RuntimeWarning): |
|
43773
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
222 hmac.HMAC('a', 'b', digestmod=MockCrazyHash) |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
223 self.fail('Expected warning about small block_size') |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
224 |
|
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
225 |
|
29278
cfba76f77643
Add more tests from RFC 2202.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28917
diff
changeset
|
226 |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
227 class ConstructorTestCase(unittest.TestCase): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
228 |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
229 def test_normal(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
230 # Standard constructor call. |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
231 failed = 0 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
232 try: |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
233 h = hmac.HMAC("key") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
234 except: |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
235 self.fail("Standard constructor call raised exception.") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
236 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
237 def test_withtext(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
238 # Constructor call with text. |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
239 try: |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
240 h = hmac.HMAC("key", "hash this!") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
241 except: |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
242 self.fail("Constructor call with text argument raised exception.") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
243 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
244 def test_withmodule(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
245 # Constructor call with text and digest module. |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
246 try: |
|
43773
d7db467cd940
Fixes Issue 1385: The hmac module now computes the correct hmac when using
Gregory P. Smith <greg@mad-scientist.com>
parents:
41969
diff
changeset
|
247 h = hmac.HMAC("key", "", hashlib.sha1) |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
248 except: |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
35375
diff
changeset
|
249 self.fail("Constructor call with hashlib.sha1 raised exception.") |
|
20967
bb8716095a81
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20848
diff
changeset
|
250 |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
251 class SanityTestCase(unittest.TestCase): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
252 |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
253 def test_default_is_md5(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
254 # Testing if HMAC defaults to MD5 algorithm. |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
29278
diff
changeset
|
255 # NOTE: this whitebox test depends on the hmac class internals |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
256 h = hmac.HMAC("key") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
257 self.assertTrue(h.digest_cons == hashlib.md5) |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
258 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
259 def test_exercise_all_methods(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
260 # Exercising all methods once. |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
261 # This must not raise any exceptions |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
262 try: |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
263 h = hmac.HMAC("my secret key") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
264 h.update("compute the hash of this text!") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
265 dig = h.digest() |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
266 dig = h.hexdigest() |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
267 h2 = h.copy() |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
268 except: |
|
22738
826bc1522d22
Use attributes appropriately
Neal Norwitz <nnorwitz@gmail.com>
parents:
20967
diff
changeset
|
269 self.fail("Exception raised during normal usage of HMAC class.") |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
270 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
271 class CopyTestCase(unittest.TestCase): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
272 |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
273 def test_attributes(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
274 # Testing if attributes are of same type. |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
275 h1 = hmac.HMAC("key") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
276 h2 = h1.copy() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
277 self.assertTrue(h1.digest_cons == h2.digest_cons, |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
29278
diff
changeset
|
278 "digest constructors don't match.") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
279 self.assertTrue(type(h1.inner) == type(h2.inner), |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
280 "Types of inner don't match.") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
281 self.assertTrue(type(h1.outer) == type(h2.outer), |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
282 "Types of outer don't match.") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
283 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
284 def test_realcopy(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
285 # Testing if the copy method created a real copy. |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
286 h1 = hmac.HMAC("key") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
287 h2 = h1.copy() |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
288 # Using id() in case somebody has overridden __cmp__. |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
289 self.assertTrue(id(h1) != id(h2), "No real copy of the HMAC instance.") |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
290 self.assertTrue(id(h1.inner) != id(h2.inner), |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
291 "No real copy of the attribute 'inner'.") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
292 self.assertTrue(id(h1.outer) != id(h2.outer), |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
293 "No real copy of the attribute 'outer'.") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
294 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
295 def test_equality(self): |
|
24955
c55e8d6c091f
Standardize behavior: no docstrings in test functions; create a single
Guido van Rossum <guido@python.org>
parents:
24364
diff
changeset
|
296 # Testing if the copy has the same digests. |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
297 h1 = hmac.HMAC("key") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
298 h1.update("some random text") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
299 h2 = h1.copy() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
300 self.assertTrue(h1.digest() == h2.digest(), |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
301 "Digest of copy doesn't match original digest.") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
49099
diff
changeset
|
302 self.assertTrue(h1.hexdigest() == h2.hexdigest(), |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
303 "Hexdigest of copy doesn't match original hexdigest.") |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
304 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
305 def test_main(): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
24955
diff
changeset
|
306 test_support.run_unittest( |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
24955
diff
changeset
|
307 TestVectorsTestCase, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
24955
diff
changeset
|
308 ConstructorTestCase, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
24955
diff
changeset
|
309 SanityTestCase, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
24955
diff
changeset
|
310 CopyTestCase |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
24955
diff
changeset
|
311 ) |
|
20848
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
312 |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
313 if __name__ == "__main__": |
|
d9a9d92624db
[Patch #477336] Add an extensive PyUnit based testsuite for the hmac
Andrew M. Kuchling <amk@amk.ca>
parents:
19863
diff
changeset
|
314 test_main() |
