test_common.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. import unittest
  16. import struct
  17. from rsa.common import byte_size, bit_size, inverse
  18. class TestByteSize(unittest.TestCase):
  19. def test_values(self):
  20. self.assertEqual(byte_size(1 << 1023), 128)
  21. self.assertEqual(byte_size((1 << 1024) - 1), 128)
  22. self.assertEqual(byte_size(1 << 1024), 129)
  23. self.assertEqual(byte_size(255), 1)
  24. self.assertEqual(byte_size(256), 2)
  25. self.assertEqual(byte_size(0xFFFF), 2)
  26. self.assertEqual(byte_size(0xFFFFFF), 3)
  27. self.assertEqual(byte_size(0xFFFFFFFF), 4)
  28. self.assertEqual(byte_size(0xFFFFFFFFFF), 5)
  29. self.assertEqual(byte_size(0xFFFFFFFFFFFF), 6)
  30. self.assertEqual(byte_size(0xFFFFFFFFFFFFFF), 7)
  31. self.assertEqual(byte_size(0xFFFFFFFFFFFFFFFF), 8)
  32. def test_zero(self):
  33. self.assertEqual(byte_size(0), 1)
  34. def test_bad_type(self):
  35. self.assertRaises(TypeError, byte_size, [])
  36. self.assertRaises(TypeError, byte_size, ())
  37. self.assertRaises(TypeError, byte_size, dict())
  38. self.assertRaises(TypeError, byte_size, "")
  39. self.assertRaises(TypeError, byte_size, None)
  40. class TestBitSize(unittest.TestCase):
  41. def test_zero(self):
  42. self.assertEqual(bit_size(0), 0)
  43. def test_values(self):
  44. self.assertEqual(bit_size(1023), 10)
  45. self.assertEqual(bit_size(1024), 11)
  46. self.assertEqual(bit_size(1025), 11)
  47. self.assertEqual(bit_size(1 << 1024), 1025)
  48. self.assertEqual(bit_size((1 << 1024) + 1), 1025)
  49. self.assertEqual(bit_size((1 << 1024) - 1), 1024)
  50. def test_negative_values(self):
  51. self.assertEqual(bit_size(-1023), 10)
  52. self.assertEqual(bit_size(-1024), 11)
  53. self.assertEqual(bit_size(-1025), 11)
  54. self.assertEqual(bit_size(-1 << 1024), 1025)
  55. self.assertEqual(bit_size(-((1 << 1024) + 1)), 1025)
  56. self.assertEqual(bit_size(-((1 << 1024) - 1)), 1024)
  57. def test_bad_type(self):
  58. self.assertRaises(TypeError, bit_size, [])
  59. self.assertRaises(TypeError, bit_size, ())
  60. self.assertRaises(TypeError, bit_size, dict())
  61. self.assertRaises(TypeError, bit_size, "")
  62. self.assertRaises(TypeError, bit_size, None)
  63. self.assertRaises(TypeError, bit_size, 0.0)
  64. class TestInverse(unittest.TestCase):
  65. def test_normal(self):
  66. self.assertEqual(3, inverse(7, 4))
  67. self.assertEqual(9, inverse(5, 11))
  68. def test_not_relprime(self):
  69. self.assertRaises(ValueError, inverse, 4, 8)
  70. self.assertRaises(ValueError, inverse, 25, 5)