Skip to content

Commit

Permalink
test: Disable automatic connections by default
Browse files Browse the repository at this point in the history
Summary:
```
This prevents the node from trying to connect to random IPs on the internet while running the functional tests. Exceptions are added when required for the test to pass.
```

Backport of [[bitcoin/bitcoin#22490 | core#22490]].

Ref T1696.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Maniphest Tasks: T1696

Differential Revision: https://reviews.bitcoinabc.org/D10966
  • Loading branch information
mzumsande authored and Fabcien committed Feb 2, 2022
1 parent 2969092 commit c58c98c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions test/functional/feature_anchors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def check_node_connections(*, node, num_in, num_out):
class AnchorsTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.disable_autoconnect = False

def setup_network(self):
self.setup_nodes()
Expand Down
1 change: 1 addition & 0 deletions test/functional/feature_config_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def set_test_params(self):
self.num_nodes = 1
self.supports_cli = False
self.wallet_names = []
self.disable_autoconnect = False

def test_config_file_parser(self):
self.stop_node(0)
Expand Down
18 changes: 15 additions & 3 deletions test/functional/test_framework/test_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ def __init__(self):
# skipped. If list is truncated, wallet creation is skipped and keys
# are not imported.
self.wallet_names = None
# Disable ThreadOpenConnections by default, so that adding entries to
# addrman will not result in automatic connections to them.
self.disable_autoconnect = True
self.set_test_params()
if self.options.timeout_factor == 0:
self.options.timeout_factor = 99999
Expand Down Expand Up @@ -722,7 +725,8 @@ def _initialize_chain(self):
initialize_datadir(
self.options.cachedir,
CACHE_NODE_ID,
self.chain)
self.chain,
self.disable_autoconnect)
self.nodes.append(
TestNode(
CACHE_NODE_ID,
Expand Down Expand Up @@ -793,15 +797,23 @@ def cache_path(*paths):
to_dir = get_datadir_path(self.options.tmpdir, i)
shutil.copytree(cache_node_dir, to_dir)
# Overwrite port/rpcport in bitcoin.conf
initialize_datadir(self.options.tmpdir, i, self.chain)
initialize_datadir(
self.options.tmpdir,
i,
self.chain,
self.disable_autoconnect)

def _initialize_chain_clean(self):
"""Initialize empty blockchain for use by the test.
Create an empty blockchain and num_nodes wallets.
Useful if a test case wants complete control over initialization."""
for i in range(self.num_nodes):
initialize_datadir(self.options.tmpdir, i, self.chain)
initialize_datadir(
self.options.tmpdir,
i,
self.chain,
self.disable_autoconnect)

def skip_if_no_py3_zmq(self):
"""Attempt to import the zmq package and skip the test if the import fails."""
Expand Down
4 changes: 3 additions & 1 deletion test/functional/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def rpc_url(datadir, chain, host, port):
################


def initialize_datadir(dirname, n, chain):
def initialize_datadir(dirname, n, chain, disable_autoconnect=True):
datadir = get_datadir_path(dirname, n)
if not os.path.isdir(datadir):
os.makedirs(datadir)
Expand Down Expand Up @@ -385,6 +385,8 @@ def initialize_datadir(dirname, n, chain):
# tests.
f.write("peertimeout=999999\n")
f.write("shrinkdebugfile=0\n")
if disable_autoconnect:
f.write("connect=0\n")
os.makedirs(os.path.join(datadir, 'stderr'), exist_ok=True)
os.makedirs(os.path.join(datadir, 'stdout'), exist_ok=True)
return datadir
Expand Down

0 comments on commit c58c98c

Please sign in to comment.