blob: ce5f03a9426bfa14385cf55d1104d0e6439abbb6 [file] [log] [blame]
Sybren A. Stüvel006cbf12011-07-30 21:39:10 +02001'''Tests block operations.'''
2
3from StringIO import StringIO
4import unittest
5
6from rsa import blocks
7
8class 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
Sybren A. Stüvel5524a392011-07-30 21:58:13 +020059
60class VarblockTest(unittest.TestCase):
61
62 def test_yield_varblock(self):
63 infile = StringIO('\x01\x0512345\x06Sybren')
64
65 varblocks = list(blocks.yield_varblocks(infile))
66 self.assertEqual(['12345', 'Sybren'], varblocks)
67
Sybren A. Stüvele8988522011-07-31 19:54:53 +020068class FixedblockTest(unittest.TestCase):
Sybren A. Stüvel5524a392011-07-30 21:58:13 +020069
Sybren A. Stüvele8988522011-07-31 19:54:53 +020070 def test_yield_fixedblock(self):
71
72 infile = StringIO('123456Sybren')
73
74 fixedblocks = list(blocks.yield_fixedblocks(infile, 6))
75 self.assertEqual(['123456', 'Sybren'], fixedblocks)