Sybren A. Stüvel | 006cbf1 | 2011-07-30 21:39:10 +0200 | [diff] [blame] | 1 | '''Tests block operations.''' |
| 2 | |
| 3 | from StringIO import StringIO |
| 4 | import unittest |
| 5 | |
Sybren A. Stüvel | fa345b6 | 2011-07-31 20:25:40 +0200 | [diff] [blame^] | 6 | import rsa |
Sybren A. Stüvel | 006cbf1 | 2011-07-30 21:39:10 +0200 | [diff] [blame] | 7 | from rsa import blocks |
| 8 | |
| 9 | class VarintTest(unittest.TestCase): |
| 10 | |
| 11 | def test_read_varint(self): |
| 12 | |
| 13 | encoded = '\xac\x02crummy' |
| 14 | infile = StringIO(encoded) |
| 15 | |
| 16 | (decoded, read) = blocks.read_varint(infile) |
| 17 | |
| 18 | # Test the returned values |
| 19 | self.assertEqual(300, decoded) |
| 20 | self.assertEqual(2, read) |
| 21 | |
| 22 | # The rest of the file should be untouched |
| 23 | self.assertEqual('crummy', infile.read()) |
| 24 | |
| 25 | def test_read_zero(self): |
| 26 | |
| 27 | encoded = '\x00crummy' |
| 28 | infile = StringIO(encoded) |
| 29 | |
| 30 | (decoded, read) = blocks.read_varint(infile) |
| 31 | |
| 32 | # Test the returned values |
| 33 | self.assertEqual(0, decoded) |
| 34 | self.assertEqual(1, read) |
| 35 | |
| 36 | # The rest of the file should be untouched |
| 37 | self.assertEqual('crummy', infile.read()) |
| 38 | |
| 39 | def test_write_varint(self): |
| 40 | |
| 41 | expected = '\xac\x02' |
| 42 | outfile = StringIO() |
| 43 | |
| 44 | written = blocks.write_varint(outfile, 300) |
| 45 | |
| 46 | # Test the returned values |
| 47 | self.assertEqual(expected, outfile.getvalue()) |
| 48 | self.assertEqual(2, written) |
| 49 | |
| 50 | |
| 51 | def test_write_zero(self): |
| 52 | |
| 53 | outfile = StringIO() |
| 54 | written = blocks.write_varint(outfile, 0) |
| 55 | |
| 56 | # Test the returned values |
| 57 | self.assertEqual('\x00', outfile.getvalue()) |
| 58 | self.assertEqual(1, written) |
| 59 | |
Sybren A. Stüvel | 5524a39 | 2011-07-30 21:58:13 +0200 | [diff] [blame] | 60 | |
| 61 | class VarblockTest(unittest.TestCase): |
| 62 | |
| 63 | def test_yield_varblock(self): |
| 64 | infile = StringIO('\x01\x0512345\x06Sybren') |
| 65 | |
| 66 | varblocks = list(blocks.yield_varblocks(infile)) |
| 67 | self.assertEqual(['12345', 'Sybren'], varblocks) |
| 68 | |
Sybren A. Stüvel | e898852 | 2011-07-31 19:54:53 +0200 | [diff] [blame] | 69 | class FixedblockTest(unittest.TestCase): |
Sybren A. Stüvel | 5524a39 | 2011-07-30 21:58:13 +0200 | [diff] [blame] | 70 | |
Sybren A. Stüvel | e898852 | 2011-07-31 19:54:53 +0200 | [diff] [blame] | 71 | def test_yield_fixedblock(self): |
| 72 | |
| 73 | infile = StringIO('123456Sybren') |
| 74 | |
| 75 | fixedblocks = list(blocks.yield_fixedblocks(infile, 6)) |
| 76 | self.assertEqual(['123456', 'Sybren'], fixedblocks) |
Sybren A. Stüvel | fa345b6 | 2011-07-31 20:25:40 +0200 | [diff] [blame^] | 77 | |
| 78 | class BigfileTest(unittest.TestCase): |
| 79 | |
| 80 | def test_encrypt_decrypt_bigfile(self): |
| 81 | |
| 82 | # Expected block size + 11 bytes padding |
| 83 | pub_key, priv_key = rsa.newkeys((6 + 11) * 8) |
| 84 | |
| 85 | # Encrypt the file |
| 86 | message = '123456Sybren' |
| 87 | infile = StringIO(message) |
| 88 | outfile = StringIO() |
| 89 | |
| 90 | blocks.encrypt_bigfile(infile, outfile, pub_key) |
| 91 | |
| 92 | # Test |
| 93 | crypto = outfile.getvalue() |
| 94 | |
| 95 | cryptfile = StringIO(crypto) |
| 96 | clearfile = StringIO() |
| 97 | |
| 98 | blocks.decrypt_bigfile(cryptfile, clearfile, priv_key) |
| 99 | self.assertEquals(clearfile.getvalue(), message) |
| 100 | |
| 101 | # We have 2x6 bytes in the message, so that should result in two |
| 102 | # blocks. |
| 103 | cryptfile.seek(0) |
| 104 | varblocks = list(blocks.yield_varblocks(cryptfile)) |
| 105 | self.assertEqual(2, len(varblocks)) |
| 106 | |