Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 1 | '''Tests varblock operations.''' |
| 2 | |
Yesudeep Mangalapilly | 5802431 | 2011-08-11 01:48:25 +0530 | [diff] [blame^] | 3 | try: |
| 4 | from StringIO import StringIO |
| 5 | except ImportError: |
| 6 | from io import StringIO |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 7 | import unittest |
| 8 | |
| 9 | import rsa |
| 10 | from rsa import varblock |
| 11 | |
| 12 | class VarintTest(unittest.TestCase): |
| 13 | |
| 14 | def test_read_varint(self): |
| 15 | |
| 16 | encoded = '\xac\x02crummy' |
| 17 | infile = StringIO(encoded) |
| 18 | |
| 19 | (decoded, read) = varblock.read_varint(infile) |
| 20 | |
| 21 | # Test the returned values |
| 22 | self.assertEqual(300, decoded) |
| 23 | self.assertEqual(2, read) |
| 24 | |
| 25 | # The rest of the file should be untouched |
| 26 | self.assertEqual('crummy', infile.read()) |
| 27 | |
| 28 | def test_read_zero(self): |
| 29 | |
| 30 | encoded = '\x00crummy' |
| 31 | infile = StringIO(encoded) |
| 32 | |
| 33 | (decoded, read) = varblock.read_varint(infile) |
| 34 | |
| 35 | # Test the returned values |
| 36 | self.assertEqual(0, decoded) |
| 37 | self.assertEqual(1, read) |
| 38 | |
| 39 | # The rest of the file should be untouched |
| 40 | self.assertEqual('crummy', infile.read()) |
| 41 | |
| 42 | def test_write_varint(self): |
| 43 | |
| 44 | expected = '\xac\x02' |
| 45 | outfile = StringIO() |
| 46 | |
| 47 | written = varblock.write_varint(outfile, 300) |
| 48 | |
| 49 | # Test the returned values |
| 50 | self.assertEqual(expected, outfile.getvalue()) |
| 51 | self.assertEqual(2, written) |
| 52 | |
| 53 | |
| 54 | def test_write_zero(self): |
| 55 | |
| 56 | outfile = StringIO() |
| 57 | written = varblock.write_varint(outfile, 0) |
| 58 | |
| 59 | # Test the returned values |
| 60 | self.assertEqual('\x00', outfile.getvalue()) |
| 61 | self.assertEqual(1, written) |
| 62 | |
| 63 | |
| 64 | class VarblockTest(unittest.TestCase): |
| 65 | |
| 66 | def test_yield_varblock(self): |
| 67 | infile = StringIO('\x01\x0512345\x06Sybren') |
| 68 | |
| 69 | varblocks = list(varblock.yield_varblocks(infile)) |
| 70 | self.assertEqual(['12345', 'Sybren'], varblocks) |
| 71 | |
| 72 | class FixedblockTest(unittest.TestCase): |
| 73 | |
| 74 | def test_yield_fixedblock(self): |
| 75 | |
| 76 | infile = StringIO('123456Sybren') |
| 77 | |
| 78 | fixedblocks = list(varblock.yield_fixedblocks(infile, 6)) |
| 79 | self.assertEqual(['123456', 'Sybren'], fixedblocks) |
| 80 | |