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