Skip to content

Commit bd29de3

Browse files
ryanofskySjors
andcommitted
test: add interface_ipc_cli.py testing bitcoin-cli -ipcconnect
Co-authored-by: Sjors Provoost <[email protected]>
1 parent 0394da2 commit bd29de3

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function(create_test_config)
2626
set_configure_variable(WITH_ZMQ ENABLE_ZMQ)
2727
set_configure_variable(ENABLE_EXTERNAL_SIGNER ENABLE_EXTERNAL_SIGNER)
2828
set_configure_variable(WITH_USDT ENABLE_USDT_TRACEPOINTS)
29+
set_configure_variable(ENABLE_IPC ENABLE_IPC)
2930

3031
configure_file(config.ini.in config.ini USE_SOURCE_PERMISSIONS @ONLY)
3132
endfunction()

test/config.ini.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ RPCAUTH=@abs_top_srcdir@/share/rpcauth/rpcauth.py
2626
@ENABLE_ZMQ_TRUE@ENABLE_ZMQ=true
2727
@ENABLE_EXTERNAL_SIGNER_TRUE@ENABLE_EXTERNAL_SIGNER=true
2828
@ENABLE_USDT_TRACEPOINTS_TRUE@ENABLE_USDT_TRACEPOINTS=true
29+
@ENABLE_IPC_TRUE@ENABLE_IPC=true

test/functional/interface_bitcoin_cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from decimal import Decimal
88
import re
9+
import subprocess
910

1011
from test_framework.blocktools import COINBASE_MATURITY
1112
from test_framework.netutil import test_ipv6_local
@@ -400,6 +401,14 @@ def run_test(self):
400401
self.log.info("Test that only one of -addrinfo, -generate, -getinfo, -netinfo may be specified at a time")
401402
assert_raises_process_error(1, "Only one of -getinfo, -netinfo may be specified", self.nodes[0].cli('-getinfo', '-netinfo').send_cli)
402403

404+
if not self.is_ipc_enabled():
405+
self.log.info("Test bitcoin-cli -ipcconnect triggers error if not built with IPC support")
406+
args = [self.binary_paths.bitcoincli, "-ipcconnect=unix", "-getinfo"]
407+
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
408+
assert_equal(result.stdout, "error: bitcoin-cli was not built with IPC support\n")
409+
assert_equal(result.stderr, None)
410+
assert_equal(result.returncode, 1)
411+
403412

404413
if __name__ == '__main__':
405414
TestBitcoinCli(__file__).main()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2025 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test IPC with bitcoin-cli"""
6+
7+
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.util import (
9+
assert_equal,
10+
rpc_port
11+
)
12+
13+
import subprocess
14+
import tempfile
15+
16+
class TestBitcoinIpcCli(BitcoinTestFramework):
17+
def set_test_params(self):
18+
self.setup_clean_chain = True
19+
self.num_nodes = 1
20+
21+
def skip_test_if_missing_module(self):
22+
self.skip_if_no_ipc()
23+
24+
def setup_nodes(self):
25+
# Always run IPC node binary
26+
self.binary_paths.bitcoind = self.binary_paths.bitcoin_node
27+
28+
# Work around default CI path exceeding maximum socket path length.
29+
# On Linux sun_path is 108 bytes, on macOS it's only 104. Includes
30+
# null terminator.
31+
self.socket_path = self.options.tmpdir + "/node0/regtest/node.sock"
32+
if len(self.socket_path.encode('utf-8')) < 104:
33+
self.extra_args = [["-ipcbind=unix"]]
34+
self.default_socket = True
35+
else:
36+
self.socket_path = tempfile.mktemp()
37+
self.extra_args = [[f"-ipcbind=unix:{self.socket_path}"]]
38+
self.default_socket = False
39+
super().setup_nodes()
40+
41+
def run_test(self):
42+
conflict_error = "error: -rpcconnect and -ipcconnect options cannot both be enabled\n"
43+
http_error = "error: Authorization failed: Incorrect rpcuser or rpcpassword\n"
44+
connect_ipc_error = "error: Connection refused\n\nProbably bitcoin-node is not running or not listening on a unix socket. Can be started with:\n\n bitcoin-node -chain=regtest -ipcbind=unix\n"
45+
connect_http_error = f"error: timeout on transient error: Could not connect to the server 127.0.0.1:{rpc_port(self.nodes[0].index)}\n\nMake sure the bitcoind server is running and that you are connecting to the correct RPC port.\nUse \"bitcoin-cli -help\" for more info.\n"
46+
47+
if not self.default_socket:
48+
self.log.info("Skipping IPC tests that rely on default socket path because it exceeds maximum socket path length")
49+
50+
for started in (True, False):
51+
auto_error = None if started else connect_http_error
52+
http_error = http_error if started else connect_http_error
53+
ipc_error = None if started else connect_ipc_error
54+
55+
if self.default_socket:
56+
self.test_cli([], auto_error)
57+
self.test_cli(["-rpcconnect=127.0.0.1"], http_error)
58+
self.test_cli(["-ipcconnect=auto"], auto_error)
59+
self.test_cli(["-ipcconnect=auto", "-rpcconnect=127.0.0.1"], http_error)
60+
self.test_cli(["-ipcconnect=unix"], ipc_error)
61+
62+
self.test_cli([f"-ipcconnect=unix:{self.socket_path}"], ipc_error)
63+
self.test_cli(["-noipcconnect"], http_error)
64+
self.test_cli(["-ipcconnect=unix", "-rpcconnect=127.0.0.1"], conflict_error)
65+
66+
self.stop_node(0)
67+
68+
def test_cli(self, args, error=None):
69+
# Intentionally set wrong RPC password so only IPC not HTTP connections work
70+
args = [self.binary_paths.bitcoincli, f"-datadir={self.nodes[0].datadir_path}", "-rpcpassword=wrong"] + args + ["echo", "foo"]
71+
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
72+
if error:
73+
assert_equal(result.stdout, error)
74+
else:
75+
assert_equal(result.stdout, '[\n "foo"\n]\n')
76+
assert_equal(result.stderr, None)
77+
assert_equal(result.returncode, 1 if error else 0)
78+
79+
if __name__ == '__main__':
80+
TestBitcoinIpcCli(__file__).main()

test/functional/test_framework/test_framework.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,15 @@ def get_binary_paths(self):
298298
"bitcoin-chainstate": ("bitcoinchainstate", "BITCOINCHAINSTATE"),
299299
"bitcoin-wallet": ("bitcoinwallet", "BITCOINWALLET"),
300300
}
301-
for binary, [attribute_name, env_variable_name] in binaries.items():
302-
default_filename = os.path.join(
301+
def binary_path(binary):
302+
return os.path.join(
303303
self.config["environment"]["BUILDDIR"],
304304
"bin",
305305
binary + self.config["environment"]["EXEEXT"],
306306
)
307-
setattr(paths, attribute_name, os.getenv(env_variable_name, default=default_filename))
307+
for binary, [attribute_name, env_variable_name] in binaries.items():
308+
setattr(paths, attribute_name, os.getenv(env_variable_name) or binary_path(binary))
309+
paths.bitcoin_node = binary_path("bitcoin-node")
308310
return paths
309311

310312
def get_binaries(self, bin_dir=None):
@@ -1037,6 +1039,11 @@ def skip_if_no_cli(self):
10371039
if not self.is_cli_compiled():
10381040
raise SkipTest("bitcoin-cli has not been compiled.")
10391041

1042+
def skip_if_no_ipc(self):
1043+
"""Skip the running test if ipc is not enabled."""
1044+
if not self.is_ipc_enabled():
1045+
raise SkipTest("ipc is not enabled.")
1046+
10401047
def skip_if_no_previous_releases(self):
10411048
"""Skip the running test if previous releases are not available."""
10421049
if not self.has_previous_releases():
@@ -1099,5 +1106,9 @@ def is_bdb_compiled(self):
10991106
"""Checks whether the wallet module was compiled with BDB support."""
11001107
return self.config["components"].getboolean("USE_BDB")
11011108

1109+
def is_ipc_enabled(self):
1110+
"""Checks whether ipc is enabled."""
1111+
return self.config["components"].getboolean("ENABLE_IPC")
1112+
11021113
def has_blockfile(self, node, filenum: str):
11031114
return (node.blocks_path/ f"blk{filenum}.dat").is_file()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@
408408
'rpc_help.py',
409409
'p2p_handshake.py',
410410
'p2p_handshake.py --v2transport',
411+
'interface_ipc_cli.py',
411412
'feature_dirsymlinks.py',
412413
'feature_help.py',
413414
'feature_shutdown.py',

0 commit comments

Comments
 (0)