annotate Lib/test/test_sha.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents b380c9c49738
children 787b969d37f0 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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):
40356
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
18 # Check digest matches the expected value
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
19 obj = sha.new(data)
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
20 computed = obj.hexdigest()
17922
5abc2329922f Move the sha tests to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 10459
diff changeset
21 self.assert_(computed == digest)
10459
4c1bbedad14b Added Jeremy's test code for the sha module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
40356
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
23 # Verify that the value doesn't change between two consecutive
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
24 # digest operations.
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
25 computed_again = obj.hexdigest()
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
26 self.assert_(computed == computed_again)
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
27
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
28 # Check hexdigest() output matches digest()'s output
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
29 digest = obj.digest()
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
30 hexd = ""
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
31 for c in digest:
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
32 hexd += '%02x' % ord(c)
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
parents: 24364
diff changeset
33 self.assert_(computed == hexd)
5fa5ef559a2f Expand checking in test_sha
Andrew M. Kuchling <amk@amk.ca>
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
40359
be2f5b2d53b3 Add a test case of data w/ bytes > 127
Andrew M. Kuchling <amk@amk.ca>
parents: 40356
diff changeset
47 def test_case_4(self):
be2f5b2d53b3 Add a test case of data w/ bytes > 127
Andrew M. Kuchling <amk@amk.ca>
parents: 40356
diff changeset
48 self.check(chr(0xAA) * 80,
be2f5b2d53b3 Add a test case of data w/ bytes > 127
Andrew M. Kuchling <amk@amk.ca>
parents: 40356
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()