1import pytest
2from srsly.msgpack import packb, unpackb, Packer, Unpacker, ExtType
3from srsly.msgpack import PackOverflowError, PackValueError, UnpackValueError
4
5
6def test_integer():
7    x = -(2 ** 63)
8    assert unpackb(packb(x)) == x
9    with pytest.raises(PackOverflowError):
10        packb(x - 1)
11
12    x = 2 ** 64 - 1
13    assert unpackb(packb(x)) == x
14    with pytest.raises(PackOverflowError):
15        packb(x + 1)
16
17
18def test_array_header():
19    packer = Packer()
20    packer.pack_array_header(2 ** 32 - 1)
21    with pytest.raises(PackValueError):
22        packer.pack_array_header(2 ** 32)
23
24
25def test_map_header():
26    packer = Packer()
27    packer.pack_map_header(2 ** 32 - 1)
28    with pytest.raises(PackValueError):
29        packer.pack_array_header(2 ** 32)
30
31
32def test_max_str_len():
33    d = "x" * 3
34    packed = packb(d)
35
36    unpacker = Unpacker(max_str_len=3, raw=False)
37    unpacker.feed(packed)
38    assert unpacker.unpack() == d
39
40    unpacker = Unpacker(max_str_len=2, raw=False)
41    with pytest.raises(UnpackValueError):
42        unpacker.feed(packed)
43        unpacker.unpack()
44
45
46def test_max_bin_len():
47    d = b"x" * 3
48    packed = packb(d, use_bin_type=True)
49
50    unpacker = Unpacker(max_bin_len=3)
51    unpacker.feed(packed)
52    assert unpacker.unpack() == d
53
54    unpacker = Unpacker(max_bin_len=2)
55    with pytest.raises(UnpackValueError):
56        unpacker.feed(packed)
57        unpacker.unpack()
58
59
60def test_max_array_len():
61    d = [1, 2, 3]
62    packed = packb(d)
63
64    unpacker = Unpacker(max_array_len=3)
65    unpacker.feed(packed)
66    assert unpacker.unpack() == d
67
68    unpacker = Unpacker(max_array_len=2)
69    with pytest.raises(UnpackValueError):
70        unpacker.feed(packed)
71        unpacker.unpack()
72
73
74def test_max_map_len():
75    d = {1: 2, 3: 4, 5: 6}
76    packed = packb(d)
77
78    unpacker = Unpacker(max_map_len=3)
79    unpacker.feed(packed)
80    assert unpacker.unpack() == d
81
82    unpacker = Unpacker(max_map_len=2)
83    with pytest.raises(UnpackValueError):
84        unpacker.feed(packed)
85        unpacker.unpack()
86
87
88def test_max_ext_len():
89    d = ExtType(42, b"abc")
90    packed = packb(d)
91
92    unpacker = Unpacker(max_ext_len=3)
93    unpacker.feed(packed)
94    assert unpacker.unpack() == d
95
96    unpacker = Unpacker(max_ext_len=2)
97    with pytest.raises(UnpackValueError):
98        unpacker.feed(packed)
99        unpacker.unpack()
100
101
102# PyPy fails following tests because of constant folding?
103# https://bugs.pypy.org/issue1721
104# @pytest.mark.skipif(True, reason="Requires very large memory.")
105# def test_binary():
106#    x = b'x' * (2**32 - 1)
107#    assert unpackb(packb(x)) == x
108#    del x
109#    x = b'x' * (2**32)
110#    with pytest.raises(ValueError):
111#        packb(x)
112#
113#
114# @pytest.mark.skipif(True, reason="Requires very large memory.")
115# def test_string():
116#    x = 'x' * (2**32 - 1)
117#    assert unpackb(packb(x)) == x
118#    x += 'y'
119#    with pytest.raises(ValueError):
120#        packb(x)
121#
122#
123# @pytest.mark.skipif(True, reason="Requires very large memory.")
124# def test_array():
125#    x = [0] * (2**32 - 1)
126#    assert unpackb(packb(x)) == x
127#    x.append(0)
128#    with pytest.raises(ValueError):
129#        packb(x)
130