From 878fc77c41d8aac721930dd2d8430c6e0ad45791 Mon Sep 17 00:00:00 2001 From: Deepak Jain Date: Sat, 21 Nov 2026 04:27:19 -0500 Subject: [PATCH 1/3] checksum: add SHA-3 (FIPS 202) via OpenSSL EVP Adds sha3-256, sha3-384 and sha3-512 to the set of algorithms that --checksum-choice accepts. Implementation is wholly through OpenSSL's EVP interface, so it only activates when rsync is built with --enable-openssl and the linked libcrypto exposes the SHA-3 family (OpenSSL >= 1.1.1). The existing verify_digest() probe automatically demotes any entry the local libcrypto does not understand to CSUM_gone, so listing the algorithms in valid_checksums_items[] is safe on older builds. SHA-3 is never selected by the "auto" negotiation path -- it has to be requested explicitly via --checksum-choice=sha3-* (single, comma-paired with itself, or as an entry in RSYNC_CHECKSUM_LIST). All defaults are unchanged; an unmodified peer keeps negotiating xxh128/xxh3/md5/MD4 as before. Because every checksum entry point (get_checksum2, file_checksum, sum_init/update/end) already short-circuits to the EVP path when xfer_sum_evp_md / file_sum_evp_md / cur_sum_evp_md is set, no extra case arms are needed for the rolling-block, whole-file, or streaming code paths -- SHA-3 just flows through them. The new lib/md-defines.h constants define the SHA-3 digest lengths (32 / 48 / 64 bytes) since OpenSSL never published SHA3_*_DIGEST_LENGTH macros; SHA3-512's 64-byte digest matches SHA-512, so MAX_DIGEST_LEN is unaffected. A testsuite case (testsuite/checksum-sha3.test) exercises whole-file, pre-transfer (-c), comma-pair and delta-update paths for all three SHA-3 variants, and self-skips when the local rsync was not built with OpenSSL or its libcrypto lacks SHA-3. Verified: - rsync --version now lists sha3-512 sha3-384 sha3-256 in "Checksum list". - rsync -a --checksum-choice=sha3-{256,384,512} round-trips byte- for-byte (verified with diff against source). - Computed SHA3-256("abc") matches the FIPS 202 vector 3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532. - With --debug=NSTR, auto negotiation still picks xxh128 by default; SHA-3 is only selected when explicitly requested. - "make check" passes the new checksum-sha3_test.py along with the existing suite (the unrelated 'devices' failure is pre-existing on this host and reproduces on unmodified upstream master). Signed-off-by: Deepak Jain --- NEWS.md | 11 ++++ checksum.c | 20 +++++++ lib/md-defines.h | 12 ++++ rsync.1.md | 7 +++ testsuite/tests/transfer/checksum-sha3.py | 73 +++++++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100755 testsuite/tests/transfer/checksum-sha3.py diff --git a/NEWS.md b/NEWS.md index 5c144db66..194f53737 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,16 @@ # NEWS for rsync 4.0.0 (UNRELEASED) +## Enhancements + +- Added SHA-3 (FIPS 202) as an opt-in `--checksum-choice` algorithm + family: `sha3-256`, `sha3-384`, and `sha3-512`. The implementation + uses OpenSSL EVP and so requires that rsync be built with OpenSSL + support and linked against libcrypto >= 1.1.1. Like `sha256` and + `sha512`, SHA-3 is never picked by `auto` negotiation; it must be + explicitly requested on both ends (e.g. + `--checksum-choice=sha3-256`) or made part of the + `RSYNC_CHECKSUM_LIST` environment override. + ## Bug fixes - Fixed `-X` transfers of user xattrs on NetBSD by using the native BSD diff --git a/checksum.c b/checksum.c index dd8aee4c9..c29d5bc6d 100644 --- a/checksum.c +++ b/checksum.c @@ -55,6 +55,15 @@ struct name_num_item valid_checksums_items[] = { #ifdef SUPPORT_XXHASH { CSUM_XXH64, 0, "xxh64", NULL }, { CSUM_XXH64, 0, "xxhash", NULL }, +#endif +#ifdef USE_OPENSSL + /* SHA-3 is offered only via OpenSSL EVP (sha3-* digests, OpenSSL >= 1.1.1). + * verify_digest() probes each entry at startup and disables any that the + * linked libcrypto does not support, so listing them here is safe even + * when the build environment is older. */ + { CSUM_SHA3_512, NNI_EVP, "sha3-512", NULL }, + { CSUM_SHA3_384, NNI_EVP, "sha3-384", NULL }, + { CSUM_SHA3_256, NNI_EVP, "sha3-256", NULL }, #endif { CSUM_MD5, NNI_BUILTIN|NNI_EVP, "md5", NULL }, { CSUM_MD4, NNI_BUILTIN|NNI_EVP, "md4", NULL }, @@ -256,6 +265,14 @@ int csum_len_for_type(int cst, BOOL flist_csum) #ifdef SHA512_DIGEST_LENGTH case CSUM_SHA512: return SHA512_DIGEST_LENGTH; +#endif +#ifdef USE_OPENSSL + case CSUM_SHA3_256: + return SHA3_256_DIGEST_LEN; + case CSUM_SHA3_384: + return SHA3_384_DIGEST_LEN; + case CSUM_SHA3_512: + return SHA3_512_DIGEST_LEN; #endif case CSUM_XXH64: case CSUM_XXH3_64: @@ -285,6 +302,9 @@ int canonical_checksum(int csum_type) case CSUM_SHA1: case CSUM_SHA256: case CSUM_SHA512: + case CSUM_SHA3_256: + case CSUM_SHA3_384: + case CSUM_SHA3_512: return -1; case CSUM_XXH64: case CSUM_XXH3_64: diff --git a/lib/md-defines.h b/lib/md-defines.h index 6ef6a6897..0b58c575c 100644 --- a/lib/md-defines.h +++ b/lib/md-defines.h @@ -10,6 +10,15 @@ #define MD4_DIGEST_LEN 16 #define MD5_DIGEST_LEN 16 + +/* SHA-3 (FIPS 202). OpenSSL exposes these via EVP only, with no + * SHA3_*_DIGEST_LENGTH macros, so define our own. SHA3-512 shares + * its 64-byte digest length with SHA-512, so MAX_DIGEST_LEN is + * unaffected. */ +#define SHA3_256_DIGEST_LEN 32 +#define SHA3_384_DIGEST_LEN 48 +#define SHA3_512_DIGEST_LEN 64 + #if defined SHA512_DIGEST_LENGTH #define MAX_DIGEST_LEN SHA512_DIGEST_LENGTH #elif defined SHA256_DIGEST_LENGTH @@ -35,3 +44,6 @@ #define CSUM_SHA1 9 #define CSUM_SHA256 10 #define CSUM_SHA512 11 +#define CSUM_SHA3_256 12 +#define CSUM_SHA3_384 13 +#define CSUM_SHA3_512 14 diff --git a/rsync.1.md b/rsync.1.md index 45f33c63b..8f8f74fb0 100644 --- a/rsync.1.md +++ b/rsync.1.md @@ -2037,11 +2037,18 @@ sign) if you want the local shell to expand it. - `xxh128` - `xxh3` - `xxh64` (aka `xxhash`) + - `sha3-512` + - `sha3-384` + - `sha3-256` - `md5` - `md4` - `sha1` - `none` + The `sha3-*` choices require an rsync built with OpenSSL support and + a libcrypto that provides SHA-3 (OpenSSL 1.1.1 or newer). They are + never selected by `auto` negotiation and must be requested explicitly. + Run `rsync --version` to see the default checksum list compiled into your version (which may differ from the list above). diff --git a/testsuite/tests/transfer/checksum-sha3.py b/testsuite/tests/transfer/checksum-sha3.py new file mode 100755 index 000000000..2762f9e2c --- /dev/null +++ b/testsuite/tests/transfer/checksum-sha3.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# SHA-3 checksum support (sha3-256, sha3-384, sha3-512). +# +# Needs rsync built with OpenSSL against a libcrypto that provides the SHA-3 +# family (OpenSSL 1.1.1 or newer); without it the test reports the openssl +# capability as unsupported. +# +# Each algorithm is used for the whole-file transfer checksum, for the +# pre-transfer file checksum (-c), and as an explicit xfer,file pair; then a +# delta update of a modified destination is checked with sha3-256. + +import json +import os + +from harness.environment import FROMDIR, TODIR +from harness.filesystem import assert_same, makepath, rmtree, walk_files +from harness.results import test_fail, test_skipped +from harness.rsync import run_rsync +from harness import metadata + +metadata(features={'openssl'}, transports={'pipe'}) + +ALGOS = ('sha3-256', 'sha3-384', 'sha3-512') + +vv = json.loads(run_rsync('-VV', check=True, capture_output=True).stdout) +missing = [a for a in ALGOS if a not in vv.get('checksum_list', [])] +if missing: + test_skipped(f"rsync was not built with OpenSSL SHA-3 support (missing: {', '.join(missing)})", + capability='openssl') + +rmtree(FROMDIR) +makepath(FROMDIR / 'sub') +# Empty, small and chunk-spanning sizes (CHUNK_SIZE is 32k internally). +(FROMDIR / 'empty').write_bytes(b'') +(FROMDIR / 'small').write_text('hello sha-3\n') +(FROMDIR / 'sub' / 'big').write_bytes(os.urandom(70 * 1024)) + +src, dst = f'{FROMDIR}/', f'{TODIR}/' + + +def check_tree(label): + names = sorted(p.relative_to(FROMDIR) for p in walk_files(FROMDIR)) + if names != sorted(p.relative_to(TODIR) for p in walk_files(TODIR)): + test_fail(f'{label}: file lists differ') + for rel in names: + assert_same(TODIR / rel, FROMDIR / rel, label=f'{label} {rel}') + + +for algo in ALGOS: + # Whole-file path (transfer checksum only). + rmtree(TODIR) + run_rsync('-a', f'--checksum-choice={algo}', src, dst) + check_tree(algo) + # Pre-transfer + transfer checksum path (-c forces file_checksum to run). + rmtree(TODIR) + run_rsync('-ac', f'--checksum-choice={algo}', src, dst) + check_tree(f'{algo} -c') + # --checksum-choice with an explicit xfer,file pair. + rmtree(TODIR) + run_rsync('-ac', f'--checksum-choice={algo},{algo}', src, dst) + check_tree(f'{algo},{algo} -c') + +# Delta-update path: change the destination, re-sync with SHA-3, confirm it matches again. +rmtree(TODIR) +run_rsync('-a', '--checksum-choice=sha3-256', src, dst) +with open(TODIR / 'small', 'a') as f: + f.write('modified locally\n') +run_rsync('-a', '--no-whole-file', '--checksum-choice=sha3-256', src, dst) +check_tree('sha3-256 delta') +if (TODIR / 'small').read_text() != 'hello sha-3\n': + test_fail('delta update with sha3-256 did not restore the source content') + +print('checksum-sha3: sha3-256/384/512 transfer and file checksums, and a sha3-256 delta update') From f8c5ab13ef93f6985968385da10c2a9f32999223 Mon Sep 17 00:00:00 2001 From: Zen Dodd Date: Sat, 21 Nov 2026 21:05:25 +1100 Subject: [PATCH 2/3] checksum: make SHA-3 transfer checksums opt-in --- NEWS.md | 12 ++-- checksum.c | 10 +--- compat.c | 5 +- lib/md-defines.h | 10 ++-- rsync.1.md | 8 +-- rsync.h | 5 ++ testsuite/tests/transfer/checksum-sha3.py | 71 ++++++++++++++--------- 7 files changed, 69 insertions(+), 52 deletions(-) diff --git a/NEWS.md b/NEWS.md index 194f53737..d6e299a8b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,14 +2,10 @@ ## Enhancements -- Added SHA-3 (FIPS 202) as an opt-in `--checksum-choice` algorithm - family: `sha3-256`, `sha3-384`, and `sha3-512`. The implementation - uses OpenSSL EVP and so requires that rsync be built with OpenSSL - support and linked against libcrypto >= 1.1.1. Like `sha256` and - `sha512`, SHA-3 is never picked by `auto` negotiation; it must be - explicitly requested on both ends (e.g. - `--checksum-choice=sha3-256`) or made part of the - `RSYNC_CHECKSUM_LIST` environment override. +- Added SHA-3 (FIPS 202) transfer checksum choices `sha3-256`, + `sha3-384` and `sha3-512` for builds with OpenSSL SHA-3 support. + Default negotiation is unchanged. Select SHA-3 with + `--checksum-choice` or include it in both peers' `RSYNC_CHECKSUM_LIST`. ## Bug fixes diff --git a/checksum.c b/checksum.c index c29d5bc6d..e10a2c195 100644 --- a/checksum.c +++ b/checksum.c @@ -43,10 +43,6 @@ extern int protocol_version; extern int proper_seed_order; extern const char *checksum_choice; -#define NNI_BUILTIN (1<<0) -#define NNI_EVP (1<<1) -#define NNI_EVP_OK (1<<2) - struct name_num_item valid_checksums_items[] = { #ifdef SUPPORT_XXH3 { CSUM_XXH3_128, 0, "xxh128", NULL }, @@ -61,9 +57,9 @@ struct name_num_item valid_checksums_items[] = { * verify_digest() probes each entry at startup and disables any that the * linked libcrypto does not support, so listing them here is safe even * when the build environment is older. */ - { CSUM_SHA3_512, NNI_EVP, "sha3-512", NULL }, - { CSUM_SHA3_384, NNI_EVP, "sha3-384", NULL }, - { CSUM_SHA3_256, NNI_EVP, "sha3-256", NULL }, + { CSUM_SHA3_512, NNI_EVP|NNI_EXPLICIT_ONLY, "sha3-512", NULL }, + { CSUM_SHA3_384, NNI_EVP|NNI_EXPLICIT_ONLY, "sha3-384", NULL }, + { CSUM_SHA3_256, NNI_EVP|NNI_EXPLICIT_ONLY, "sha3-256", NULL }, #endif { CSUM_MD5, NNI_BUILTIN|NNI_EVP, "md5", NULL }, { CSUM_MD4, NNI_BUILTIN|NNI_EVP, "md4", NULL }, diff --git a/compat.c b/compat.c index df53b1fb2..3edbd6c58 100644 --- a/compat.c +++ b/compat.c @@ -459,7 +459,8 @@ void validate_choice_vs_env(int ntype, int num1, int num2) * for the order of the args in the array. If dup_markup == '\0', duplicates * are removed otherwise the char is prefixed to the duplicate term and, if it * is an opening paren/bracket/brace, the matching closing char is suffixed. - * "none" is removed on the client side unless dup_markup != '\0'. */ + * "none" is removed on the client side unless dup_markup != '\0'. + * Negotiation lists omit explicit-only choices. Display lists keep them. */ int get_default_nno_list(struct name_num_obj *nno, char *to_buf, int to_buf_len, char dup_markup) { struct name_num_item *nni; @@ -476,7 +477,7 @@ int get_default_nno_list(struct name_num_obj *nno, char *to_buf, int to_buf_len, init_nno_saw(nno, 0); for (nni = nno->list, len = 0; nni->name; nni++) { - if (nni->num == CSUM_gone) + if (nni->num == CSUM_gone || (!dup_markup && (nni->flags & NNI_EXPLICIT_ONLY))) continue; if (nni->main_nni) { if (!dup_markup || nni->main_nni->num == CSUM_gone) diff --git a/lib/md-defines.h b/lib/md-defines.h index 0b58c575c..59f0480cc 100644 --- a/lib/md-defines.h +++ b/lib/md-defines.h @@ -11,15 +11,15 @@ #define MD4_DIGEST_LEN 16 #define MD5_DIGEST_LEN 16 -/* SHA-3 (FIPS 202). OpenSSL exposes these via EVP only, with no - * SHA3_*_DIGEST_LENGTH macros, so define our own. SHA3-512 shares - * its 64-byte digest length with SHA-512, so MAX_DIGEST_LEN is - * unaffected. */ +/* OpenSSL exposes SHA-3 via EVP without SHA3_*_DIGEST_LENGTH macros. */ #define SHA3_256_DIGEST_LEN 32 #define SHA3_384_DIGEST_LEN 48 #define SHA3_512_DIGEST_LEN 64 -#if defined SHA512_DIGEST_LENGTH +#if defined USE_OPENSSL +/* SHA3-512 can be used even when SHA-512 is disabled. */ +#define MAX_DIGEST_LEN SHA3_512_DIGEST_LEN +#elif defined SHA512_DIGEST_LENGTH #define MAX_DIGEST_LEN SHA512_DIGEST_LENGTH #elif defined SHA256_DIGEST_LENGTH #define MAX_DIGEST_LEN SHA256_DIGEST_LENGTH diff --git a/rsync.1.md b/rsync.1.md index 8f8f74fb0..af152e2aa 100644 --- a/rsync.1.md +++ b/rsync.1.md @@ -2046,11 +2046,11 @@ sign) if you want the local shell to expand it. - `none` The `sha3-*` choices require an rsync built with OpenSSL support and - a libcrypto that provides SHA-3 (OpenSSL 1.1.1 or newer). They are - never selected by `auto` negotiation and must be requested explicitly. + a libcrypto that provides SHA-3. They are not offered during default + negotiation. Use `--checksum-choice` or include them in both peers' + [`RSYNC_CHECKSUM_LIST`](#). - Run `rsync --version` to see the default checksum list compiled into your - version (which may differ from the list above). + Run `rsync --version` to see the checksum choices available in your build. If "none" is specified for the first (or only) name, the [`--whole-file`](#opt) option is forced on and no checksum verification is performed on the diff --git a/rsync.h b/rsync.h index adf9bf1f1..8d5c10a7d 100644 --- a/rsync.h +++ b/rsync.h @@ -1235,6 +1235,11 @@ typedef struct { #define NSTR_CHECKSUM 0 #define NSTR_COMPRESS 1 +#define NNI_BUILTIN (1<<0) +#define NNI_EVP (1<<1) +#define NNI_EVP_OK (1<<2) +#define NNI_EXPLICIT_ONLY (1<<3) + struct name_num_item { int num, flags; const char *name; diff --git a/testsuite/tests/transfer/checksum-sha3.py b/testsuite/tests/transfer/checksum-sha3.py index 2762f9e2c..faa683614 100755 --- a/testsuite/tests/transfer/checksum-sha3.py +++ b/testsuite/tests/transfer/checksum-sha3.py @@ -1,39 +1,32 @@ #!/usr/bin/env python3 # SHA-3 checksum support (sha3-256, sha3-384, sha3-512). # -# Needs rsync built with OpenSSL against a libcrypto that provides the SHA-3 -# family (OpenSSL 1.1.1 or newer); without it the test reports the openssl -# capability as unsupported. -# -# Each algorithm is used for the whole-file transfer checksum, for the -# pre-transfer file checksum (-c), and as an explicit xfer,file pair; then a -# delta update of a modified destination is checked with sha3-256. - import json import os +from random import Random +import re from harness.environment import FROMDIR, TODIR from harness.filesystem import assert_same, makepath, rmtree, walk_files -from harness.results import test_fail, test_skipped +from harness.results import test_fail from harness.rsync import run_rsync from harness import metadata -metadata(features={'openssl'}, transports={'pipe'}) +metadata(transports={'pipe'}, mutates={'environment', 'filesystem'}, tags={'transfer', 'regression'}) ALGOS = ('sha3-256', 'sha3-384', 'sha3-512') -vv = json.loads(run_rsync('-VV', check=True, capture_output=True).stdout) -missing = [a for a in ALGOS if a not in vv.get('checksum_list', [])] -if missing: - test_skipped(f"rsync was not built with OpenSSL SHA-3 support (missing: {', '.join(missing)})", - capability='openssl') +choices = json.loads(run_rsync('-VV', capture_output=True).stdout)['checksum_list'] +available = [algo for algo in ALGOS if algo in choices] +os.environ.pop('RSYNC_CHECKSUM_LIST', None) rmtree(FROMDIR) makepath(FROMDIR / 'sub') # Empty, small and chunk-spanning sizes (CHUNK_SIZE is 32k internally). (FROMDIR / 'empty').write_bytes(b'') (FROMDIR / 'small').write_text('hello sha-3\n') -(FROMDIR / 'sub' / 'big').write_bytes(os.urandom(70 * 1024)) +size = 70 * 1024 +(FROMDIR / 'sub' / 'big').write_bytes(Random(0).getrandbits(size * 8).to_bytes(size, 'big')) src, dst = f'{FROMDIR}/', f'{TODIR}/' @@ -46,9 +39,26 @@ def check_tree(label): assert_same(TODIR / rel, FROMDIR / rel, label=f'{label} {rel}') +rmtree(TODIR) +result = run_rsync('-a', '--debug=NSTR2', src, dst, capture_output=True) +for peer in ('Client', 'Server'): + prefix = f'{peer} checksum list (on client): ' + offers = [line[len(prefix):].split() for line in result.stdout.splitlines() + if line.startswith(prefix)] + if len(offers) != 1 or set(offers[0]).intersection(ALGOS): + test_fail(f'{peer}: invalid default checksum offer: {offers!r}') +check_tree('auto') + + for algo in ALGOS: # Whole-file path (transfer checksum only). rmtree(TODIR) + if algo not in available: + result = run_rsync('-a', f'--checksum-choice={algo}', src, dst, + capture_output=True, check=False) + if result.returncode != 4 or f'unknown checksum name: {algo}' not in result.stderr: + test_fail(f'{algo}: an unavailable digest was not refused: {result.returncode}: {result.stderr}') + continue run_rsync('-a', f'--checksum-choice={algo}', src, dst) check_tree(algo) # Pre-transfer + transfer checksum path (-c forces file_checksum to run). @@ -60,14 +70,23 @@ def check_tree(label): run_rsync('-ac', f'--checksum-choice={algo},{algo}', src, dst) check_tree(f'{algo},{algo} -c') -# Delta-update path: change the destination, re-sync with SHA-3, confirm it matches again. -rmtree(TODIR) -run_rsync('-a', '--checksum-choice=sha3-256', src, dst) -with open(TODIR / 'small', 'a') as f: - f.write('modified locally\n') -run_rsync('-a', '--no-whole-file', '--checksum-choice=sha3-256', src, dst) -check_tree('sha3-256 delta') -if (TODIR / 'small').read_text() != 'hello sha-3\n': - test_fail('delta update with sha3-256 did not restore the source content') +if available: + algo = available[0] + rmtree(TODIR) + os.environ['RSYNC_CHECKSUM_LIST'] = algo + result = run_rsync('-a', '--debug=NSTR2', src, dst, capture_output=True) + del os.environ['RSYNC_CHECKSUM_LIST'] + if f'Client negotiated checksum: {algo}' not in result.stdout.splitlines(): + test_fail(f'RSYNC_CHECKSUM_LIST did not negotiate {algo}: {result.stdout!r}') + check_tree(f'negotiated {algo}') -print('checksum-sha3: sha3-256/384/512 transfer and file checksums, and a sha3-256 delta update') + with (TODIR / 'sub' / 'big').open('r+b') as stream: + stream.seek(32 * 1024) + stream.write(b'modified locally\n') + result = run_rsync('-ac', '--no-whole-file', '--stats', f'--checksum-choice={algo}', + src, dst, capture_output=True) + for field in ('Matched', 'Literal'): + count = re.search(rf'^{field} data: ([0-9,]+) bytes$', result.stdout, re.MULTILINE) + if not count or int(count[1].replace(',', '')) == 0: + test_fail(f'{algo} delta: no {field.lower()} data: {result.stdout!r}') + check_tree(f'{algo} delta') From e17be7784382f9432b080da19ac0cf39c71d8672 Mon Sep 17 00:00:00 2001 From: Zen Dodd Date: Sat, 21 Nov 2026 21:56:53 +1100 Subject: [PATCH 3/3] testsuite: cover SHA-3 on legacy protocols --- testsuite/tests/transfer/checksum-sha3.py | 69 ++++++++++++++++------- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/testsuite/tests/transfer/checksum-sha3.py b/testsuite/tests/transfer/checksum-sha3.py index faa683614..2ba385cce 100755 --- a/testsuite/tests/transfer/checksum-sha3.py +++ b/testsuite/tests/transfer/checksum-sha3.py @@ -5,11 +5,12 @@ import os from random import Random import re +import subprocess from harness.environment import FROMDIR, TODIR from harness.filesystem import assert_same, makepath, rmtree, walk_files from harness.results import test_fail -from harness.rsync import run_rsync +from harness.rsync import forced_protocol, rsync_argv, run_rsync from harness import metadata metadata(transports={'pipe'}, mutates={'environment', 'filesystem'}, tags={'transfer', 'regression'}) @@ -19,6 +20,8 @@ choices = json.loads(run_rsync('-VV', capture_output=True).stdout)['checksum_list'] available = [algo for algo in ALGOS if algo in choices] os.environ.pop('RSYNC_CHECKSUM_LIST', None) +protocol = forced_protocol() +legacy_checksum = protocol is not None and protocol < 30 rmtree(FROMDIR) makepath(FROMDIR / 'sub') @@ -39,15 +42,47 @@ def check_tree(label): assert_same(TODIR / rel, FROMDIR / rel, label=f'{label} {rel}') -rmtree(TODIR) -result = run_rsync('-a', '--debug=NSTR2', src, dst, capture_output=True) -for peer in ('Client', 'Server'): - prefix = f'{peer} checksum list (on client): ' - offers = [line[len(prefix):].split() for line in result.stdout.splitlines() - if line.startswith(prefix)] - if len(offers) != 1 or set(offers[0]).intersection(ALGOS): - test_fail(f'{peer}: invalid default checksum offer: {offers!r}') -check_tree('auto') +def check_default_checksum(): + rmtree(TODIR) + result = run_rsync('-a', '--debug=NSTR2', src, dst, capture_output=True, check=False) + if result.returncode: + test_fail(f'default checksum transfer failed: {result}') + lines = result.stdout.splitlines() + if legacy_checksum: + if 'Client checksum: md4' not in lines: + test_fail(f'protocol {protocol}: expected the MD4 default: {result.stdout!r}') + else: + for peer in ('Client', 'Server'): + prefix = f'{peer} checksum list (on client): ' + offers = [line[len(prefix):].split() for line in lines if line.startswith(prefix)] + if len(offers) != 1 or set(offers[0]).intersection(ALGOS): + test_fail(f'{peer}: invalid default checksum offer: {offers!r}') + check_tree('auto') + + +def transfer_with_checksum_list(choices): + return subprocess.run(rsync_argv('-a', '--debug=NSTR2', src, dst), + env={**os.environ, 'RSYNC_CHECKSUM_LIST': choices}, + capture_output=True, text=True) + + +def check_checksum_list(algo): + rmtree(TODIR) + result = transfer_with_checksum_list(algo) + expected = f'Client negotiated checksum: {algo}' + if legacy_checksum: + # Without negotiation the list may allow MD4 or refuse it, not replace it. + if result.returncode != 4 or 'Failed to negotiate a checksum choice.' not in result.stderr: + test_fail(f'protocol {protocol}: expected a SHA-3-only list to be refused: {result}') + rmtree(TODIR) + result = transfer_with_checksum_list(f'{algo} md4') + expected = 'Client checksum: md4' + if result.returncode or expected not in result.stdout.splitlines(): + test_fail(f'RSYNC_CHECKSUM_LIST: expected {expected!r}: {result}') + check_tree('checksum list') + + +check_default_checksum() for algo in ALGOS: @@ -59,7 +94,10 @@ def check_tree(label): if result.returncode != 4 or f'unknown checksum name: {algo}' not in result.stderr: test_fail(f'{algo}: an unavailable digest was not refused: {result.returncode}: {result.stderr}') continue - run_rsync('-a', f'--checksum-choice={algo}', src, dst) + result = run_rsync('-a', f'--checksum-choice={algo}', '--debug=NSTR2', src, dst, + capture_output=True, check=False) + if result.returncode or f'Client checksum: {algo}' not in result.stdout.splitlines(): + test_fail(f'explicit choice did not select {algo}: {result}') check_tree(algo) # Pre-transfer + transfer checksum path (-c forces file_checksum to run). rmtree(TODIR) @@ -72,14 +110,7 @@ def check_tree(label): if available: algo = available[0] - rmtree(TODIR) - os.environ['RSYNC_CHECKSUM_LIST'] = algo - result = run_rsync('-a', '--debug=NSTR2', src, dst, capture_output=True) - del os.environ['RSYNC_CHECKSUM_LIST'] - if f'Client negotiated checksum: {algo}' not in result.stdout.splitlines(): - test_fail(f'RSYNC_CHECKSUM_LIST did not negotiate {algo}: {result.stdout!r}') - check_tree(f'negotiated {algo}') - + check_checksum_list(algo) with (TODIR / 'sub' / 'big').open('r+b') as stream: stream.seek(32 * 1024) stream.write(b'modified locally\n')