summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMichael Manganiello <adamantike@users.noreply.github.com>2017-01-16 08:38:08 -0300
committerSybren A. Stüvel <sybren@stuvel.eu>2017-01-16 12:38:08 +0100
commitd3727172cedb409613739be6af197c3b03cc163d (patch)
tree9d5ff18b9a197fa3ef460dc879f6eb1d4ed2359c /tests
parent81f0e95dd17bbe99df2ac4958bd9511dd05c788f (diff)
downloadrsa-git-d3727172cedb409613739be6af197c3b03cc163d.tar.gz
Implementation of bitwise XOR function for bytes object (#72)
Diffstat (limited to 'tests')
-rw-r--r--tests/test_compat.py47
1 files changed, 46 insertions, 1 deletions
diff --git a/tests/test_compat.py b/tests/test_compat.py
index a47f890..62e933f 100644
--- a/tests/test_compat.py
+++ b/tests/test_compat.py
@@ -17,10 +17,12 @@
import unittest
import struct
-from rsa._compat import byte, is_bytes, range
+from rsa._compat import byte, is_bytes, range, xor_bytes
class TestByte(unittest.TestCase):
+ """Tests for single bytes."""
+
def test_byte(self):
for i in range(256):
byt = byte(i)
@@ -33,3 +35,46 @@ class TestByte(unittest.TestCase):
def test_byte_literal(self):
self.assertIsInstance(b'abc', bytes)
+
+
+class TestBytes(unittest.TestCase):
+ """Tests for bytes objects."""
+
+ def setUp(self):
+ self.b1 = b'\xff\xff\xff\xff'
+ self.b2 = b'\x00\x00\x00\x00'
+ self.b3 = b'\xf0\xf0\xf0\xf0'
+ self.b4 = b'\x4d\x23\xca\xe2'
+ self.b5 = b'\x9b\x61\x3b\xdc'
+ self.b6 = b'\xff\xff'
+
+ self.byte_strings = (self.b1, self.b2, self.b3, self.b4, self.b5, self.b6)
+
+ def test_xor_bytes(self):
+ self.assertEqual(xor_bytes(self.b1, self.b2), b'\xff\xff\xff\xff')
+ self.assertEqual(xor_bytes(self.b1, self.b3), b'\x0f\x0f\x0f\x0f')
+ self.assertEqual(xor_bytes(self.b1, self.b4), b'\xb2\xdc\x35\x1d')
+ self.assertEqual(xor_bytes(self.b1, self.b5), b'\x64\x9e\xc4\x23')
+ self.assertEqual(xor_bytes(self.b2, self.b3), b'\xf0\xf0\xf0\xf0')
+ self.assertEqual(xor_bytes(self.b2, self.b4), b'\x4d\x23\xca\xe2')
+ self.assertEqual(xor_bytes(self.b2, self.b5), b'\x9b\x61\x3b\xdc')
+ self.assertEqual(xor_bytes(self.b3, self.b4), b'\xbd\xd3\x3a\x12')
+ self.assertEqual(xor_bytes(self.b3, self.b5), b'\x6b\x91\xcb\x2c')
+ self.assertEqual(xor_bytes(self.b4, self.b5), b'\xd6\x42\xf1\x3e')
+
+ def test_xor_bytes_length(self):
+ self.assertEqual(xor_bytes(self.b1, self.b6), b'\x00\x00')
+ self.assertEqual(xor_bytes(self.b2, self.b6), b'\xff\xff')
+ self.assertEqual(xor_bytes(self.b3, self.b6), b'\x0f\x0f')
+ self.assertEqual(xor_bytes(self.b4, self.b6), b'\xb2\xdc')
+ self.assertEqual(xor_bytes(self.b5, self.b6), b'\x64\x9e')
+ self.assertEqual(xor_bytes(self.b6, b''), b'')
+
+ def test_xor_bytes_commutative(self):
+ for first in self.byte_strings:
+ for second in self.byte_strings:
+ min_length = min(len(first), len(second))
+ result = xor_bytes(first, second)
+
+ self.assertEqual(result, xor_bytes(second, first))
+ self.assertEqual(len(result), min_length)