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"""
8 from test_framework
.test_framework
import BitcoinTestFramework
9 from test_framework
.util
import (
11 assert_raises_jsonrpc
,
16 class DisconnectBanTest(BitcoinTestFramework
):
17 def set_test_params(self
):
21 self
.log
.info("Test setban and listbanned RPCs")
23 self
.log
.info("setban: successfully ban single IP address")
24 assert_equal(len(self
.nodes
[1].getpeerinfo()), 2) # node1 should have 2 connections to node0 at this point
25 self
.nodes
[1].setban("127.0.0.1", "add")
26 wait_until(lambda: len(self
.nodes
[1].getpeerinfo()) == 0, timeout
=10)
27 assert_equal(len(self
.nodes
[1].getpeerinfo()), 0) # all nodes must be disconnected at this point
28 assert_equal(len(self
.nodes
[1].listbanned()), 1)
30 self
.log
.info("clearbanned: successfully clear ban list")
31 self
.nodes
[1].clearbanned()
32 assert_equal(len(self
.nodes
[1].listbanned()), 0)
33 self
.nodes
[1].setban("127.0.0.0/24", "add")
35 self
.log
.info("setban: fail to ban an already banned subnet")
36 assert_equal(len(self
.nodes
[1].listbanned()), 1)
37 assert_raises_jsonrpc(-23, "IP/Subnet already banned", self
.nodes
[1].setban
, "127.0.0.1", "add")
39 self
.log
.info("setban: fail to ban an invalid subnet")
40 assert_raises_jsonrpc(-30, "Error: Invalid IP/Subnet", self
.nodes
[1].setban
, "127.0.0.1/42", "add")
41 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
43 self
.log
.info("setban remove: fail to unban a non-banned subnet")
44 assert_raises_jsonrpc(-30, "Error: Unban failed", self
.nodes
[1].setban
, "127.0.0.1", "remove")
45 assert_equal(len(self
.nodes
[1].listbanned()), 1)
47 self
.log
.info("setban remove: successfully unban subnet")
48 self
.nodes
[1].setban("127.0.0.0/24", "remove")
49 assert_equal(len(self
.nodes
[1].listbanned()), 0)
50 self
.nodes
[1].clearbanned()
51 assert_equal(len(self
.nodes
[1].listbanned()), 0)
53 self
.log
.info("setban: test persistence across node restart")
54 self
.nodes
[1].setban("127.0.0.0/32", "add")
55 self
.nodes
[1].setban("127.0.0.0/24", "add")
56 # Set the mocktime so we can control when bans expire
57 old_time
= int(time
.time())
58 self
.nodes
[1].setmocktime(old_time
)
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 # Move time forward by 3 seconds so the third ban has expired
64 self
.nodes
[1].setmocktime(old_time
+ 3)
65 assert_equal(len(self
.nodes
[1].listbanned()), 3)
70 listAfterShutdown
= self
.nodes
[1].listbanned()
71 assert_equal("127.0.0.0/24", listAfterShutdown
[0]['address'])
72 assert_equal("127.0.0.0/32", listAfterShutdown
[1]['address'])
73 assert_equal("/19" in listAfterShutdown
[2]['address'], True)
76 self
.nodes
[1].clearbanned()
77 connect_nodes_bi(self
.nodes
, 0, 1)
79 self
.log
.info("Test disconnectnode RPCs")
81 self
.log
.info("disconnectnode: fail to disconnect when calling with address and nodeid")
82 address1
= self
.nodes
[0].getpeerinfo()[0]['addr']
83 node1
= self
.nodes
[0].getpeerinfo()[0]['addr']
84 assert_raises_jsonrpc(-32602, "Only one of address and nodeid should be provided.", self
.nodes
[0].disconnectnode
, address
=address1
, nodeid
=node1
)
86 self
.log
.info("disconnectnode: fail to disconnect when calling with junk address")
87 assert_raises_jsonrpc(-29, "Node not found in connected nodes", self
.nodes
[0].disconnectnode
, address
="221B Baker Street")
89 self
.log
.info("disconnectnode: successfully disconnect node by address")
90 address1
= self
.nodes
[0].getpeerinfo()[0]['addr']
91 self
.nodes
[0].disconnectnode(address
=address1
)
92 wait_until(lambda: len(self
.nodes
[0].getpeerinfo()) == 1, timeout
=10)
93 assert not [node
for node
in self
.nodes
[0].getpeerinfo() if node
['addr'] == address1
]
95 self
.log
.info("disconnectnode: successfully reconnect node")
96 connect_nodes_bi(self
.nodes
, 0, 1) # reconnect the node
97 assert_equal(len(self
.nodes
[0].getpeerinfo()), 2)
98 assert [node
for node
in self
.nodes
[0].getpeerinfo() if node
['addr'] == address1
]
100 self
.log
.info("disconnectnode: successfully disconnect node by node id")
101 id1
= self
.nodes
[0].getpeerinfo()[0]['id']
102 self
.nodes
[0].disconnectnode(nodeid
=id1
)
103 wait_until(lambda: len(self
.nodes
[0].getpeerinfo()) == 1, timeout
=10)
104 assert not [node
for node
in self
.nodes
[0].getpeerinfo() if node
['id'] == id1
]
106 if __name__
== '__main__':
107 DisconnectBanTest().main()