From ae8bc2e26070e9a2894d3533260d1bfa433af5c0 Mon Sep 17 00:00:00 2001 From: rwarren Date: Wed, 7 Feb 2018 11:25:11 -0500 Subject: [PATCH 1/2] added test case "test_bintype_symmetry" (see issue #281) --- test/test_case.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/test_case.py b/test/test_case.py index 5a4bb6c4..7dc91967 100644 --- a/test/test_case.py +++ b/test/test_case.py @@ -100,3 +100,18 @@ def test_match(): def test_unicode(): assert unpackb(packb('foobar'), use_list=1) == b'foobar' +def test_bintype_symmetry(): + # For background on this test see github issue #281 + # - basically: packing with use_bin_type=True and unpacking with raw=False + # should be completely symmetric + buf = b'\x92\xa3\xe2\x98\x83\xc4\x03\x00\x01\x02' + unpacked = unpackb(buf, raw=False) + packed = packb(unpacked, use_bin_type=True) + assert packed == buf + # Also confirm that unpacking went to the expected types + # - Note that we can't use test_match since it just uses basic equality + # testing. We need to assert exact type matching here. + expected = [u'\u2603', b'\x00\x01\x02'] + for x, y in zip(expected, unpacked): + assert type(x) is type(y) + assert x == y From 653d81cbfbd1f81abf67f466021e393fa3df85c8 Mon Sep 17 00:00:00 2001 From: rwarren Date: Thu, 8 Feb 2018 11:21:39 -0500 Subject: [PATCH 2/2] more explanation in test_case.test_mixed_bin_str (also renamed it) --- test/test_case.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/test/test_case.py b/test/test_case.py index 7dc91967..88d8604d 100644 --- a/test/test_case.py +++ b/test/test_case.py @@ -100,18 +100,26 @@ def test_match(): def test_unicode(): assert unpackb(packb('foobar'), use_list=1) == b'foobar' -def test_bintype_symmetry(): +def test_mixed_bin_str(): + # This test explicitly checks behaviour when a MessagePack byte sequence has both + # "bin family" types and "str family" types. # For background on this test see github issue #281 - # - basically: packing with use_bin_type=True and unpacking with raw=False - # should be completely symmetric - buf = b'\x92\xa3\xe2\x98\x83\xc4\x03\x00\x01\x02' + # - basically: packing with use_bin_type=True and unpacking with raw=False should be + # completely symmetric + expected = [u'\u2603', b'\x00\x01\x02'] + buf = (b'\x92' # array with length 2 (fixarray) + b'\xa3\xe2\x98\x83' # str format with length 3 (fixstr) (UTF8 snowman) + b'\xc4\x03\x00\x01\x02' # bin format with length 3 (bin8) (0x000102) + ) + + # Check symmetry unpacked = unpackb(buf, raw=False) packed = packb(unpacked, use_bin_type=True) assert packed == buf + # Also confirm that unpacking went to the expected types - # - Note that we can't use test_match since it just uses basic equality - # testing. We need to assert exact type matching here. - expected = [u'\u2603', b'\x00\x01\x02'] + # - Note that we can't use test_match since it just uses basic equality testing. We + # need to assert exact type matching here. for x, y in zip(expected, unpacked): assert type(x) is type(y) assert x == y