test_common.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2011 Sybren A. Stüvel <sybren@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. import unittest
  18. import struct
  19. from rsa._compat import byte
  20. from rsa.common import byte_size, bit_size, inverse
  21. class TestByte(unittest.TestCase):
  22. def test_values(self):
  23. self.assertEqual(byte(0), b'\x00')
  24. self.assertEqual(byte(255), b'\xff')
  25. def test_struct_error_when_out_of_bounds(self):
  26. self.assertRaises(struct.error, byte, 256)
  27. self.assertRaises(struct.error, byte, -1)
  28. class TestByteSize(unittest.TestCase):
  29. def test_values(self):
  30. self.assertEqual(byte_size(1 << 1023), 128)
  31. self.assertEqual(byte_size((1 << 1024) - 1), 128)
  32. self.assertEqual(byte_size(1 << 1024), 129)
  33. self.assertEqual(byte_size(255), 1)
  34. self.assertEqual(byte_size(256), 2)
  35. self.assertEqual(byte_size(0xffff), 2)
  36. self.assertEqual(byte_size(0xffffff), 3)
  37. self.assertEqual(byte_size(0xffffffff), 4)
  38. self.assertEqual(byte_size(0xffffffffff), 5)
  39. self.assertEqual(byte_size(0xffffffffffff), 6)
  40. self.assertEqual(byte_size(0xffffffffffffff), 7)
  41. self.assertEqual(byte_size(0xffffffffffffffff), 8)
  42. def test_zero(self):
  43. self.assertEqual(byte_size(0), 1)
  44. def test_bad_type(self):
  45. self.assertRaises(TypeError, byte_size, [])
  46. self.assertRaises(TypeError, byte_size, ())
  47. self.assertRaises(TypeError, byte_size, dict())
  48. self.assertRaises(TypeError, byte_size, "")
  49. self.assertRaises(TypeError, byte_size, None)
  50. class TestBitSize(unittest.TestCase):
  51. def test_zero(self):
  52. self.assertEqual(bit_size(0), 0)
  53. def test_values(self):
  54. self.assertEqual(bit_size(1023), 10)
  55. self.assertEqual(bit_size(1024), 11)
  56. self.assertEqual(bit_size(1025), 11)
  57. self.assertEqual(bit_size(1 << 1024), 1025)
  58. self.assertEqual(bit_size((1 << 1024) + 1), 1025)
  59. self.assertEqual(bit_size((1 << 1024) - 1), 1024)
  60. def test_negative_values(self):
  61. self.assertEqual(bit_size(-1023), 10)
  62. self.assertEqual(bit_size(-1024), 11)
  63. self.assertEqual(bit_size(-1025), 11)
  64. self.assertEqual(bit_size(-1 << 1024), 1025)
  65. self.assertEqual(bit_size(-((1 << 1024) + 1)), 1025)
  66. self.assertEqual(bit_size(-((1 << 1024) - 1)), 1024)
  67. def test_bad_type(self):
  68. self.assertRaises(TypeError, bit_size, [])
  69. self.assertRaises(TypeError, bit_size, ())
  70. self.assertRaises(TypeError, bit_size, dict())
  71. self.assertRaises(TypeError, bit_size, "")
  72. self.assertRaises(TypeError, bit_size, None)
  73. self.assertRaises(TypeError, bit_size, 0.0)
  74. class TestInverse(unittest.TestCase):
  75. def test_normal(self):
  76. self.assertEqual(3, inverse(7, 4))
  77. self.assertEqual(9, inverse(5, 11))
  78. def test_not_relprime(self):
  79. self.assertRaises(ValueError, inverse, 4, 8)
  80. self.assertRaises(ValueError, inverse, 25, 5)