| 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 TestCompressor_stream_reader_fuzzing(unittest.TestCase): |
| 18 |
@hypothesis.settings( |
| 19 |
suppress_health_check=[hypothesis.HealthCheck.large_base_example] |
| 20 |
) |
| 21 |
@hypothesis.given( |
| 22 |
original=strategies.sampled_from(random_input_data()), |
| 23 |
level=strategies.integers(min_value=1, max_value=5), |
| 24 |
source_read_size=strategies.integers(1, 16384), |
| 25 |
read_size=strategies.integers( |
| 26 |
-1, zstd.COMPRESSION_RECOMMENDED_OUTPUT_SIZE |
| 27 |
), |
| 28 |
) |
| 29 |
def test_stream_source_read( |
| 30 |
self, original, level, source_read_size, read_size |
| 31 |
): |
| 32 |
if read_size == 0: |
| 33 |
read_size = -1 |
| 34 |
|
| 35 |
refctx = zstd.ZstdCompressor(level=level) |
| 36 |
ref_frame = refctx.compress(original) |
| 37 |
|
| 38 |
cctx = zstd.ZstdCompressor(level=level) |
| 39 |
with cctx.stream_reader( |
| 40 |
io.BytesIO(original), size=len(original), read_size=source_read_size |
| 41 |
) as reader: |
| 42 |
chunks = [] |
| 43 |
while True: |
| 44 |
chunk = reader.read(read_size) |
| 45 |
if not chunk: |
| 46 |
break |
| 47 |
|
| 48 |
chunks.append(chunk) |
| 49 |
|
| 50 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 51 |
|
| 52 |
@hypothesis.settings( |
| 53 |
suppress_health_check=[hypothesis.HealthCheck.large_base_example] |
| 54 |
) |
| 55 |
@hypothesis.given( |
| 56 |
original=strategies.sampled_from(random_input_data()), |
| 57 |
level=strategies.integers(min_value=1, max_value=5), |
| 58 |
source_read_size=strategies.integers(1, 16384), |
| 59 |
read_size=strategies.integers( |
| 60 |
-1, zstd.COMPRESSION_RECOMMENDED_OUTPUT_SIZE |
| 61 |
), |
| 62 |
) |
| 63 |
def test_buffer_source_read( |
| 64 |
self, original, level, source_read_size, read_size |
| 65 |
): |
| 66 |
if read_size == 0: |
| 67 |
read_size = -1 |
| 68 |
|
| 69 |
refctx = zstd.ZstdCompressor(level=level) |
| 70 |
ref_frame = refctx.compress(original) |
| 71 |
|
| 72 |
cctx = zstd.ZstdCompressor(level=level) |
| 73 |
with cctx.stream_reader( |
| 74 |
original, size=len(original), read_size=source_read_size |
| 75 |
) as reader: |
| 76 |
chunks = [] |
| 77 |
while True: |
| 78 |
chunk = reader.read(read_size) |
| 79 |
if not chunk: |
| 80 |
break |
| 81 |
|
| 82 |
chunks.append(chunk) |
| 83 |
|
| 84 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 85 |
|
| 86 |
@hypothesis.settings( |
| 87 |
suppress_health_check=[ |
| 88 |
hypothesis.HealthCheck.large_base_example, |
| 89 |
hypothesis.HealthCheck.too_slow, |
| 90 |
] |
| 91 |
) |
| 92 |
@hypothesis.given( |
| 93 |
original=strategies.sampled_from(random_input_data()), |
| 94 |
level=strategies.integers(min_value=1, max_value=5), |
| 95 |
source_read_size=strategies.integers(1, 16384), |
| 96 |
read_sizes=strategies.data(), |
| 97 |
) |
| 98 |
def test_stream_source_read_variance( |
| 99 |
self, original, level, source_read_size, read_sizes |
| 100 |
): |
| 101 |
refctx = zstd.ZstdCompressor(level=level) |
| 102 |
ref_frame = refctx.compress(original) |
| 103 |
|
| 104 |
cctx = zstd.ZstdCompressor(level=level) |
| 105 |
with cctx.stream_reader( |
| 106 |
io.BytesIO(original), size=len(original), read_size=source_read_size |
| 107 |
) as reader: |
| 108 |
chunks = [] |
| 109 |
while True: |
| 110 |
read_size = read_sizes.draw(strategies.integers(-1, 16384)) |
| 111 |
chunk = reader.read(read_size) |
| 112 |
if not chunk and read_size: |
| 113 |
break |
| 114 |
|
| 115 |
chunks.append(chunk) |
| 116 |
|
| 117 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 118 |
|
| 119 |
@hypothesis.settings( |
| 120 |
suppress_health_check=[ |
| 121 |
hypothesis.HealthCheck.large_base_example, |
| 122 |
hypothesis.HealthCheck.too_slow, |
| 123 |
] |
| 124 |
) |
| 125 |
@hypothesis.given( |
| 126 |
original=strategies.sampled_from(random_input_data()), |
| 127 |
level=strategies.integers(min_value=1, max_value=5), |
| 128 |
source_read_size=strategies.integers(1, 16384), |
| 129 |
read_sizes=strategies.data(), |
| 130 |
) |
| 131 |
def test_buffer_source_read_variance( |
| 132 |
self, original, level, source_read_size, read_sizes |
| 133 |
): |
| 134 |
refctx = zstd.ZstdCompressor(level=level) |
| 135 |
ref_frame = refctx.compress(original) |
| 136 |
|
| 137 |
cctx = zstd.ZstdCompressor(level=level) |
| 138 |
with cctx.stream_reader( |
| 139 |
original, size=len(original), read_size=source_read_size |
| 140 |
) as reader: |
| 141 |
chunks = [] |
| 142 |
while True: |
| 143 |
read_size = read_sizes.draw(strategies.integers(-1, 16384)) |
| 144 |
chunk = reader.read(read_size) |
| 145 |
if not chunk and read_size: |
| 146 |
break |
| 147 |
|
| 148 |
chunks.append(chunk) |
| 149 |
|
| 150 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 151 |
|
| 152 |
@hypothesis.settings( |
| 153 |
suppress_health_check=[hypothesis.HealthCheck.large_base_example] |
| 154 |
) |
| 155 |
@hypothesis.given( |
| 156 |
original=strategies.sampled_from(random_input_data()), |
| 157 |
level=strategies.integers(min_value=1, max_value=5), |
| 158 |
source_read_size=strategies.integers(1, 16384), |
| 159 |
read_size=strategies.integers( |
| 160 |
1, zstd.COMPRESSION_RECOMMENDED_OUTPUT_SIZE |
| 161 |
), |
| 162 |
) |
| 163 |
def test_stream_source_readinto( |
| 164 |
self, original, level, source_read_size, read_size |
| 165 |
): |
| 166 |
refctx = zstd.ZstdCompressor(level=level) |
| 167 |
ref_frame = refctx.compress(original) |
| 168 |
|
| 169 |
cctx = zstd.ZstdCompressor(level=level) |
| 170 |
with cctx.stream_reader( |
| 171 |
io.BytesIO(original), size=len(original), read_size=source_read_size |
| 172 |
) as reader: |
| 173 |
chunks = [] |
| 174 |
while True: |
| 175 |
b = bytearray(read_size) |
| 176 |
count = reader.readinto(b) |
| 177 |
|
| 178 |
if not count: |
| 179 |
break |
| 180 |
|
| 181 |
chunks.append(bytes(b[0:count])) |
| 182 |
|
| 183 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 184 |
|
| 185 |
@hypothesis.settings( |
| 186 |
suppress_health_check=[hypothesis.HealthCheck.large_base_example] |
| 187 |
) |
| 188 |
@hypothesis.given( |
| 189 |
original=strategies.sampled_from(random_input_data()), |
| 190 |
level=strategies.integers(min_value=1, max_value=5), |
| 191 |
source_read_size=strategies.integers(1, 16384), |
| 192 |
read_size=strategies.integers( |
| 193 |
1, zstd.COMPRESSION_RECOMMENDED_OUTPUT_SIZE |
| 194 |
), |
| 195 |
) |
| 196 |
def test_buffer_source_readinto( |
| 197 |
self, original, level, source_read_size, read_size |
| 198 |
): |
| 199 |
refctx = zstd.ZstdCompressor(level=level) |
| 200 |
ref_frame = refctx.compress(original) |
| 201 |
|
| 202 |
cctx = zstd.ZstdCompressor(level=level) |
| 203 |
with cctx.stream_reader( |
| 204 |
original, size=len(original), read_size=source_read_size |
| 205 |
) as reader: |
| 206 |
chunks = [] |
| 207 |
while True: |
| 208 |
b = bytearray(read_size) |
| 209 |
count = reader.readinto(b) |
| 210 |
|
| 211 |
if not count: |
| 212 |
break |
| 213 |
|
| 214 |
chunks.append(bytes(b[0:count])) |
| 215 |
|
| 216 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 217 |
|
| 218 |
@hypothesis.settings( |
| 219 |
suppress_health_check=[ |
| 220 |
hypothesis.HealthCheck.large_base_example, |
| 221 |
hypothesis.HealthCheck.too_slow, |
| 222 |
] |
| 223 |
) |
| 224 |
@hypothesis.given( |
| 225 |
original=strategies.sampled_from(random_input_data()), |
| 226 |
level=strategies.integers(min_value=1, max_value=5), |
| 227 |
source_read_size=strategies.integers(1, 16384), |
| 228 |
read_sizes=strategies.data(), |
| 229 |
) |
| 230 |
def test_stream_source_readinto_variance( |
| 231 |
self, original, level, source_read_size, read_sizes |
| 232 |
): |
| 233 |
refctx = zstd.ZstdCompressor(level=level) |
| 234 |
ref_frame = refctx.compress(original) |
| 235 |
|
| 236 |
cctx = zstd.ZstdCompressor(level=level) |
| 237 |
with cctx.stream_reader( |
| 238 |
io.BytesIO(original), size=len(original), read_size=source_read_size |
| 239 |
) as reader: |
| 240 |
chunks = [] |
| 241 |
while True: |
| 242 |
read_size = read_sizes.draw(strategies.integers(1, 16384)) |
| 243 |
b = bytearray(read_size) |
| 244 |
count = reader.readinto(b) |
| 245 |
|
| 246 |
if not count: |
| 247 |
break |
| 248 |
|
| 249 |
chunks.append(bytes(b[0:count])) |
| 250 |
|
| 251 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 252 |
|
| 253 |
@hypothesis.settings( |
| 254 |
suppress_health_check=[ |
| 255 |
hypothesis.HealthCheck.large_base_example, |
| 256 |
hypothesis.HealthCheck.too_slow, |
| 257 |
] |
| 258 |
) |
| 259 |
@hypothesis.given( |
| 260 |
original=strategies.sampled_from(random_input_data()), |
| 261 |
level=strategies.integers(min_value=1, max_value=5), |
| 262 |
source_read_size=strategies.integers(1, 16384), |
| 263 |
read_sizes=strategies.data(), |
| 264 |
) |
| 265 |
def test_buffer_source_readinto_variance( |
| 266 |
self, original, level, source_read_size, read_sizes |
| 267 |
): |
| 268 |
refctx = zstd.ZstdCompressor(level=level) |
| 269 |
ref_frame = refctx.compress(original) |
| 270 |
|
| 271 |
cctx = zstd.ZstdCompressor(level=level) |
| 272 |
with cctx.stream_reader( |
| 273 |
original, size=len(original), read_size=source_read_size |
| 274 |
) as reader: |
| 275 |
chunks = [] |
| 276 |
while True: |
| 277 |
read_size = read_sizes.draw(strategies.integers(1, 16384)) |
| 278 |
b = bytearray(read_size) |
| 279 |
count = reader.readinto(b) |
| 280 |
|
| 281 |
if not count: |
| 282 |
break |
| 283 |
|
| 284 |
chunks.append(bytes(b[0:count])) |
| 285 |
|
| 286 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 287 |
|
| 288 |
@hypothesis.settings( |
| 289 |
suppress_health_check=[hypothesis.HealthCheck.large_base_example] |
| 290 |
) |
| 291 |
@hypothesis.given( |
| 292 |
original=strategies.sampled_from(random_input_data()), |
| 293 |
level=strategies.integers(min_value=1, max_value=5), |
| 294 |
source_read_size=strategies.integers(1, 16384), |
| 295 |
read_size=strategies.integers( |
| 296 |
-1, zstd.COMPRESSION_RECOMMENDED_OUTPUT_SIZE |
| 297 |
), |
| 298 |
) |
| 299 |
def test_stream_source_read1( |
| 300 |
self, original, level, source_read_size, read_size |
| 301 |
): |
| 302 |
if read_size == 0: |
| 303 |
read_size = -1 |
| 304 |
|
| 305 |
refctx = zstd.ZstdCompressor(level=level) |
| 306 |
ref_frame = refctx.compress(original) |
| 307 |
|
| 308 |
cctx = zstd.ZstdCompressor(level=level) |
| 309 |
with cctx.stream_reader( |
| 310 |
io.BytesIO(original), size=len(original), read_size=source_read_size |
| 311 |
) as reader: |
| 312 |
chunks = [] |
| 313 |
while True: |
| 314 |
chunk = reader.read1(read_size) |
| 315 |
if not chunk: |
| 316 |
break |
| 317 |
|
| 318 |
chunks.append(chunk) |
| 319 |
|
| 320 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 321 |
|
| 322 |
@hypothesis.settings( |
| 323 |
suppress_health_check=[hypothesis.HealthCheck.large_base_example] |
| 324 |
) |
| 325 |
@hypothesis.given( |
| 326 |
original=strategies.sampled_from(random_input_data()), |
| 327 |
level=strategies.integers(min_value=1, max_value=5), |
| 328 |
source_read_size=strategies.integers(1, 16384), |
| 329 |
read_size=strategies.integers( |
| 330 |
-1, zstd.COMPRESSION_RECOMMENDED_OUTPUT_SIZE |
| 331 |
), |
| 332 |
) |
| 333 |
def test_buffer_source_read1( |
| 334 |
self, original, level, source_read_size, read_size |
| 335 |
): |
| 336 |
if read_size == 0: |
| 337 |
read_size = -1 |
| 338 |
|
| 339 |
refctx = zstd.ZstdCompressor(level=level) |
| 340 |
ref_frame = refctx.compress(original) |
| 341 |
|
| 342 |
cctx = zstd.ZstdCompressor(level=level) |
| 343 |
with cctx.stream_reader( |
| 344 |
original, size=len(original), read_size=source_read_size |
| 345 |
) as reader: |
| 346 |
chunks = [] |
| 347 |
while True: |
| 348 |
chunk = reader.read1(read_size) |
| 349 |
if not chunk: |
| 350 |
break |
| 351 |
|
| 352 |
chunks.append(chunk) |
| 353 |
|
| 354 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 355 |
|
| 356 |
@hypothesis.settings( |
| 357 |
suppress_health_check=[ |
| 358 |
hypothesis.HealthCheck.large_base_example, |
| 359 |
hypothesis.HealthCheck.too_slow, |
| 360 |
] |
| 361 |
) |
| 362 |
@hypothesis.given( |
| 363 |
original=strategies.sampled_from(random_input_data()), |
| 364 |
level=strategies.integers(min_value=1, max_value=5), |
| 365 |
source_read_size=strategies.integers(1, 16384), |
| 366 |
read_sizes=strategies.data(), |
| 367 |
) |
| 368 |
def test_stream_source_read1_variance( |
| 369 |
self, original, level, source_read_size, read_sizes |
| 370 |
): |
| 371 |
refctx = zstd.ZstdCompressor(level=level) |
| 372 |
ref_frame = refctx.compress(original) |
| 373 |
|
| 374 |
cctx = zstd.ZstdCompressor(level=level) |
| 375 |
with cctx.stream_reader( |
| 376 |
io.BytesIO(original), size=len(original), read_size=source_read_size |
| 377 |
) as reader: |
| 378 |
chunks = [] |
| 379 |
while True: |
| 380 |
read_size = read_sizes.draw(strategies.integers(-1, 16384)) |
| 381 |
chunk = reader.read1(read_size) |
| 382 |
if not chunk and read_size: |
| 383 |
break |
| 384 |
|
| 385 |
chunks.append(chunk) |
| 386 |
|
| 387 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 388 |
|
| 389 |
@hypothesis.settings( |
| 390 |
suppress_health_check=[ |
| 391 |
hypothesis.HealthCheck.large_base_example, |
| 392 |
hypothesis.HealthCheck.too_slow, |
| 393 |
] |
| 394 |
) |
| 395 |
@hypothesis.given( |
| 396 |
original=strategies.sampled_from(random_input_data()), |
| 397 |
level=strategies.integers(min_value=1, max_value=5), |
| 398 |
source_read_size=strategies.integers(1, 16384), |
| 399 |
read_sizes=strategies.data(), |
| 400 |
) |
| 401 |
def test_buffer_source_read1_variance( |
| 402 |
self, original, level, source_read_size, read_sizes |
| 403 |
): |
| 404 |
refctx = zstd.ZstdCompressor(level=level) |
| 405 |
ref_frame = refctx.compress(original) |
| 406 |
|
| 407 |
cctx = zstd.ZstdCompressor(level=level) |
| 408 |
with cctx.stream_reader( |
| 409 |
original, size=len(original), read_size=source_read_size |
| 410 |
) as reader: |
| 411 |
chunks = [] |
| 412 |
while True: |
| 413 |
read_size = read_sizes.draw(strategies.integers(-1, 16384)) |
| 414 |
chunk = reader.read1(read_size) |
| 415 |
if not chunk and read_size: |
| 416 |
break |
| 417 |
|
| 418 |
chunks.append(chunk) |
| 419 |
|
| 420 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 421 |
|
| 422 |
@hypothesis.settings( |
| 423 |
suppress_health_check=[hypothesis.HealthCheck.large_base_example] |
| 424 |
) |
| 425 |
@hypothesis.given( |
| 426 |
original=strategies.sampled_from(random_input_data()), |
| 427 |
level=strategies.integers(min_value=1, max_value=5), |
| 428 |
source_read_size=strategies.integers(1, 16384), |
| 429 |
read_size=strategies.integers( |
| 430 |
1, zstd.COMPRESSION_RECOMMENDED_OUTPUT_SIZE |
| 431 |
), |
| 432 |
) |
| 433 |
def test_stream_source_readinto1( |
| 434 |
self, original, level, source_read_size, read_size |
| 435 |
): |
| 436 |
if read_size == 0: |
| 437 |
read_size = -1 |
| 438 |
|
| 439 |
refctx = zstd.ZstdCompressor(level=level) |
| 440 |
ref_frame = refctx.compress(original) |
| 441 |
|
| 442 |
cctx = zstd.ZstdCompressor(level=level) |
| 443 |
with cctx.stream_reader( |
| 444 |
io.BytesIO(original), size=len(original), read_size=source_read_size |
| 445 |
) as reader: |
| 446 |
chunks = [] |
| 447 |
while True: |
| 448 |
b = bytearray(read_size) |
| 449 |
count = reader.readinto1(b) |
| 450 |
|
| 451 |
if not count: |
| 452 |
break |
| 453 |
|
| 454 |
chunks.append(bytes(b[0:count])) |
| 455 |
|
| 456 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 457 |
|
| 458 |
@hypothesis.settings( |
| 459 |
suppress_health_check=[hypothesis.HealthCheck.large_base_example] |
| 460 |
) |
| 461 |
@hypothesis.given( |
| 462 |
original=strategies.sampled_from(random_input_data()), |
| 463 |
level=strategies.integers(min_value=1, max_value=5), |
| 464 |
source_read_size=strategies.integers(1, 16384), |
| 465 |
read_size=strategies.integers( |
| 466 |
1, zstd.COMPRESSION_RECOMMENDED_OUTPUT_SIZE |
| 467 |
), |
| 468 |
) |
| 469 |
def test_buffer_source_readinto1( |
| 470 |
self, original, level, source_read_size, read_size |
| 471 |
): |
| 472 |
if read_size == 0: |
| 473 |
read_size = -1 |
| 474 |
|
| 475 |
refctx = zstd.ZstdCompressor(level=level) |
| 476 |
ref_frame = refctx.compress(original) |
| 477 |
|
| 478 |
cctx = zstd.ZstdCompressor(level=level) |
| 479 |
with cctx.stream_reader( |
| 480 |
original, size=len(original), read_size=source_read_size |
| 481 |
) as reader: |
| 482 |
chunks = [] |
| 483 |
while True: |
| 484 |
b = bytearray(read_size) |
| 485 |
count = reader.readinto1(b) |
| 486 |
|
| 487 |
if not count: |
| 488 |
break |
| 489 |
|
| 490 |
chunks.append(bytes(b[0:count])) |
| 491 |
|
| 492 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 493 |
|
| 494 |
@hypothesis.settings( |
| 495 |
suppress_health_check=[ |
| 496 |
hypothesis.HealthCheck.large_base_example, |
| 497 |
hypothesis.HealthCheck.too_slow, |
| 498 |
] |
| 499 |
) |
| 500 |
@hypothesis.given( |
| 501 |
original=strategies.sampled_from(random_input_data()), |
| 502 |
level=strategies.integers(min_value=1, max_value=5), |
| 503 |
source_read_size=strategies.integers(1, 16384), |
| 504 |
read_sizes=strategies.data(), |
| 505 |
) |
| 506 |
def test_stream_source_readinto1_variance( |
| 507 |
self, original, level, source_read_size, read_sizes |
| 508 |
): |
| 509 |
refctx = zstd.ZstdCompressor(level=level) |
| 510 |
ref_frame = refctx.compress(original) |
| 511 |
|
| 512 |
cctx = zstd.ZstdCompressor(level=level) |
| 513 |
with cctx.stream_reader( |
| 514 |
io.BytesIO(original), size=len(original), read_size=source_read_size |
| 515 |
) as reader: |
| 516 |
chunks = [] |
| 517 |
while True: |
| 518 |
read_size = read_sizes.draw(strategies.integers(1, 16384)) |
| 519 |
b = bytearray(read_size) |
| 520 |
count = reader.readinto1(b) |
| 521 |
|
| 522 |
if not count: |
| 523 |
break |
| 524 |
|
| 525 |
chunks.append(bytes(b[0:count])) |
| 526 |
|
| 527 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 528 |
|
| 529 |
@hypothesis.settings( |
| 530 |
suppress_health_check=[ |
| 531 |
hypothesis.HealthCheck.large_base_example, |
| 532 |
hypothesis.HealthCheck.too_slow, |
| 533 |
] |
| 534 |
) |
| 535 |
@hypothesis.given( |
| 536 |
original=strategies.sampled_from(random_input_data()), |
| 537 |
level=strategies.integers(min_value=1, max_value=5), |
| 538 |
source_read_size=strategies.integers(1, 16384), |
| 539 |
read_sizes=strategies.data(), |
| 540 |
) |
| 541 |
def test_buffer_source_readinto1_variance( |
| 542 |
self, original, level, source_read_size, read_sizes |
| 543 |
): |
| 544 |
refctx = zstd.ZstdCompressor(level=level) |
| 545 |
ref_frame = refctx.compress(original) |
| 546 |
|
| 547 |
cctx = zstd.ZstdCompressor(level=level) |
| 548 |
with cctx.stream_reader( |
| 549 |
original, size=len(original), read_size=source_read_size |
| 550 |
) as reader: |
| 551 |
chunks = [] |
| 552 |
while True: |
| 553 |
read_size = read_sizes.draw(strategies.integers(1, 16384)) |
| 554 |
b = bytearray(read_size) |
| 555 |
count = reader.readinto1(b) |
| 556 |
|
| 557 |
if not count: |
| 558 |
break |
| 559 |
|
| 560 |
chunks.append(bytes(b[0:count])) |
| 561 |
|
| 562 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 563 |
|
| 564 |
|
| 565 |
@unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set") |
| 566 |
class TestCompressor_stream_writer_fuzzing(unittest.TestCase): |
| 567 |
@hypothesis.given( |
| 568 |
original=strategies.sampled_from(random_input_data()), |
| 569 |
level=strategies.integers(min_value=1, max_value=5), |
| 570 |
write_size=strategies.integers(min_value=1, max_value=1048576), |
| 571 |
) |
| 572 |
def test_write_size_variance(self, original, level, write_size): |
| 573 |
refctx = zstd.ZstdCompressor(level=level) |
| 574 |
ref_frame = refctx.compress(original) |
| 575 |
|
| 576 |
cctx = zstd.ZstdCompressor(level=level) |
| 577 |
b = io.BytesIO() |
| 578 |
with cctx.stream_writer( |
| 579 |
b, size=len(original), write_size=write_size, closefd=False |
| 580 |
) as compressor: |
| 581 |
compressor.write(original) |
| 582 |
|
| 583 |
self.assertEqual(b.getvalue(), ref_frame) |
| 584 |
|
| 585 |
|
| 586 |
@unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set") |
| 587 |
class TestCompressor_copy_stream_fuzzing(unittest.TestCase): |
| 588 |
@hypothesis.given( |
| 589 |
original=strategies.sampled_from(random_input_data()), |
| 590 |
level=strategies.integers(min_value=1, max_value=5), |
| 591 |
read_size=strategies.integers(min_value=1, max_value=1048576), |
| 592 |
write_size=strategies.integers(min_value=1, max_value=1048576), |
| 593 |
) |
| 594 |
def test_read_write_size_variance( |
| 595 |
self, original, level, read_size, write_size |
| 596 |
): |
| 597 |
refctx = zstd.ZstdCompressor(level=level) |
| 598 |
ref_frame = refctx.compress(original) |
| 599 |
|
| 600 |
cctx = zstd.ZstdCompressor(level=level) |
| 601 |
source = io.BytesIO(original) |
| 602 |
dest = io.BytesIO() |
| 603 |
|
| 604 |
cctx.copy_stream( |
| 605 |
source, |
| 606 |
dest, |
| 607 |
size=len(original), |
| 608 |
read_size=read_size, |
| 609 |
write_size=write_size, |
| 610 |
) |
| 611 |
|
| 612 |
self.assertEqual(dest.getvalue(), ref_frame) |
| 613 |
|
| 614 |
|
| 615 |
@unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set") |
| 616 |
class TestCompressor_compressobj_fuzzing(unittest.TestCase): |
| 617 |
@hypothesis.settings( |
| 618 |
suppress_health_check=[ |
| 619 |
hypothesis.HealthCheck.large_base_example, |
| 620 |
hypothesis.HealthCheck.too_slow, |
| 621 |
] |
| 622 |
) |
| 623 |
@hypothesis.given( |
| 624 |
original=strategies.sampled_from(random_input_data()), |
| 625 |
level=strategies.integers(min_value=1, max_value=5), |
| 626 |
chunk_sizes=strategies.data(), |
| 627 |
) |
| 628 |
def test_random_input_sizes(self, original, level, chunk_sizes): |
| 629 |
refctx = zstd.ZstdCompressor(level=level) |
| 630 |
ref_frame = refctx.compress(original) |
| 631 |
|
| 632 |
cctx = zstd.ZstdCompressor(level=level) |
| 633 |
cobj = cctx.compressobj(size=len(original)) |
| 634 |
|
| 635 |
chunks = [] |
| 636 |
i = 0 |
| 637 |
while True: |
| 638 |
chunk_size = chunk_sizes.draw(strategies.integers(1, 4096)) |
| 639 |
source = original[i : i + chunk_size] |
| 640 |
if not source: |
| 641 |
break |
| 642 |
|
| 643 |
chunks.append(cobj.compress(source)) |
| 644 |
i += chunk_size |
| 645 |
|
| 646 |
chunks.append(cobj.flush()) |
| 647 |
|
| 648 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 649 |
|
| 650 |
@hypothesis.settings( |
| 651 |
suppress_health_check=[ |
| 652 |
hypothesis.HealthCheck.large_base_example, |
| 653 |
hypothesis.HealthCheck.too_slow, |
| 654 |
] |
| 655 |
) |
| 656 |
@hypothesis.given( |
| 657 |
original=strategies.sampled_from(random_input_data()), |
| 658 |
level=strategies.integers(min_value=1, max_value=5), |
| 659 |
chunk_sizes=strategies.data(), |
| 660 |
flushes=strategies.data(), |
| 661 |
) |
| 662 |
def test_flush_block(self, original, level, chunk_sizes, flushes): |
| 663 |
cctx = zstd.ZstdCompressor(level=level) |
| 664 |
cobj = cctx.compressobj() |
| 665 |
|
| 666 |
dctx = zstd.ZstdDecompressor() |
| 667 |
dobj = dctx.decompressobj() |
| 668 |
|
| 669 |
compressed_chunks = [] |
| 670 |
decompressed_chunks = [] |
| 671 |
i = 0 |
| 672 |
while True: |
| 673 |
input_size = chunk_sizes.draw(strategies.integers(1, 4096)) |
| 674 |
source = original[i : i + input_size] |
| 675 |
if not source: |
| 676 |
break |
| 677 |
|
| 678 |
i += input_size |
| 679 |
|
| 680 |
chunk = cobj.compress(source) |
| 681 |
compressed_chunks.append(chunk) |
| 682 |
decompressed_chunks.append(dobj.decompress(chunk)) |
| 683 |
|
| 684 |
if not flushes.draw(strategies.booleans()): |
| 685 |
continue |
| 686 |
|
| 687 |
chunk = cobj.flush(zstd.COMPRESSOBJ_FLUSH_BLOCK) |
| 688 |
compressed_chunks.append(chunk) |
| 689 |
decompressed_chunks.append(dobj.decompress(chunk)) |
| 690 |
|
| 691 |
self.assertEqual(b"".join(decompressed_chunks), original[0:i]) |
| 692 |
|
| 693 |
chunk = cobj.flush(zstd.COMPRESSOBJ_FLUSH_FINISH) |
| 694 |
compressed_chunks.append(chunk) |
| 695 |
decompressed_chunks.append(dobj.decompress(chunk)) |
| 696 |
|
| 697 |
self.assertEqual( |
| 698 |
dctx.decompress( |
| 699 |
b"".join(compressed_chunks), max_output_size=len(original) |
| 700 |
), |
| 701 |
original, |
| 702 |
) |
| 703 |
self.assertEqual(b"".join(decompressed_chunks), original) |
| 704 |
|
| 705 |
|
| 706 |
@unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set") |
| 707 |
class TestCompressor_read_to_iter_fuzzing(unittest.TestCase): |
| 708 |
@hypothesis.given( |
| 709 |
original=strategies.sampled_from(random_input_data()), |
| 710 |
level=strategies.integers(min_value=1, max_value=5), |
| 711 |
read_size=strategies.integers(min_value=1, max_value=4096), |
| 712 |
write_size=strategies.integers(min_value=1, max_value=4096), |
| 713 |
) |
| 714 |
def test_read_write_size_variance( |
| 715 |
self, original, level, read_size, write_size |
| 716 |
): |
| 717 |
refcctx = zstd.ZstdCompressor(level=level) |
| 718 |
ref_frame = refcctx.compress(original) |
| 719 |
|
| 720 |
source = io.BytesIO(original) |
| 721 |
|
| 722 |
cctx = zstd.ZstdCompressor(level=level) |
| 723 |
chunks = list( |
| 724 |
cctx.read_to_iter( |
| 725 |
source, |
| 726 |
size=len(original), |
| 727 |
read_size=read_size, |
| 728 |
write_size=write_size, |
| 729 |
) |
| 730 |
) |
| 731 |
|
| 732 |
self.assertEqual(b"".join(chunks), ref_frame) |
| 733 |
|
| 734 |
|
| 735 |
@unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set") |
| 736 |
@unittest.skipUnless( |
| 737 |
"multi_compress_to_buffer" in zstd.backend_features, |
| 738 |
"multi_compress_to_buffer not available", |
| 739 |
) |
| 740 |
class TestCompressor_multi_compress_to_buffer_fuzzing(unittest.TestCase): |
| 741 |
@hypothesis.given( |
| 742 |
original=strategies.lists( |
| 743 |
strategies.sampled_from(random_input_data()), |
| 744 |
min_size=1, |
| 745 |
max_size=1024, |
| 746 |
), |
| 747 |
threads=strategies.integers(min_value=1, max_value=8), |
| 748 |
use_dict=strategies.booleans(), |
| 749 |
) |
| 750 |
def test_data_equivalence(self, original, threads, use_dict): |
| 751 |
kwargs = {} |
| 752 |
|
| 753 |
# Use a content dictionary because it is cheap to create. |
| 754 |
if use_dict: |
| 755 |
kwargs["dict_data"] = zstd.ZstdCompressionDict(original[0]) |
| 756 |
|
| 757 |
cctx = zstd.ZstdCompressor(level=1, write_checksum=True, **kwargs) |
| 758 |
|
| 759 |
result = cctx.multi_compress_to_buffer(original, threads=-1) |
| 760 |
|
| 761 |
self.assertEqual(len(result), len(original)) |
| 762 |
|
| 763 |
# The frame produced via the batch APIs may not be bit identical to that |
| 764 |
# produced by compress() because compression parameters are adjusted |
| 765 |
# from the first input in batch mode. So the only thing we can do is |
| 766 |
# verify the decompressed data matches the input. |
| 767 |
dctx = zstd.ZstdDecompressor(**kwargs) |
| 768 |
|
| 769 |
for i, frame in enumerate(result): |
| 770 |
self.assertEqual(dctx.decompress(frame), original[i]) |
| 771 |
|
| 772 |
|
| 773 |
@unittest.skipUnless("ZSTD_SLOW_TESTS" in os.environ, "ZSTD_SLOW_TESTS not set") |
| 774 |
class TestCompressor_chunker_fuzzing(unittest.TestCase): |
| 775 |
@hypothesis.settings( |
| 776 |
suppress_health_check=[ |
| 777 |
hypothesis.HealthCheck.data_too_large, |
| 778 |
hypothesis.HealthCheck.large_base_example, |
| 779 |
hypothesis.HealthCheck.too_slow, |
| 780 |
] |
| 781 |
) |
| 782 |
@hypothesis.given( |
| 783 |
original=strategies.sampled_from(random_input_data()), |
| 784 |
level=strategies.integers(min_value=1, max_value=5), |
| 785 |
chunk_size=strategies.integers(min_value=1, max_value=32 * 1048576), |
| 786 |
input_sizes=strategies.data(), |
| 787 |
) |
| 788 |
def test_random_input_sizes(self, original, level, chunk_size, input_sizes): |
| 789 |
cctx = zstd.ZstdCompressor(level=level) |
| 790 |
chunker = cctx.chunker(chunk_size=chunk_size) |
| 791 |
|
| 792 |
chunks = [] |
| 793 |
i = 0 |
| 794 |
while True: |
| 795 |
input_size = input_sizes.draw(strategies.integers(1, 4096)) |
| 796 |
source = original[i : i + input_size] |
| 797 |
if not source: |
| 798 |
break |
| 799 |
|
| 800 |
chunks.extend(chunker.compress(source)) |
| 801 |
i += input_size |
| 802 |
|
| 803 |
chunks.extend(chunker.finish()) |
| 804 |
|
| 805 |
dctx = zstd.ZstdDecompressor() |
| 806 |
|
| 807 |
self.assertEqual( |
| 808 |
dctx.decompress(b"".join(chunks), max_output_size=len(original)), |
| 809 |
original, |
| 810 |
) |
| 811 |
|
| 812 |
self.assertTrue(all(len(chunk) == chunk_size for chunk in chunks[:-1])) |
| 813 |
|
| 814 |
@hypothesis.settings( |
| 815 |
suppress_health_check=[ |
| 816 |
hypothesis.HealthCheck.large_base_example, |
| 817 |
hypothesis.HealthCheck.too_slow, |
| 818 |
] |
| 819 |
) |
| 820 |
@hypothesis.given( |
| 821 |
original=strategies.sampled_from(random_input_data()), |
| 822 |
level=strategies.integers(min_value=1, max_value=5), |
| 823 |
chunk_size=strategies.integers(min_value=1, max_value=32 * 1048576), |
| 824 |
input_sizes=strategies.data(), |
| 825 |
flushes=strategies.data(), |
| 826 |
) |
| 827 |
def test_flush_block( |
| 828 |
self, original, level, chunk_size, input_sizes, flushes |
| 829 |
): |
| 830 |
cctx = zstd.ZstdCompressor(level=level) |
| 831 |
chunker = cctx.chunker(chunk_size=chunk_size) |
| 832 |
|
| 833 |
dctx = zstd.ZstdDecompressor() |
| 834 |
dobj = dctx.decompressobj() |
| 835 |
|
| 836 |
compressed_chunks = [] |
| 837 |
decompressed_chunks = [] |
| 838 |
i = 0 |
| 839 |
while True: |
| 840 |
input_size = input_sizes.draw(strategies.integers(1, 4096)) |
| 841 |
source = original[i : i + input_size] |
| 842 |
if not source: |
| 843 |
break |
| 844 |
|
| 845 |
i += input_size |
| 846 |
|
| 847 |
chunks = list(chunker.compress(source)) |
| 848 |
compressed_chunks.extend(chunks) |
| 849 |
decompressed_chunks.append(dobj.decompress(b"".join(chunks))) |
| 850 |
|
| 851 |
if not flushes.draw(strategies.booleans()): |
| 852 |
continue |
| 853 |
|
| 854 |
chunks = list(chunker.flush()) |
| 855 |
compressed_chunks.extend(chunks) |
| 856 |
decompressed_chunks.append(dobj.decompress(b"".join(chunks))) |
| 857 |
|
| 858 |
self.assertEqual(b"".join(decompressed_chunks), original[0:i]) |
| 859 |
|
| 860 |
chunks = list(chunker.finish()) |
| 861 |
compressed_chunks.extend(chunks) |
| 862 |
decompressed_chunks.append(dobj.decompress(b"".join(chunks))) |
| 863 |
|
| 864 |
self.assertEqual( |
| 865 |
dctx.decompress( |
| 866 |
b"".join(compressed_chunks), max_output_size=len(original) |
| 867 |
), |
| 868 |
original, |
| 869 |
) |
| 870 |
self.assertEqual(b"".join(decompressed_chunks), original) |