Roy Kokkelkoren | 0659aac | 2015-10-25 16:12:11 +0100 | [diff] [blame] | 1 | # |
| 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 Mangalapilly | b5bab22 | 2011-08-24 16:53:39 +0530 | [diff] [blame] | 16 | '''Tests varblock operations.''' |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 17 | |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 18 | |
Yesudeep Mangalapilly | 5802431 | 2011-08-11 01:48:25 +0530 | [diff] [blame] | 19 | try: |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 20 | from StringIO import StringIO as BytesIO |
Yesudeep Mangalapilly | 5802431 | 2011-08-11 01:48:25 +0530 | [diff] [blame] | 21 | except ImportError: |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 22 | from io import BytesIO |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 23 | import unittest |
| 24 | |
| 25 | import rsa |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 26 | from rsa._compat import b |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 27 | from rsa import varblock |
| 28 | |
| 29 | class VarintTest(unittest.TestCase): |
| 30 | |
| 31 | def test_read_varint(self): |
| 32 | |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 33 | encoded = b('\xac\x02crummy') |
| 34 | infile = BytesIO(encoded) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 35 | |
| 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 Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 43 | self.assertEqual(b('crummy'), infile.read()) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 44 | |
| 45 | def test_read_zero(self): |
| 46 | |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 47 | encoded = b('\x00crummy') |
| 48 | infile = BytesIO(encoded) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 49 | |
| 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 Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 57 | self.assertEqual(b('crummy'), infile.read()) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 58 | |
| 59 | def test_write_varint(self): |
| 60 | |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 61 | expected = b('\xac\x02') |
| 62 | outfile = BytesIO() |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 63 | |
| 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 Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 73 | outfile = BytesIO() |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 74 | written = varblock.write_varint(outfile, 0) |
| 75 | |
| 76 | # Test the returned values |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 77 | self.assertEqual(b('\x00'), outfile.getvalue()) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 78 | self.assertEqual(1, written) |
| 79 | |
| 80 | |
| 81 | class VarblockTest(unittest.TestCase): |
| 82 | |
| 83 | def test_yield_varblock(self): |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 84 | infile = BytesIO(b('\x01\x0512345\x06Sybren')) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 85 | |
| 86 | varblocks = list(varblock.yield_varblocks(infile)) |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 87 | self.assertEqual([b('12345'), b('Sybren')], varblocks) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 88 | |
| 89 | class FixedblockTest(unittest.TestCase): |
| 90 | |
| 91 | def test_yield_fixedblock(self): |
| 92 | |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 93 | infile = BytesIO(b('123456Sybren')) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 94 | |
| 95 | fixedblocks = list(varblock.yield_fixedblocks(infile, 6)) |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 96 | self.assertEqual([b('123456'), b('Sybren')], fixedblocks) |
Sybren A. Stüvel | 3200f89 | 2011-07-31 20:47:49 +0200 | [diff] [blame] | 97 | |