msgpack-python/test/test_limits.py

168 lines
3.8 KiB
Python
Raw Normal View History

2014-03-26 02:49:03 +09:00
#!/usr/bin/env python
# coding: utf-8
2014-03-26 13:32:28 +09:00
from __future__ import absolute_import, division, print_function, unicode_literals
2014-03-26 02:49:03 +09:00
import pytest
2016-02-14 11:58:56 +09:00
from msgpack import (
2019-12-05 18:51:45 +09:00
packb,
unpackb,
Packer,
Unpacker,
ExtType,
PackOverflowError,
PackValueError,
UnpackValueError,
2016-02-14 11:58:56 +09:00
)
2014-03-26 02:49:03 +09:00
2016-02-14 11:58:56 +09:00
def test_integer():
2014-03-26 02:49:03 +09:00
x = -(2 ** 63)
assert unpackb(packb(x)) == x
2016-02-14 11:58:56 +09:00
with pytest.raises(PackOverflowError):
2019-12-05 18:51:45 +09:00
packb(x - 1)
2014-03-26 02:49:03 +09:00
x = 2 ** 64 - 1
assert unpackb(packb(x)) == x
2016-02-14 11:58:56 +09:00
with pytest.raises(PackOverflowError):
2019-12-05 18:51:45 +09:00
packb(x + 1)
2014-03-26 02:49:03 +09:00
2014-03-26 03:00:47 +09:00
2016-02-14 11:58:56 +09:00
def test_array_header():
2014-03-26 03:00:47 +09:00
packer = Packer()
2019-12-05 18:51:45 +09:00
packer.pack_array_header(2 ** 32 - 1)
2016-02-14 11:58:56 +09:00
with pytest.raises(PackValueError):
2019-12-05 18:51:45 +09:00
packer.pack_array_header(2 ** 32)
2014-03-26 03:00:47 +09:00
2016-02-14 11:58:56 +09:00
def test_map_header():
2014-03-26 03:00:47 +09:00
packer = Packer()
2019-12-05 18:51:45 +09:00
packer.pack_map_header(2 ** 32 - 1)
2016-02-14 11:58:56 +09:00
with pytest.raises(PackValueError):
2019-12-05 18:51:45 +09:00
packer.pack_array_header(2 ** 32)
2014-03-26 03:00:47 +09:00
2016-02-14 11:58:56 +09:00
def test_max_str_len():
2019-12-05 18:51:45 +09:00
d = "x" * 3
packed = packb(d)
2018-01-12 19:22:36 +09:00
unpacker = Unpacker(max_str_len=3, raw=False)
unpacker.feed(packed)
assert unpacker.unpack() == d
2018-01-12 19:22:36 +09:00
unpacker = Unpacker(max_str_len=2, raw=False)
2016-02-14 11:58:56 +09:00
with pytest.raises(UnpackValueError):
unpacker.feed(packed)
unpacker.unpack()
2016-02-14 11:58:56 +09:00
def test_max_bin_len():
2019-12-05 18:51:45 +09:00
d = b"x" * 3
packed = packb(d, use_bin_type=True)
unpacker = Unpacker(max_bin_len=3)
unpacker.feed(packed)
assert unpacker.unpack() == d
unpacker = Unpacker(max_bin_len=2)
2016-02-14 11:58:56 +09:00
with pytest.raises(UnpackValueError):
unpacker.feed(packed)
unpacker.unpack()
2016-02-14 11:58:56 +09:00
def test_max_array_len():
2019-12-05 18:51:45 +09:00
d = [1, 2, 3]
packed = packb(d)
unpacker = Unpacker(max_array_len=3)
unpacker.feed(packed)
assert unpacker.unpack() == d
unpacker = Unpacker(max_array_len=2)
2016-02-14 11:58:56 +09:00
with pytest.raises(UnpackValueError):
unpacker.feed(packed)
unpacker.unpack()
2016-02-14 11:58:56 +09:00
def test_max_map_len():
d = {1: 2, 3: 4, 5: 6}
packed = packb(d)
unpacker = Unpacker(max_map_len=3)
unpacker.feed(packed)
assert unpacker.unpack() == d
unpacker = Unpacker(max_map_len=2)
2016-02-14 11:58:56 +09:00
with pytest.raises(UnpackValueError):
unpacker.feed(packed)
unpacker.unpack()
2016-02-14 11:58:56 +09:00
def test_max_ext_len():
d = ExtType(42, b"abc")
packed = packb(d)
unpacker = Unpacker(max_ext_len=3)
unpacker.feed(packed)
assert unpacker.unpack() == d
unpacker = Unpacker(max_ext_len=2)
2016-02-14 11:58:56 +09:00
with pytest.raises(UnpackValueError):
unpacker.feed(packed)
unpacker.unpack()
2014-03-26 15:12:28 +09:00
# PyPy fails following tests because of constant folding?
# https://bugs.pypy.org/issue1721
2019-12-05 18:51:45 +09:00
# @pytest.mark.skipif(True, reason="Requires very large memory.")
# def test_binary():
2014-03-26 15:12:28 +09:00
# x = b'x' * (2**32 - 1)
# assert unpackb(packb(x)) == x
# del x
# x = b'x' * (2**32)
# with pytest.raises(ValueError):
# packb(x)
#
#
2019-12-05 18:51:45 +09:00
# @pytest.mark.skipif(True, reason="Requires very large memory.")
# def test_string():
2014-03-26 15:12:28 +09:00
# x = 'x' * (2**32 - 1)
# assert unpackb(packb(x)) == x
# x += 'y'
# with pytest.raises(ValueError):
# packb(x)
#
#
2019-12-05 18:51:45 +09:00
# @pytest.mark.skipif(True, reason="Requires very large memory.")
# def test_array():
2014-03-26 15:12:28 +09:00
# x = [0] * (2**32 - 1)
# assert unpackb(packb(x)) == x
# x.append(0)
# with pytest.raises(ValueError):
# packb(x)
2019-01-24 18:46:39 +09:00
# auto max len
2019-12-05 18:51:45 +09:00
2019-01-24 18:46:39 +09:00
def test_auto_max_array_len():
2019-12-05 18:51:45 +09:00
packed = b"\xde\x00\x06zz"
2019-01-24 18:46:39 +09:00
with pytest.raises(UnpackValueError):
unpackb(packed, raw=False)
unpacker = Unpacker(max_buffer_size=5, raw=False)
unpacker.feed(packed)
with pytest.raises(UnpackValueError):
unpacker.unpack()
2019-12-05 18:51:45 +09:00
2019-01-24 18:46:39 +09:00
def test_auto_max_map_len():
# len(packed) == 6 -> max_map_len == 3
2019-12-05 18:51:45 +09:00
packed = b"\xde\x00\x04zzz"
2019-01-24 18:46:39 +09:00
with pytest.raises(UnpackValueError):
unpackb(packed, raw=False)
unpacker = Unpacker(max_buffer_size=6, raw=False)
unpacker.feed(packed)
with pytest.raises(UnpackValueError):
unpacker.unpack()