| 1 |
#!/usr/bin/env python |
| 2 |
import sysconfig |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from msgpack import ( |
| 7 |
ExtType, |
| 8 |
Packer, |
| 9 |
PackOverflowError, |
| 10 |
PackValueError, |
| 11 |
Unpacker, |
| 12 |
UnpackValueError, |
| 13 |
packb, |
| 14 |
unpackb, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def test_integer(): |
| 19 |
x = -(2**63) |
| 20 |
assert unpackb(packb(x)) == x |
| 21 |
with pytest.raises(PackOverflowError): |
| 22 |
packb(x - 1) |
| 23 |
|
| 24 |
x = 2**64 - 1 |
| 25 |
assert unpackb(packb(x)) == x |
| 26 |
with pytest.raises(PackOverflowError): |
| 27 |
packb(x + 1) |
| 28 |
|
| 29 |
|
| 30 |
def test_array_header(): |
| 31 |
packer = Packer() |
| 32 |
packer.pack_array_header(2**32 - 1) |
| 33 |
with pytest.raises(PackValueError): |
| 34 |
packer.pack_array_header(2**32) |
| 35 |
|
| 36 |
|
| 37 |
def test_map_header(): |
| 38 |
packer = Packer() |
| 39 |
packer.pack_map_header(2**32 - 1) |
| 40 |
with pytest.raises(PackValueError): |
| 41 |
packer.pack_array_header(2**32) |
| 42 |
|
| 43 |
|
| 44 |
def test_max_str_len(): |
| 45 |
d = "x" * 3 |
| 46 |
packed = packb(d) |
| 47 |
|
| 48 |
unpacker = Unpacker(max_str_len=3, raw=False) |
| 49 |
unpacker.feed(packed) |
| 50 |
assert unpacker.unpack() == d |
| 51 |
|
| 52 |
unpacker = Unpacker(max_str_len=2, raw=False) |
| 53 |
with pytest.raises(UnpackValueError): |
| 54 |
unpacker.feed(packed) |
| 55 |
unpacker.unpack() |
| 56 |
|
| 57 |
|
| 58 |
def test_max_bin_len(): |
| 59 |
d = b"x" * 3 |
| 60 |
packed = packb(d, use_bin_type=True) |
| 61 |
|
| 62 |
unpacker = Unpacker(max_bin_len=3) |
| 63 |
unpacker.feed(packed) |
| 64 |
assert unpacker.unpack() == d |
| 65 |
|
| 66 |
unpacker = Unpacker(max_bin_len=2) |
| 67 |
with pytest.raises(UnpackValueError): |
| 68 |
unpacker.feed(packed) |
| 69 |
unpacker.unpack() |
| 70 |
|
| 71 |
|
| 72 |
def test_max_array_len(): |
| 73 |
d = [1, 2, 3] |
| 74 |
packed = packb(d) |
| 75 |
|
| 76 |
unpacker = Unpacker(max_array_len=3) |
| 77 |
unpacker.feed(packed) |
| 78 |
assert unpacker.unpack() == d |
| 79 |
|
| 80 |
unpacker = Unpacker(max_array_len=2) |
| 81 |
with pytest.raises(UnpackValueError): |
| 82 |
unpacker.feed(packed) |
| 83 |
unpacker.unpack() |
| 84 |
|
| 85 |
|
| 86 |
def test_max_map_len(): |
| 87 |
d = {1: 2, 3: 4, 5: 6} |
| 88 |
packed = packb(d) |
| 89 |
|
| 90 |
unpacker = Unpacker(max_map_len=3, strict_map_key=False) |
| 91 |
unpacker.feed(packed) |
| 92 |
assert unpacker.unpack() == d |
| 93 |
|
| 94 |
unpacker = Unpacker(max_map_len=2, strict_map_key=False) |
| 95 |
with pytest.raises(UnpackValueError): |
| 96 |
unpacker.feed(packed) |
| 97 |
unpacker.unpack() |
| 98 |
|
| 99 |
|
| 100 |
def test_max_ext_len(): |
| 101 |
d = ExtType(42, b"abc") |
| 102 |
packed = packb(d) |
| 103 |
|
| 104 |
unpacker = Unpacker(max_ext_len=3) |
| 105 |
unpacker.feed(packed) |
| 106 |
assert unpacker.unpack() == d |
| 107 |
|
| 108 |
unpacker = Unpacker(max_ext_len=2) |
| 109 |
with pytest.raises(UnpackValueError): |
| 110 |
unpacker.feed(packed) |
| 111 |
unpacker.unpack() |
| 112 |
|
| 113 |
|
| 114 |
# PyPy fails following tests because of constant folding? |
| 115 |
# https://bugs.pypy.org/issue1721 |
| 116 |
# @pytest.mark.skipif(True, reason="Requires very large memory.") |
| 117 |
# def test_binary(): |
| 118 |
# x = b'x' * (2**32 - 1) |
| 119 |
# assert unpackb(packb(x)) == x |
| 120 |
# del x |
| 121 |
# x = b'x' * (2**32) |
| 122 |
# with pytest.raises(ValueError): |
| 123 |
# packb(x) |
| 124 |
# |
| 125 |
# |
| 126 |
# @pytest.mark.skipif(True, reason="Requires very large memory.") |
| 127 |
# def test_string(): |
| 128 |
# x = 'x' * (2**32 - 1) |
| 129 |
# assert unpackb(packb(x)) == x |
| 130 |
# x += 'y' |
| 131 |
# with pytest.raises(ValueError): |
| 132 |
# packb(x) |
| 133 |
# |
| 134 |
# |
| 135 |
# @pytest.mark.skipif(True, reason="Requires very large memory.") |
| 136 |
# def test_array(): |
| 137 |
# x = [0] * (2**32 - 1) |
| 138 |
# assert unpackb(packb(x)) == x |
| 139 |
# x.append(0) |
| 140 |
# with pytest.raises(ValueError): |
| 141 |
# packb(x) |
| 142 |
|
| 143 |
|
| 144 |
# auto max len |
| 145 |
|
| 146 |
|
| 147 |
def test_auto_max_array_len(): |
| 148 |
packed = b"\xde\x00\x06zz" |
| 149 |
with pytest.raises(UnpackValueError): |
| 150 |
unpackb(packed, raw=False) |
| 151 |
|
| 152 |
unpacker = Unpacker(max_buffer_size=5, raw=False) |
| 153 |
unpacker.feed(packed) |
| 154 |
with pytest.raises(UnpackValueError): |
| 155 |
unpacker.unpack() |
| 156 |
|
| 157 |
|
| 158 |
# Skip on free-threaded CPython builds because this test depends on recursion behavior. |
| 159 |
IS_FREE_THREADED_BUILD = bool(sysconfig.get_config_var("Py_GIL_DISABLED")) |
| 160 |
|
| 161 |
|
| 162 |
@pytest.mark.skipif(IS_FREE_THREADED_BUILD, reason="Skipped on free-threaded build") |
| 163 |
def test_nest_limit_1024(): |
| 164 |
import sys |
| 165 |
|
| 166 |
# Build a list nested 1024 levels deep |
| 167 |
d = None |
| 168 |
for _ in range(1024): |
| 169 |
d = [d] |
| 170 |
|
| 171 |
# Temporarily raise Python's recursion limit so packing 1024 levels succeeds |
| 172 |
old_limit = sys.getrecursionlimit() |
| 173 |
sys.setrecursionlimit(max(old_limit, 30000)) |
| 174 |
try: |
| 175 |
packed = packb(d) |
| 176 |
result = unpackb(packed) |
| 177 |
finally: |
| 178 |
sys.setrecursionlimit(old_limit) |
| 179 |
|
| 180 |
# Verify structure iteratively to avoid hitting C-level recursion limit in == |
| 181 |
for _ in range(1024): |
| 182 |
assert isinstance(result, list) and len(result) == 1 |
| 183 |
result = result[0] |
| 184 |
assert result is None |
| 185 |
|
| 186 |
|
| 187 |
def test_auto_max_map_len(): |
| 188 |
# len(packed) == 6 -> max_map_len == 3 |
| 189 |
packed = b"\xde\x00\x04zzz" |
| 190 |
with pytest.raises(UnpackValueError): |
| 191 |
unpackb(packed, raw=False) |
| 192 |
|
| 193 |
unpacker = Unpacker(max_buffer_size=6, raw=False) |
| 194 |
unpacker.feed(packed) |
| 195 |
with pytest.raises(UnpackValueError): |
| 196 |
unpacker.unpack() |