From 773a16daa39b87cb47b0ecec14b74d78032f87dd Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 7 Mar 2018 14:51:58 +0100 Subject: [PATCH] Merge #12482: [tests] bind functional test nodes to 127.0.0.1 b156ff7c3 [tests] bind functional test nodes to 127.0.0.1 (Sjors Provoost) Pull request description: Replaces #12200 which broke `rpc_bind.py`. Prevents OSX firewall allow-this-application-to-accept-inbound-connections permission popups and is generally safer. To prevent binding to `127.0.0.1`, set `self.bind_to_localhost_only = False`. cc @jnewbery Tree-SHA512: 5e700124c91bd0cbdee83ca44910071d71d61d8842334755b685d14fbff6454d75de1ea7de67340370386f58b41361e80e90bb4dca5c4d5992f9d2b27985f999 --- test/functional/import-rescan.py | 2 +- test/functional/rpcbind_test.py | 1 + test/functional/test_framework/test_framework.py | 11 ++++++++--- test/functional/test_framework/test_node.py | 8 ++++++-- test/functional/test_framework/util.py | 6 ++++++ test/functional/wallet-dump.py | 2 +- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/test/functional/import-rescan.py b/test/functional/import-rescan.py index bd33daa7f3..d36045bb29 100755 --- a/test/functional/import-rescan.py +++ b/test/functional/import-rescan.py @@ -126,7 +126,7 @@ class ImportRescanTest(BitcoinTestFramework): # txindex is enabled by default in Dash and needs to be disabled for import-rescan.py extra_args[i] += ["-prune=1", "-txindex=0", "-reindex"] - self.add_nodes(self.num_nodes, extra_args, stderr=sys.stdout) + self.add_nodes(self.num_nodes, extra_args=extra_args, stderr=sys.stdout) self.start_nodes() for i in range(1, self.num_nodes): connect_nodes(self.nodes[i], 0) diff --git a/test/functional/rpcbind_test.py b/test/functional/rpcbind_test.py index 14b9a4c001..75c3522a35 100755 --- a/test/functional/rpcbind_test.py +++ b/test/functional/rpcbind_test.py @@ -14,6 +14,7 @@ from test_framework.netutil import * class RPCBindTest(BitcoinTestFramework): def set_test_params(self): self.setup_clean_chain = True + self.bind_to_localhost_only = False self.num_nodes = 1 def setup_network(self): diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index b3d059593f..08a33d983c 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -73,6 +73,7 @@ class BitcoinTestFramework(): self.nodes = [] self.mocktime = 0 self.supports_cli = False + self.bind_to_localhost_only = True self.extra_args_from_options = [] self.set_test_params() @@ -241,16 +242,20 @@ class BitcoinTestFramework(): def add_nodes(self, num_nodes, extra_args=None, rpchost=None, timewait=None, binary=None, stderr=None): """Instantiate TestNode objects""" - + if self.bind_to_localhost_only: + extra_confs = [["bind=127.0.0.1"]] * num_nodes + else: + extra_confs = [[]] * num_nodes if extra_args is None: extra_args = [[]] * num_nodes if binary is None: binary = [None] * num_nodes + assert_equal(len(extra_confs), num_nodes) assert_equal(len(extra_args), num_nodes) assert_equal(len(binary), num_nodes) old_num_nodes = len(self.nodes) for i in range(num_nodes): - self.nodes.append(TestNode(old_num_nodes + i, self.options.tmpdir, extra_args[i], self.extra_args_from_options, rpchost, timewait=timewait, binary=binary[i], stderr=stderr, mocktime=self.mocktime, coverage_dir=self.options.coveragedir, use_cli=self.options.usecli)) + self.nodes.append(TestNode(old_num_nodes + i, self.options.tmpdir, extra_args[i], self.extra_args_from_options, rpchost=rpchost, timewait=timewait, binary=binary[i], stderr=stderr, mocktime=self.mocktime, coverage_dir=self.options.coveragedir, extra_conf=extra_confs[i], extra_args=extra_args[i], use_cli=self.options.usecli)) def start_node(self, i, *args, **kwargs): """Start a dashd""" @@ -442,7 +447,7 @@ class BitcoinTestFramework(): args.append("-connect=127.0.0.1:" + str(p2p_port(0))) if extra_args is not None: args.extend(extra_args) - self.nodes.append(TestNode(i, self.options.cachedir, extra_args=[], extra_args_from_options=self.extra_args_from_options, rpchost=None, timewait=None, binary=None, stderr=stderr, mocktime=self.mocktime, coverage_dir=None)) + self.nodes.append(TestNode(i, self.options.cachedir, extra_conf=["bind=127.0.0.1"], extra_args=[],extra_args_from_options=self.extra_args_from_options, rpchost=None, timewait=None, binary=None, stderr=stderr, mocktime=self.mocktime, coverage_dir=None)) self.nodes[i].args = args self.start_node(i) diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index cf98ed5264..2f4ef9f94f 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -18,6 +18,7 @@ import time from .authproxy import JSONRPCException from .messages import MY_SUBVERSION from .util import ( + append_config, assert_equal, delete_cookie_file, get_rpc_proxy, @@ -45,7 +46,7 @@ class TestNode(): To make things easier for the test writer, any unrecognised messages will be dispatched to the RPC connection.""" - def __init__(self, i, dirname, extra_args, extra_args_from_options, rpchost, timewait, binary, stderr, mocktime, coverage_dir, use_cli=False): + def __init__(self, i, dirname, extra_args_from_options, rpchost, timewait, binary, stderr, mocktime, coverage_dir, extra_conf=None, extra_args=None, use_cli=False): self.index = i self.datadir = os.path.join(dirname, "node" + str(i)) self.rpchost = rpchost @@ -61,7 +62,10 @@ class TestNode(): self.stderr = stderr self.coverage_dir = coverage_dir self.mocktime = mocktime - # Most callers will just need to add extra args to the standard list below. For those callers that need more flexibility, they can just set the args property directly. + if extra_conf != None: + append_config(dirname, i, extra_conf) + # Most callers will just need to add extra args to the standard list below. + # For those callers that need more flexibity, they can just set the args property directly. self.extra_args = extra_args self.extra_args_from_options = extra_args_from_options self.args = [self.binary, "-datadir=" + self.datadir, "-server", "-keypool=1", "-discover=0", "-rest", "-logtimemicros", "-debug", "-debugexclude=libevent", "-debugexclude=leveldb", "-mocktime=" + str(mocktime), "-uacomment=testnode%d" % i] diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index e8727fcb6d..9edc2cb5b6 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -312,6 +312,12 @@ def initialize_datadir(dirname, n): def get_datadir_path(dirname, n): return os.path.join(dirname, "node" + str(n)) +def append_config(dirname, n, options): + datadir = get_datadir_path(dirname, n) + with open(os.path.join(datadir, "bitcoin.conf"), 'a', encoding='utf8') as f: + for option in options: + f.write(option + "\n") + def get_auth_cookie(datadir): user = None password = None diff --git a/test/functional/wallet-dump.py b/test/functional/wallet-dump.py index 50c8cf2d5e..e99da7a8cb 100755 --- a/test/functional/wallet-dump.py +++ b/test/functional/wallet-dump.py @@ -86,7 +86,7 @@ class WalletDumpTest(BitcoinTestFramework): # longer than the default 30 seconds due to an expensive # CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in # the test often takes even longer. - self.add_nodes(self.num_nodes, self.extra_args, timewait=60, stderr=sys.stdout) + self.add_nodes(self.num_nodes, extra_args=self.extra_args, timewait=60, stderr=sys.stdout) self.start_nodes() def run_test (self):