Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame^] | 1 | '''Tests block operations.''' |
| 2 | |
| 3 | from StringIO import StringIO |
| 4 | import unittest |
| 5 | |
| 6 | import rsa |
| 7 | from rsa import bigfile, varblock |
| 8 | |
| 9 | class BigfileTest(unittest.TestCase): |
| 10 | |
| 11 | def test_encrypt_decrypt_bigfile(self): |
| 12 | |
| 13 | # Expected block size + 11 bytes padding |
| 14 | pub_key, priv_key = rsa.newkeys((6 + 11) * 8) |
| 15 | |
| 16 | # Encrypt the file |
| 17 | message = '123456Sybren' |
| 18 | infile = StringIO(message) |
| 19 | outfile = StringIO() |
| 20 | |
| 21 | bigfile.encrypt_bigfile(infile, outfile, pub_key) |
| 22 | |
| 23 | # Test |
| 24 | crypto = outfile.getvalue() |
| 25 | |
| 26 | cryptfile = StringIO(crypto) |
| 27 | clearfile = StringIO() |
| 28 | |
| 29 | bigfile.decrypt_bigfile(cryptfile, clearfile, priv_key) |
| 30 | self.assertEquals(clearfile.getvalue(), message) |
| 31 | |
| 32 | # We have 2x6 bytes in the message, so that should result in two |
| 33 | # bigfile. |
| 34 | cryptfile.seek(0) |
| 35 | varblocks = list(varblock.yield_varblocks(cryptfile)) |
| 36 | self.assertEqual(2, len(varblocks)) |
| 37 | |