[tests] bind functional test nodes to 127.0.0.1

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.
This commit is contained in:
Sjors Provoost 2018-03-06 16:48:15 -05:00
parent d3f4dd313e
commit b156ff7c30
No known key found for this signature in database
GPG key ID: 57FF9BDBCC301009
6 changed files with 21 additions and 6 deletions

View file

@ -14,6 +14,7 @@ from test_framework.netutil import *
class RPCBindTest(BitcoinTestFramework): class RPCBindTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.setup_clean_chain = True self.setup_clean_chain = True
self.bind_to_localhost_only = False
self.num_nodes = 1 self.num_nodes = 1
def setup_network(self): def setup_network(self):

View file

@ -63,6 +63,7 @@ class BitcoinTestFramework():
self.nodes = [] self.nodes = []
self.mocktime = 0 self.mocktime = 0
self.supports_cli = False self.supports_cli = False
self.bind_to_localhost_only = True
self.set_test_params() self.set_test_params()
assert hasattr(self, "num_nodes"), "Test must set self.num_nodes in set_test_params()" assert hasattr(self, "num_nodes"), "Test must set self.num_nodes in set_test_params()"
@ -215,15 +216,19 @@ class BitcoinTestFramework():
def add_nodes(self, num_nodes, extra_args=None, rpchost=None, timewait=None, binary=None): def add_nodes(self, num_nodes, extra_args=None, rpchost=None, timewait=None, binary=None):
"""Instantiate TestNode objects""" """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: if extra_args is None:
extra_args = [[]] * num_nodes extra_args = [[]] * num_nodes
if binary is None: if binary is None:
binary = [None] * num_nodes binary = [None] * num_nodes
assert_equal(len(extra_confs), num_nodes)
assert_equal(len(extra_args), num_nodes) assert_equal(len(extra_args), num_nodes)
assert_equal(len(binary), num_nodes) assert_equal(len(binary), num_nodes)
for i in range(num_nodes): for i in range(num_nodes):
self.nodes.append(TestNode(i, self.options.tmpdir, extra_args[i], rpchost, timewait=timewait, binary=binary[i], stderr=None, mocktime=self.mocktime, coverage_dir=self.options.coveragedir, use_cli=self.options.usecli)) self.nodes.append(TestNode(i, self.options.tmpdir, rpchost=rpchost, timewait=timewait, binary=binary[i], stderr=None, 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): def start_node(self, i, *args, **kwargs):
"""Start a bitcoind""" """Start a bitcoind"""
@ -395,7 +400,7 @@ class BitcoinTestFramework():
args = [os.getenv("BITCOIND", "bitcoind"), "-datadir=" + datadir] args = [os.getenv("BITCOIND", "bitcoind"), "-datadir=" + datadir]
if i > 0: if i > 0:
args.append("-connect=127.0.0.1:" + str(p2p_port(0))) args.append("-connect=127.0.0.1:" + str(p2p_port(0)))
self.nodes.append(TestNode(i, self.options.cachedir, extra_args=[], rpchost=None, timewait=None, binary=None, stderr=None, mocktime=self.mocktime, coverage_dir=None)) self.nodes.append(TestNode(i, self.options.cachedir, extra_conf=["bind=127.0.0.1"], extra_args=[],rpchost=None, timewait=None, binary=None, stderr=None, mocktime=self.mocktime, coverage_dir=None))
self.nodes[i].args = args self.nodes[i].args = args
self.start_node(i) self.start_node(i)

View file

@ -16,6 +16,7 @@ import time
from .authproxy import JSONRPCException from .authproxy import JSONRPCException
from .util import ( from .util import (
append_config,
assert_equal, assert_equal,
get_rpc_proxy, get_rpc_proxy,
rpc_url, rpc_url,
@ -42,7 +43,7 @@ class TestNode():
To make things easier for the test writer, any unrecognised messages will To make things easier for the test writer, any unrecognised messages will
be dispatched to the RPC connection.""" be dispatched to the RPC connection."""
def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mocktime, coverage_dir, use_cli=False): def __init__(self, i, dirname, rpchost, timewait, binary, stderr, mocktime, coverage_dir, extra_conf=None, extra_args=None, use_cli=False):
self.index = i self.index = i
self.datadir = os.path.join(dirname, "node" + str(i)) self.datadir = os.path.join(dirname, "node" + str(i))
self.rpchost = rpchost self.rpchost = rpchost
@ -57,6 +58,8 @@ class TestNode():
self.binary = binary self.binary = binary
self.stderr = stderr self.stderr = stderr
self.coverage_dir = coverage_dir self.coverage_dir = coverage_dir
if extra_conf != None:
append_config(dirname, i, extra_conf)
# Most callers will just need to add extra args to the standard list below. # 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. # For those callers that need more flexibity, they can just set the args property directly.
# Note that common args are set in the config file (see initialize_datadir) # Note that common args are set in the config file (see initialize_datadir)

View file

@ -300,6 +300,12 @@ def initialize_datadir(dirname, n):
def get_datadir_path(dirname, n): def get_datadir_path(dirname, n):
return os.path.join(dirname, "node" + str(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): def get_auth_cookie(datadir):
user = None user = None
password = None password = None

View file

@ -84,7 +84,7 @@ class WalletDumpTest(BitcoinTestFramework):
# longer than the default 30 seconds due to an expensive # longer than the default 30 seconds due to an expensive
# CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in # CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in
# the test often takes even longer. # the test often takes even longer.
self.add_nodes(self.num_nodes, self.extra_args, timewait=60) self.add_nodes(self.num_nodes, extra_args=self.extra_args, timewait=60)
self.start_nodes() self.start_nodes()
def run_test (self): def run_test (self):

View file

@ -124,7 +124,7 @@ class ImportRescanTest(BitcoinTestFramework):
if import_node.prune: if import_node.prune:
extra_args[i] += ["-prune=1"] extra_args[i] += ["-prune=1"]
self.add_nodes(self.num_nodes, extra_args) self.add_nodes(self.num_nodes, extra_args=extra_args)
self.start_nodes() self.start_nodes()
for i in range(1, self.num_nodes): for i in range(1, self.num_nodes):
connect_nodes(self.nodes[i], 0) connect_nodes(self.nodes[i], 0)