| 1 | #!/usr/bin/env python3 |
| 2 | # Regression test for issue #951. |
| 3 | # |
| 4 | # When rsync is built against a system zlib (no bundled Z_INSERT_ONLY |
| 5 | # extension), send_deflated_token() falls back to Z_SYNC_FLUSH to add a |
| 6 | # matched block to the compressor history -- but Z_SYNC_FLUSH emits a flush |
| 7 | # block into a fixed-size obuf. A large incompressible matched block |
| 8 | # overflowed obuf and aborted the transfer with |
| 9 | # "deflate on token returned 0 (N bytes left)" at token.c |
| 10 | # The fix loops, discarding the (never-sent) output, until the input is |
| 11 | # consumed. A bundled-zlib build emits no output here, so this test passes |
| 12 | # on either build; it is RED only on a pre-fix system-zlib build. |
| 13 | # |
| 14 | # The matched-block insert path needs all of: --compress-choice=zlib (the |
| 15 | # only method that feeds matched blocks into the deflate history), a large |
| 16 | # --block-size so a single matched token exceeds obuf, incompressible |
| 17 | # (random) data, and a delta over a real connection (compression is skipped |
| 18 | # for purely local transfers). We assert the upload SUCCEEDS *and* the |
| 19 | # result matches the source, so the fix is verified correct, not merely |
| 20 | # non-crashing. |
| 21 | |
| 22 | import filecmp |
| 23 | import shutil |
| 24 | import subprocess |
| 25 | |
| 26 | from rsyncfns import ( |
| 27 | SCRATCHDIR, make_data_file, makepath, rmtree, rsync_argv, |
| 28 | start_test_daemon, test_fail, write_daemon_conf, |
| 29 | ) |
| 30 | |
| 31 | DAEMON_PORT = 12922 |
| 32 | SIZE = 8 * 1024 * 1024 # enough blocks to exercise many inserts |
| 33 | # 65535 (0xffff) is a single insert fragment larger than the deflate output |
| 34 | # buffer (AVAIL_OUT_SIZE(CHUNK_SIZE) ~= 32816). It exercises both failure |
| 35 | # modes of the pre-fix code: the obuf overflow abort, and -- once that is |
| 36 | # loop-drained -- pending insert output left in the stream that leaks into |
| 37 | # the next send. A block that splits into chunks ending in a tiny fragment |
| 38 | # (e.g. 131072 = 65535+65535+2) would hide the pending case. |
| 39 | BLOCK = 65535 |
| 40 | |
| 41 | moddir = SCRATCHDIR / 'zmod' |
| 42 | srcdir = SCRATCHDIR / 'zsrc' |
| 43 | rmtree(moddir) |
| 44 | rmtree(srcdir) |
| 45 | makepath(moddir) |
| 46 | makepath(srcdir) |
| 47 | |
| 48 | # Source is incompressible. The basis (already in the module) is the same |
| 49 | # data with a few bytes changed in one block, so every other 128KB block |
| 50 | # matches exactly and is sent as a token -> the deflate insert path. |
| 51 | make_data_file(srcdir / 'big.dat', SIZE) |
| 52 | shutil.copy(srcdir / 'big.dat', moddir / 'big.dat') |
| 53 | with open(srcdir / 'big.dat', 'r+b') as f: |
| 54 | f.seek(SIZE // 2 + 1000) |
| 55 | f.write(b'\x00' * 32) |
| 56 | |
| 57 | conf = write_daemon_conf([('zmod', {'path': str(moddir), 'read only': 'no'})]) |
| 58 | url = start_test_daemon(conf, DAEMON_PORT) + 'zmod/' |
| 59 | |
| 60 | # -I forces the delta even though the basis has the same size (otherwise the |
| 61 | # quick check skips the file and the matched-block insert path never runs). |
| 62 | proc = subprocess.run( |
| 63 | rsync_argv('-zI', '--compress-choice=zlib', '--no-whole-file', |
| 64 | f'--block-size={BLOCK}', str(srcdir / 'big.dat'), url), |
| 65 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 66 | if proc.returncode != 0: |
| 67 | print(proc.stdout) |
| 68 | test_fail(f"zlib delta upload failed (rc={proc.returncode}); " |
| 69 | "regression of #951 deflate-token overflow") |
| 70 | |
| 71 | if not filecmp.cmp(srcdir / 'big.dat', moddir / 'big.dat', shallow=False): |
| 72 | test_fail("uploaded file differs from source -- zlib delta corruption") |