blob: 7b9335e19642252c644fe78f6758ddeb79583cfb [file] [log] [blame]
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01001# 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üvel3934ab42016-02-05 16:01:20 +01007# https://www.apache.org/licenses/LICENSE-2.0
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01008#
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 Mangalapillyc19a21b2011-08-11 04:48:11 +053014
Sybren A. Stüveled1c81d2016-01-14 12:23:32 +010015import unittest
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020016from rsa.transform import int2bytes, bytes2int
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053017
18
Sybren A. Stüveled1c81d2016-01-14 12:23:32 +010019class Test_int2bytes(unittest.TestCase):
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053020 def test_accuracy(self):
adamantike9f577402016-05-08 15:36:57 -030021 self.assertEqual(int2bytes(123456789), b'\x07[\xcd\x15')
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053022
23 def test_codec_identity(self):
24 self.assertEqual(bytes2int(int2bytes(123456789, 128)), 123456789)
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053025
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053026 def test_chunk_size(self):
adamantike9f577402016-05-08 15:36:57 -030027 self.assertEqual(int2bytes(123456789, 6), b'\x00\x00\x07[\xcd\x15')
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053028 self.assertEqual(int2bytes(123456789, 7),
adamantike9f577402016-05-08 15:36:57 -030029 b'\x00\x00\x00\x07[\xcd\x15')
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053030
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053031 def test_zero(self):
adamantike9f577402016-05-08 15:36:57 -030032 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 Mangalapilly81325242011-08-12 13:06:51 +053035
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053036 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 Mangalapilly81325242011-08-12 13:06:51 +053044 self.assertEqual(bytes2int(int2bytes(value)),
45 value,
46 "Boom %d" % value)
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053047
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053048 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 Mangalapilly81325242011-08-12 13:06:51 +053051
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053052 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)