Debian Codegraph @

contrib/python-zstandard/tests/test_compressor_copy_stream.py

raw
1 import io
2 import struct
3 import unittest
4
5 import zstandard as zstd
6
7 from .common import (
8 CustomBytesIO,
9 )
10
11
12 class TestCompressor_copy_stream(unittest.TestCase):
13 def test_no_read(self):
14 source = object()
15 dest = io.BytesIO()
16
17 cctx = zstd.ZstdCompressor()
18 with self.assertRaises(ValueError):
19 cctx.copy_stream(source, dest)
20
21 def test_no_write(self):
22 source = io.BytesIO()
23 dest = object()
24
25 cctx = zstd.ZstdCompressor()
26 with self.assertRaises(ValueError):
27 cctx.copy_stream(source, dest)
28
29 def test_empty(self):
30 source = io.BytesIO()
31 dest = io.BytesIO()
32
33 cctx = zstd.ZstdCompressor(level=1, write_content_size=False)
34 r, w = cctx.copy_stream(source, dest)
35 self.assertEqual(int(r), 0)
36 self.assertEqual(w, 9)
37
38 self.assertEqual(
39 dest.getvalue(), b"\x28\xb5\x2f\xfd\x00\x00\x01\x00\x00"
40 )
41
42 def test_large_data(self):
43 source = io.BytesIO()
44 for i in range(255):
45 source.write(struct.Struct(">B").pack(i) * 16384)
46 source.seek(0)
47
48 dest = io.BytesIO()
49 cctx = zstd.ZstdCompressor()
50 r, w = cctx.copy_stream(source, dest)
51
52 self.assertEqual(r, 255 * 16384)
53 self.assertEqual(w, 999)
54
55 params = zstd.get_frame_parameters(dest.getvalue())
56 self.assertEqual(params.content_size, zstd.CONTENTSIZE_UNKNOWN)
57 self.assertEqual(params.window_size, 2097152)
58 self.assertEqual(params.dict_id, 0)
59 self.assertFalse(params.has_checksum)
60
61 def test_write_checksum(self):
62 source = io.BytesIO(b"foobar")
63 no_checksum = io.BytesIO()
64
65 cctx = zstd.ZstdCompressor(level=1)
66 cctx.copy_stream(source, no_checksum)
67
68 source.seek(0)
69 with_checksum = io.BytesIO()
70 cctx = zstd.ZstdCompressor(level=1, write_checksum=True)
71 cctx.copy_stream(source, with_checksum)
72
73 self.assertEqual(
74 len(with_checksum.getvalue()), len(no_checksum.getvalue()) + 4
75 )
76
77 no_params = zstd.get_frame_parameters(no_checksum.getvalue())
78 with_params = zstd.get_frame_parameters(with_checksum.getvalue())
79 self.assertEqual(no_params.content_size, zstd.CONTENTSIZE_UNKNOWN)
80 self.assertEqual(with_params.content_size, zstd.CONTENTSIZE_UNKNOWN)
81 self.assertEqual(no_params.dict_id, 0)
82 self.assertEqual(with_params.dict_id, 0)
83 self.assertFalse(no_params.has_checksum)
84 self.assertTrue(with_params.has_checksum)
85
86 def test_write_content_size(self):
87 source = io.BytesIO(b"foobar" * 256)
88 no_size = io.BytesIO()
89
90 cctx = zstd.ZstdCompressor(level=1, write_content_size=False)
91 cctx.copy_stream(source, no_size)
92
93 source.seek(0)
94 with_size = io.BytesIO()
95 cctx = zstd.ZstdCompressor(level=1)
96 cctx.copy_stream(source, with_size)
97
98 # Source content size is unknown, so no content size written.
99 self.assertEqual(len(with_size.getvalue()), len(no_size.getvalue()))
100
101 source.seek(0)
102 with_size = io.BytesIO()
103 cctx.copy_stream(source, with_size, size=len(source.getvalue()))
104
105 # We specified source size, so content size header is present.
106 self.assertEqual(len(with_size.getvalue()), len(no_size.getvalue()) + 1)
107
108 no_params = zstd.get_frame_parameters(no_size.getvalue())
109 with_params = zstd.get_frame_parameters(with_size.getvalue())
110 self.assertEqual(no_params.content_size, zstd.CONTENTSIZE_UNKNOWN)
111 self.assertEqual(with_params.content_size, 1536)
112 self.assertEqual(no_params.dict_id, 0)
113 self.assertEqual(with_params.dict_id, 0)
114 self.assertFalse(no_params.has_checksum)
115 self.assertFalse(with_params.has_checksum)
116
117 def test_read_write_size(self):
118 source = CustomBytesIO(b"foobarfoobar")
119 dest = CustomBytesIO()
120 cctx = zstd.ZstdCompressor()
121 r, w = cctx.copy_stream(source, dest, read_size=1, write_size=1)
122
123 self.assertEqual(r, len(source.getvalue()))
124 self.assertEqual(w, 21)
125 self.assertEqual(source._read_count, len(source.getvalue()) + 1)
126 self.assertEqual(dest._write_count, len(dest.getvalue()))
127
128 def test_multithreaded(self):
129 source = io.BytesIO()
130 source.write(b"a" * 1048576)
131 source.write(b"b" * 1048576)
132 source.write(b"c" * 1048576)
133 source.seek(0)
134
135 dest = io.BytesIO()
136 cctx = zstd.ZstdCompressor(threads=2, write_content_size=False)
137 r, w = cctx.copy_stream(source, dest)
138 self.assertEqual(r, 3145728)
139 self.assertEqual(w, 111)
140
141 params = zstd.get_frame_parameters(dest.getvalue())
142 self.assertEqual(params.content_size, zstd.CONTENTSIZE_UNKNOWN)
143 self.assertEqual(params.dict_id, 0)
144 self.assertFalse(params.has_checksum)
145
146 # Writing content size and checksum works.
147 cctx = zstd.ZstdCompressor(threads=2, write_checksum=True)
148 dest = io.BytesIO()
149 source.seek(0)
150 cctx.copy_stream(source, dest, size=len(source.getvalue()))
151
152 params = zstd.get_frame_parameters(dest.getvalue())
153 self.assertEqual(params.content_size, 3145728)
154 self.assertEqual(params.dict_id, 0)
155 self.assertTrue(params.has_checksum)
156
157 def test_bad_size(self):
158 source = io.BytesIO()
159 source.write(b"a" * 32768)
160 source.write(b"b" * 32768)
161 source.seek(0)
162
163 dest = io.BytesIO()
164
165 cctx = zstd.ZstdCompressor()
166
167 with self.assertRaisesRegex(zstd.ZstdError, "Src size is incorrect"):
168 cctx.copy_stream(source, dest, size=42)
169
170 # Try another operation on this compressor.
171 source.seek(0)
172 dest = io.BytesIO()
173 cctx.copy_stream(source, dest)
174
175 def test_read_exception(self):
176 source = CustomBytesIO(b"foo" * 1024)
177 dest = CustomBytesIO()
178
179 source.read_exception = IOError("read")
180
181 cctx = zstd.ZstdCompressor()
182
183 with self.assertRaisesRegex(IOError, "read"):
184 cctx.copy_stream(source, dest)
185
186 def test_write_exception(self):
187 source = CustomBytesIO(b"foo" * 1024)
188 dest = CustomBytesIO()
189
190 dest.write_exception = IOError("write")
191
192 cctx = zstd.ZstdCompressor()
193
194 with self.assertRaisesRegex(IOError, "write"):
195 cctx.copy_stream(source, dest)