blob: 83c3934c58732d36d521d4b9b6ae5489eb6cfc2b [file] [log] [blame]
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +05301# -*- coding: utf-8 -*-
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01002#
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üvel3934ab42016-02-05 16:01:20 +01009# https://www.apache.org/licenses/LICENSE-2.0
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +010010#
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 Mangalapillyc19a21b2011-08-11 04:48:11 +053016
Sybren A. Stüveled1c81d2016-01-14 12:23:32 +010017import unittest
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020018from rsa.transform import int2bytes, bytes2int
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053019
20
Sybren A. Stüveled1c81d2016-01-14 12:23:32 +010021class Test_int2bytes(unittest.TestCase):
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053022 def test_accuracy(self):
adamantike9f577402016-05-08 15:36:57 -030023 self.assertEqual(int2bytes(123456789), b'\x07[\xcd\x15')
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053024
25 def test_codec_identity(self):
26 self.assertEqual(bytes2int(int2bytes(123456789, 128)), 123456789)
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053027
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053028 def test_chunk_size(self):
adamantike9f577402016-05-08 15:36:57 -030029 self.assertEqual(int2bytes(123456789, 6), b'\x00\x00\x07[\xcd\x15')
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053030 self.assertEqual(int2bytes(123456789, 7),
adamantike9f577402016-05-08 15:36:57 -030031 b'\x00\x00\x00\x07[\xcd\x15')
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053032
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053033 def test_zero(self):
adamantike9f577402016-05-08 15:36:57 -030034 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 Mangalapilly81325242011-08-12 13:06:51 +053037
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053038 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 Mangalapilly81325242011-08-12 13:06:51 +053046 self.assertEqual(bytes2int(int2bytes(value)),
47 value,
48 "Boom %d" % value)
Yesudeep Mangalapilly81325242011-08-12 13:06:51 +053049
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053050 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 Mangalapilly81325242011-08-12 13:06:51 +053053
Yesudeep Mangalapillyc19a21b2011-08-11 04:48:11 +053054 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)