|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2014-2021 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 | +""" |
| 6 | +Test starting bitcoind with -bind and/or -bind=...=onion and confirm |
| 7 | +that bind happens on the expected ports. |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | + |
| 12 | +from test_framework.netutil import ( |
| 13 | + addr_to_hex, |
| 14 | + get_bind_addrs, |
| 15 | +) |
| 16 | +from test_framework.test_framework import ( |
| 17 | + BitcoinTestFramework, |
| 18 | + SkipTest, |
| 19 | +) |
| 20 | +from test_framework.util import ( |
| 21 | + PORT_MIN, |
| 22 | + PORT_RANGE, |
| 23 | + assert_equal, |
| 24 | + rpc_port, |
| 25 | +) |
| 26 | + |
| 27 | +class BindExtraTest(BitcoinTestFramework): |
| 28 | + def set_test_params(self): |
| 29 | + self.setup_clean_chain = True |
| 30 | + # Avoid any -bind= on the command line. Force the framework to avoid |
| 31 | + # adding -bind=127.0.0.1. |
| 32 | + self.bind_to_localhost_only = False |
| 33 | + self.num_nodes = 2 |
| 34 | + |
| 35 | + def setup_network(self): |
| 36 | + # Override setup_network() because we want to put the result of |
| 37 | + # p2p_port() in self.extra_args[], before the nodes are started. |
| 38 | + # p2p_port() is not usable in set_test_params() because PortSeed.n is |
| 39 | + # not set at that time. |
| 40 | + |
| 41 | + # Due to OS-specific network stats queries, we only run on Linux. |
| 42 | + self.log.info("Checking for Linux") |
| 43 | + if not sys.platform.startswith('linux'): |
| 44 | + raise SkipTest("This test can only be run on Linux.") |
| 45 | + |
| 46 | + loopback_ipv4 = addr_to_hex("127.0.0.1") |
| 47 | + |
| 48 | + # Start custom ports after p2p and rpc ports. |
| 49 | + port = PORT_MIN + 2 * PORT_RANGE |
| 50 | + |
| 51 | + # Array of tuples [command line arguments, expected bind addresses]. |
| 52 | + self.expected = [] |
| 53 | + |
| 54 | + # Node0, no normal -bind=... with -bind=...=onion, thus only the tor target. |
| 55 | + self.expected.append( |
| 56 | + [ |
| 57 | + [f"-bind=127.0.0.1:{port}=onion"], |
| 58 | + [(loopback_ipv4, port)] |
| 59 | + ], |
| 60 | + ) |
| 61 | + port += 1 |
| 62 | + |
| 63 | + # Node1, both -bind=... and -bind=...=onion. |
| 64 | + self.expected.append( |
| 65 | + [ |
| 66 | + [f"-bind=127.0.0.1:{port}", f"-bind=127.0.0.1:{port + 1}=onion"], |
| 67 | + [(loopback_ipv4, port), (loopback_ipv4, port + 1)] |
| 68 | + ], |
| 69 | + ) |
| 70 | + port += 2 |
| 71 | + |
| 72 | + self.extra_args = list(map(lambda e: e[0], self.expected)) |
| 73 | + self.add_nodes(self.num_nodes, self.extra_args) |
| 74 | + # Don't start the nodes, as some of them would collide trying to bind on the same port. |
| 75 | + |
| 76 | + def run_test(self): |
| 77 | + for i in range(len(self.expected)): |
| 78 | + self.log.info(f"Starting node {i} with {self.expected[i][0]}") |
| 79 | + self.start_node(i) |
| 80 | + pid = self.nodes[i].process.pid |
| 81 | + binds = set(get_bind_addrs(pid)) |
| 82 | + # Remove IPv6 addresses because on some CI environments "::1" is not configured |
| 83 | + # on the system (so our test_ipv6_local() would return False), but it is |
| 84 | + # possible to bind on "::". This makes it unpredictable whether to expect |
| 85 | + # that bitcoind has bound on "::1" (for RPC) and "::" (for P2P). |
| 86 | + ipv6_addr_len_bytes = 32 |
| 87 | + binds = set(filter(lambda e: len(e[0]) != ipv6_addr_len_bytes, binds)) |
| 88 | + # Remove RPC ports. They are not relevant for this test. |
| 89 | + binds = set(filter(lambda e: e[1] != rpc_port(i), binds)) |
| 90 | + assert_equal(binds, set(self.expected[i][1])) |
| 91 | + self.stop_node(i) |
| 92 | + self.log.info(f"Stopped node {i}") |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + BindExtraTest().main() |
0 commit comments