Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 #
      4 #  Copyright 2011 Sybren A. Stvel <sybren (at] stuvel.eu>
      5 #
      6 #  Licensed under the Apache License, Version 2.0 (the "License");
      7 #  you may not use this file except in compliance with the License.
      8 #  You may obtain a copy of the License at
      9 #
     10 #      https://www.apache.org/licenses/LICENSE-2.0
     11 #
     12 #  Unless required by applicable law or agreed to in writing, software
     13 #  distributed under the License is distributed on an "AS IS" BASIS,
     14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 #  See the License for the specific language governing permissions and
     16 #  limitations under the License.
     17 
     18 import unittest
     19 import struct
     20 from rsa._compat import byte
     21 from rsa.common import byte_size, bit_size, inverse
     22 
     23 
     24 class TestByte(unittest.TestCase):
     25     def test_values(self):
     26         self.assertEqual(byte(0), b'\x00')
     27         self.assertEqual(byte(255), b'\xff')
     28 
     29     def test_struct_error_when_out_of_bounds(self):
     30         self.assertRaises(struct.error, byte, 256)
     31         self.assertRaises(struct.error, byte, -1)
     32 
     33 
     34 class TestByteSize(unittest.TestCase):
     35     def test_values(self):
     36         self.assertEqual(byte_size(1 << 1023), 128)
     37         self.assertEqual(byte_size((1 << 1024) - 1), 128)
     38         self.assertEqual(byte_size(1 << 1024), 129)
     39         self.assertEqual(byte_size(255), 1)
     40         self.assertEqual(byte_size(256), 2)
     41         self.assertEqual(byte_size(0xffff), 2)
     42         self.assertEqual(byte_size(0xffffff), 3)
     43         self.assertEqual(byte_size(0xffffffff), 4)
     44         self.assertEqual(byte_size(0xffffffffff), 5)
     45         self.assertEqual(byte_size(0xffffffffffff), 6)
     46         self.assertEqual(byte_size(0xffffffffffffff), 7)
     47         self.assertEqual(byte_size(0xffffffffffffffff), 8)
     48 
     49     def test_zero(self):
     50         self.assertEqual(byte_size(0), 1)
     51 
     52     def test_bad_type(self):
     53         self.assertRaises(TypeError, byte_size, [])
     54         self.assertRaises(TypeError, byte_size, ())
     55         self.assertRaises(TypeError, byte_size, dict())
     56         self.assertRaises(TypeError, byte_size, "")
     57         self.assertRaises(TypeError, byte_size, None)
     58 
     59 
     60 class TestBitSize(unittest.TestCase):
     61     def test_zero(self):
     62         self.assertEqual(bit_size(0), 0)
     63 
     64     def test_values(self):
     65         self.assertEqual(bit_size(1023), 10)
     66         self.assertEqual(bit_size(1024), 11)
     67         self.assertEqual(bit_size(1025), 11)
     68         self.assertEqual(bit_size(1 << 1024), 1025)
     69         self.assertEqual(bit_size((1 << 1024) + 1), 1025)
     70         self.assertEqual(bit_size((1 << 1024) - 1), 1024)
     71 
     72     def test_negative_values(self):
     73         self.assertEqual(bit_size(-1023), 10)
     74         self.assertEqual(bit_size(-1024), 11)
     75         self.assertEqual(bit_size(-1025), 11)
     76         self.assertEqual(bit_size(-1 << 1024), 1025)
     77         self.assertEqual(bit_size(-((1 << 1024) + 1)), 1025)
     78         self.assertEqual(bit_size(-((1 << 1024) - 1)), 1024)
     79 
     80     def test_bad_type(self):
     81         self.assertRaises(TypeError, bit_size, [])
     82         self.assertRaises(TypeError, bit_size, ())
     83         self.assertRaises(TypeError, bit_size, dict())
     84         self.assertRaises(TypeError, bit_size, "")
     85         self.assertRaises(TypeError, bit_size, None)
     86         self.assertRaises(TypeError, bit_size, 0.0)
     87 
     88 
     89 class TestInverse(unittest.TestCase):
     90     def test_normal(self):
     91         self.assertEqual(3, inverse(7, 4))
     92         self.assertEqual(9, inverse(5, 11))
     93 
     94     def test_not_relprime(self):
     95         self.assertRaises(ValueError, inverse, 4, 8)
     96         self.assertRaises(ValueError, inverse, 25, 5)
     97