summaryrefslogtreecommitdiff
path: root/tests/test_common.py
blob: a563d21dc3b2821359c9c716f3f5befeb35a318b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import unittest
import struct
from rsa._compat import byte, b
from rsa.common import byte_size, bit_size, _bit_size


class Test_byte(unittest.TestCase):
    def test_values(self):
        self.assertEqual(byte(0), b('\x00'))
        self.assertEqual(byte(255), b('\xff'))

    def test_struct_error_when_out_of_bounds(self):
        self.assertRaises(struct.error, byte, 256)
        self.assertRaises(struct.error, byte, -1)

class Test_byte_size(unittest.TestCase):
    def test_values(self):
        self.assertEqual(byte_size(1 << 1023), 128)
        self.assertEqual(byte_size((1 << 1024) - 1), 128)
        self.assertEqual(byte_size(1 << 1024), 129)
        self.assertEqual(byte_size(255), 1)
        self.assertEqual(byte_size(256), 2)
        self.assertEqual(byte_size(0xffff), 2)
        self.assertEqual(byte_size(0xffffff), 3)
        self.assertEqual(byte_size(0xffffffff), 4)
        self.assertEqual(byte_size(0xffffffffff), 5)
        self.assertEqual(byte_size(0xffffffffffff), 6)
        self.assertEqual(byte_size(0xffffffffffffff), 7)
        self.assertEqual(byte_size(0xffffffffffffffff), 8)

    def test_zero(self):
        self.assertEqual(byte_size(0), 1)

    def test_bad_type(self):
        self.assertRaises(TypeError, byte_size, [])
        self.assertRaises(TypeError, byte_size, ())
        self.assertRaises(TypeError, byte_size, dict())
        self.assertRaises(TypeError, byte_size, "")
        self.assertRaises(TypeError, byte_size, None)

class Test_bit_size(unittest.TestCase):
    def test_zero(self):
        self.assertEqual(bit_size(0), 0)

    def test_values(self):
        self.assertEqual(bit_size(1023), 10)
        self.assertEqual(bit_size(1024), 11)
        self.assertEqual(bit_size(1025), 11)
        self.assertEqual(bit_size(1 << 1024), 1025)
        self.assertEqual(bit_size((1 << 1024) + 1), 1025)
        self.assertEqual(bit_size((1 << 1024) - 1), 1024)

        self.assertEqual(_bit_size(1023), 10)
        self.assertEqual(_bit_size(1024), 11)
        self.assertEqual(_bit_size(1025), 11)
        self.assertEqual(_bit_size(1 << 1024), 1025)
        self.assertEqual(_bit_size((1 << 1024) + 1), 1025)
        self.assertEqual(_bit_size((1 << 1024) - 1), 1024)