blob: 6dad5abfb98cc5ed4f28be2ce7312e89ad37be10 [file] [log] [blame]
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01001#
2# Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Yesudeep Mangalapillyb5bab222011-08-24 16:53:39 +053016'''Tests varblock operations.'''
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020017
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053018
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053019try:
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053020 from StringIO import StringIO as BytesIO
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053021except ImportError:
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053022 from io import BytesIO
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020023import unittest
24
25import rsa
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053026from rsa._compat import b
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020027from rsa import varblock
28
29class VarintTest(unittest.TestCase):
30
31 def test_read_varint(self):
32
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053033 encoded = b('\xac\x02crummy')
34 infile = BytesIO(encoded)
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020035
36 (decoded, read) = varblock.read_varint(infile)
37
38 # Test the returned values
39 self.assertEqual(300, decoded)
40 self.assertEqual(2, read)
41
42 # The rest of the file should be untouched
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053043 self.assertEqual(b('crummy'), infile.read())
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020044
45 def test_read_zero(self):
46
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053047 encoded = b('\x00crummy')
48 infile = BytesIO(encoded)
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020049
50 (decoded, read) = varblock.read_varint(infile)
51
52 # Test the returned values
53 self.assertEqual(0, decoded)
54 self.assertEqual(1, read)
55
56 # The rest of the file should be untouched
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053057 self.assertEqual(b('crummy'), infile.read())
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020058
59 def test_write_varint(self):
60
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053061 expected = b('\xac\x02')
62 outfile = BytesIO()
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020063
64 written = varblock.write_varint(outfile, 300)
65
66 # Test the returned values
67 self.assertEqual(expected, outfile.getvalue())
68 self.assertEqual(2, written)
69
70
71 def test_write_zero(self):
72
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053073 outfile = BytesIO()
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020074 written = varblock.write_varint(outfile, 0)
75
76 # Test the returned values
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053077 self.assertEqual(b('\x00'), outfile.getvalue())
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020078 self.assertEqual(1, written)
79
80
81class VarblockTest(unittest.TestCase):
82
83 def test_yield_varblock(self):
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053084 infile = BytesIO(b('\x01\x0512345\x06Sybren'))
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020085
86 varblocks = list(varblock.yield_varblocks(infile))
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053087 self.assertEqual([b('12345'), b('Sybren')], varblocks)
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020088
89class FixedblockTest(unittest.TestCase):
90
91 def test_yield_fixedblock(self):
92
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053093 infile = BytesIO(b('123456Sybren'))
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020094
95 fixedblocks = list(varblock.yield_fixedblocks(infile, 6))
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053096 self.assertEqual([b('123456'), b('Sybren')], fixedblocks)
Sybren A. Stüvel3200f892011-07-31 20:47:49 +020097