The testing framework will add -bind=0.0.0.0
, we want to test this. But we also want to test if no -bind
is given at all. Here is a patch to do that and also to test -whitebind=0.0.0.0
:
0--- i/test/functional/feature_bind_port_discover.py
1+++ w/test/functional/feature_bind_port_discover.py
2@@ -26,23 +26,47 @@ from test_framework.util import (
3 ADDR1 = '1.1.1.1'
4 ADDR2 = '2.2.2.2'
5
6 class BindPortDiscoverTest(BitcoinTestFramework):
7 def set_test_params(self):
8 # Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1.
9- self.setup_clean_chain = True
10 self.bind_to_localhost_only = False
11 # Get dynamic ports for each node from the test framework
12- self.bind_port_0 = p2p_port(0)
13- self.bind_port_1 = p2p_port(1)
14+ self.bind_ports = [
15+ p2p_port(0),
16+ p2p_port(2), # node0 will use their port + 1 for onion listen, which is the same as p2p_port(1), so avoid collision
17+ p2p_port(3),
18+ p2p_port(4),
19+ ]
20 self.extra_args = [
21- ['-discover', f'-port={self.bind_port_0}'], # bind on any
22- ['-discover', f'-bind={ADDR1}:{self.bind_port_1}'],
23+ ['-discover', f'-port={self.bind_ports[0]}', '-listen=1'], # Without any -bind
24+ ['-discover', f'-bind=0.0.0.0:{self.bind_ports[1]}'], # Explicit -bind=0.0.0.0
25+ ['-discover', f'-whitebind=0.0.0.0:{self.bind_ports[2]}'], # Explicit -whitebind=0.0.0.0
26+ ['-discover', f'-bind={ADDR1}:{self.bind_ports[3]}'], # Explicit -bind=routable_addr
27 ]
28 self.num_nodes = len(self.extra_args)
29
30+ def setup_network(self):
31+ """
32+ BitcoinTestFramework.setup_network() without connecting node0 to node1,
33+ we don't need that and avoiding it makes the test faster.
34+ """
35+ self.setup_nodes()
36+
37+ def setup_nodes(self):
38+ """
39+ BitcoinTestFramework.setup_nodes() with overriden has_explicit_bind=True
40+ for each node and without the chain setup.
41+ """
42+ self.add_nodes(self.num_nodes, self.extra_args)
43+ # TestNode.start() will add -bind= to extra_args if has_explicit_bind is
44+ # False. We do not want any -bind= thus set has_explicit_bind to True.
45+ for node in self.nodes:
46+ node.has_explicit_bind = True
47+ self.start_nodes()
48+
49 def add_options(self, parser):
50 parser.add_argument(
51 "--ihave1111and2222", action='store_true', dest="ihave1111and2222",
52 help=f"Run the test, assuming {ADDR1} and {ADDR2} are configured on the machine",
53 default=False)
54
55@@ -51,33 +75,39 @@ class BindPortDiscoverTest(BitcoinTestFramework):
56 raise SkipTest(
57 f"To run this test make sure that {ADDR1} and {ADDR2} (routable addresses) are "
58 "assigned to the interfaces on this machine and rerun with --ihave1111and2222")
59
60 def run_test(self):
61 self.log.info(
62- "Test that if -bind= is not passed then all addresses are "
63+ "Test that if -bind= is not passed or -bind=0.0.0.0 is used then all addresses are "
64 "added to localaddresses")
65- found_addr1 = False
66- found_addr2 = False
67- for local in self.nodes[0].getnetworkinfo()['localaddresses']:
68- if local['address'] == ADDR1:
69- found_addr1 = True
70- assert_equal(local['port'], self.bind_port_0)
71- if local['address'] == ADDR2:
72- found_addr2 = True
73- assert_equal(local['port'], self.bind_port_0)
74- assert found_addr1
75- assert found_addr2
76+ for i in [0, 1, 2]:
77+ found_addr1 = False
78+ found_addr2 = False
79+ for local in self.nodes[i].getnetworkinfo()['localaddresses']:
80+ if local['address'] == ADDR1:
81+ found_addr1 = True
82+ assert_equal(local['port'], self.bind_ports[i])
83+ if local['address'] == ADDR2:
84+ found_addr2 = True
85+ assert_equal(local['port'], self.bind_ports[i])
86+ if not found_addr1:
87+ self.log.error(f"Address {ADDR1} not found in node{i}'s local addresses: {local}")
88+ assert False
89+ if not found_addr2:
90+ self.log.error(f"Address {ADDR2} not found in node{i}'s local addresses: {local}")
91+ assert False
92
93 self.log.info(
94- "Test that if -bind= is passed then only that address is "
95+ "Test that if -bind=routable_addr is passed then only that address is "
96 "added to localaddresses")
97 found_addr1 = False
98- for local in self.nodes[1].getnetworkinfo()['localaddresses']:
99+ i = 3
100+ for local in self.nodes[i].getnetworkinfo()['localaddresses']:
101 if local['address'] == ADDR1:
102 found_addr1 = True
103- assert_equal(local['port'], self.bind_port_1)
104+ assert_equal(local['port'], self.bind_ports[i])
105 assert_not_equal(local['address'], ADDR2)
106 assert found_addr1
107
108 if __name__ == '__main__':
109 BindPortDiscoverTest(__file__).main()