test: Add coverage for TorControl SAFECOOKIE authentication #36497

Open
winterrdog wants to merge 1 commit from test/torcontrol-safecookie-auth into master AGit
winterrdog commented 2026-10-10 19:04:19 +00:00 (Migrated from github.com)

adds functional test coverage for SAFECOOKIE authentication, including
successful authentication, AUTHCHALLENGE failure, an invalid server
nonce, a mismatched server hash, an invalid-length cookie file, and a
missing cookie file

while working on #35292, i realised we did not have coverage for
SAFECOOKIE authentication either, so i added it here

adds functional test coverage for `SAFECOOKIE` authentication, including successful authentication, `AUTHCHALLENGE` failure, an invalid server nonce, a mismatched server hash, an invalid-length cookie file, and a missing cookie file while working on #35292, i realised we did not have coverage for `SAFECOOKIE` authentication either, so i added it here
DrahtBot commented 2026-10-10 19:04:29 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK jpk68, Pranav-error
Concept ACK mercie-ux

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #35292 (test: Add coverage for Tor control HASHEDPASSWORD authentication by winterrdog)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36497 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36497.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36497#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [jpk68](https://github.com/bitcoin/bitcoin/pull/36497#issuecomment-6281552302), [Pranav-error](https://github.com/bitcoin/bitcoin/pull/36497#issuecomment-6834634929) | | Concept ACK | [mercie-ux](https://github.com/bitcoin/bitcoin/pull/36497#issuecomment-6327384742) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#35292](https://github.com/bitcoin/bitcoin/pull/35292) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35292.svg"></sub> (test: Add coverage for Tor control `HASHEDPASSWORD` authentication by winterrdog) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🟠 Minor · Guarantee that the fabricated server hash mismatches

feature_torcontrol.py:149-151 still replaces SERVERHASH with 32 zero bytes instead of flipping a bit of the computed hash. A mismatch is therefore near-certain (about 2^-256 chance of failure), not guaranteed. The comment now correctly says 'practically never'. Practical impact is negligible.

Location: test/functional/feature_torcontrol.py:147 (head).

Review coverage was incomplete.


