Skip to content

test: Add coverage for TorControl SAFECOOKIE authentication - #36497

Open
winterrdog wants to merge 1 commit into
bitcoin:masterfrom
winterrdog:test/torcontrol-safecookie-auth
Open

winterrdog wants to merge 1 commit into
bitcoin:masterfrom
winterrdog:test/torcontrol-safecookie-auth

Conversation

@winterrdog

Copy link
Copy Markdown
Contributor

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 DrahtBot added the Tests label Oct 10, 2026
@DrahtBot

DrahtBot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

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.

@winterrdog

Copy link
Copy Markdown
Contributor Author

"Targeted" Test Coverage Improvements

Scope Metric Before (merge-base at cf80493) 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

@jpk68

jpk68 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

ACK 141c4a7

@mercie-ux

Copy link
Copy Markdown
Contributor

Concept ACK 6827006

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.

Test successful authentication and failures caused by an unsuccessful
`AUTHCHALLENGE`, an invalid server nonce or hash, and a missing or
invalid-length cookie file.
@winterrdog
winterrdog force-pushed the test/torcontrol-safecookie-auth branch from 88eafcc to 141c4a7 Compare November 6, 2026 19:49
@winterrdog

winterrdog commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

changes:

  • took up ralph's suggestion here
  • made a rebase, as well

@Pranav-error

Copy link
Copy Markdown

crACK 141c4a7

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

Copy link
Copy Markdown

Tested ACK 141c4a7

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants