Roy Kokkelkoren | 0659aac | 2015-10-25 16:12:11 +0100 | [diff] [blame] | 1 | # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu> |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
Sybren A. Stüvel | 3934ab4 | 2016-02-05 16:01:20 +0100 | [diff] [blame] | 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
Roy Kokkelkoren | 0659aac | 2015-10-25 16:12:11 +0100 | [diff] [blame] | 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Yesudeep Mangalapilly | c19a21b | 2011-08-11 04:48:11 +0530 | [diff] [blame] | 14 | |
Sybren A. Stüvel | ed1c81d | 2016-01-14 12:23:32 +0100 | [diff] [blame] | 15 | import unittest |
Sybren A. Stüvel | ded036c | 2019-08-04 15:02:20 +0200 | [diff] [blame] | 16 | from rsa.transform import int2bytes, bytes2int |
Yesudeep Mangalapilly | c19a21b | 2011-08-11 04:48:11 +0530 | [diff] [blame] | 17 | |
| 18 | |
Sybren A. Stüvel | ed1c81d | 2016-01-14 12:23:32 +0100 | [diff] [blame] | 19 | class Test_int2bytes(unittest.TestCase): |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 20 | def test_accuracy(self): |
adamantike | 9f57740 | 2016-05-08 15:36:57 -0300 | [diff] [blame] | 21 | self.assertEqual(int2bytes(123456789), b'\x07[\xcd\x15') |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 22 | |
| 23 | def test_codec_identity(self): |
| 24 | self.assertEqual(bytes2int(int2bytes(123456789, 128)), 123456789) |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 25 | |
Yesudeep Mangalapilly | c19a21b | 2011-08-11 04:48:11 +0530 | [diff] [blame] | 26 | def test_chunk_size(self): |
adamantike | 9f57740 | 2016-05-08 15:36:57 -0300 | [diff] [blame] | 27 | self.assertEqual(int2bytes(123456789, 6), b'\x00\x00\x07[\xcd\x15') |
Yesudeep Mangalapilly | c19a21b | 2011-08-11 04:48:11 +0530 | [diff] [blame] | 28 | self.assertEqual(int2bytes(123456789, 7), |
adamantike | 9f57740 | 2016-05-08 15:36:57 -0300 | [diff] [blame] | 29 | b'\x00\x00\x00\x07[\xcd\x15') |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 30 | |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 31 | def test_zero(self): |
adamantike | 9f57740 | 2016-05-08 15:36:57 -0300 | [diff] [blame] | 32 | self.assertEqual(int2bytes(0, 4), b'\x00' * 4) |
| 33 | self.assertEqual(int2bytes(0, 7), b'\x00' * 7) |
| 34 | self.assertEqual(int2bytes(0), b'\x00') |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 35 | |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 36 | def test_correctness_against_base_implementation(self): |
| 37 | # Slow test. |
| 38 | values = [ |
| 39 | 1 << 512, |
| 40 | 1 << 8192, |
| 41 | 1 << 77, |
| 42 | ] |
| 43 | for value in values: |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 44 | self.assertEqual(bytes2int(int2bytes(value)), |
| 45 | value, |
| 46 | "Boom %d" % value) |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 47 | |
Yesudeep Mangalapilly | c19a21b | 2011-08-11 04:48:11 +0530 | [diff] [blame] | 48 | def test_raises_OverflowError_when_chunk_size_is_insufficient(self): |
| 49 | self.assertRaises(OverflowError, int2bytes, 123456789, 3) |
| 50 | self.assertRaises(OverflowError, int2bytes, 299999999999, 4) |
Yesudeep Mangalapilly | 8132524 | 2011-08-12 13:06:51 +0530 | [diff] [blame] | 51 | |
Yesudeep Mangalapilly | c19a21b | 2011-08-11 04:48:11 +0530 | [diff] [blame] | 52 | def test_raises_ValueError_when_negative_integer(self): |
| 53 | self.assertRaises(ValueError, int2bytes, -1) |
| 54 | |
| 55 | def test_raises_TypeError_when_not_integer(self): |
| 56 | self.assertRaises(TypeError, int2bytes, None) |