Skip to content

Commit

Permalink
[tests] bind functional test nodes to 127.0.0.1 Prevents OSX firewall
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sjors authored and furszy committed Aug 10, 2021
1 parent 6a4f1e0 commit 61a08af
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions test/functional/rpc_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class RPCBindTest(PivxTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.bind_to_localhost_only = False
self.num_nodes = 1

def setup_network(self):
Expand Down
11 changes: 8 additions & 3 deletions test/functional/test_framework/test_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(self):
self.mocktime = 0
self.rpc_timewait = 600 # Wait for up to 600 seconds for the RPC server to respond
self.supports_cli = False
self.bind_to_localhost_only = True
self.set_test_params()

assert hasattr(self, "num_nodes"), "Test must set self.num_nodes in set_test_params()"
Expand Down Expand Up @@ -272,7 +273,10 @@ def run_test(self):

def add_nodes(self, num_nodes, extra_args=None, *, rpchost=None, binary=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
# Check wallet version
Expand All @@ -282,10 +286,11 @@ def add_nodes(self, num_nodes, extra_args=None, *, rpchost=None, binary=None):
self.log.info("Running test with legacy (pre-HD) wallet")
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)
for i in range(num_nodes):
self.nodes.append(TestNode(i, self.options.tmpdir, extra_args[i], rpchost, timewait=self.rpc_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=self.rpc_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):
"""Start a pivxd"""
Expand Down Expand Up @@ -531,7 +536,7 @@ def start_nodes_from_dir(ddir, num_nodes=MAX_NODES):
args = [os.getenv("BITCOIND", "pivxd"), "-spendzeroconfchange=1", "-server", "-keypool=1",
"-datadir=" + datadir, "-discover=0"]
self.nodes.append(
TestNode(i, ddir, extra_args=[], rpchost=None, timewait=self.rpc_timewait, binary=None, stderr=None,
TestNode(i, ddir, extra_conf=["bind=127.0.0.1"], extra_args=[], rpchost=None, timewait=self.rpc_timewait, binary=None, stderr=None,
mocktime=self.mocktime, coverage_dir=None))
self.nodes[i].args = args
self.start_node(i)
Expand Down
9 changes: 7 additions & 2 deletions test/functional/test_framework/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .authproxy import JSONRPCException
from .util import (
append_config,
assert_equal,
delete_cookie_file,
get_rpc_proxy,
Expand Down Expand Up @@ -53,7 +54,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, 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.datadir = os.path.join(dirname, "node" + str(i))
self.rpchost = rpchost
Expand All @@ -64,7 +65,11 @@ def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mo
self.binary = binary
self.stderr = stderr
self.coverage_dir = coverage_dir
# 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.
if extra_conf is not 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.
# Note that common args are set in the config file (see initialize_datadir)
self.extra_args = extra_args
self.args = [
self.binary,
Expand Down
6 changes: 6 additions & 0 deletions test/functional/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,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, "pivx.conf"), 'a', encoding='utf8') as f:
for option in options:
f.write(option + "\n")

def get_auth_cookie(datadir):
user = None
password = None
Expand Down
2 changes: 1 addition & 1 deletion test/functional/wallet_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def setup_network(self, split=False):
# 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)
self.add_nodes(self.num_nodes, extra_args=self.extra_args)
self.start_nodes()

def run_test (self):
Expand Down
2 changes: 1 addition & 1 deletion test/functional/wallet_import_rescan.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def setup_network(self):
if import_node.prune:
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()
for i in range(1, self.num_nodes):
connect_nodes(self.nodes[i], 0)
Expand Down

0 comments on commit 61a08af

Please sign in to comment.