Yesudeep Mangalapilly | fd1ec36 | 2011-08-11 01:47:46 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Roy Kokkelkoren | 0659aac | 2015-10-25 16:12:11 +0100 | [diff] [blame] | 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 | # |
Sybren A. Stüvel | 3934ab4 | 2016-02-05 16:01:20 +0100 | [diff] [blame] | 9 | # https://www.apache.org/licenses/LICENSE-2.0 |
Roy Kokkelkoren | 0659aac | 2015-10-25 16:12:11 +0100 | [diff] [blame] | 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. |
Yesudeep Mangalapilly | fd1ec36 | 2011-08-11 01:47:46 +0530 | [diff] [blame] | 16 | |
Sybren A. Stüvel | ed1c81d | 2016-01-14 12:23:32 +0100 | [diff] [blame] | 17 | import unittest |
Yesudeep Mangalapilly | fd1ec36 | 2011-08-11 01:47:46 +0530 | [diff] [blame] | 18 | import struct |
| 19 | |
Michael Manganiello | d372717 | 2017-01-16 08:38:08 -0300 | [diff] [blame] | 20 | from rsa._compat import byte, is_bytes, range, xor_bytes |
Yesudeep Mangalapilly | fd1ec36 | 2011-08-11 01:47:46 +0530 | [diff] [blame] | 21 | |
Sybren A. Stüvel | d3d1034 | 2016-01-22 11:36:06 +0100 | [diff] [blame] | 22 | |
| 23 | class TestByte(unittest.TestCase): |
Michael Manganiello | d372717 | 2017-01-16 08:38:08 -0300 | [diff] [blame] | 24 | """Tests for single bytes.""" |
| 25 | |
Yesudeep Mangalapilly | fd1ec36 | 2011-08-11 01:47:46 +0530 | [diff] [blame] | 26 | def test_byte(self): |
| 27 | for i in range(256): |
| 28 | byt = byte(i) |
| 29 | self.assertTrue(is_bytes(byt)) |
| 30 | self.assertEqual(ord(byt), i) |
| 31 | |
| 32 | def test_raises_StructError_on_overflow(self): |
| 33 | self.assertRaises(struct.error, byte, 256) |
| 34 | self.assertRaises(struct.error, byte, -1) |
Sybren A. Stüvel | f0627be | 2016-03-17 15:52:23 +0100 | [diff] [blame] | 35 | |
| 36 | def test_byte_literal(self): |
adamantike | 9f57740 | 2016-05-08 15:36:57 -0300 | [diff] [blame] | 37 | self.assertIsInstance(b'abc', bytes) |
Michael Manganiello | d372717 | 2017-01-16 08:38:08 -0300 | [diff] [blame] | 38 | |
| 39 | |
| 40 | class TestBytes(unittest.TestCase): |
| 41 | """Tests for bytes objects.""" |
| 42 | |
| 43 | def setUp(self): |
| 44 | self.b1 = b'\xff\xff\xff\xff' |
| 45 | self.b2 = b'\x00\x00\x00\x00' |
| 46 | self.b3 = b'\xf0\xf0\xf0\xf0' |
| 47 | self.b4 = b'\x4d\x23\xca\xe2' |
| 48 | self.b5 = b'\x9b\x61\x3b\xdc' |
| 49 | self.b6 = b'\xff\xff' |
| 50 | |
| 51 | self.byte_strings = (self.b1, self.b2, self.b3, self.b4, self.b5, self.b6) |
| 52 | |
| 53 | def test_xor_bytes(self): |
| 54 | self.assertEqual(xor_bytes(self.b1, self.b2), b'\xff\xff\xff\xff') |
| 55 | self.assertEqual(xor_bytes(self.b1, self.b3), b'\x0f\x0f\x0f\x0f') |
| 56 | self.assertEqual(xor_bytes(self.b1, self.b4), b'\xb2\xdc\x35\x1d') |
| 57 | self.assertEqual(xor_bytes(self.b1, self.b5), b'\x64\x9e\xc4\x23') |
| 58 | self.assertEqual(xor_bytes(self.b2, self.b3), b'\xf0\xf0\xf0\xf0') |
| 59 | self.assertEqual(xor_bytes(self.b2, self.b4), b'\x4d\x23\xca\xe2') |
| 60 | self.assertEqual(xor_bytes(self.b2, self.b5), b'\x9b\x61\x3b\xdc') |
| 61 | self.assertEqual(xor_bytes(self.b3, self.b4), b'\xbd\xd3\x3a\x12') |
| 62 | self.assertEqual(xor_bytes(self.b3, self.b5), b'\x6b\x91\xcb\x2c') |
| 63 | self.assertEqual(xor_bytes(self.b4, self.b5), b'\xd6\x42\xf1\x3e') |
| 64 | |
| 65 | def test_xor_bytes_length(self): |
| 66 | self.assertEqual(xor_bytes(self.b1, self.b6), b'\x00\x00') |
| 67 | self.assertEqual(xor_bytes(self.b2, self.b6), b'\xff\xff') |
| 68 | self.assertEqual(xor_bytes(self.b3, self.b6), b'\x0f\x0f') |
| 69 | self.assertEqual(xor_bytes(self.b4, self.b6), b'\xb2\xdc') |
| 70 | self.assertEqual(xor_bytes(self.b5, self.b6), b'\x64\x9e') |
| 71 | self.assertEqual(xor_bytes(self.b6, b''), b'') |
| 72 | |
| 73 | def test_xor_bytes_commutative(self): |
| 74 | for first in self.byte_strings: |
| 75 | for second in self.byte_strings: |
| 76 | min_length = min(len(first), len(second)) |
| 77 | result = xor_bytes(first, second) |
| 78 | |
| 79 | self.assertEqual(result, xor_bytes(second, first)) |
| 80 | self.assertEqual(len(result), min_length) |