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