Debian Codegraph @

contrib/python-zstandard/tests/test_decompressor_fuzzing.py

raw
1 import io
2 import os
3 import unittest
4
5 try:
6 import hypothesis
7 import hypothesis.strategies as strategies
8 except ImportError:
9 raise unittest.SkipTest("hypothesis not available")
10
11 import zstandard as zstd
12
13 from .common import random_input_data
14
15
16 @unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set")
17 class TestDecompressor_stream_reader_fuzzing(unittest.TestCase):
18 @hypothesis.settings(
19 suppress_health_check=[
20 hypothesis.HealthCheck.large_base_example,
21 hypothesis.HealthCheck.too_slow,
22 ]
23 )
24 @hypothesis.given(
25 original=strategies.sampled_from(random_input_data()),
26 level=strategies.integers(min_value=1, max_value=5),
27 streaming=strategies.booleans(),
28 source_read_size=strategies.integers(1, 1048576),
29 read_sizes=strategies.data(),
30 )
31 def test_stream_source_read_variance(
32 self, original, level, streaming, source_read_size, read_sizes
33 ):
34 cctx = zstd.ZstdCompressor(level=level)
35
36 if streaming:
37 source = io.BytesIO()
38 writer = cctx.stream_writer(source)
39 writer.write(original)
40 writer.flush(zstd.FLUSH_FRAME)
41 source.seek(0)
42 else:
43 frame = cctx.compress(original)
44 source = io.BytesIO(frame)
45
46 dctx = zstd.ZstdDecompressor()
47
48 chunks = []
49 with dctx.stream_reader(source, read_size=source_read_size) as reader:
50 while True:
51 read_size = read_sizes.draw(strategies.integers(-1, 131072))
52 chunk = reader.read(read_size)
53 if not chunk and read_size:
54 break
55
56 chunks.append(chunk)
57
58 self.assertEqual(b"".join(chunks), original)
59
60 # Similar to above except we have a constant read() size.
61 @hypothesis.settings(
62 suppress_health_check=[hypothesis.HealthCheck.large_base_example]
63 )
64 @hypothesis.given(
65 original=strategies.sampled_from(random_input_data()),
66 level=strategies.integers(min_value=1, max_value=5),
67 streaming=strategies.booleans(),
68 source_read_size=strategies.integers(1, 1048576),
69 read_size=strategies.integers(-1, 131072),
70 )
71 def test_stream_source_read_size(
72 self, original, level, streaming, source_read_size, read_size
73 ):
74 if read_size == 0:
75 read_size = 1
76
77 cctx = zstd.ZstdCompressor(level=level)
78
79 if streaming:
80 source = io.BytesIO()
81 writer = cctx.stream_writer(source)
82 writer.write(original)
83 writer.flush(zstd.FLUSH_FRAME)
84 source.seek(0)
85 else:
86 frame = cctx.compress(original)
87 source = io.BytesIO(frame)
88
89 dctx = zstd.ZstdDecompressor()
90
91 chunks = []
92 reader = dctx.stream_reader(source, read_size=source_read_size)
93 while True:
94 chunk = reader.read(read_size)
95 if not chunk and read_size:
96 break
97
98 chunks.append(chunk)
99
100 self.assertEqual(b"".join(chunks), original)
101
102 @hypothesis.settings(
103 suppress_health_check=[
104 hypothesis.HealthCheck.large_base_example,
105 hypothesis.HealthCheck.too_slow,
106 ]
107 )
108 @hypothesis.given(
109 original=strategies.sampled_from(random_input_data()),
110 level=strategies.integers(min_value=1, max_value=5),
111 streaming=strategies.booleans(),
112 source_read_size=strategies.integers(1, 1048576),
113 read_sizes=strategies.data(),
114 )
115 def test_buffer_source_read_variance(
116 self, original, level, streaming, source_read_size, read_sizes
117 ):
118 cctx = zstd.ZstdCompressor(level=level)
119
120 if streaming:
121 source = io.BytesIO()
122 writer = cctx.stream_writer(source)
123 writer.write(original)
124 writer.flush(zstd.FLUSH_FRAME)
125 frame = source.getvalue()
126 else:
127 frame = cctx.compress(original)
128
129 dctx = zstd.ZstdDecompressor()
130 chunks = []
131
132 with dctx.stream_reader(frame, read_size=source_read_size) as reader:
133 while True:
134 read_size = read_sizes.draw(strategies.integers(-1, 131072))
135 chunk = reader.read(read_size)
136 if not chunk and read_size:
137 break
138
139 chunks.append(chunk)
140
141 self.assertEqual(b"".join(chunks), original)
142
143 # Similar to above except we have a constant read() size.
144 @hypothesis.settings(
145 suppress_health_check=[hypothesis.HealthCheck.large_base_example]
146 )
147 @hypothesis.given(
148 original=strategies.sampled_from(random_input_data()),
149 level=strategies.integers(min_value=1, max_value=5),
150 streaming=strategies.booleans(),
151 source_read_size=strategies.integers(1, 1048576),
152 read_size=strategies.integers(-1, 131072),
153 )
154 def test_buffer_source_constant_read_size(
155 self, original, level, streaming, source_read_size, read_size
156 ):
157 if read_size == 0:
158 read_size = -1
159
160 cctx = zstd.ZstdCompressor(level=level)
161
162 if streaming:
163 source = io.BytesIO()
164 writer = cctx.stream_writer(source)
165 writer.write(original)
166 writer.flush(zstd.FLUSH_FRAME)
167 frame = source.getvalue()
168 else:
169 frame = cctx.compress(original)
170
171 dctx = zstd.ZstdDecompressor()
172 chunks = []
173
174 reader = dctx.stream_reader(frame, read_size=source_read_size)
175 while True:
176 chunk = reader.read(read_size)
177 if not chunk and read_size:
178 break
179
180 chunks.append(chunk)
181
182 self.assertEqual(b"".join(chunks), original)
183
184 @hypothesis.settings(
185 suppress_health_check=[hypothesis.HealthCheck.large_base_example]
186 )
187 @hypothesis.given(
188 original=strategies.sampled_from(random_input_data()),
189 level=strategies.integers(min_value=1, max_value=5),
190 streaming=strategies.booleans(),
191 source_read_size=strategies.integers(1, 1048576),
192 )
193 def test_stream_source_readall(
194 self, original, level, streaming, source_read_size
195 ):
196 cctx = zstd.ZstdCompressor(level=level)
197
198 if streaming:
199 source = io.BytesIO()
200 writer = cctx.stream_writer(source)
201 writer.write(original)
202 writer.flush(zstd.FLUSH_FRAME)
203 source.seek(0)
204 else:
205 frame = cctx.compress(original)
206 source = io.BytesIO(frame)
207
208 dctx = zstd.ZstdDecompressor()
209
210 data = dctx.stream_reader(source, read_size=source_read_size).readall()
211 self.assertEqual(data, original)
212
213 @hypothesis.settings(
214 suppress_health_check=[
215 hypothesis.HealthCheck.large_base_example,
216 hypothesis.HealthCheck.too_slow,
217 ]
218 )
219 @hypothesis.given(
220 original=strategies.sampled_from(random_input_data()),
221 level=strategies.integers(min_value=1, max_value=5),
222 streaming=strategies.booleans(),
223 source_read_size=strategies.integers(1, 1048576),
224 read_sizes=strategies.data(),
225 )
226 def test_stream_source_read1_variance(
227 self, original, level, streaming, source_read_size, read_sizes
228 ):
229 cctx = zstd.ZstdCompressor(level=level)
230
231 if streaming:
232 source = io.BytesIO()
233 writer = cctx.stream_writer(source)
234 writer.write(original)
235 writer.flush(zstd.FLUSH_FRAME)
236 source.seek(0)
237 else:
238 frame = cctx.compress(original)
239 source = io.BytesIO(frame)
240
241 dctx = zstd.ZstdDecompressor()
242
243 chunks = []
244 with dctx.stream_reader(source, read_size=source_read_size) as reader:
245 while True:
246 read_size = read_sizes.draw(strategies.integers(-1, 131072))
247 chunk = reader.read1(read_size)
248 if not chunk and read_size:
249 break
250
251 chunks.append(chunk)
252
253 self.assertEqual(b"".join(chunks), original)
254
255 @hypothesis.settings(
256 suppress_health_check=[
257 hypothesis.HealthCheck.large_base_example,
258 hypothesis.HealthCheck.too_slow,
259 ]
260 )
261 @hypothesis.given(
262 original=strategies.sampled_from(random_input_data()),
263 level=strategies.integers(min_value=1, max_value=5),
264 streaming=strategies.booleans(),
265 source_read_size=strategies.integers(1, 1048576),
266 read_sizes=strategies.data(),
267 )
268 def test_stream_source_readinto1_variance(
269 self, original, level, streaming, source_read_size, read_sizes
270 ):
271 cctx = zstd.ZstdCompressor(level=level)
272
273 if streaming:
274 source = io.BytesIO()
275 writer = cctx.stream_writer(source)
276 writer.write(original)
277 writer.flush(zstd.FLUSH_FRAME)
278 source.seek(0)
279 else:
280 frame = cctx.compress(original)
281 source = io.BytesIO(frame)
282
283 dctx = zstd.ZstdDecompressor()
284
285 chunks = []
286 with dctx.stream_reader(source, read_size=source_read_size) as reader:
287 while True:
288 read_size = read_sizes.draw(strategies.integers(1, 131072))
289 b = bytearray(read_size)
290 count = reader.readinto1(b)
291
292 if not count:
293 break
294
295 chunks.append(bytes(b[0:count]))
296
297 self.assertEqual(b"".join(chunks), original)
298
299 @hypothesis.settings(
300 suppress_health_check=[
301 hypothesis.HealthCheck.data_too_large,
302 hypothesis.HealthCheck.large_base_example,
303 hypothesis.HealthCheck.too_slow,
304 ]
305 )
306 @hypothesis.given(
307 original=strategies.sampled_from(random_input_data()),
308 level=strategies.integers(min_value=1, max_value=5),
309 source_read_size=strategies.integers(1, 1048576),
310 seek_amounts=strategies.data(),
311 read_sizes=strategies.data(),
312 )
313 def test_relative_seeks(
314 self, original, level, source_read_size, seek_amounts, read_sizes
315 ):
316 cctx = zstd.ZstdCompressor(level=level)
317 frame = cctx.compress(original)
318
319 dctx = zstd.ZstdDecompressor()
320
321 with dctx.stream_reader(frame, read_size=source_read_size) as reader:
322 while True:
323 amount = seek_amounts.draw(strategies.integers(0, 16384))
324 reader.seek(amount, os.SEEK_CUR)
325
326 offset = reader.tell()
327 read_amount = read_sizes.draw(strategies.integers(1, 16384))
328 chunk = reader.read(read_amount)
329
330 if not chunk:
331 break
332
333 self.assertEqual(original[offset : offset + len(chunk)], chunk)
334
335 @hypothesis.settings(
336 suppress_health_check=[
337 hypothesis.HealthCheck.large_base_example,
338 hypothesis.HealthCheck.too_slow,
339 ]
340 )
341 @hypothesis.given(
342 chunks=strategies.lists(
343 strategies.sampled_from(random_input_data()),
344 min_size=2,
345 max_size=10,
346 ),
347 level=strategies.integers(min_value=1, max_value=5),
348 source_read_size=strategies.integers(1, 1048576),
349 read_sizes=strategies.data(),
350 )
351 def test_multiple_frames(self, chunks, level, source_read_size, read_sizes):
352 cctx = zstd.ZstdCompressor(level=level)
353 source = io.BytesIO()
354 buffer = io.BytesIO()
355 writer = cctx.stream_writer(buffer)
356
357 for chunk in chunks:
358 source.write(chunk)
359 writer.write(chunk)
360 writer.flush(zstd.FLUSH_FRAME)
361
362 dctx = zstd.ZstdDecompressor()
363 buffer.seek(0)
364 reader = dctx.stream_reader(
365 buffer, read_size=source_read_size, read_across_frames=True
366 )
367
368 chunks = []
369
370 while True:
371 read_amount = read_sizes.draw(strategies.integers(-1, 16384))
372 chunk = reader.read(read_amount)
373
374 if not chunk and read_amount:
375 break
376
377 chunks.append(chunk)
378
379 self.assertEqual(source.getvalue(), b"".join(chunks))
380
381
382 @unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set")
383 class TestDecompressor_stream_writer_fuzzing(unittest.TestCase):
384 @hypothesis.settings(
385 suppress_health_check=[
386 hypothesis.HealthCheck.large_base_example,
387 hypothesis.HealthCheck.too_slow,
388 ]
389 )
390 @hypothesis.given(
391 original=strategies.sampled_from(random_input_data()),
392 level=strategies.integers(min_value=1, max_value=5),
393 write_size=strategies.integers(min_value=1, max_value=8192),
394 input_sizes=strategies.data(),
395 )
396 def test_write_size_variance(
397 self, original, level, write_size, input_sizes
398 ):
399 cctx = zstd.ZstdCompressor(level=level)
400 frame = cctx.compress(original)
401
402 dctx = zstd.ZstdDecompressor()
403 source = io.BytesIO(frame)
404 dest = io.BytesIO()
405
406 with dctx.stream_writer(
407 dest, write_size=write_size, closefd=False
408 ) as decompressor:
409 while True:
410 input_size = input_sizes.draw(strategies.integers(1, 4096))
411 chunk = source.read(input_size)
412 if not chunk:
413 break
414
415 decompressor.write(chunk)
416
417 self.assertEqual(dest.getvalue(), original)
418
419
420 @unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set")
421 class TestDecompressor_copy_stream_fuzzing(unittest.TestCase):
422 @hypothesis.settings(
423 suppress_health_check=[
424 hypothesis.HealthCheck.large_base_example,
425 hypothesis.HealthCheck.too_slow,
426 ]
427 )
428 @hypothesis.given(
429 original=strategies.sampled_from(random_input_data()),
430 level=strategies.integers(min_value=1, max_value=5),
431 read_size=strategies.integers(min_value=1, max_value=8192),
432 write_size=strategies.integers(min_value=1, max_value=8192),
433 )
434 def test_read_write_size_variance(
435 self, original, level, read_size, write_size
436 ):
437 cctx = zstd.ZstdCompressor(level=level)
438 frame = cctx.compress(original)
439
440 source = io.BytesIO(frame)
441 dest = io.BytesIO()
442
443 dctx = zstd.ZstdDecompressor()
444 dctx.copy_stream(
445 source, dest, read_size=read_size, write_size=write_size
446 )
447
448 self.assertEqual(dest.getvalue(), original)
449
450
451 @unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set")
452 class TestDecompressor_decompressobj_fuzzing(unittest.TestCase):
453 @hypothesis.settings(
454 suppress_health_check=[
455 hypothesis.HealthCheck.large_base_example,
456 hypothesis.HealthCheck.too_slow,
457 ]
458 )
459 @hypothesis.given(
460 original=strategies.sampled_from(random_input_data()),
461 level=strategies.integers(min_value=1, max_value=5),
462 chunk_sizes=strategies.data(),
463 )
464 def test_random_input_sizes(self, original, level, chunk_sizes):
465 cctx = zstd.ZstdCompressor(level=level)
466 frame = cctx.compress(original)
467
468 source = io.BytesIO(frame)
469
470 dctx = zstd.ZstdDecompressor()
471 dobj = dctx.decompressobj()
472
473 chunks = []
474 while True:
475 chunk_size = chunk_sizes.draw(strategies.integers(1, 4096))
476 chunk = source.read(chunk_size)
477 if not chunk:
478 break
479
480 chunks.append(dobj.decompress(chunk))
481
482 self.assertEqual(b"".join(chunks), original)
483
484 @hypothesis.settings(
485 suppress_health_check=[
486 hypothesis.HealthCheck.large_base_example,
487 hypothesis.HealthCheck.too_slow,
488 ]
489 )
490 @hypothesis.given(
491 original=strategies.sampled_from(random_input_data()),
492 level=strategies.integers(min_value=1, max_value=5),
493 write_size=strategies.integers(
494 min_value=1,
495 max_value=4 * zstd.DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE,
496 ),
497 chunk_sizes=strategies.data(),
498 )
499 def test_random_output_sizes(
500 self, original, level, write_size, chunk_sizes
501 ):
502 cctx = zstd.ZstdCompressor(level=level)
503 frame = cctx.compress(original)
504
505 source = io.BytesIO(frame)
506
507 dctx = zstd.ZstdDecompressor()
508 dobj = dctx.decompressobj(write_size=write_size)
509
510 chunks = []
511 while True:
512 chunk_size = chunk_sizes.draw(strategies.integers(1, 4096))
513 chunk = source.read(chunk_size)
514 if not chunk:
515 break
516
517 chunks.append(dobj.decompress(chunk))
518
519 self.assertEqual(b"".join(chunks), original)
520
521 @hypothesis.given(
522 chunks=strategies.lists(
523 strategies.sampled_from(random_input_data()),
524 min_size=2,
525 max_size=10,
526 ),
527 level=strategies.integers(min_value=1, max_value=5),
528 write_size=strategies.integers(
529 min_value=1,
530 max_value=4 * zstd.DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE,
531 ),
532 read_sizes=strategies.data(),
533 )
534 def test_read_across_frames_false(
535 self, chunks, level, write_size, read_sizes
536 ):
537 cctx = zstd.ZstdCompressor(level=level)
538
539 source = io.BytesIO()
540 source_chunks = []
541 compressed = io.BytesIO()
542
543 for chunk in chunks:
544 source.write(chunk)
545 source_chunks.append(chunk)
546 compressed.write(cctx.compress(chunk))
547
548 compressed.seek(0)
549
550 dctx = zstd.ZstdDecompressor()
551 dobj = dctx.decompressobj(
552 write_size=write_size, read_across_frames=False
553 )
554
555 decompressed = io.BytesIO()
556
557 while True:
558 read_size = read_sizes.draw(strategies.integers(1, 4096))
559 chunk = compressed.read(read_size)
560 if not chunk:
561 break
562
563 try:
564 decompressed.write(dobj.decompress(chunk))
565 except zstd.ZstdError as e:
566 if e.args[0] == "cannot use a decompressobj multiple times":
567 break
568 else:
569 raise
570
571 self.assertEqual(decompressed.getvalue(), source_chunks[0])
572
573 @hypothesis.settings(
574 suppress_health_check=[
575 hypothesis.HealthCheck.large_base_example,
576 ]
577 )
578 @hypothesis.given(
579 chunks=strategies.lists(
580 strategies.sampled_from(random_input_data()),
581 min_size=2,
582 max_size=10,
583 ),
584 level=strategies.integers(min_value=1, max_value=5),
585 write_size=strategies.integers(
586 min_value=1,
587 max_value=4 * zstd.DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE,
588 ),
589 read_sizes=strategies.data(),
590 )
591 def test_read_across_frames_true(
592 self, chunks, level, write_size, read_sizes
593 ):
594 cctx = zstd.ZstdCompressor(level=level)
595
596 source = io.BytesIO()
597 source_chunks = []
598 compressed = io.BytesIO()
599
600 for chunk in chunks:
601 source.write(chunk)
602 source_chunks.append(chunk)
603 compressed.write(cctx.compress(chunk))
604
605 compressed.seek(0)
606
607 dctx = zstd.ZstdDecompressor()
608 dobj = dctx.decompressobj(
609 write_size=write_size, read_across_frames=True
610 )
611
612 decompressed = io.BytesIO()
613
614 while True:
615 read_size = read_sizes.draw(strategies.integers(1, 4096))
616 chunk = compressed.read(read_size)
617 if not chunk:
618 break
619
620 decompressed.write(dobj.decompress(chunk))
621
622 self.assertEqual(decompressed.getvalue(), source.getvalue())
623
624
625 @unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set")
626 class TestDecompressor_read_to_iter_fuzzing(unittest.TestCase):
627 @hypothesis.given(
628 original=strategies.sampled_from(random_input_data()),
629 level=strategies.integers(min_value=1, max_value=5),
630 read_size=strategies.integers(min_value=1, max_value=4096),
631 write_size=strategies.integers(min_value=1, max_value=4096),
632 )
633 def test_read_write_size_variance(
634 self, original, level, read_size, write_size
635 ):
636 cctx = zstd.ZstdCompressor(level=level)
637 frame = cctx.compress(original)
638
639 source = io.BytesIO(frame)
640
641 dctx = zstd.ZstdDecompressor()
642 chunks = list(
643 dctx.read_to_iter(
644 source, read_size=read_size, write_size=write_size
645 )
646 )
647
648 self.assertEqual(b"".join(chunks), original)
649
650
651 @unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set")
652 @unittest.skipUnless(
653 "multi_decompress_to_buffer" in zstd.backend_features,
654 "multi_decompress_to_buffer not available",
655 )
656 class TestDecompressor_multi_decompress_to_buffer_fuzzing(unittest.TestCase):
657 @hypothesis.given(
658 original=strategies.lists(
659 strategies.sampled_from(random_input_data()),
660 min_size=1,
661 max_size=1024,
662 ),
663 threads=strategies.integers(min_value=1, max_value=8),
664 use_dict=strategies.booleans(),
665 )
666 def test_data_equivalence(self, original, threads, use_dict):
667 kwargs = {}
668 if use_dict:
669 kwargs["dict_data"] = zstd.ZstdCompressionDict(original[0])
670
671 cctx = zstd.ZstdCompressor(
672 level=1, write_content_size=True, write_checksum=True, **kwargs
673 )
674
675 frames_buffer = cctx.multi_compress_to_buffer(original, threads=-1)
676
677 dctx = zstd.ZstdDecompressor(**kwargs)
678 result = dctx.multi_decompress_to_buffer(frames_buffer)
679
680 self.assertEqual(len(result), len(original))
681 for i, frame in enumerate(result):
682 self.assertEqual(frame.tobytes(), original[i])
683
684 frames_list = [f.tobytes() for f in frames_buffer]
685 result = dctx.multi_decompress_to_buffer(frames_list)
686
687 self.assertEqual(len(result), len(original))
688 for i, frame in enumerate(result):
689 self.assertEqual(frame.tobytes(), original[i])