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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014-2015 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 for -rpcbind, as well as -rpcallowip and -rpcconnect
  6. # Add python-bitcoinrpc to module search path:
  7. import os
  8. import sys
  9. import json
  10. import shutil
  11. import subprocess
  12. import tempfile
  13. import traceback
  14. from test_framework.util import *
  15. from test_framework.netutil import *
  16. def run_bind_test(tmpdir, allow_ips, connect_to, addresses, expected):
  17. '''
  18. Start a node with requested rpcallowip and rpcbind parameters,
  19. then try to connect, and check if the set of bound addresses
  20. matches the expected set.
  21. '''
  22. expected = [(addr_to_hex(addr), port) for (addr, port) in expected]
  23. base_args = ['-disablewallet', '-nolisten']
  24. if allow_ips:
  25. base_args += ['-rpcallowip=' + x for x in allow_ips]
  26. binds = ['-rpcbind='+addr for addr in addresses]
  27. nodes = start_nodes(1, tmpdir, [base_args + binds], connect_to)
  28. try:
  29. pid = bitcoind_processes[0].pid
  30. assert_equal(set(get_bind_addrs(pid)), set(expected))
  31. finally:
  32. stop_nodes(nodes)
  33. wait_bitcoinds()
  34. def run_allowip_test(tmpdir, allow_ips, rpchost, rpcport):
  35. '''
  36. Start a node with rpcwallow IP, and request getinfo
  37. at a non-localhost IP.
  38. '''
  39. base_args = ['-disablewallet', '-nolisten'] + ['-rpcallowip='+x for x in allow_ips]
  40. nodes = start_nodes(1, tmpdir, [base_args])
  41. try:
  42. # connect to node through non-loopback interface
  43. url = "http://rt:rt@%s:%d" % (rpchost, rpcport,)
  44. node = get_rpc_proxy(url, 1)
  45. node.getinfo()
  46. finally:
  47. node = None # make sure connection will be garbage collected and closed
  48. stop_nodes(nodes)
  49. wait_bitcoinds()
  50. def run_test(tmpdir):
  51. assert(sys.platform == 'linux2') # due to OS-specific network stats queries, this test works only on Linux
  52. # find the first non-loopback interface for testing
  53. non_loopback_ip = None
  54. for name,ip in all_interfaces():
  55. if ip != '127.0.0.1':
  56. non_loopback_ip = ip
  57. break
  58. if non_loopback_ip is None:
  59. assert(not 'This test requires at least one non-loopback IPv4 interface')
  60. print("Using interface %s for testing" % non_loopback_ip)
  61. defaultport = rpc_port(0)
  62. # check default without rpcallowip (IPv4 and IPv6 localhost)
  63. run_bind_test(tmpdir, None, '127.0.0.1', [],
  64. [('127.0.0.1', defaultport), ('::1', defaultport)])
  65. # check default with rpcallowip (IPv6 any)
  66. run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1', [],
  67. [('::0', defaultport)])
  68. # check only IPv4 localhost (explicit)
  69. run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1', ['127.0.0.1'],
  70. [('127.0.0.1', defaultport)])
  71. # check only IPv4 localhost (explicit) with alternative port
  72. run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1:32171', ['127.0.0.1:32171'],
  73. [('127.0.0.1', 32171)])
  74. # check only IPv4 localhost (explicit) with multiple alternative ports on same host
  75. run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1:32171', ['127.0.0.1:32171', '127.0.0.1:32172'],
  76. [('127.0.0.1', 32171), ('127.0.0.1', 32172)])
  77. # check only IPv6 localhost (explicit)
  78. run_bind_test(tmpdir, ['[::1]'], '[::1]', ['[::1]'],
  79. [('::1', defaultport)])
  80. # check both IPv4 and IPv6 localhost (explicit)
  81. run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1', ['127.0.0.1', '[::1]'],
  82. [('127.0.0.1', defaultport), ('::1', defaultport)])
  83. # check only non-loopback interface
  84. run_bind_test(tmpdir, [non_loopback_ip], non_loopback_ip, [non_loopback_ip],
  85. [(non_loopback_ip, defaultport)])
  86. # Check that with invalid rpcallowip, we are denied
  87. run_allowip_test(tmpdir, [non_loopback_ip], non_loopback_ip, defaultport)
  88. try:
  89. run_allowip_test(tmpdir, ['1.1.1.1'], non_loopback_ip, defaultport)
  90. assert(not 'Connection not denied by rpcallowip as expected')
  91. except ValueError:
  92. pass
  93. def main():
  94. import optparse
  95. parser = optparse.OptionParser(usage="%prog [options]")
  96. parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
  97. help="Leave bitcoinds and test.* datadir on exit or error")
  98. parser.add_option("--srcdir", dest="srcdir", default="../../src",
  99. help="Source directory containing bitcoind/bitcoin-cli (default: %default%)")
  100. parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
  101. help="Root directory for datadirs")
  102. (options, args) = parser.parse_args()
  103. os.environ['PATH'] = options.srcdir+":"+os.environ['PATH']
  104. check_json_precision()
  105. success = False
  106. nodes = []
  107. try:
  108. print("Initializing test directory "+options.tmpdir)
  109. if not os.path.isdir(options.tmpdir):
  110. os.makedirs(options.tmpdir)
  111. initialize_chain(options.tmpdir)
  112. run_test(options.tmpdir)
  113. success = True
  114. except AssertionError as e:
  115. print("Assertion failed: "+e.message)
  116. except Exception as e:
  117. print("Unexpected exception caught during testing: "+str(e))
  118. traceback.print_tb(sys.exc_info()[2])
  119. if not options.nocleanup:
  120. print("Cleaning up")
  121. wait_bitcoinds()
  122. shutil.rmtree(options.tmpdir)
  123. if success:
  124. print("Tests successful")
  125. sys.exit(0)
  126. else:
  127. print("Failed")
  128. sys.exit(1)
  129. if __name__ == '__main__':
  130. main()