You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpcbind_test.py 4.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 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. """Test running bitcoind with the -rpcbind and -rpcallowip options."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. from test_framework.netutil import *
  9. class RPCBindTest(BitcoinTestFramework):
  10. def __init__(self):
  11. super().__init__()
  12. self.setup_clean_chain = True
  13. self.num_nodes = 1
  14. def setup_network(self):
  15. pass
  16. def setup_nodes(self):
  17. pass
  18. def run_bind_test(self, allow_ips, connect_to, addresses, expected):
  19. '''
  20. Start a node with requested rpcallowip and rpcbind parameters,
  21. then try to connect, and check if the set of bound addresses
  22. matches the expected set.
  23. '''
  24. expected = [(addr_to_hex(addr), port) for (addr, port) in expected]
  25. base_args = ['-disablewallet', '-nolisten']
  26. if allow_ips:
  27. base_args += ['-rpcallowip=' + x for x in allow_ips]
  28. binds = ['-rpcbind='+addr for addr in addresses]
  29. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, [base_args + binds], connect_to)
  30. pid = bitcoind_processes[0].pid
  31. assert_equal(set(get_bind_addrs(pid)), set(expected))
  32. stop_nodes(self.nodes)
  33. def run_allowip_test(self, allow_ips, rpchost, rpcport):
  34. '''
  35. Start a node with rpcallow IP, and request getnetworkinfo
  36. at a non-localhost IP.
  37. '''
  38. base_args = ['-disablewallet', '-nolisten'] + ['-rpcallowip='+x for x in allow_ips]
  39. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, [base_args])
  40. # connect to node through non-loopback interface
  41. node = get_rpc_proxy(rpc_url(0, "%s:%d" % (rpchost, rpcport)), 0)
  42. node.getnetworkinfo()
  43. stop_nodes(self.nodes)
  44. def run_test(self):
  45. # due to OS-specific network stats queries, this test works only on Linux
  46. assert(sys.platform.startswith('linux'))
  47. # find the first non-loopback interface for testing
  48. non_loopback_ip = None
  49. for name,ip in all_interfaces():
  50. if ip != '127.0.0.1':
  51. non_loopback_ip = ip
  52. break
  53. if non_loopback_ip is None:
  54. assert(not 'This test requires at least one non-loopback IPv4 interface')
  55. self.log.info("Using interface %s for testing" % non_loopback_ip)
  56. defaultport = rpc_port(0)
  57. # check default without rpcallowip (IPv4 and IPv6 localhost)
  58. self.run_bind_test(None, '127.0.0.1', [],
  59. [('127.0.0.1', defaultport), ('::1', defaultport)])
  60. # check default with rpcallowip (IPv6 any)
  61. self.run_bind_test(['127.0.0.1'], '127.0.0.1', [],
  62. [('::0', defaultport)])
  63. # check only IPv4 localhost (explicit)
  64. self.run_bind_test(['127.0.0.1'], '127.0.0.1', ['127.0.0.1'],
  65. [('127.0.0.1', defaultport)])
  66. # check only IPv4 localhost (explicit) with alternative port
  67. self.run_bind_test(['127.0.0.1'], '127.0.0.1:32171', ['127.0.0.1:32171'],
  68. [('127.0.0.1', 32171)])
  69. # check only IPv4 localhost (explicit) with multiple alternative ports on same host
  70. self.run_bind_test(['127.0.0.1'], '127.0.0.1:32171', ['127.0.0.1:32171', '127.0.0.1:32172'],
  71. [('127.0.0.1', 32171), ('127.0.0.1', 32172)])
  72. # check only IPv6 localhost (explicit)
  73. self.run_bind_test(['[::1]'], '[::1]', ['[::1]'],
  74. [('::1', defaultport)])
  75. # check both IPv4 and IPv6 localhost (explicit)
  76. self.run_bind_test(['127.0.0.1'], '127.0.0.1', ['127.0.0.1', '[::1]'],
  77. [('127.0.0.1', defaultport), ('::1', defaultport)])
  78. # check only non-loopback interface
  79. self.run_bind_test([non_loopback_ip], non_loopback_ip, [non_loopback_ip],
  80. [(non_loopback_ip, defaultport)])
  81. # Check that with invalid rpcallowip, we are denied
  82. self.run_allowip_test([non_loopback_ip], non_loopback_ip, defaultport)
  83. assert_raises_jsonrpc(-342, "non-JSON HTTP response with '403 Forbidden' from server", self.run_allowip_test, ['1.1.1.1'], non_loopback_ip, defaultport)
  84. if __name__ == '__main__':
  85. RPCBindTest().main()