Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_sha.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 |
|---|---|
|
10459
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 # Testing sha module (NIST's Secure Hash Algorithm) |
|
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 # use the three examples from Federal Information Processing Standards |
|
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 # Publication 180-1, Secure Hash Standard, 1995 April 17 |
|
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 # http://www.itl.nist.gov/div897/pubs/fip180-1.htm |
|
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
41924
b380c9c49738
Have the sha module raise a DeprecationWarning as specified in PEP 4.
Brett Cannon <bcannon@gmail.com>
parents:
40359
diff
changeset
|
7 import warnings |
|
b380c9c49738
Have the sha module raise a DeprecationWarning as specified in PEP 4.
Brett Cannon <bcannon@gmail.com>
parents:
40359
diff
changeset
|
8 warnings.filterwarnings("ignore", "the sha module is deprecated.*", |
|
b380c9c49738
Have the sha module raise a DeprecationWarning as specified in PEP 4.
Brett Cannon <bcannon@gmail.com>
parents:
40359
diff
changeset
|
9 DeprecationWarning) |
|
b380c9c49738
Have the sha module raise a DeprecationWarning as specified in PEP 4.
Brett Cannon <bcannon@gmail.com>
parents:
40359
diff
changeset
|
10 |
|
17922
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
11 import sha |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
12 import unittest |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
20013
diff
changeset
|
13 from test import test_support |
|
17922
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
14 |
|
10459
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
17922
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
16 class SHATestCase(unittest.TestCase): |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
17 def check(self, data, digest): |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
18 # Check digest matches the expected value |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
19 obj = sha.new(data) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
20 computed = obj.hexdigest() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
41924
diff
changeset
|
21 self.assertTrue(computed == digest) |
|
10459
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
23 # Verify that the value doesn't change between two consecutive |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
24 # digest operations. |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
25 computed_again = obj.hexdigest() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
41924
diff
changeset
|
26 self.assertTrue(computed == computed_again) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
27 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
28 # Check hexdigest() output matches digest()'s output |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
29 digest = obj.digest() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
30 hexd = "" |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
31 for c in digest: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
32 hexd += '%02x' % ord(c) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
41924
diff
changeset
|
33 self.assertTrue(computed == hexd) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
34 |
|
17922
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
35 def test_case_1(self): |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
36 self.check("abc", |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
37 "a9993e364706816aba3e25717850c26c9cd0d89d") |
|
10459
4c1bbedad14b
Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 |
|
17922
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
39 def test_case_2(self): |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
40 self.check("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
41 "84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
42 |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
43 def test_case_3(self): |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
44 self.check("a" * 1000000, |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
45 "34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
|
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
46 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
47 def test_case_4(self): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
48 self.check(chr(0xAA) * 80, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
24364
diff
changeset
|
49 '4ca0ef38f1794b28a8f8ee110ee79d48ce13be25') |
|
17922
5abc2329922f
Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents:
10459
diff
changeset
|
50 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17922
diff
changeset
|
51 def test_main(): |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17922
diff
changeset
|
52 test_support.run_unittest(SHATestCase) |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17922
diff
changeset
|
53 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17922
diff
changeset
|
54 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17922
diff
changeset
|
55 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17922
diff
changeset
|
56 test_main() |
