test_transform.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # https://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import unittest
  17. from rsa.transform import int2bytes, bytes2int, _int2bytes
  18. class Test_int2bytes(unittest.TestCase):
  19. def test_accuracy(self):
  20. self.assertEqual(int2bytes(123456789), b'\x07[\xcd\x15')
  21. self.assertEqual(_int2bytes(123456789), b'\x07[\xcd\x15')
  22. def test_codec_identity(self):
  23. self.assertEqual(bytes2int(int2bytes(123456789, 128)), 123456789)
  24. self.assertEqual(bytes2int(_int2bytes(123456789, 128)), 123456789)
  25. def test_chunk_size(self):
  26. self.assertEqual(int2bytes(123456789, 6), b'\x00\x00\x07[\xcd\x15')
  27. self.assertEqual(int2bytes(123456789, 7),
  28. b'\x00\x00\x00\x07[\xcd\x15')
  29. self.assertEqual(_int2bytes(123456789, 6),
  30. b'\x00\x00\x07[\xcd\x15')
  31. self.assertEqual(_int2bytes(123456789, 7),
  32. b'\x00\x00\x00\x07[\xcd\x15')
  33. def test_zero(self):
  34. self.assertEqual(int2bytes(0, 4), b'\x00' * 4)
  35. self.assertEqual(int2bytes(0, 7), b'\x00' * 7)
  36. self.assertEqual(int2bytes(0), b'\x00')
  37. self.assertEqual(_int2bytes(0, 4), b'\x00' * 4)
  38. self.assertEqual(_int2bytes(0, 7), b'\x00' * 7)
  39. self.assertEqual(_int2bytes(0), b'\x00')
  40. def test_correctness_against_base_implementation(self):
  41. # Slow test.
  42. values = [
  43. 1 << 512,
  44. 1 << 8192,
  45. 1 << 77,
  46. ]
  47. for value in values:
  48. self.assertEqual(int2bytes(value), _int2bytes(value),
  49. "Boom %d" % value)
  50. self.assertEqual(bytes2int(int2bytes(value)),
  51. value,
  52. "Boom %d" % value)
  53. self.assertEqual(bytes2int(_int2bytes(value)),
  54. value,
  55. "Boom %d" % value)
  56. def test_raises_OverflowError_when_chunk_size_is_insufficient(self):
  57. self.assertRaises(OverflowError, int2bytes, 123456789, 3)
  58. self.assertRaises(OverflowError, int2bytes, 299999999999, 4)
  59. self.assertRaises(OverflowError, _int2bytes, 123456789, 3)
  60. self.assertRaises(OverflowError, _int2bytes, 299999999999, 4)
  61. def test_raises_ValueError_when_negative_integer(self):
  62. self.assertRaises(ValueError, int2bytes, -1)
  63. self.assertRaises(ValueError, _int2bytes, -1)
  64. def test_raises_TypeError_when_not_integer(self):
  65. self.assertRaises(TypeError, int2bytes, None)
  66. self.assertRaises(TypeError, _int2bytes, None)