OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # |
| 3 # SelfTest/Hash/common.py: Common code for Crypto.SelfTest.Hash |
| 4 # |
| 5 # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net> |
| 6 # |
| 7 # =================================================================== |
| 8 # The contents of this file are dedicated to the public domain. To |
| 9 # the extent that dedication to the public domain is not available, |
| 10 # everyone is granted a worldwide, perpetual, royalty-free, |
| 11 # non-exclusive license to exercise all rights associated with the |
| 12 # contents of this file for any purpose whatsoever. |
| 13 # No rights are reserved. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 # SOFTWARE. |
| 23 # =================================================================== |
| 24 |
| 25 """Self-testing for PyCrypto hash modules""" |
| 26 |
| 27 __revision__ = "$Id$" |
| 28 |
| 29 import sys |
| 30 import unittest |
| 31 import binascii |
| 32 from Crypto.Util.py3compat import * |
| 33 |
| 34 # For compatibility with Python 2.1 and Python 2.2 |
| 35 if sys.hexversion < 0x02030000: |
| 36 # Python 2.1 doesn't have a dict() function |
| 37 # Python 2.2 dict() function raises TypeError if you do dict(MD5='blah') |
| 38 def dict(**kwargs): |
| 39 return kwargs.copy() |
| 40 else: |
| 41 dict = dict |
| 42 |
| 43 |
| 44 class HashDigestSizeSelfTest(unittest.TestCase): |
| 45 |
| 46 def __init__(self, hashmod, description, expected): |
| 47 unittest.TestCase.__init__(self) |
| 48 self.hashmod = hashmod |
| 49 self.expected = expected |
| 50 self.description = description |
| 51 |
| 52 def shortDescription(self): |
| 53 return self.description |
| 54 |
| 55 def runTest(self): |
| 56 self.failUnless(hasattr(self.hashmod, "digest_size")) |
| 57 self.assertEquals(self.hashmod.digest_size, self.expected) |
| 58 h = self.hashmod.new() |
| 59 self.failUnless(hasattr(h, "digest_size")) |
| 60 self.assertEquals(h.digest_size, self.expected) |
| 61 |
| 62 |
| 63 class HashSelfTest(unittest.TestCase): |
| 64 |
| 65 def __init__(self, hashmod, description, expected, input): |
| 66 unittest.TestCase.__init__(self) |
| 67 self.hashmod = hashmod |
| 68 self.expected = expected |
| 69 self.input = input |
| 70 self.description = description |
| 71 |
| 72 def shortDescription(self): |
| 73 return self.description |
| 74 |
| 75 def runTest(self): |
| 76 h = self.hashmod.new() |
| 77 h.update(self.input) |
| 78 |
| 79 out1 = binascii.b2a_hex(h.digest()) |
| 80 out2 = h.hexdigest() |
| 81 |
| 82 h = self.hashmod.new(self.input) |
| 83 |
| 84 out3 = h.hexdigest() |
| 85 out4 = binascii.b2a_hex(h.digest()) |
| 86 |
| 87 # PY3K: hexdigest() should return str(), and digest() bytes |
| 88 self.assertEqual(self.expected, out1) # h = .new(); h.update(data); h.
digest() |
| 89 if sys.version_info[0] == 2: |
| 90 self.assertEqual(self.expected, out2) # h = .new(); h.update(data)
; h.hexdigest() |
| 91 self.assertEqual(self.expected, out3) # h = .new(data); h.hexdiges
t() |
| 92 else: |
| 93 self.assertEqual(self.expected.decode(), out2) # h = .new(); h.upd
ate(data); h.hexdigest() |
| 94 self.assertEqual(self.expected.decode(), out3) # h = .new(data); h
.hexdigest() |
| 95 self.assertEqual(self.expected, out4) # h = .new(data); h.digest() |
| 96 |
| 97 # Verify that new() object method produces a fresh hash object |
| 98 h2 = h.new() |
| 99 h2.update(self.input) |
| 100 out5 = binascii.b2a_hex(h2.digest()) |
| 101 self.assertEqual(self.expected, out5) |
| 102 |
| 103 class HashTestOID(unittest.TestCase): |
| 104 def __init__(self, hashmod, oid): |
| 105 unittest.TestCase.__init__(self) |
| 106 self.hashmod = hashmod |
| 107 self.oid = oid |
| 108 |
| 109 def runTest(self): |
| 110 h = self.hashmod.new() |
| 111 if self.oid==None: |
| 112 try: |
| 113 raised = 0 |
| 114 a = h.oid |
| 115 except AttributeError: |
| 116 raised = 1 |
| 117 self.assertEqual(raised,1) |
| 118 else: |
| 119 self.assertEqual(h.oid, self.oid) |
| 120 |
| 121 class MACSelfTest(unittest.TestCase): |
| 122 |
| 123 def __init__(self, hashmod, description, expected_dict, input, key, hashmods
): |
| 124 unittest.TestCase.__init__(self) |
| 125 self.hashmod = hashmod |
| 126 self.expected_dict = expected_dict |
| 127 self.input = input |
| 128 self.key = key |
| 129 self.hashmods = hashmods |
| 130 self.description = description |
| 131 |
| 132 def shortDescription(self): |
| 133 return self.description |
| 134 |
| 135 def runTest(self): |
| 136 for hashname in self.expected_dict.keys(): |
| 137 hashmod = self.hashmods[hashname] |
| 138 key = binascii.a2b_hex(b(self.key)) |
| 139 data = binascii.a2b_hex(b(self.input)) |
| 140 |
| 141 # Strip whitespace from the expected string (which should be in lowe
rcase-hex) |
| 142 expected = b("".join(self.expected_dict[hashname].split())) |
| 143 |
| 144 h = self.hashmod.new(key, digestmod=hashmod) |
| 145 h.update(data) |
| 146 out1 = binascii.b2a_hex(h.digest()) |
| 147 out2 = h.hexdigest() |
| 148 |
| 149 h = self.hashmod.new(key, data, hashmod) |
| 150 |
| 151 out3 = h.hexdigest() |
| 152 out4 = binascii.b2a_hex(h.digest()) |
| 153 |
| 154 # Test .copy() |
| 155 h2 = h.copy() |
| 156 h.update(b("blah blah blah")) # Corrupt the original hash object |
| 157 out5 = binascii.b2a_hex(h2.digest()) # The copied hash object sho
uld return the correct result |
| 158 |
| 159 # PY3K: hexdigest() should return str(), and digest() bytes |
| 160 self.assertEqual(expected, out1) |
| 161 if sys.version_info[0] == 2: |
| 162 self.assertEqual(expected, out2) |
| 163 self.assertEqual(expected, out3) |
| 164 else: |
| 165 self.assertEqual(expected.decode(), out2) |
| 166 self.assertEqual(expected.decode(), out3) |
| 167 self.assertEqual(expected, out4) |
| 168 self.assertEqual(expected, out5) |
| 169 |
| 170 def make_hash_tests(module, module_name, test_data, digest_size, oid=None): |
| 171 tests = [] |
| 172 for i in range(len(test_data)): |
| 173 row = test_data[i] |
| 174 (expected, input) = map(b,row[0:2]) |
| 175 if len(row) < 3: |
| 176 description = repr(input) |
| 177 else: |
| 178 description = row[2].encode('latin-1') |
| 179 name = "%s #%d: %s" % (module_name, i+1, description) |
| 180 tests.append(HashSelfTest(module, name, expected, input)) |
| 181 if oid is not None: |
| 182 oid = b(oid) |
| 183 name = "%s #%d: digest_size" % (module_name, i+1) |
| 184 tests.append(HashDigestSizeSelfTest(module, name, digest_size)) |
| 185 tests.append(HashTestOID(module, oid)) |
| 186 return tests |
| 187 |
| 188 def make_mac_tests(module, module_name, test_data, hashmods): |
| 189 tests = [] |
| 190 for i in range(len(test_data)): |
| 191 row = test_data[i] |
| 192 (key, data, results, description) = row |
| 193 name = "%s #%d: %s" % (module_name, i+1, description) |
| 194 tests.append(MACSelfTest(module, name, results, data, key, hashmods)) |
| 195 return tests |
| 196 |
| 197 # vim:set ts=4 sw=4 sts=4 expandtab: |
OLD | NEW |