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 15 class TestBitcoinIpcCli(BitcoinTestFramework):
16 def set_test_params(self):
17 self.setup_clean_chain = True
18 self.num_nodes = 1
19 20 def skip_test_if_missing_module(self):
21 self.skip_if_no_ipc()
22 23 def setup_nodes(self):
24 self.extra_init = [{"ipcbind": True}]
25 super().setup_nodes()
26 27 def test_cli(self, args, error=None):
28 # Intentionally set wrong RPC password so only IPC not HTTP connections work
29 args = [self.binary_paths.bitcoincli, f"-datadir={self.nodes[0].datadir_path}", "-rpcpassword=wrong"] + args + ["echo", "foo"]
30 result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
31 if error is None:
32 assert_equal(result.stdout, '[\n "foo"\n]\n')
33 else:
34 assert result.stdout.startswith(error), f"Output didn't start with the expected error {error!r}:\n{result.stdout}"
35 assert_equal(result.stderr, None)
36 assert_equal(result.returncode, 0 if error is None else 1)
37 38 def run_test(self):
39 node = self.nodes[0]
40 if node.ipc_tmp_dir:
41 self.log.info("Skipping a few checks because temporary directory path is too long")
42 43 http_auth_error = "error: Authorization failed: Incorrect rpcuser or rpcpassword were specified."
44 http_connect_error = f"error: Error while attempting to communicate with server 127.0.0.1:{rpc_port(node.index)} (Could not connect to the server)\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"
45 ipc_connect_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"
46 ipc_http_conflict = "error: -rpcconnect and -ipcconnect options cannot both be enabled\n"
47 48 for started in (True, False):
49 auto_error = None if started else http_connect_error
50 http_error = http_auth_error if started else http_connect_error
51 ipc_error = None if started else ipc_connect_error
52 53 if not node.ipc_tmp_dir:
54 self.test_cli([], auto_error)
55 self.test_cli(["-rpcconnect=127.0.0.1"], http_error)
56 self.test_cli(["-ipcconnect=auto"], auto_error)
57 self.test_cli(["-ipcconnect=auto", "-rpcconnect=127.0.0.1"], http_error)
58 self.test_cli(["-ipcconnect=unix"], ipc_error)
59 60 self.test_cli([f"-ipcconnect=unix:{node.ipc_socket_path}"], ipc_error)
61 self.test_cli(["-noipcconnect"], http_error)
62 self.test_cli(["-ipcconnect=unix", "-rpcconnect=127.0.0.1"], ipc_http_conflict)
63 64 self.stop_node(0)
65 66 67 if __name__ == '__main__':
68 TestBitcoinIpcCli(__file__).main()
69