feature_bind_port_externalip.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2020-2021 The Limenka 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 the proper port is used for -externalip=
   7  """
   8  
   9  from test_framework.test_framework import LimenkaTestFramework, SkipTest
  10  from test_framework.util import assert_equal, p2p_port
  11  
  12  # We need to bind to a routable address for this test to exercise the relevant code.
  13  # To set a routable address on the machine use:
  14  # Linux:
  15  # ifconfig lo:0 1.1.1.1/32 up  # to set up
  16  # ifconfig lo:0 down  # to remove it, after the test
  17  # FreeBSD:
  18  # ifconfig lo0 1.1.1.1/32 alias  # to set up
  19  # ifconfig lo0 1.1.1.1 -alias  # to remove it, after the test
  20  ADDR = '1.1.1.1'
  21  
  22  # array of tuples [arguments, expected port in localaddresses]
  23  EXPECTED = [
  24      [['-externalip=2.2.2.2',       '-port=30001'],                        30001],
  25      [['-externalip=2.2.2.2',       '-port=30002', f'-bind={ADDR}'],       30002],
  26      [['-externalip=2.2.2.2',                      f'-bind={ADDR}'],       'default_p2p_port'],
  27      [['-externalip=2.2.2.2',       '-port=30003', f'-bind={ADDR}:30004'], 30004],
  28      [['-externalip=2.2.2.2',                      f'-bind={ADDR}:30005'], 30005],
  29      [['-externalip=2.2.2.2:30006', '-port=30007'],                        30006],
  30      [['-externalip=2.2.2.2:30008', '-port=30009', f'-bind={ADDR}'],       30008],
  31      [['-externalip=2.2.2.2:30010',                f'-bind={ADDR}'],       30010],
  32      [['-externalip=2.2.2.2:30011', '-port=30012', f'-bind={ADDR}:30013'], 30011],
  33      [['-externalip=2.2.2.2:30014',                f'-bind={ADDR}:30015'], 30014],
  34      [['-externalip=2.2.2.2',       '-port=30016', f'-bind={ADDR}:30017',
  35                                               f'-whitebind={ADDR}:30018'], 30017],
  36      [['-externalip=2.2.2.2',       '-port=30019',
  37                                               f'-whitebind={ADDR}:30020'], 30020],
  38  ]
  39  
  40  class BindPortExternalIPTest(LimenkaTestFramework):
  41      def set_test_params(self):
  42          # Avoid any -bind= on the command line. Force the framework to avoid adding -bind=127.0.0.1.
  43          self.setup_clean_chain = True
  44          self.bind_to_localhost_only = False
  45          self.num_nodes = len(EXPECTED)
  46          self.extra_args = list(map(lambda e: e[0], EXPECTED))
  47  
  48      def add_options(self, parser):
  49          parser.add_argument(
  50              "--ihave1111", action='store_true', dest="ihave1111",
  51              help=f"Run the test, assuming {ADDR} is configured on the machine",
  52              default=False)
  53  
  54      def skip_test_if_missing_module(self):
  55          if not self.options.ihave1111:
  56              raise SkipTest(
  57                  f"To run this test make sure that {ADDR} (a routable address) is assigned "
  58                  "to one of the interfaces on this machine and rerun with --ihave1111")
  59  
  60      def run_test(self):
  61          self.log.info("Test the proper port is used for -externalip=")
  62          for i in range(len(EXPECTED)):
  63              expected_port = EXPECTED[i][1]
  64              if expected_port == 'default_p2p_port':
  65                  expected_port = p2p_port(i)
  66              found = False
  67              for local in self.nodes[i].getnetworkinfo()['localaddresses']:
  68                  if local['address'] == '2.2.2.2':
  69                      assert_equal(local['port'], expected_port)
  70                      found = True
  71                      break
  72              assert found
  73  
  74  if __name__ == '__main__':
  75      BindPortExternalIPTest(__file__).main()
  76