blob: d8addb420d512f0e9432e5393f15e72ce5e84c8b [file] [log] [blame]
Sybren A. Stüvel3200f892011-07-31 20:47:49 +02001'''Tests varblock operations.'''
2
3from StringIO import StringIO
4import unittest
5
6import rsa
7from rsa import varblock
8
9class VarintTest(unittest.TestCase):
10
11 def test_read_varint(self):
12
13 encoded = '\xac\x02crummy'
14 infile = StringIO(encoded)
15
16 (decoded, read) = varblock.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) = varblock.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 = varblock.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 = varblock.write_varint(outfile, 0)
55
56 # Test the returned values
57 self.assertEqual('\x00', outfile.getvalue())
58 self.assertEqual(1, written)
59
60
61class VarblockTest(unittest.TestCase):
62
63 def test_yield_varblock(self):
64 infile = StringIO('\x01\x0512345\x06Sybren')
65
66 varblocks = list(varblock.yield_varblocks(infile))
67 self.assertEqual(['12345', 'Sybren'], varblocks)
68
69class FixedblockTest(unittest.TestCase):
70
71 def test_yield_fixedblock(self):
72
73 infile = StringIO('123456Sybren')
74
75 fixedblocks = list(varblock.yield_fixedblocks(infile, 6))
76 self.assertEqual(['123456', 'Sybren'], fixedblocks)
77