From 860bc3535858a6eace475dd102d971947a9bd7b9 Mon Sep 17 00:00:00 2001 From: akash-suresh Date: Wed, 20 May 2020 17:35:18 +0530 Subject: [PATCH 1/7] merge conflicts resolved --- tests/test_encode_decode.py | 52 ++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/tests/test_encode_decode.py b/tests/test_encode_decode.py index 634db0d..440dd94 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -1,10 +1,11 @@ import base64 +import binascii import colorsys import quopri import string import unittest -from hypothesis import example, given, strategies as st, target +from hypothesis import assume, example, given, strategies as st, target def add_padding(payload): @@ -95,8 +96,53 @@ def test_b85_encode_decode_round_trip(self, payload, pad): class TestBinASCII(unittest.TestCase): - # TODO: https://docs.python.org/3/library/binascii.html - pass + @given(payload=st.binary(), backtick=st.booleans()) + def test_b2a_uu_a2b_uu_round_trip(self, payload, backtick): + x = binascii.b2a_uu(payload, backtick=backtick) + self.assertEqual(payload, binascii.a2b_uu(x)) + + @given(payload=st.binary(), newline=st.booleans()) + def test_b2a_base64_a2b_base64_round_trip(self, payload, newline): + x = binascii.b2a_base64(payload, newline=newline) + self.assertEqual(payload, binascii.a2b_base64(x)) + + @given( + payload=st.binary(), + quotetabs=st.booleans(), + istext=st.booleans(), + header=st.booleans(), + ) + def test_b2a_qp_a2b_qp_round_trip(self, payload, quotetabs, istext, header): + x = binascii.b2a_qp(payload, quotetabs=quotetabs, istext=istext, header=header) + self.assertEqual(payload, binascii.a2b_qp(x, header=header)) + + @given(payload=st.binary()) + def test_rlecode_hqx_rledecode_hqx_round_trip(self, payload): + x = binascii.rlecode_hqx(payload) + self.assertEqual(payload, binascii.rledecode_hqx(x)) + + # todo fix this + @given(payload=st.binary()) + def test_b2a_hqx_a2b_hqx_round_trip(self, payload): + rle = binascii.rlecode_hqx(payload) + x = binascii.b2a_hqx(rle) + assume(len(x) % 4 == 0) + b, _ = binascii.a2b_hqx(x) + res = binascii.rledecode_hqx(b) + self.assertEqual(payload, res) + + # todo test crc_hqx + # todo test crc32 + + @given(payload=st.binary()) + def test_b2a_hex_a2b_hex_round_trip(self, payload): + x = binascii.b2a_hex(payload) + self.assertEqual(payload, binascii.a2b_hex(x)) + + @given(payload=st.binary()) + def test_hexlify_unhexlify_round_trip(self, payload): + x = binascii.hexlify(payload) + self.assertEqual(payload, binascii.unhexlify(x)) class TestColorsys(unittest.TestCase): From 973f7c4266abd156141c83dca5d62790145bfeaa Mon Sep 17 00:00:00 2001 From: akashsuresh96 Date: Wed, 20 May 2020 11:19:21 +0530 Subject: [PATCH 2/7] Added test cases for crc_hqx and crc32 --- tests/test_encode_decode.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/tests/test_encode_decode.py b/tests/test_encode_decode.py index 440dd94..fb466b1 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -131,8 +131,37 @@ def test_b2a_hqx_a2b_hqx_round_trip(self, payload): res = binascii.rledecode_hqx(b) self.assertEqual(payload, res) - # todo test crc_hqx - # todo test crc32 + @given(payload=st.binary(), value=st.just(0) | st.integers()) + def test_crc_hqx(self, payload, value): + crc = binascii.crc_hqx(payload, value) + self.assertIs(type(crc), int) + + @given( + payload_piece_1=st.binary(), + payload_piece_2=st.binary(), + value=st.just(0) | st.integers(), + ) + def test_crc_hqx_two_pieces(self, payload_piece_1, payload_piece_2, value): + combined_crc = binascii.crc_hqx(payload_piece_1 + payload_piece_2, value) + crc = binascii.crc_hqx(payload_piece_1, value) + crc = binascii.crc_hqx(payload_piece_2, crc) + self.assertEqual(combined_crc, crc) + + @given(payload=st.binary(), value=st.just(0) | st.integers()) + def test_crc32(self, payload, value): + crc = binascii.crc32(payload, value) + self.assertIs(type(crc), int) + + @given( + payload_piece_1=st.binary(), + payload_piece_2=st.binary(), + value=st.just(0) | st.integers(), + ) + def test_crc32_two_part(self, payload_piece_1, payload_piece_2, value): + combined_crc = binascii.crc32(payload_piece_1 + payload_piece_2, value) + crc = binascii.crc32(payload_piece_1, value) + crc = binascii.crc32(payload_piece_2, crc) + self.assertEqual(combined_crc, crc) @given(payload=st.binary()) def test_b2a_hex_a2b_hex_round_trip(self, payload): From b9eb2839526f8e3b5a6a036762f430e7bb689d14 Mon Sep 17 00:00:00 2001 From: akashsuresh96 Date: Wed, 20 May 2020 11:59:49 +0530 Subject: [PATCH 3/7] Corrected test case for b2a_hqx_a2b_hqx_round_trip --- tests/test_encode_decode.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_encode_decode.py b/tests/test_encode_decode.py index fb466b1..95a1e62 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -121,14 +121,12 @@ def test_rlecode_hqx_rledecode_hqx_round_trip(self, payload): x = binascii.rlecode_hqx(payload) self.assertEqual(payload, binascii.rledecode_hqx(x)) - # todo fix this @given(payload=st.binary()) def test_b2a_hqx_a2b_hqx_round_trip(self, payload): - rle = binascii.rlecode_hqx(payload) - x = binascii.b2a_hqx(rle) - assume(len(x) % 4 == 0) - b, _ = binascii.a2b_hqx(x) - res = binascii.rledecode_hqx(b) + # assuming len(payload) as 3, since it throws exception: binascii.Incomplete, when length is not a multiple of 3 + assume(len(payload) % 3 == 0) + x = binascii.b2a_hqx(payload) + res, _ = binascii.a2b_hqx(x) self.assertEqual(payload, res) @given(payload=st.binary(), value=st.just(0) | st.integers()) From 2156ac5bf5a23d6d77a58348944f9699f17a201e Mon Sep 17 00:00:00 2001 From: akashsuresh96 Date: Wed, 20 May 2020 13:42:56 +0530 Subject: [PATCH 4/7] renamed variable, to explicitly indicate that we are not discarding it --- tests/test_encode_decode.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_encode_decode.py b/tests/test_encode_decode.py index 95a1e62..55414be 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -141,8 +141,8 @@ def test_crc_hqx(self, payload, value): ) def test_crc_hqx_two_pieces(self, payload_piece_1, payload_piece_2, value): combined_crc = binascii.crc_hqx(payload_piece_1 + payload_piece_2, value) - crc = binascii.crc_hqx(payload_piece_1, value) - crc = binascii.crc_hqx(payload_piece_2, crc) + crc_part1 = binascii.crc_hqx(payload_piece_1, value) + crc = binascii.crc_hqx(payload_piece_2, crc_part1) self.assertEqual(combined_crc, crc) @given(payload=st.binary(), value=st.just(0) | st.integers()) @@ -157,8 +157,8 @@ def test_crc32(self, payload, value): ) def test_crc32_two_part(self, payload_piece_1, payload_piece_2, value): combined_crc = binascii.crc32(payload_piece_1 + payload_piece_2, value) - crc = binascii.crc32(payload_piece_1, value) - crc = binascii.crc32(payload_piece_2, crc) + crc_part1 = binascii.crc32(payload_piece_1, value) + crc = binascii.crc32(payload_piece_2, crc_part1) self.assertEqual(combined_crc, crc) @given(payload=st.binary()) From 965548a7a4a960c5178e90af494482b021a334dc Mon Sep 17 00:00:00 2001 From: akash-suresh Date: Wed, 20 May 2020 17:37:55 +0530 Subject: [PATCH 5/7] merge conflicts resolved 2 --- tests/test_encode_decode.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/test_encode_decode.py b/tests/test_encode_decode.py index 55414be..298bbfb 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -3,9 +3,10 @@ import colorsys import quopri import string +import sys import unittest -from hypothesis import assume, example, given, strategies as st, target +from hypothesis import example, given, strategies as st, target def add_padding(payload): @@ -96,8 +97,16 @@ def test_b85_encode_decode_round_trip(self, payload, pad): class TestBinASCII(unittest.TestCase): + @given(payload=st.binary()) + def test_b2a_uu_a2b_uu_round_trip(self, payload): + x = binascii.b2a_uu(payload) + self.assertEqual(payload, binascii.a2b_uu(x)) + + @unittest.skipIf( + sys.version_info[:2] < (3, 7), "backtick not supported in this library version" + ) @given(payload=st.binary(), backtick=st.booleans()) - def test_b2a_uu_a2b_uu_round_trip(self, payload, backtick): + def test_b2a_uu_a2b_uu_round_trip_with_backtick(self, payload, backtick): x = binascii.b2a_uu(payload, backtick=backtick) self.assertEqual(payload, binascii.a2b_uu(x)) @@ -124,7 +133,11 @@ def test_rlecode_hqx_rledecode_hqx_round_trip(self, payload): @given(payload=st.binary()) def test_b2a_hqx_a2b_hqx_round_trip(self, payload): # assuming len(payload) as 3, since it throws exception: binascii.Incomplete, when length is not a multiple of 3 - assume(len(payload) % 3 == 0) + if len(payload) % 3: + with self.assertRaises(binascii.Incomplete): + x = binascii.b2a_hqx(payload) + binascii.a2b_hqx(x) + payload += b"\x00" * (-len(payload) % 3) x = binascii.b2a_hqx(payload) res, _ = binascii.a2b_hqx(x) self.assertEqual(payload, res) From 9f3bc6d27b78c90fa28dcdf20db576337993f465 Mon Sep 17 00:00:00 2001 From: akash-suresh Date: Wed, 20 May 2020 14:50:14 +0530 Subject: [PATCH 6/7] modified skip based on version --- tests/test_encode_decode.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/test_encode_decode.py b/tests/test_encode_decode.py index 298bbfb..4279c02 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -97,17 +97,12 @@ def test_b85_encode_decode_round_trip(self, payload, pad): class TestBinASCII(unittest.TestCase): - @given(payload=st.binary()) - def test_b2a_uu_a2b_uu_round_trip(self, payload): - x = binascii.b2a_uu(payload) - self.assertEqual(payload, binascii.a2b_uu(x)) - - @unittest.skipIf( - sys.version_info[:2] < (3, 7), "backtick not supported in this library version" - ) @given(payload=st.binary(), backtick=st.booleans()) - def test_b2a_uu_a2b_uu_round_trip_with_backtick(self, payload, backtick): - x = binascii.b2a_uu(payload, backtick=backtick) + def test_b2a_uu_a2b_uu_round_trip(self, payload, backtick): + if sys.version_info[:2] >= (3, 7): + x = binascii.b2a_uu(payload, backtick=backtick) + else: + x = binascii.b2a_uu(payload) self.assertEqual(payload, binascii.a2b_uu(x)) @given(payload=st.binary(), newline=st.booleans()) From 966559d6ec4c04085731f9016270d215dba5212b Mon Sep 17 00:00:00 2001 From: akash-suresh Date: Wed, 20 May 2020 16:44:54 +0530 Subject: [PATCH 7/7] Added example, for case which was failing on PyPy, with OverflowError --- tests/test_encode_decode.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_encode_decode.py b/tests/test_encode_decode.py index 4279c02..bedd88e 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -138,6 +138,7 @@ def test_b2a_hqx_a2b_hqx_round_trip(self, payload): self.assertEqual(payload, res) @given(payload=st.binary(), value=st.just(0) | st.integers()) + @example(payload=b"", value=2 ** 63) def test_crc_hqx(self, payload, value): crc = binascii.crc_hqx(payload, value) self.assertIs(type(crc), int)