CFB: added segment_size to docstring of the cipher wrappers
[python-cryptoplus.git] / src / CryptoPlus / SelfTest / Hash / common.py
blob4c2bf1fa3d07c187c9bbffd4d7c4d409a33b1ea1
1 # -*- coding: utf-8 -*-
3 # SelfTest/Hash/common.py: Common code for CryptoPlus.SelfTest.Hash
5 # =======================================================================
6 # Copyright (C) 2008 Dwayne C. Litzenberger <dlitz@dlitz.net>
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 # =======================================================================
29 """Self-testing for PyCryptoPlus hash modules"""
31 __revision__ = "$Id$"
33 import sys
34 import unittest
35 import binascii
36 import string
38 # For compatibility with Python 2.1 and Python 2.2
39 if sys.hexversion < 0x02030000:
40 # Python 2.1 doesn't have a dict() function
41 # Python 2.2 dict() function raises TypeError if you do dict(MD5='blah')
42 def dict(**kwargs):
43 return kwargs.copy()
44 else:
45 dict = __builtins__['dict']
48 class HashSelfTest(unittest.TestCase):
50 def __init__(self, hashmod, description, expected, input):
51 unittest.TestCase.__init__(self)
52 self.hashmod = hashmod
53 self.expected = expected
54 self.input = input
55 self.description = description
57 def shortDescription(self):
58 return self.description
60 def runTest(self):
61 h = self.hashmod.new()
62 h.update(self.input)
64 out1 = binascii.b2a_hex(h.digest())
65 out2 = h.hexdigest()
67 h = self.hashmod.new(self.input)
69 out3 = h.hexdigest()
70 out4 = binascii.b2a_hex(h.digest())
72 self.assertEqual(self.expected, out1)
73 self.assertEqual(self.expected, out2)
74 self.assertEqual(self.expected, out3)
75 self.assertEqual(self.expected, out4)
77 class MACSelfTest(unittest.TestCase):
79 def __init__(self, hashmod, description, expected_dict, input, key, hashmods):
80 unittest.TestCase.__init__(self)
81 self.hashmod = hashmod
82 self.expected_dict = expected_dict
83 self.input = input
84 self.key = key
85 self.hashmods = hashmods
86 self.description = description
88 def shortDescription(self):
89 return self.description
91 def runTest(self):
92 for hashname in self.expected_dict.keys():
93 hashmod = self.hashmods[hashname]
94 key = binascii.a2b_hex(self.key)
95 data = binascii.a2b_hex(self.input)
97 # Strip whitespace from the expected string (which should be in lowercase-hex)
98 expected = self.expected_dict[hashname]
99 for ch in string.whitespace:
100 expected = expected.replace(ch, "")
102 h = self.hashmod.new(key, digestmod=hashmod)
103 h.update(data)
104 out1 = binascii.b2a_hex(h.digest())
105 out2 = h.hexdigest()
107 h = self.hashmod.new(key, data, hashmod)
109 out3 = h.hexdigest()
110 out4 = binascii.b2a_hex(h.digest())
112 # Test .copy()
113 h2 = h.copy()
114 h.update("blah blah blah") # Corrupt the original hash object
115 out5 = binascii.b2a_hex(h2.digest()) # The copied hash object should return the correct result
117 self.assertEqual(expected, out1)
118 self.assertEqual(expected, out2)
119 self.assertEqual(expected, out3)
120 self.assertEqual(expected, out4)
121 self.assertEqual(expected, out5)
123 def make_hash_tests(module, module_name, test_data):
124 tests = []
125 for i in range(len(test_data)):
126 row = test_data[i]
127 if len(row) < 3:
128 (expected, input) = row
129 description = repr(input)
130 else:
131 (expected, input, description) = row
132 name = "%s #%d: %s" % (module_name, i+1, description)
133 tests.append(HashSelfTest(module, name, expected, input))
134 return tests
136 def make_mac_tests(module, module_name, test_data, hashmods):
137 tests = []
138 for i in range(len(test_data)):
139 row = test_data[i]
140 (key, data, results, description) = row
141 name = "%s #%d: %s" % (module_name, i+1, description)
142 tests.append(MACSelfTest(module, name, results, data, key, hashmods))
143 return tests
145 # vim:set ts=4 sw=4 sts=4 expandtab: