|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2020 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 bitcoind with 'onion'-tagged peers. |
| 6 | +
|
| 7 | +Tested outbound and inbound peers. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | + |
| 12 | +from test_framework.p2p import P2PInterface |
| 13 | +from test_framework.socks5 import Socks5Configuration, Socks5Server |
| 14 | +from test_framework.test_framework import BitcoinTestFramework |
| 15 | +from test_framework.util import ( |
| 16 | + PORT_MIN, |
| 17 | + PORT_RANGE, |
| 18 | + assert_equal, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class OnionTest(BitcoinTestFramework): |
| 23 | + def set_test_params(self): |
| 24 | + self.num_nodes = 2 |
| 25 | + self.setup_clean_chain = True |
| 26 | + |
| 27 | + def setup_network(self): |
| 28 | + self.proxy_conf = Socks5Configuration() |
| 29 | + self.proxy_conf.addr = ('127.0.0.1', PORT_MIN + 2 * PORT_RANGE + (os.getpid() % 1000)) |
| 30 | + self.proxy_conf.unauth = True |
| 31 | + |
| 32 | + self.proxy = Socks5Server(self.proxy_conf) |
| 33 | + self.proxy.start() |
| 34 | + |
| 35 | + args = [ |
| 36 | + ['-bind=127.0.0.2:18445=onion', '-onion=%s:%i' % (self.proxy_conf.addr)], |
| 37 | + [], |
| 38 | + ] |
| 39 | + self.add_nodes(self.num_nodes, extra_args=args) |
| 40 | + self.start_nodes() |
| 41 | + |
| 42 | + def run_test(self): |
| 43 | + self.log.info("Checking a node without any peers...") |
| 44 | + network_info = self.nodes[0].getnetworkinfo() |
| 45 | + assert_equal(network_info["connections"], 0) |
| 46 | + assert_equal(network_info["connections_in"], 0) |
| 47 | + assert_equal(network_info["connections_out"], 0) |
| 48 | + assert_equal(network_info["connections_onion_only"], False) |
| 49 | + |
| 50 | + self.log.info("Accepting an inbound 'onion'-tagged connection, and checking...") |
| 51 | + self.nodes[1].addnode("127.0.0.2:18445", "onetry") |
| 52 | + network_info = self.nodes[0].getnetworkinfo() |
| 53 | + assert_equal(network_info["connections"], 1) |
| 54 | + assert_equal(network_info["connections_in"], 1) |
| 55 | + assert_equal(network_info["connections_out"], 0) |
| 56 | + assert_equal(network_info["connections_onion_only"], True) |
| 57 | + |
| 58 | + self.log.info("Creating an outbound onion connection, and checking...") |
| 59 | + onion_address = "bitcoinostk4e4re.onion:8333" |
| 60 | + self.nodes[0].addnode(onion_address, "onetry") |
| 61 | + network_info = self.nodes[0].getnetworkinfo() |
| 62 | + assert_equal(network_info["connections"], 2) |
| 63 | + assert_equal(network_info["connections_in"], 1) |
| 64 | + assert_equal(network_info["connections_out"], 1) |
| 65 | + assert_equal(network_info["connections_onion_only"], True) |
| 66 | + |
| 67 | + self.log.info("Adding an outbound non-onion peer, and checking...") |
| 68 | + self.nodes[0].add_p2p_connection(P2PInterface()) |
| 69 | + network_info = self.nodes[0].getnetworkinfo() |
| 70 | + assert_equal(network_info["connections"], 2) |
| 71 | + assert_equal(network_info["connections_in"], 2) |
| 72 | + assert_equal(network_info["connections_out"], 0) |
| 73 | + assert_equal(network_info["connections_onion_only"], False) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + OnionTest().main() |
0 commit comments