|
| 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() |
0 commit comments