/ test / functional / feature_bind_port_discover.py
feature_bind_port_discover.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2020-2021 The Bitcoin Core developers
 3  # Distributed under the MIT software license, see the accompanying
 4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
 5  """
 6  Test that -discover does not add all interfaces' addresses if we listen on only some of them
 7  """
 8  
 9  from test_framework.test_framework import BitcoinTestFramework, SkipTest
10  from test_framework.util import assert_equal
11  
12  # We need to bind to a routable address for this test to exercise the relevant code
13  # and also must have another routable address on another interface which must not
14  # be named "lo" or "lo0".
15  # To set these routable addresses on the machine, use:
16  # Linux:
17  # ifconfig lo:0 1.1.1.1/32 up && ifconfig lo:1 2.2.2.2/32 up  # to set up
18  # ifconfig lo:0 down && ifconfig lo:1 down  # to remove it, after the test
19  # FreeBSD:
20  # ifconfig em0 1.1.1.1/32 alias && ifconfig wlan0 2.2.2.2/32 alias  # to set up
21  # ifconfig em0 1.1.1.1 -alias && ifconfig wlan0 2.2.2.2 -alias  # to remove it, after the test
22  ADDR1 = '1.1.1.1'
23  ADDR2 = '2.2.2.2'
24  
25  BIND_PORT = 31001
26  
27  class BindPortDiscoverTest(BitcoinTestFramework):
28      def set_test_params(self):
29          # Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1.
30          self.setup_clean_chain = True
31          self.bind_to_localhost_only = False
32          self.extra_args = [
33              ['-discover', f'-port={BIND_PORT}'], # bind on any
34              ['-discover', f'-bind={ADDR1}:{BIND_PORT}'],
35          ]
36          self.num_nodes = len(self.extra_args)
37  
38      def add_options(self, parser):
39          parser.add_argument(
40              "--ihave1111and2222", action='store_true', dest="ihave1111and2222",
41              help=f"Run the test, assuming {ADDR1} and {ADDR2} are configured on the machine",
42              default=False)
43  
44      def skip_test_if_missing_module(self):
45          if not self.options.ihave1111and2222:
46              raise SkipTest(
47                  f"To run this test make sure that {ADDR1} and {ADDR2} (routable addresses) are "
48                  "assigned to the interfaces on this machine and rerun with --ihave1111and2222")
49  
50      def run_test(self):
51          self.log.info(
52                  "Test that if -bind= is not passed then all addresses are "
53                  "added to localaddresses")
54          found_addr1 = False
55          found_addr2 = False
56          for local in self.nodes[0].getnetworkinfo()['localaddresses']:
57              if local['address'] == ADDR1:
58                  found_addr1 = True
59                  assert_equal(local['port'], BIND_PORT)
60              if local['address'] == ADDR2:
61                  found_addr2 = True
62                  assert_equal(local['port'], BIND_PORT)
63          assert found_addr1
64          assert found_addr2
65  
66          self.log.info(
67                  "Test that if -bind= is passed then only that address is "
68                  "added to localaddresses")
69          found_addr1 = False
70          for local in self.nodes[1].getnetworkinfo()['localaddresses']:
71              if local['address'] == ADDR1:
72                  found_addr1 = True
73                  assert_equal(local['port'], BIND_PORT)
74              assert local['address'] != ADDR2
75          assert found_addr1
76  
77  if __name__ == '__main__':
78      BindPortDiscoverTest().main()