| 1 |
"""A test script to compare MD5 implementations. |
| 2 |
|
| 3 |
A note about performance: the pure Python MD5 takes roughly 160 sec. per |
| 4 |
MB of data on a 233 MHz Intel Pentium CPU. |
| 5 |
""" |
| 6 |
import md5 |
| 7 |
|
| 8 |
from pypy.module.test_lib_pypy.support import import_lib_pypy |
| 9 |
|
| 10 |
|
| 11 |
def compare_host(message, d2, d2h): |
| 12 |
"""Compare results against the host Python's builtin md5. |
| 13 |
|
| 14 |
For equal digests this returns None, otherwise it returns a tuple of |
| 15 |
both digests. |
| 16 |
""" |
| 17 |
|
| 18 |
m1 = md5.md5() |
| 19 |
m1.update(message) |
| 20 |
d1 = m1.digest() |
| 21 |
d1h = m1.hexdigest() |
| 22 |
|
| 23 |
return None if d1 == d2 and d1h == d2h else (d1, d2) |
| 24 |
|
| 25 |
|
| 26 |
class TestMD5Update: |
| 27 |
|
| 28 |
spaceconfig = dict(usemodules=('struct',)) |
| 29 |
|
| 30 |
def setup_class(cls): |
| 31 |
if cls.runappdirect: |
| 32 |
|
| 33 |
import py |
| 34 |
py.test.skip('Unavailable under py3 runappdirect') |
| 35 |
|
| 36 |
def test_update(self): |
| 37 |
"""Test updating cloned objects.""" |
| 38 |
cases = ( |
| 39 |
b"123", |
| 40 |
b"1234", |
| 41 |
b"12345", |
| 42 |
b"123456", |
| 43 |
b"1234567", |
| 44 |
b"12345678", |
| 45 |
b"123456789 123456789 123456789 ", |
| 46 |
b"123456789 123456789 ", |
| 47 |
b"123456789 123456789 1", |
| 48 |
b"123456789 123456789 12", |
| 49 |
b"123456789 123456789 123", |
| 50 |
b"123456789 123456789 1234", |
| 51 |
b"123456789 123456789 123456789 1", |
| 52 |
b"123456789 123456789 123456789 12", |
| 53 |
b"123456789 123456789 123456789 123", |
| 54 |
b"123456789 123456789 123456789 1234", |
| 55 |
b"123456789 123456789 123456789 12345", |
| 56 |
b"123456789 123456789 123456789 123456", |
| 57 |
b"123456789 123456789 123456789 1234567", |
| 58 |
b"123456789 123456789 123456789 12345678", |
| 59 |
) |
| 60 |
space = self.space |
| 61 |
w__md5 = import_lib_pypy(space, '_md5') |
| 62 |
|
| 63 |
|
| 64 |
prefix1 = 2**10 * b'a' |
| 65 |
|
| 66 |
|
| 67 |
m1 = md5.md5() |
| 68 |
m1.update(prefix1) |
| 69 |
m1c = m1.copy() |
| 70 |
|
| 71 |
|
| 72 |
w_m2 = space.call_method(w__md5, 'md5') |
| 73 |
space.call_method(w_m2, 'update', space.newbytes(prefix1)) |
| 74 |
w_m2c = space.call_method(w_m2, 'copy') |
| 75 |
|
| 76 |
|
| 77 |
for message in cases: |
| 78 |
m1c.update(message) |
| 79 |
d1 = m1c.hexdigest() |
| 80 |
|
| 81 |
space.call_method(w_m2c, 'update', space.newbytes(message)) |
| 82 |
w_d2 = space.call_method(w_m2c, 'hexdigest') |
| 83 |
d2 = space.text_w(w_d2) |
| 84 |
|
| 85 |
assert d1 == d2 |
| 86 |
|
| 87 |
|
| 88 |
class AppTestMD5Compare: |
| 89 |
"""Compare pure Python MD5 against Python's std. lib. version.""" |
| 90 |
|
| 91 |
spaceconfig = dict(usemodules=('struct',)) |
| 92 |
|
| 93 |
def setup_class(cls): |
| 94 |
from pypy.interpreter import gateway |
| 95 |
space = cls.space |
| 96 |
cls.w__md5 = import_lib_pypy(space, '_md5') |
| 97 |
if cls.runappdirect: |
| 98 |
|
| 99 |
import py |
| 100 |
py.test.skip('Unavailable under py3 runappdirect') |
| 101 |
else: |
| 102 |
compare_host.unwrap_spec = ['bytes', 'bytes', 'text'] |
| 103 |
cls.w_compare_host = space.wrap(gateway.interp2app(compare_host)) |
| 104 |
|
| 105 |
def w_compare(self, message): |
| 106 |
|
| 107 |
|
| 108 |
m2 = self._md5.md5() |
| 109 |
m2.update(message) |
| 110 |
return self.compare_host(message, m2.digest(), m2.hexdigest()) |
| 111 |
|
| 112 |
def w__format_hex(self, string): |
| 113 |
"""Print a string's HEX code in groups of two digits.""" |
| 114 |
d = map(None, string) |
| 115 |
d = map(ord, d) |
| 116 |
d = map(lambda x: "%02x" % x, d) |
| 117 |
return ' '.join(d) |
| 118 |
|
| 119 |
def w__format(self, string): |
| 120 |
"""Print a string as-is in groups of two characters.""" |
| 121 |
s = '' |
| 122 |
for i in range(0, len(string) - 1, 2): |
| 123 |
s = s + "%03s" % string[i:i + 2] |
| 124 |
return s[1:] |
| 125 |
|
| 126 |
def w_print_diff(self, message, d1, d2, expectedResult=None): |
| 127 |
"""Print different outputs for same message.""" |
| 128 |
print("Message: '%s'" % message) |
| 129 |
print("Message length: %d" % len(message)) |
| 130 |
if expectedResult: |
| 131 |
print("%-48s (expected)" % self._format(expectedResult)) |
| 132 |
print("%-48s (Std. lib. MD5)" % self._format_hex(d1)) |
| 133 |
print("%-48s (Pure Python MD5)" % self._format_hex(d2)) |
| 134 |
print() |
| 135 |
|
| 136 |
def test1(self): |
| 137 |
"""Test cases with known digest result.""" |
| 138 |
cases = ( |
| 139 |
(b"", |
| 140 |
"d41d8cd98f00b204e9800998ecf8427e"), |
| 141 |
(b"a", |
| 142 |
"0cc175b9c0f1b6a831c399e269772661"), |
| 143 |
(b"abc", |
| 144 |
"900150983cd24fb0d6963f7d28e17f72"), |
| 145 |
(b"message digest", |
| 146 |
"f96b697d7cb7938d525a2f31aaf161d0"), |
| 147 |
(b"abcdefghijklmnopqrstuvwxyz", |
| 148 |
"c3fcd3d76192e4007dfb496cca67e13b"), |
| 149 |
(b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", |
| 150 |
"d174ab98d277d9f5a5611c2c9f419d9f"), |
| 151 |
(b"1234567890"*8, |
| 152 |
"57edf4a22be3c955ac49da2e2107b67a"), |
| 153 |
) |
| 154 |
|
| 155 |
for message, expectedResult in cases: |
| 156 |
res = self.compare(message) |
| 157 |
if res is not None: |
| 158 |
d1, d2 = res |
| 159 |
self.print_diff(message, d1, d2, expectedResult) |
| 160 |
assert res is None |
| 161 |
|
| 162 |
def test2(self): |
| 163 |
"""Test cases without known digest result.""" |
| 164 |
cases = ( |
| 165 |
b"123", |
| 166 |
b"1234", |
| 167 |
b"12345", |
| 168 |
b"123456", |
| 169 |
b"1234567", |
| 170 |
b"12345678", |
| 171 |
b"123456789 123456789 123456789 ", |
| 172 |
b"123456789 123456789 ", |
| 173 |
b"123456789 123456789 1", |
| 174 |
b"123456789 123456789 12", |
| 175 |
b"123456789 123456789 123", |
| 176 |
b"123456789 123456789 1234", |
| 177 |
b"123456789 123456789 123456789 1", |
| 178 |
b"123456789 123456789 123456789 12", |
| 179 |
b"123456789 123456789 123456789 123", |
| 180 |
b"123456789 123456789 123456789 1234", |
| 181 |
b"123456789 123456789 123456789 12345", |
| 182 |
b"123456789 123456789 123456789 123456", |
| 183 |
b"123456789 123456789 123456789 1234567", |
| 184 |
b"123456789 123456789 123456789 12345678", |
| 185 |
) |
| 186 |
|
| 187 |
for message in cases: |
| 188 |
res = self.compare(message) |
| 189 |
if res is not None: |
| 190 |
d1, d2 = res |
| 191 |
self.print_diff(message, d1, d2) |
| 192 |
assert res is None |
| 193 |
|
| 194 |
def test3(self): |
| 195 |
"""Test cases with long messages (can take a while).""" |
| 196 |
cases = ( |
| 197 |
2**10*b'a', |
| 198 |
2**10*b'abcd', |
| 199 |
|
| 200 |
) |
| 201 |
|
| 202 |
for message in cases: |
| 203 |
res = self.compare(message) |
| 204 |
if res is not None: |
| 205 |
d1, d2 = res |
| 206 |
self.print_diff(message, d1, d2) |
| 207 |
assert res is None |
| 208 |
|
| 209 |
def test4(self): |
| 210 |
"""Test cases with increasingly growing message lengths.""" |
| 211 |
i = 0 |
| 212 |
while i < 2**5: |
| 213 |
message = i * b'a' |
| 214 |
res = self.compare(message) |
| 215 |
if res is not None: |
| 216 |
d1, d2 = res |
| 217 |
self.print_diff(message, d1, d2) |
| 218 |
assert res is None |
| 219 |
i += 1 |
| 220 |
|
| 221 |
def test_attributes(self): |
| 222 |
_md5 = self._md5 |
| 223 |
assert _md5.md5().digest_size == 16 |
| 224 |
assert _md5.md5().digestsize == 16 |
| 225 |
assert _md5.md5().block_size == 64 |