test: Make torcontrol max line length test stricter and test boundaries.
What changed, and why it matters
This commit only changes a test file. It makes the existing Tor control line-length test more precise by checking that a line exactly at the maximum allowed length does not cause a disconnect, while a line one character over still does. There is no change to Bitcoin Core's actual production code, so it cannot directly affect live node security.
No security action required. Review as a normal test-quality improvement.
Security signals we found
No production code changes
Test-only refinement of an existing security-relevant boundary check
Does not alter Tor control parsing logic or MAX_LINE_LENGTH constant
Evidence from the diff
The diff modifies test/functional/feature_torcontrol.py. It adds a context manager expect_disconnect() and rewrites test_oversized_line() to test the boundary values of MAX_LINE_LENGTH (100,000 bytes). The test now verifies that a 100,000-byte line is accepted and a 100,001-byte line triggers a disconnect/reconnect. No C++ or Python production code in src/ or elsewhere is altered.
Changed components
test/functional/feature_torcontrol.pyInspect captured patch +27 / −8
diff --git a/test/functional/feature_torcontrol.py b/test/functional/feature_torcontrol.py
index 5f8cf9f6..9693030b 100755
--- a/test/functional/feature_torcontrol.py
+++ b/test/functional/feature_torcontrol.py
@@ -3,6 +3,7 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test torcontrol functionality with a mock Tor control server."""
+from contextlib import contextmanager
import socket
import threading
from test_framework.test_framework import BitcoinTestFramework
@@ -124,6 +125,20 @@ class TorControlTest(BitcoinTestFramework):
self.wait_until(lambda: len(mock_tor.received_commands) >= 1, timeout=10)
assert_equal(mock_tor.received_commands[0], "PROTOCOLINFO 1")
+ @contextmanager
+ def expect_disconnect(self, expect, mock_tor):
+ initial_len = len(mock_tor.received_commands)
+ yield
+
+ if expect:
+ # Expect to receive a PROTOCOLINFO 1 on reconnect, bumping the received
+ # commands length.
+ self.wait_until(lambda: len(mock_tor.received_commands) == initial_len + 1)
+ assert_equal(mock_tor.received_commands[initial_len], "PROTOCOLINFO 1")
+ else:
+ # No disconnect, so no reconnect message
+ ensure_for(duration=2, f=lambda: len(mock_tor.received_commands) == initial_len)
+
def test_basic(self):
self.log.info("Test Tor control basic functionality")
@@ -202,19 +217,23 @@ class TorControlTest(BitcoinTestFramework):
mock_tor.stop()
def test_oversized_line(self):
- self.log.info("Test that Tor control disconnects on oversized response lines")
-
mock_tor = MockTorControlServer(self.next_port(), manual_mode=True)
self.restart_with_mock(mock_tor)
- # Send a single line longer than MAX_LINE_LENGTH. The node should disconnect.
MAX_LINE_LENGTH = 100000
- mock_tor.send_raw("250-" + ("A" * (MAX_LINE_LENGTH + 1)) + "\r\n")
- ensure_for(duration=2, f=lambda: self.nodes[0].process.poll() is None)
- # Connection should be dropped and retried, causing another PROTOCOLINFO.
- self.wait_until(lambda: len(mock_tor.received_commands) >= 2, timeout=10)
- assert_equal(mock_tor.received_commands[1], "PROTOCOLINFO 1")
+ self.log.info("Test that Tor control does not disconnect with a MAX_LINE_LENGTH line.")
+ with self.expect_disconnect(False, mock_tor):
+ msg = "250-" + ("A" * (MAX_LINE_LENGTH - 5)) + "\r"
+ assert_equal(len(msg), MAX_LINE_LENGTH)
+ # The \n is not counted in line length.
+ mock_tor.send_raw(msg + "\n")
+
+ self.log.info("Test that Tor control disconnects with a MAX_LINE_LENGTH + 1 line")
+ with self.expect_disconnect(True, mock_tor):
+ msg = "250-" + ("A" * (MAX_LINE_LENGTH - 4)) + "\r"
+ assert_equal(len(msg), MAX_LINE_LENGTH + 1)
+ mock_tor.send_raw(msg + "\n")
mock_tor.stop()
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.