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