|
6 | 6 | As of the Change Date specified in that file, in accordance with the Business Source License, use of this software will be governed by the Apache License, version 2.0.
|
7 | 7 | """
|
8 | 8 | import datetime
|
| 9 | +from email import errors |
9 | 10 | import inspect
|
10 | 11 | import itertools
|
11 | 12 | import sys
|
@@ -92,6 +93,113 @@ def test_msg_pack_legacy_2():
|
92 | 93 | assert data == loc_dt
|
93 | 94 |
|
94 | 95 |
|
| 96 | +def test_decode_python2_pickle_in_msgpack_dict(): |
| 97 | + """See python2_pickles.py for the generation steps. This is the py2_dict.bin case. |
| 98 | +
|
| 99 | + This is to check that we can still deserialize pickles that were written with Python 2 correctly. |
| 100 | + """ |
| 101 | + norm = test_msgpack_normalizer |
| 102 | + packed = b"\x81\xa8dict_key\xc7:f\xda\x007cemail.errors\nBoundaryError\np0\n(S'bananas'\np1\ntp2\nRp3\n." |
| 103 | + data = norm._msgpack_unpackb(packed) |
| 104 | + assert list(data.keys()) == ["dict_key"] |
| 105 | + assert isinstance(data["dict_key"], errors.BoundaryError) |
| 106 | + assert data["dict_key"].args[0] == "bananas" |
| 107 | + |
| 108 | + |
| 109 | +def test_decode_python2_pickle_in_msgpack_obj(): |
| 110 | + """See python2_pickles.py for the generation steps. This is the py2_obj.bin case. |
| 111 | +
|
| 112 | + This is to check that we can still deserialize pickles that were written with Python 2 correctly. |
| 113 | + """ |
| 114 | + norm = test_msgpack_normalizer |
| 115 | + packed = b"\xc7:f\xda\x007cemail.errors\nBoundaryError\np0\n(S'bananas'\np1\ntp2\nRp3\n." |
| 116 | + data = norm._msgpack_unpackb(packed) |
| 117 | + assert isinstance(data, errors.BoundaryError) |
| 118 | + assert data.args[0] == "bananas" |
| 119 | + |
| 120 | + |
| 121 | +def test_decode_python2_str_in_msgpack(): |
| 122 | + """See python2_pickles.py for the generation steps. This is the py2_str.bin case. |
| 123 | +
|
| 124 | + This is to check that we can still deserialize strings that were written with Python 2 correctly. |
| 125 | + """ |
| 126 | + norm = test_msgpack_normalizer |
| 127 | + packed = b'\xa9my_string' |
| 128 | + data = norm._msgpack_unpackb(packed) |
| 129 | + assert data == "my_string" |
| 130 | + assert isinstance(data, str) |
| 131 | + |
| 132 | + |
| 133 | +def test_decode_python2_bytes_in_old_msgpack(): |
| 134 | + """See python2_pickles.py for the generation steps. This is the py2_str_bytes.bin case. |
| 135 | +
|
| 136 | + This is to check that we can still deserialize bytes that were written with Python 2 correctly. |
| 137 | + """ |
| 138 | + norm = test_msgpack_normalizer |
| 139 | + packed = b'\xa8my_bytes' |
| 140 | + data = norm._msgpack_unpackb(packed) |
| 141 | + |
| 142 | + # We claim it's `str` upon decoding because the `xa8` leading bytes tells us this is a fixed string type. |
| 143 | + assert data == "my_bytes" |
| 144 | + assert isinstance(data, str) |
| 145 | + |
| 146 | + |
| 147 | +def test_decode_python2_bytes_in_newer_msgpack(): |
| 148 | + """See python2_pickles.py for the generation steps. This is the py2_str_bytes.bin case. |
| 149 | +
|
| 150 | + This was written with msgpack 1.0.5 not 0.6.2 like the other examples. In this version, msgpack has |
| 151 | + a dedicated type for bytes. |
| 152 | +
|
| 153 | + This is to check that we can still deserialize bytes that were written with Python 2 correctly. |
| 154 | + """ |
| 155 | + norm = test_msgpack_normalizer |
| 156 | + packed = b'\xc4\x08my_bytes' |
| 157 | + data = norm._msgpack_unpackb(packed) |
| 158 | + assert data == b"my_bytes" |
| 159 | + assert isinstance(data, bytes) |
| 160 | + |
| 161 | + |
| 162 | +def test_decode_python3_pickle_in_msgpack_dict(): |
| 163 | + norm = test_msgpack_normalizer |
| 164 | + obj = {"dict_key": errors.BoundaryError("bananas")} |
| 165 | + packed = norm._msgpack_packb(obj) |
| 166 | + |
| 167 | + data = norm._msgpack_unpackb(packed) |
| 168 | + assert list(data.keys()) == ["dict_key"] |
| 169 | + assert isinstance(data["dict_key"], errors.BoundaryError) |
| 170 | + assert data["dict_key"].args[0] == "bananas" |
| 171 | + |
| 172 | + |
| 173 | +def test_decode_python3_pickle_in_msgpack_obj(): |
| 174 | + norm = test_msgpack_normalizer |
| 175 | + obj = errors.BoundaryError("bananas") |
| 176 | + packed = norm._msgpack_packb(obj) |
| 177 | + |
| 178 | + data = norm._msgpack_unpackb(packed) |
| 179 | + assert isinstance(data, errors.BoundaryError) |
| 180 | + assert data.args[0] == "bananas" |
| 181 | + |
| 182 | + |
| 183 | +def test_decode_python3_pickle_in_msgpack_str(): |
| 184 | + norm = test_msgpack_normalizer |
| 185 | + obj = "bananas" |
| 186 | + packed = norm._msgpack_packb(obj) |
| 187 | + |
| 188 | + data = norm._msgpack_unpackb(packed) |
| 189 | + assert isinstance(data, str) |
| 190 | + assert data == "bananas" |
| 191 | + |
| 192 | + |
| 193 | +def test_decode_python3_pickle_in_msgpack_bytes(): |
| 194 | + norm = test_msgpack_normalizer |
| 195 | + obj = b"bananas" |
| 196 | + packed = norm._msgpack_packb(obj) |
| 197 | + |
| 198 | + data = norm._msgpack_unpackb(packed) |
| 199 | + assert isinstance(data, bytes) |
| 200 | + assert data == b"bananas" |
| 201 | + |
| 202 | + |
95 | 203 | @param_dict("d", params)
|
96 | 204 | def test_user_meta_and_msg_pack(d):
|
97 | 205 | n = normalize_metadata(d)
|
|
0 commit comments