| 1 |
import io |
| 2 |
import os |
| 3 |
import random |
| 4 |
import struct |
| 5 |
import unittest |
| 6 |
|
| 7 |
import zstandard as zstd |
| 8 |
|
| 9 |
from .common import ( |
| 10 |
CustomBytesIO, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class TestDecompressor_read_to_iter(unittest.TestCase): |
| 15 |
def test_type_validation(self): |
| 16 |
dctx = zstd.ZstdDecompressor() |
| 17 |
|
| 18 |
# Object with read() works. |
| 19 |
dctx.read_to_iter(io.BytesIO()) |
| 20 |
|
| 21 |
# Buffer protocol works. |
| 22 |
dctx.read_to_iter(b"foobar") |
| 23 |
|
| 24 |
with self.assertRaisesRegex( |
| 25 |
ValueError, "must pass an object with a read" |
| 26 |
): |
| 27 |
b"".join(dctx.read_to_iter(True)) |
| 28 |
|
| 29 |
def test_empty_input(self): |
| 30 |
dctx = zstd.ZstdDecompressor() |
| 31 |
|
| 32 |
source = io.BytesIO() |
| 33 |
it = dctx.read_to_iter(source) |
| 34 |
# TODO this is arguably wrong. Should get an error about missing frame foo. |
| 35 |
with self.assertRaises(StopIteration): |
| 36 |
next(it) |
| 37 |
|
| 38 |
it = dctx.read_to_iter(b"") |
| 39 |
with self.assertRaises(StopIteration): |
| 40 |
next(it) |
| 41 |
|
| 42 |
def test_invalid_input(self): |
| 43 |
dctx = zstd.ZstdDecompressor() |
| 44 |
|
| 45 |
source = io.BytesIO(b"foobar") |
| 46 |
it = dctx.read_to_iter(source) |
| 47 |
with self.assertRaisesRegex(zstd.ZstdError, "Unknown frame descriptor"): |
| 48 |
next(it) |
| 49 |
|
| 50 |
it = dctx.read_to_iter(b"foobar") |
| 51 |
with self.assertRaisesRegex(zstd.ZstdError, "Unknown frame descriptor"): |
| 52 |
next(it) |
| 53 |
|
| 54 |
def test_empty_roundtrip(self): |
| 55 |
cctx = zstd.ZstdCompressor(level=1, write_content_size=False) |
| 56 |
empty = cctx.compress(b"") |
| 57 |
|
| 58 |
source = io.BytesIO(empty) |
| 59 |
source.seek(0) |
| 60 |
|
| 61 |
dctx = zstd.ZstdDecompressor() |
| 62 |
it = dctx.read_to_iter(source) |
| 63 |
|
| 64 |
# No chunks should be emitted since there is no data. |
| 65 |
with self.assertRaises(StopIteration): |
| 66 |
next(it) |
| 67 |
|
| 68 |
# Again for good measure. |
| 69 |
with self.assertRaises(StopIteration): |
| 70 |
next(it) |
| 71 |
|
| 72 |
def test_skip_bytes_too_large(self): |
| 73 |
dctx = zstd.ZstdDecompressor() |
| 74 |
|
| 75 |
with self.assertRaisesRegex( |
| 76 |
ValueError, "skip_bytes must be smaller than read_size" |
| 77 |
): |
| 78 |
b"".join(dctx.read_to_iter(b"", skip_bytes=1, read_size=1)) |
| 79 |
|
| 80 |
with self.assertRaisesRegex( |
| 81 |
ValueError, "skip_bytes larger than first input chunk" |
| 82 |
): |
| 83 |
b"".join(dctx.read_to_iter(b"foobar", skip_bytes=10)) |
| 84 |
|
| 85 |
def test_skip_bytes(self): |
| 86 |
cctx = zstd.ZstdCompressor(write_content_size=False) |
| 87 |
compressed = cctx.compress(b"foobar") |
| 88 |
|
| 89 |
dctx = zstd.ZstdDecompressor() |
| 90 |
output = b"".join(dctx.read_to_iter(b"hdr" + compressed, skip_bytes=3)) |
| 91 |
self.assertEqual(output, b"foobar") |
| 92 |
|
| 93 |
def test_large_output(self): |
| 94 |
source = io.BytesIO() |
| 95 |
source.write(b"f" * zstd.DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE) |
| 96 |
source.write(b"o") |
| 97 |
source.seek(0) |
| 98 |
|
| 99 |
cctx = zstd.ZstdCompressor(level=1) |
| 100 |
compressed = io.BytesIO(cctx.compress(source.getvalue())) |
| 101 |
compressed.seek(0) |
| 102 |
|
| 103 |
dctx = zstd.ZstdDecompressor() |
| 104 |
it = dctx.read_to_iter(compressed) |
| 105 |
|
| 106 |
chunks = [] |
| 107 |
chunks.append(next(it)) |
| 108 |
chunks.append(next(it)) |
| 109 |
|
| 110 |
with self.assertRaises(StopIteration): |
| 111 |
next(it) |
| 112 |
|
| 113 |
decompressed = b"".join(chunks) |
| 114 |
self.assertEqual(decompressed, source.getvalue()) |
| 115 |
|
| 116 |
# And again with buffer protocol. |
| 117 |
it = dctx.read_to_iter(compressed.getvalue()) |
| 118 |
chunks = [] |
| 119 |
chunks.append(next(it)) |
| 120 |
chunks.append(next(it)) |
| 121 |
|
| 122 |
with self.assertRaises(StopIteration): |
| 123 |
next(it) |
| 124 |
|
| 125 |
decompressed = b"".join(chunks) |
| 126 |
self.assertEqual(decompressed, source.getvalue()) |
| 127 |
|
| 128 |
@unittest.skipUnless( |
| 129 |
"ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set" |
| 130 |
) |
| 131 |
def test_large_input(self): |
| 132 |
bytes = list(struct.Struct(">B").pack(i) for i in range(256)) |
| 133 |
compressed = io.BytesIO() |
| 134 |
input_size = 0 |
| 135 |
cctx = zstd.ZstdCompressor(level=1) |
| 136 |
with cctx.stream_writer(compressed, closefd=False) as compressor: |
| 137 |
while True: |
| 138 |
compressor.write(random.choice(bytes)) |
| 139 |
input_size += 1 |
| 140 |
|
| 141 |
have_compressed = ( |
| 142 |
len(compressed.getvalue()) |
| 143 |
> zstd.DECOMPRESSION_RECOMMENDED_INPUT_SIZE |
| 144 |
) |
| 145 |
have_raw = ( |
| 146 |
input_size > zstd.DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE * 2 |
| 147 |
) |
| 148 |
if have_compressed and have_raw: |
| 149 |
break |
| 150 |
|
| 151 |
compressed = io.BytesIO(compressed.getvalue()) |
| 152 |
self.assertGreater( |
| 153 |
len(compressed.getvalue()), |
| 154 |
zstd.DECOMPRESSION_RECOMMENDED_INPUT_SIZE, |
| 155 |
) |
| 156 |
|
| 157 |
dctx = zstd.ZstdDecompressor() |
| 158 |
it = dctx.read_to_iter(compressed) |
| 159 |
|
| 160 |
chunks = [] |
| 161 |
chunks.append(next(it)) |
| 162 |
chunks.append(next(it)) |
| 163 |
chunks.append(next(it)) |
| 164 |
|
| 165 |
with self.assertRaises(StopIteration): |
| 166 |
next(it) |
| 167 |
|
| 168 |
decompressed = b"".join(chunks) |
| 169 |
self.assertEqual(len(decompressed), input_size) |
| 170 |
|
| 171 |
# And again with buffer protocol. |
| 172 |
it = dctx.read_to_iter(compressed.getvalue()) |
| 173 |
|
| 174 |
chunks = [] |
| 175 |
chunks.append(next(it)) |
| 176 |
chunks.append(next(it)) |
| 177 |
chunks.append(next(it)) |
| 178 |
|
| 179 |
with self.assertRaises(StopIteration): |
| 180 |
next(it) |
| 181 |
|
| 182 |
decompressed = b"".join(chunks) |
| 183 |
self.assertEqual(len(decompressed), input_size) |
| 184 |
|
| 185 |
def test_interesting(self): |
| 186 |
# Found this edge case via fuzzing. |
| 187 |
cctx = zstd.ZstdCompressor(level=1) |
| 188 |
|
| 189 |
source = io.BytesIO() |
| 190 |
|
| 191 |
compressed = io.BytesIO() |
| 192 |
with cctx.stream_writer(compressed, closefd=False) as compressor: |
| 193 |
for i in range(256): |
| 194 |
chunk = b"\0" * 1024 |
| 195 |
compressor.write(chunk) |
| 196 |
source.write(chunk) |
| 197 |
|
| 198 |
dctx = zstd.ZstdDecompressor() |
| 199 |
|
| 200 |
simple = dctx.decompress( |
| 201 |
compressed.getvalue(), max_output_size=len(source.getvalue()) |
| 202 |
) |
| 203 |
self.assertEqual(simple, source.getvalue()) |
| 204 |
|
| 205 |
compressed = io.BytesIO(compressed.getvalue()) |
| 206 |
streamed = b"".join(dctx.read_to_iter(compressed)) |
| 207 |
self.assertEqual(streamed, source.getvalue()) |
| 208 |
|
| 209 |
def test_read_write_size(self): |
| 210 |
source = CustomBytesIO(zstd.ZstdCompressor().compress(b"foobarfoobar")) |
| 211 |
dctx = zstd.ZstdDecompressor() |
| 212 |
for chunk in dctx.read_to_iter(source, read_size=1, write_size=1): |
| 213 |
self.assertEqual(len(chunk), 1) |
| 214 |
|
| 215 |
self.assertEqual(source._read_count, len(source.getvalue())) |
| 216 |
|
| 217 |
def test_magic_less(self): |
| 218 |
params = zstd.ZstdCompressionParameters.from_level( |
| 219 |
1, format=zstd.FORMAT_ZSTD1_MAGICLESS |
| 220 |
) |
| 221 |
cctx = zstd.ZstdCompressor(compression_params=params) |
| 222 |
frame = cctx.compress(b"foobar") |
| 223 |
|
| 224 |
self.assertNotEqual(frame[0:4], b"\x28\xb5\x2f\xfd") |
| 225 |
|
| 226 |
dctx = zstd.ZstdDecompressor() |
| 227 |
with self.assertRaisesRegex( |
| 228 |
zstd.ZstdError, "error determining content size from frame header" |
| 229 |
): |
| 230 |
dctx.decompress(frame) |
| 231 |
|
| 232 |
dctx = zstd.ZstdDecompressor(format=zstd.FORMAT_ZSTD1_MAGICLESS) |
| 233 |
res = b"".join(dctx.read_to_iter(frame)) |
| 234 |
self.assertEqual(res, b"foobar") |