| 1 | """ |
| 2 | Tests for the md5 module implemented at interp-level in pypy/module/_md5. |
| 3 | """ |
| 4 | |
| 5 | import py, sys |
| 6 | |
| 7 | |
| 8 | class AppTestMD5(object): |
| 9 | spaceconfig = { |
| 10 | 'usemodules': ['_md5', 'binascii', 'time', 'struct'], |
| 11 | } |
| 12 | |
| 13 | def setup_class(cls): |
| 14 | """ |
| 15 | Create a space with the md5 module and import it for use by the |
| 16 | tests. |
| 17 | """ |
| 18 | cls.w_md5 = cls.space.appexec([], """(): |
| 19 | import _md5 |
| 20 | return _md5 |
| 21 | """) |
| 22 | |
| 23 | |
| 24 | def test_name(self): |
| 25 | """ |
| 26 | md5.name should be 'md5'. |
| 27 | """ |
| 28 | assert self.md5.md5().name == 'md5' |
| 29 | |
| 30 | def test_digest_size(self): |
| 31 | """ |
| 32 | md5.digest_size should be 16. |
| 33 | """ |
| 34 | assert self.md5.md5().digest_size == 16 |
| 35 | |
| 36 | def test_MD5Type(self): |
| 37 | """ |
| 38 | Test the construction of an md5 object. |
| 39 | """ |
| 40 | md5 = self.md5 |
| 41 | d = md5.md5() |
| 42 | |
| 43 | def test_md5object(self): |
| 44 | """ |
| 45 | Feed example strings into a md5 object and check the digest and |
| 46 | hexdigest. |
| 47 | """ |
| 48 | md5 = self.md5 |
| 49 | import binascii |
| 50 | cases = ( |
| 51 | (b"", |
| 52 | "d41d8cd98f00b204e9800998ecf8427e"), |
| 53 | (b"a", |
| 54 | "0cc175b9c0f1b6a831c399e269772661"), |
| 55 | (b"abc", |
| 56 | "900150983cd24fb0d6963f7d28e17f72"), |
| 57 | (b"message digest", |
| 58 | "f96b697d7cb7938d525a2f31aaf161d0"), |
| 59 | (b"abcdefghijklmnopqrstuvwxyz", |
| 60 | "c3fcd3d76192e4007dfb496cca67e13b"), |
| 61 | (b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", |
| 62 | "d174ab98d277d9f5a5611c2c9f419d9f"), |
| 63 | (b"1234567890" * 8, |
| 64 | "57edf4a22be3c955ac49da2e2107b67a"), |
| 65 | ) |
| 66 | for input, expected in cases: |
| 67 | d = md5.md5(input) |
| 68 | assert d.hexdigest() == expected |
| 69 | assert d.digest() == binascii.unhexlify(expected.encode('ascii')) |
| 70 | |
| 71 | def test_copy(self): |
| 72 | """ |
| 73 | Test the copy() method. |
| 74 | """ |
| 75 | md5 = self.md5 |
| 76 | d1 = md5.md5() |
| 77 | d1.update(b"abcde") |
| 78 | d2 = d1.copy() |
| 79 | d2.update(b"fgh") |
| 80 | d1.update(b"jkl") |
| 81 | assert d1.hexdigest() == 'e570e7110ecef72fcb772a9c05d03373' |
| 82 | assert d2.hexdigest() == 'e8dc4081b13434b45189a720b77b6818' |
| 83 | |
| 84 | def test_buffer(self): |
| 85 | """ |
| 86 | Test passing a buffer object. |
| 87 | """ |
| 88 | md5 = self.md5 |
| 89 | d1 = md5.md5(memoryview(b"abcde")) |
| 90 | d1.update(memoryview(b"jkl")) |
| 91 | assert d1.hexdigest() == 'e570e7110ecef72fcb772a9c05d03373' |
| 92 | |
| 93 | def test_unicode(self): |
| 94 | """ |
| 95 | Test passing unicode strings. |
| 96 | """ |
| 97 | md5 = self.md5 |
| 98 | raises(TypeError, md5.md5, "abcde") |
| 99 | d1 = md5.md5() |
| 100 | raises(TypeError, d1.update, "jkl") |
| 101 | |
| 102 | def test_repr(self): |
| 103 | md5 = self.md5 |
| 104 | assert 'md5' in repr(md5.md5()) |