11'''Tests varblock operations.'''
22
3+
34try :
4- from StringIO import StringIO
5+ from StringIO import StringIO as BytesIO
56except ImportError :
6- from io import StringIO
7+ from io import BytesIO
78import unittest
89
910import rsa
11+ from rsa ._compat import b
1012from rsa import varblock
1113
1214class VarintTest (unittest .TestCase ):
1315
1416 def test_read_varint (self ):
1517
16- encoded = '\xac \x02 crummy'
17- infile = StringIO (encoded )
18+ encoded = b ( '\xac \x02 crummy' )
19+ infile = BytesIO (encoded )
1820
1921 (decoded , read ) = varblock .read_varint (infile )
2022
@@ -23,12 +25,12 @@ def test_read_varint(self):
2325 self .assertEqual (2 , read )
2426
2527 # The rest of the file should be untouched
26- self .assertEqual ('crummy' , infile .read ())
28+ self .assertEqual (b ( 'crummy' ) , infile .read ())
2729
2830 def test_read_zero (self ):
2931
30- encoded = '\x00 crummy'
31- infile = StringIO (encoded )
32+ encoded = b ( '\x00 crummy' )
33+ infile = BytesIO (encoded )
3234
3335 (decoded , read ) = varblock .read_varint (infile )
3436
@@ -37,12 +39,12 @@ def test_read_zero(self):
3739 self .assertEqual (1 , read )
3840
3941 # The rest of the file should be untouched
40- self .assertEqual ('crummy' , infile .read ())
42+ self .assertEqual (b ( 'crummy' ) , infile .read ())
4143
4244 def test_write_varint (self ):
4345
44- expected = '\xac \x02 '
45- outfile = StringIO ()
46+ expected = b ( '\xac \x02 ' )
47+ outfile = BytesIO ()
4648
4749 written = varblock .write_varint (outfile , 300 )
4850
@@ -53,28 +55,28 @@ def test_write_varint(self):
5355
5456 def test_write_zero (self ):
5557
56- outfile = StringIO ()
58+ outfile = BytesIO ()
5759 written = varblock .write_varint (outfile , 0 )
5860
5961 # Test the returned values
60- self .assertEqual ('\x00 ' , outfile .getvalue ())
62+ self .assertEqual (b ( '\x00 ' ) , outfile .getvalue ())
6163 self .assertEqual (1 , written )
6264
6365
6466class VarblockTest (unittest .TestCase ):
6567
6668 def test_yield_varblock (self ):
67- infile = StringIO ( '\x01 \x05 12345\x06 Sybren' )
69+ infile = BytesIO ( b ( '\x01 \x05 12345\x06 Sybren' ) )
6870
6971 varblocks = list (varblock .yield_varblocks (infile ))
70- self .assertEqual (['12345' , 'Sybren' ], varblocks )
72+ self .assertEqual ([b ( '12345' ), b ( 'Sybren' ) ], varblocks )
7173
7274class FixedblockTest (unittest .TestCase ):
7375
7476 def test_yield_fixedblock (self ):
7577
76- infile = StringIO ( '123456Sybren' )
78+ infile = BytesIO ( b ( '123456Sybren' ) )
7779
7880 fixedblocks = list (varblock .yield_fixedblocks (infile , 6 ))
79- self .assertEqual (['123456' , 'Sybren' ], fixedblocks )
81+ self .assertEqual ([b ( '123456' ), b ( 'Sybren' ) ], fixedblocks )
8082
0 commit comments