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 node disconnect and ban behavior"""
7 from test_framework
.mininode
import wait_until
8 from test_framework
.test_framework
import BitcoinTestFramework
9 from test_framework
.util
import (assert_equal
,
10 assert_raises_jsonrpc
,
16 class DisconnectBanTest(BitcoinTestFramework
):
21 self
.setup_clean_chain
= False
24 self
.log
.info("Test setban and listbanned RPCs")
26 self
.log
.info("setban: successfully ban single IP address")
27 assert_equal(len(self
.nodes
[1].getpeerinfo()), 2) # node1 should have 2 connections to node0 at this point
28 self
.nodes
[1].setban("127.0.0.1", "add")
29 assert wait_until(lambda: len(self
.nodes
[1].getpeerinfo()) == 0, timeout
=10)
30 assert_equal(len(self
.nodes
[1].getpeerinfo()), 0) # all nodes must be disconnected at this point
31 assert_equal(len(self
.nodes
[1].listbanned()), 1)
33 self
.log
.info("clearbanned: successfully clear ban list")
34 self
.nodes
[1].clearbanned()
35 assert_equal(len(self
.nodes
[1].listbanned()), 0)
36 self
.nodes
[1].setban("127.0.0.0/24", "add")
38 self
.log
.info("setban: fail to ban an already banned subnet")
39 assert_equal(len(self
.nodes
[1].listbanned()), 1)
40 assert_raises_jsonrpc(-23, "IP/Subnet already banned", self
.nodes
[1].setban
, "127.0.0.1", "add")
42 self
.log
.info("setban: fail to ban an invalid subnet")
43 assert_raises_jsonrpc(-30, "Error: Invalid IP/Subnet", self
.nodes
[1].setban
, "127.0.0.1/42", "add")
44 assert_equal(len(self
.nodes
[1].listbanned()), 1) # still only one banned ip because 127.0.0.1 is within the range of 127.0.0.0/24
46 self
.log
.info("setban remove: fail to unban a non-banned subnet")
47 assert_raises_jsonrpc(-30, "Error: Unban failed", self
.nodes
[1].setban
, "127.0.0.1", "remove")
48 assert_equal(len(self
.nodes
[1].listbanned()), 1)
50 self
.log
.info("setban remove: successfully unban subnet")
51 self
.nodes
[1].setban("127.0.0.0/24", "remove")
52 assert_equal(len(self
.nodes
[1].listbanned()), 0)
53 self
.nodes
[1].clearbanned()
54 assert_equal(len(self
.nodes
[1].listbanned()), 0)
56 self
.log
.info("setban: test persistence across node restart")
57 self
.nodes
[1].setban("127.0.0.0/32", "add")
58 self
.nodes
[1].setban("127.0.0.0/24", "add")
59 self
.nodes
[1].setban("192.168.0.1", "add", 1) # ban for 1 seconds
60 self
.nodes
[1].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) # ban for 1000 seconds
61 listBeforeShutdown
= self
.nodes
[1].listbanned()
62 assert_equal("192.168.0.1/32", listBeforeShutdown
[2]['address'])
63 assert wait_until(lambda: len(self
.nodes
[1].listbanned()) == 3, timeout
=10)
65 stop_node(self
.nodes
[1], 1)
67 self
.nodes
[1] = start_node(1, self
.options
.tmpdir
)
68 listAfterShutdown
= self
.nodes
[1].listbanned()
69 assert_equal("127.0.0.0/24", listAfterShutdown
[0]['address'])
70 assert_equal("127.0.0.0/32", listAfterShutdown
[1]['address'])
71 assert_equal("/19" in listAfterShutdown
[2]['address'], True)
74 self
.nodes
[1].clearbanned()
75 connect_nodes_bi(self
.nodes
, 0, 1)
77 self
.log
.info("Test disconnectnode RPCs")
79 self
.log
.info("disconnectnode: fail to disconnect when calling with address and nodeid")
80 address1
= self
.nodes
[0].getpeerinfo()[0]['addr']
81 node1
= self
.nodes
[0].getpeerinfo()[0]['addr']
82 assert_raises_jsonrpc(-32602, "Only one of address and nodeid should be provided.", self
.nodes
[0].disconnectnode
, address
=address1
, nodeid
=node1
)
84 self
.log
.info("disconnectnode: fail to disconnect when calling with junk address")
85 assert_raises_jsonrpc(-29, "Node not found in connected nodes", self
.nodes
[0].disconnectnode
, address
="221B Baker Street")
87 self
.log
.info("disconnectnode: successfully disconnect node by address")
88 address1
= self
.nodes
[0].getpeerinfo()[0]['addr']
89 self
.nodes
[0].disconnectnode(address
=address1
)
90 assert wait_until(lambda: len(self
.nodes
[0].getpeerinfo()) == 1, timeout
=10)
91 assert not [node
for node
in self
.nodes
[0].getpeerinfo() if node
['addr'] == address1
]
93 self
.log
.info("disconnectnode: successfully reconnect node")
94 connect_nodes_bi(self
.nodes
, 0, 1) # reconnect the node
95 assert_equal(len(self
.nodes
[0].getpeerinfo()), 2)
96 assert [node
for node
in self
.nodes
[0].getpeerinfo() if node
['addr'] == address1
]
98 self
.log
.info("disconnectnode: successfully disconnect node by node id")
99 id1
= self
.nodes
[0].getpeerinfo()[0]['id']
100 self
.nodes
[0].disconnectnode(nodeid
=id1
)
101 assert wait_until(lambda: len(self
.nodes
[0].getpeerinfo()) == 1, timeout
=10)
102 assert not [node
for node
in self
.nodes
[0].getpeerinfo() if node
['id'] == id1
]
104 if __name__
== '__main__':
105 DisconnectBanTest().main()