test_compat.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import struct
  18. from rsa._compat import byte, is_bytes, range, xor_bytes
  19. class TestByte(unittest.TestCase):
  20. """Tests for single bytes."""
  21. def test_byte(self):
  22. for i in range(256):
  23. byt = byte(i)
  24. self.assertTrue(is_bytes(byt))
  25. self.assertEqual(ord(byt), i)
  26. def test_raises_StructError_on_overflow(self):
  27. self.assertRaises(struct.error, byte, 256)
  28. self.assertRaises(struct.error, byte, -1)
  29. def test_byte_literal(self):
  30. self.assertIsInstance(b'abc', bytes)
  31. class TestBytes(unittest.TestCase):
  32. """Tests for bytes objects."""
  33. def setUp(self):
  34. self.b1 = b'\xff\xff\xff\xff'
  35. self.b2 = b'\x00\x00\x00\x00'
  36. self.b3 = b'\xf0\xf0\xf0\xf0'
  37. self.b4 = b'\x4d\x23\xca\xe2'
  38. self.b5 = b'\x9b\x61\x3b\xdc'
  39. self.b6 = b'\xff\xff'
  40. self.byte_strings = (self.b1, self.b2, self.b3, self.b4, self.b5, self.b6)
  41. def test_xor_bytes(self):
  42. self.assertEqual(xor_bytes(self.b1, self.b2), b'\xff\xff\xff\xff')
  43. self.assertEqual(xor_bytes(self.b1, self.b3), b'\x0f\x0f\x0f\x0f')
  44. self.assertEqual(xor_bytes(self.b1, self.b4), b'\xb2\xdc\x35\x1d')
  45. self.assertEqual(xor_bytes(self.b1, self.b5), b'\x64\x9e\xc4\x23')
  46. self.assertEqual(xor_bytes(self.b2, self.b3), b'\xf0\xf0\xf0\xf0')
  47. self.assertEqual(xor_bytes(self.b2, self.b4), b'\x4d\x23\xca\xe2')
  48. self.assertEqual(xor_bytes(self.b2, self.b5), b'\x9b\x61\x3b\xdc')
  49. self.assertEqual(xor_bytes(self.b3, self.b4), b'\xbd\xd3\x3a\x12')
  50. self.assertEqual(xor_bytes(self.b3, self.b5), b'\x6b\x91\xcb\x2c')
  51. self.assertEqual(xor_bytes(self.b4, self.b5), b'\xd6\x42\xf1\x3e')
  52. def test_xor_bytes_length(self):
  53. self.assertEqual(xor_bytes(self.b1, self.b6), b'\x00\x00')
  54. self.assertEqual(xor_bytes(self.b2, self.b6), b'\xff\xff')
  55. self.assertEqual(xor_bytes(self.b3, self.b6), b'\x0f\x0f')
  56. self.assertEqual(xor_bytes(self.b4, self.b6), b'\xb2\xdc')
  57. self.assertEqual(xor_bytes(self.b5, self.b6), b'\x64\x9e')
  58. self.assertEqual(xor_bytes(self.b6, b''), b'')
  59. def test_xor_bytes_commutative(self):
  60. for first in self.byte_strings:
  61. for second in self.byte_strings:
  62. min_length = min(len(first), len(second))
  63. result = xor_bytes(first, second)
  64. self.assertEqual(result, xor_bytes(second, first))
  65. self.assertEqual(len(result), min_length)