Reviewed: 141c4a798a82 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:df0443dc5cff1224f58cf2f0af2a814b52ac77c4fa18c64584a637f43b36d781:adfdaf6241fe97209d62717f4b34912a3f3c05551d5eb04e28ec078dd3f7e4f2 --> <!-- ralph-base:5bf3bc5e263407a252c14a17afe04fc3bf24a889 --> <!-- ralph-head:141c4a798a82678aa71c9be50e960dc5c4f37804 --> <!-- ralph-case:a30b540b2ef1d5670d5e9c55f13dd1146270d134365bb094d7f58674775a2203 --> **🟠 Minor · Guarantee that the fabricated server hash mismatches** feature_torcontrol.py:149-151 still replaces SERVERHASH with 32 zero bytes instead of flipping a bit of the computed hash. A mismatch is therefore near-certain (about 2^-256 chance of failure), not guaranteed. The comment now correctly says 'practically never'. Practical impact is negligible. Location: [`test/functional/feature_torcontrol.py:147`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/141c4a798a82678aa71c9be50e960dc5c4f37804/test/functional/feature_torcontrol.py#L147>) (head). Review coverage was incomplete. <!-- Review tier: routine --> --- Reviewed: [`141c4a798a82`](<https://git.fish.foo/bitcoin/bitcoin/commit/141c4a798a82678aa71c9be50e960dc5c4f37804>) · [Full review report](<https://review.fish.foo/traces/36497-141c4a798a82678aa71c9be50e960dc5c4f37804-2336-6.html>) · [Previous reports](<https://review.fish.foo/traces/36497/>) [permalink](<https://review.fish.foo/traces/36497-141c4a798a82678aa71c9be50e960dc5c4f37804-2336-6.html>)
winterrdog commented 2026-10-10 19:15:38 +00:00 (Migrated from github.com)

"Targeted" Test Coverage Improvements

Scope Metric Before (merge-base at cf80493e23) After (PR HEAD) Change
src/torcontrol\.cpp$ Lines 71.7% (281/392) 81.9% (321/392) +10.2 pp (+40 lines)
src/torcontrol\.cpp$ Functions 88.9% (24/27) 96.3% (26/27) +7.4 pp (+2 funcs)
src/torcontrol\.cpp$ Branches 39.7% (265/668) 47.9% (320/668) +8.2 pp (+55 branches)

to get these results, i only ran feature_torcontrol.py before and after the
change on the same build, using GCC's coverage instrumentation and lcov
2.0

bash script used to get test coverage locally
# 1. compile once with coverage baked in
cmake -B build -DCMAKE_BUILD_TYPE=Coverage
cmake --build build -j$(nproc)

# 2. BEFORE: run the merge-base version of the test file
git checkout $(git merge-base HEAD master) -- test/functional/feature_torcontrol.py
find build -name '*.gcda' -delete
python3 test/functional/feature_torcontrol.py --configfile=build/test/config.ini
lcov --capture --directory build --gcov-tool gcov-$(gcc -dumpversion | cut -d. -f1) \
     --rc branch_coverage=1 --ignore-errors inconsistent,mismatch,unused,negative \
     --output-file base.info

# 3. AFTER: run the PR version of the test file
git checkout HEAD -- test/functional/feature_torcontrol.py
find build -name '*.gcda' -delete
python3 test/functional/feature_torcontrol.py --configfile=build/test/config.ini
lcov --capture --directory build --gcov-tool gcov-$(gcc -dumpversion | cut -d. -f1) \
     --rc branch_coverage=1 --ignore-errors inconsistent,mismatch,unused,negative \
     --output-file after.info

# 4. keep only the file under test, then build the HTML report
lcov --extract base.info  '*/src/torcontrol.cpp' --rc branch_coverage=1 -o base_tor.info
lcov --extract after.info '*/src/torcontrol.cpp' --rc branch_coverage=1 -o after_tor.info
genhtml after_tor.info --baseline-file base_tor.info --branch-coverage -o cov_diff

## DONE...

# opens the system's default browser on linux. on other systems, just open the
# differential coverage report (./cov_diff/index.html) in your browser of choice
xdg-open cov_diff/index.html
### "Targeted" Test Coverage Improvements | Scope | Metric | Before (merge-base at cf80493e2367786ce9946c522eb8313a52c90557) | After (PR `HEAD`) | Change | |---|---|---|---|---| | `src/torcontrol\.cpp$` | Lines | 71.7% (281/392) | 81.9% (321/392) | +10.2 pp (+40 lines) | | `src/torcontrol\.cpp$` | Functions | 88.9% (24/27) | 96.3% (26/27) | +7.4 pp (+2 funcs) | | `src/torcontrol\.cpp$` | Branches | 39.7% (265/668) | 47.9% (320/668) | +8.2 pp (+55 branches) | to get these results, i only ran `feature_torcontrol.py` before and after the change on the same build, using GCC's coverage instrumentation and lcov `2.0` <details><summary>bash script used to get test coverage locally</summary> ```sh # 1. compile once with coverage baked in cmake -B build -DCMAKE_BUILD_TYPE=Coverage cmake --build build -j$(nproc) # 2. BEFORE: run the merge-base version of the test file git checkout $(git merge-base HEAD master) -- test/functional/feature_torcontrol.py find build -name '*.gcda' -delete python3 test/functional/feature_torcontrol.py --configfile=build/test/config.ini lcov --capture --directory build --gcov-tool gcov-$(gcc -dumpversion | cut -d. -f1) \ --rc branch_coverage=1 --ignore-errors inconsistent,mismatch,unused,negative \ --output-file base.info # 3. AFTER: run the PR version of the test file git checkout HEAD -- test/functional/feature_torcontrol.py find build -name '*.gcda' -delete python3 test/functional/feature_torcontrol.py --configfile=build/test/config.ini lcov --capture --directory build --gcov-tool gcov-$(gcc -dumpversion | cut -d. -f1) \ --rc branch_coverage=1 --ignore-errors inconsistent,mismatch,unused,negative \ --output-file after.info # 4. keep only the file under test, then build the HTML report lcov --extract base.info '*/src/torcontrol.cpp' --rc branch_coverage=1 -o base_tor.info lcov --extract after.info '*/src/torcontrol.cpp' --rc branch_coverage=1 -o after_tor.info genhtml after_tor.info --baseline-file base_tor.info --branch-coverage -o cov_diff ## DONE... # opens the system's default browser on linux. on other systems, just open the # differential coverage report (./cov_diff/index.html) in your browser of choice xdg-open cov_diff/index.html ``` </details>
jpk68 commented 2026-10-20 01:59:10 +00:00 (Migrated from github.com)
ACK 141c4a798a82678aa71c9be50e960dc5c4f37804
mercie-ux commented 2026-10-21 21:53:40 +00:00 (Migrated from github.com)

Concept ACK 6827006f4f

I introduced a few changes in torcontrol.cpp skipping the ServerHash comparison, dropping the ServerNonce length check, using the wrong key for the client hash and accepting any cookie length, all of this pass on master and fail on this branch, so the new tests catch them.

Concept ACK 6827006f4f I introduced a few changes in `torcontrol.cpp` skipping the ServerHash comparison, dropping the ServerNonce length check, using the wrong key for the client hash and accepting any cookie length, all of this pass on master and fail on this branch, so the new tests catch them.
DrahtBot commented 2026-11-05 12:05:22 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
winterrdog commented 2026-11-06 19:50:57 +00:00 (Migrated from github.com)

changes:

  • took up ralph's suggestion here
  • made a rebase, as well
changes: - took up ralph's suggestion [here](https://git.fish.foo/bitcoin/bitcoin/pulls/36497#author-ralph) - made a rebase, as well
Pranav-error commented 2026-11-12 20:07:27 +00:00 (Migrated from github.com)

crACK 141c4a798a

Reviewed against torcontrol.cpp:

  • The mock's HMAC construction — cookie + client_nonce + server_nonce keyed by TOR_SAFE_SERVERKEY/TOR_SAFE_CLIENTKEY — matches ComputeResponse() exactly, which writes cookie → client_nonce → server_nonce in that order (src/torcontrol.cpp:606-611).
  • All five asserted debug-log substrings line up with the real messages at the right branches: SAFECOOKIE authentication challenge failed, ServerNonce is not 32 bytes, does not match expected ServerHash, ... could not be opened, and the short-cookie one (is not exactly %i bytes at :715, where %i→TOR_COOKIE_SIZE=32, so the asserted ... is not exactly 32 bytes is a substring of the emitted line).

Together with @mercie-ux's mutation testing, this confirms the six methods are genuine regression coverage rather than just line-hitting: happy path plus the five distinct failure branches, each pinned to a specific log line and an ensure_for check that the node sends no further command. Nice bump (71.7%→81.9%).

Minor, non-blocking: the six ensure_for(duration=2) absence-checks add ~12s of fixed wait; unavoidable for asserting "no command follows," just noting it for total runtime.

Code-review only — didn't build/run locally.

crACK 141c4a798a82678aa71c9be50e960dc5c4f37804 Reviewed against `torcontrol.cpp`: - The mock's HMAC construction — `cookie + client_nonce + server_nonce` keyed by `TOR_SAFE_SERVERKEY`/`TOR_SAFE_CLIENTKEY` — matches `ComputeResponse()` exactly, which writes `cookie` → `client_nonce` → `server_nonce` in that order (src/torcontrol.cpp:606-611). - All five asserted debug-log substrings line up with the real messages at the right branches: `SAFECOOKIE authentication challenge failed`, `ServerNonce is not 32 bytes`, `does not match expected ServerHash`, `... could not be opened`, and the short-cookie one (`is not exactly %i bytes` at :715, where `%i`→`TOR_COOKIE_SIZE`=32, so the asserted `... is not exactly 32 bytes` is a substring of the emitted line). Together with @mercie-ux's mutation testing, this confirms the six methods are genuine regression coverage rather than just line-hitting: happy path plus the five distinct failure branches, each pinned to a specific log line **and** an `ensure_for` check that the node sends no further command. Nice bump (71.7%→81.9%). Minor, non-blocking: the six `ensure_for(duration=2)` absence-checks add ~12s of fixed wait; unavoidable for asserting "no command follows," just noting it for total runtime. Code-review only — didn't build/run locally.
Pranav-error commented 2026-11-12 20:16:32 +00:00 (Migrated from github.com)

Tested ACK 141c4a798a

Ran the six new methods against a master build (v32.99.0-791173ae), all pass:

SAFECOOKIE authentication succeeds
AUTHCHALLENGE returns an error code
... cookie file shorter than 32 bytes
... cookie file cannot be read
... server nonce is not 32 bytes
... server hash does not match the computed one
Tests successful

Upgrading my earlier code-review ACK — together with @mercie-ux's mutation testing this confirms the methods pass on correct code and fail when the checks are broken.

Tested ACK 141c4a798a82678aa71c9be50e960dc5c4f37804 Ran the six new methods against a master build (v32.99.0-791173ae), all pass: ``` SAFECOOKIE authentication succeeds AUTHCHALLENGE returns an error code ... cookie file shorter than 32 bytes ... cookie file cannot be read ... server nonce is not 32 bytes ... server hash does not match the computed one Tests successful ``` Upgrading my earlier code-review ACK — together with @mercie-ux's mutation testing this confirms the methods pass on correct code and fail when the checks are broken.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36497
No description provided.