Dash Core  0.12.2.1
P2P Digital Currency
rpcbind_test.py
Go to the documentation of this file.
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 
6 # Test for -rpcbind, as well as -rpcallowip and -rpcconnect
7 
8 # TODO extend this test from the test framework (like all other tests)
9 
10 import tempfile
11 import traceback
12 
13 from test_framework.util import *
14 from test_framework.netutil import *
15 
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)
34 
35 def run_allowip_test(tmpdir, allow_ips, rpchost, rpcport):
36  '''
37  Start a node with rpcwallow IP, and request getinfo
38  at a non-localhost IP.
39  '''
40  base_args = ['-disablewallet', '-nolisten'] + ['-rpcallowip='+x for x in allow_ips]
41  nodes = start_nodes(1, tmpdir, [base_args])
42  try:
43  # connect to node through non-loopback interface
44  url = "http://rt:rt@%s:%d" % (rpchost, rpcport,)
45  node = get_rpc_proxy(url, 1)
46  node.getinfo()
47  finally:
48  node = None # make sure connection will be garbage collected and closed
49  stop_nodes(nodes)
51 
52 
53 def run_test(tmpdir):
54  assert(sys.platform == 'linux2') # due to OS-specific network stats queries, this test works only on Linux
55  # find the first non-loopback interface for testing
56  non_loopback_ip = None
57  for name,ip in all_interfaces():
58  if ip != '127.0.0.1':
59  non_loopback_ip = ip
60  break
61  if non_loopback_ip is None:
62  assert(not 'This test requires at least one non-loopback IPv4 interface')
63  print("Using interface %s for testing" % non_loopback_ip)
64 
65  defaultport = rpc_port(0)
66 
67  # check default without rpcallowip (IPv4 and IPv6 localhost)
68  run_bind_test(tmpdir, None, '127.0.0.1', [],
69  [('127.0.0.1', defaultport), ('::1', defaultport)])
70  # check default with rpcallowip (IPv6 any)
71  run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1', [],
72  [('::0', defaultport)])
73  # check only IPv4 localhost (explicit)
74  run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1', ['127.0.0.1'],
75  [('127.0.0.1', defaultport)])
76  # check only IPv4 localhost (explicit) with alternative port
77  run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1:32171', ['127.0.0.1:32171'],
78  [('127.0.0.1', 32171)])
79  # check only IPv4 localhost (explicit) with multiple alternative ports on same host
80  run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1:32171', ['127.0.0.1:32171', '127.0.0.1:32172'],
81  [('127.0.0.1', 32171), ('127.0.0.1', 32172)])
82  # check only IPv6 localhost (explicit)
83  run_bind_test(tmpdir, ['[::1]'], '[::1]', ['[::1]'],
84  [('::1', defaultport)])
85  # check both IPv4 and IPv6 localhost (explicit)
86  run_bind_test(tmpdir, ['127.0.0.1'], '127.0.0.1', ['127.0.0.1', '[::1]'],
87  [('127.0.0.1', defaultport), ('::1', defaultport)])
88  # check only non-loopback interface
89  run_bind_test(tmpdir, [non_loopback_ip], non_loopback_ip, [non_loopback_ip],
90  [(non_loopback_ip, defaultport)])
91 
92  # Check that with invalid rpcallowip, we are denied
93  run_allowip_test(tmpdir, [non_loopback_ip], non_loopback_ip, defaultport)
94  try:
95  run_allowip_test(tmpdir, ['1.1.1.1'], non_loopback_ip, defaultport)
96  assert(not 'Connection not denied by rpcallowip as expected')
97  except ValueError:
98  pass
99 
100 def main():
101  import optparse
102 
103  parser = optparse.OptionParser(usage="%prog [options]")
104  parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
105  help="Leave bitcoinds and test.* datadir on exit or error")
106  parser.add_option("--srcdir", dest="srcdir", default="../../src",
107  help="Source directory containing bitcoind/bitcoin-cli (default: %default%)")
108  parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
109  help="Root directory for datadirs")
110  (options, args) = parser.parse_args()
111 
112  os.environ['PATH'] = options.srcdir+":"+os.environ['PATH']
113 
115 
116  success = False
117  nodes = []
118  try:
119  print("Initializing test directory "+options.tmpdir)
120  if not os.path.isdir(options.tmpdir):
121  os.makedirs(options.tmpdir)
122  initialize_chain(options.tmpdir)
123 
124  run_test(options.tmpdir)
125 
126  success = True
127 
128  except AssertionError as e:
129  print("Assertion failed: "+e.message)
130  except Exception as e:
131  print("Unexpected exception caught during testing: "+str(e))
132  traceback.print_tb(sys.exc_info()[2])
133 
134  if not options.nocleanup:
135  print("Cleaning up")
137  shutil.rmtree(options.tmpdir)
138 
139  if success:
140  print("Tests successful")
141  sys.exit(0)
142  else:
143  print("Failed")
144  sys.exit(1)
145 
146 if __name__ == '__main__':
147  main()
def wait_bitcoinds()
Definition: util.py:337
def get_rpc_proxy(url, node_number, timeout=None)
Definition: util.py:58
def initialize_chain(test_dir)
Definition: util.py:184
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
def check_json_precision()
Definition: spendfrom.py:28
def run_bind_test(tmpdir, allow_ips, connect_to, addresses, expected)
Definition: rpcbind_test.py:16
def stop_nodes(nodes)
Definition: util.py:328
def rpc_port(n)
Definition: util.py:95
def get_bind_addrs(pid)
Definition: netutil.py:76
def run_test(tmpdir)
Definition: rpcbind_test.py:53
def addr_to_hex(addr)
Definition: netutil.py:113
def run_allowip_test(tmpdir, allow_ips, rpchost, rpcport)
Definition: rpcbind_test.py:35
def assert_equal(thing1, thing2)
Definition: util.py:461