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"""
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')
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
55 self
.description
= description
57 def shortDescription(self
):
58 return self
.description
61 h
= self
.hashmod
.new()
64 out1
= binascii
.b2a_hex(h
.digest())
67 h
= self
.hashmod
.new(self
.input)
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
85 self
.hashmods
= hashmods
86 self
.description
= description
88 def shortDescription(self
):
89 return self
.description
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
)
104 out1
= binascii
.b2a_hex(h
.digest())
107 h
= self
.hashmod
.new(key
, data
, hashmod
)
110 out4
= binascii
.b2a_hex(h
.digest())
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
):
125 for i
in range(len(test_data
)):
128 (expected
, input) = row
129 description
= repr(input)
131 (expected
, input, description
) = row
132 name
= "%s #%d: %s" % (module_name
, i
+1, description
)
133 tests
.append(HashSelfTest(module
, name
, expected
, input))
136 def make_mac_tests(module
, module_name
, test_data
, hashmods
):
138 for i
in range(len(test_data
)):
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
))
145 # vim:set ts=4 sw=4 sts=4 expandtab: