Debian Codegraph @

test/test_simple_decompression.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 test_simple_decompression
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 Tests for decompression of single chunks.
7 """
8 import brotlicffi
9
10 import pytest
11
12
13 def test_decompression(simple_compressed_file):
14 """
15 Decompressing files returns their original form using decompress.
16 """
17 with open(simple_compressed_file[0], 'rb') as f:
18 uncompressed_data = f.read()
19
20 with open(simple_compressed_file[1], 'rb') as f:
21 compressed_data = f.read()
22
23 assert brotlicffi.decompress(compressed_data) == uncompressed_data
24
25
26 def test_decompressobj(simple_compressed_file):
27 with open(simple_compressed_file[0], 'rb') as f:
28 uncompressed_data = f.read()
29
30 with open(simple_compressed_file[1], 'rb') as f:
31 compressed_data = f.read()
32
33 o = brotlicffi.Decompressor()
34 data = o.decompress(compressed_data)
35 data += o.flush()
36 data += o.finish()
37
38 assert data == uncompressed_data
39
40
41 # `more_data_limit` allows testing `decompress(b'')` with and without a limit.
42 @pytest.mark.parametrize('more_data_limit', [100, None])
43 def test_decompressobj_with_output_buffer_limit(
44 simple_compressed_file, more_data_limit
45 ):
46 """
47 Test decompression with `output_buffer_limit` set.
48 """
49 with open(simple_compressed_file[0], 'rb') as f:
50 uncompressed_data = f.read()
51
52 with open(simple_compressed_file[1], 'rb') as f:
53 compressed_data = f.read()
54
55 o = brotlicffi.Decompressor()
56 assert o.can_accept_more_data()
57 small_limit = 100
58 result = o.decompress(compressed_data, output_buffer_limit=small_limit)
59 assert len(result) <= small_limit
60
61 # Ensure `output_buffer_limit` of zero works.
62 assert o.decompress(b'', output_buffer_limit=0) == b''
63
64 if o._unconsumed_data:
65 with pytest.raises(
66 brotlicffi.error,
67 match=(
68 r"brotli: decoder process called with data when "
69 r"'can_accept_more_data\(\)' is False"
70 ),
71 ):
72 o.decompress(b'additional data')
73
74 if not o.is_finished():
75 assert not o.can_accept_more_data()
76
77 # Continue decompressing with empty input.
78 all_output = [result]
79 while not o.can_accept_more_data() and not o.is_finished():
80 more_output = o.decompress(
81 b'', output_buffer_limit=more_data_limit
82 )
83 if more_data_limit is not None:
84 assert len(more_output) <= more_data_limit
85 all_output.append(more_output)
86 assert o.can_accept_more_data() or o.is_finished()
87
88 final_result = b''.join(all_output)
89 assert final_result == uncompressed_data
90
91
92 def test_drip_feed(simple_compressed_file):
93 """
94 Sending in the data one byte at a time still works.
95 """
96 with open(simple_compressed_file[0], 'rb') as f:
97 uncompressed_data = f.read()
98
99 with open(simple_compressed_file[1], 'rb') as f:
100 compressed_data = f.read()
101
102 outdata = []
103 o = brotlicffi.Decompressor()
104 for i in range(0, len(compressed_data)):
105 assert not o.is_finished()
106 outdata.append(o.decompress(compressed_data[i:i+1]))
107
108 assert o.is_finished()
109 outdata.append(o.flush())
110 assert o.is_finished()
111 outdata.append(o.finish())
112 assert o.is_finished()
113
114 assert b''.join(outdata) == uncompressed_data
115
116
117 @pytest.mark.parametrize('exception_cls', [brotlicffi.Error, brotlicffi.error])
118 def test_streaming_decompression_fails_properly_on_garbage(exception_cls):
119 """
120 Garbage data properly fails decompression.
121 """
122 o = brotlicffi.Decompressor()
123 with pytest.raises(exception_cls):
124 o.decompress(b'some random garbage')
125 assert not o.is_finished()
126
127
128 @pytest.mark.parametrize('exception_cls', [brotlicffi.Error, brotlicffi.error])
129 @pytest.mark.parametrize('bogus', (b'some random garbage', b'bogus'))
130 def test_decompression_fails_properly_on_garbage(bogus, exception_cls):
131 """
132 Garbage data properly fails decompression.
133 """
134 with pytest.raises(exception_cls):
135 brotlicffi.decompress(bogus)