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 | |
| 6 | from rsa import blocks |
| 7 | |
| 8 | class VarintTest(unittest.TestCase): |
| 9 | |
| 10 | def test_read_varint(self): |
| 11 | |
| 12 | encoded = '\xac\x02crummy' |
| 13 | infile = StringIO(encoded) |
| 14 | |
| 15 | (decoded, read) = blocks.read_varint(infile) |
| 16 | |
| 17 | # Test the returned values |
| 18 | self.assertEqual(300, decoded) |
| 19 | self.assertEqual(2, read) |
| 20 | |
| 21 | # The rest of the file should be untouched |
| 22 | self.assertEqual('crummy', infile.read()) |
| 23 | |
| 24 | def test_read_zero(self): |
| 25 | |
| 26 | encoded = '\x00crummy' |
| 27 | infile = StringIO(encoded) |
| 28 | |
| 29 | (decoded, read) = blocks.read_varint(infile) |
| 30 | |
| 31 | # Test the returned values |
| 32 | self.assertEqual(0, decoded) |
| 33 | self.assertEqual(1, read) |
| 34 | |
| 35 | # The rest of the file should be untouched |
| 36 | self.assertEqual('crummy', infile.read()) |
| 37 | |
| 38 | def test_write_varint(self): |
| 39 | |
| 40 | expected = '\xac\x02' |
| 41 | outfile = StringIO() |
| 42 | |
| 43 | written = blocks.write_varint(outfile, 300) |
| 44 | |
| 45 | # Test the returned values |
| 46 | self.assertEqual(expected, outfile.getvalue()) |
| 47 | self.assertEqual(2, written) |
| 48 | |
| 49 | |
| 50 | def test_write_zero(self): |
| 51 | |
| 52 | outfile = StringIO() |
| 53 | written = blocks.write_varint(outfile, 0) |
| 54 | |
| 55 | # Test the returned values |
| 56 | self.assertEqual('\x00', outfile.getvalue()) |
| 57 | self.assertEqual(1, written) |
| 58 | |