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
.mininode
import wait_until
9 from test_framework
.test_framework
import BitcoinTestFramework
10 from test_framework
.util
import (assert_equal
,
11 assert_raises_jsonrpc
,
14 class DisconnectBanTest(BitcoinTestFramework
):
19 self
.setup_clean_chain
= False
22 self
.log
.info("Test setban and listbanned RPCs")
24 self
.log
.info("setban: successfully ban single IP address")
25 assert_equal(len(self
.nodes
[1].getpeerinfo()), 2) # node1 should have 2 connections to node0 at this point
26 self
.nodes
[1].setban("127.0.0.1", "add")
27 assert wait_until(lambda: len(self
.nodes
[1].getpeerinfo()) == 0, timeout
=10)
28 assert_equal(len(self
.nodes
[1].getpeerinfo()), 0) # all nodes must be disconnected at this point
29 assert_equal(len(self
.nodes
[1].listbanned()), 1)
31 self
.log
.info("clearbanned: successfully clear ban list")
32 self
.nodes
[1].clearbanned()
33 assert_equal(len(self
.nodes
[1].listbanned()), 0)
34 self
.nodes
[1].setban("127.0.0.0/24", "add")
36 self
.log
.info("setban: fail to ban an already banned subnet")
37 assert_equal(len(self
.nodes
[1].listbanned()), 1)
38 assert_raises_jsonrpc(-23, "IP/Subnet already banned", self
.nodes
[1].setban
, "127.0.0.1", "add")
40 self
.log
.info("setban: fail to ban an invalid subnet")
41 assert_raises_jsonrpc(-30, "Error: Invalid IP/Subnet", self
.nodes
[1].setban
, "127.0.0.1/42", "add")
42 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
44 self
.log
.info("setban remove: fail to unban a non-banned subnet")
45 assert_raises_jsonrpc(-30, "Error: Unban failed", self
.nodes
[1].setban
, "127.0.0.1", "remove")
46 assert_equal(len(self
.nodes
[1].listbanned()), 1)
48 self
.log
.info("setban remove: successfully unban subnet")
49 self
.nodes
[1].setban("127.0.0.0/24", "remove")
50 assert_equal(len(self
.nodes
[1].listbanned()), 0)
51 self
.nodes
[1].clearbanned()
52 assert_equal(len(self
.nodes
[1].listbanned()), 0)
54 self
.log
.info("setban: test persistence across node restart")
55 self
.nodes
[1].setban("127.0.0.0/32", "add")
56 self
.nodes
[1].setban("127.0.0.0/24", "add")
57 # Set the mocktime so we can control when bans expire
58 old_time
= int(time
.time())
59 self
.nodes
[1].setmocktime(old_time
)
60 self
.nodes
[1].setban("192.168.0.1", "add", 1) # ban for 1 seconds
61 self
.nodes
[1].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) # ban for 1000 seconds
62 listBeforeShutdown
= self
.nodes
[1].listbanned()
63 assert_equal("192.168.0.1/32", listBeforeShutdown
[2]['address'])
64 # Move time forward by 3 seconds so the third ban has expired
65 self
.nodes
[1].setmocktime(old_time
+ 3)
66 assert_equal(len(self
.nodes
[1].listbanned()), 3)
70 self
.nodes
[1] = self
.start_node(1, self
.options
.tmpdir
)
71 listAfterShutdown
= self
.nodes
[1].listbanned()
72 assert_equal("127.0.0.0/24", listAfterShutdown
[0]['address'])
73 assert_equal("127.0.0.0/32", listAfterShutdown
[1]['address'])
74 assert_equal("/19" in listAfterShutdown
[2]['address'], True)
77 self
.nodes
[1].clearbanned()
78 connect_nodes_bi(self
.nodes
, 0, 1)
80 self
.log
.info("Test disconnectnode RPCs")
82 self
.log
.info("disconnectnode: fail to disconnect when calling with address and nodeid")
83 address1
= self
.nodes
[0].getpeerinfo()[0]['addr']
84 node1
= self
.nodes
[0].getpeerinfo()[0]['addr']
85 assert_raises_jsonrpc(-32602, "Only one of address and nodeid should be provided.", self
.nodes
[0].disconnectnode
, address
=address1
, nodeid
=node1
)
87 self
.log
.info("disconnectnode: fail to disconnect when calling with junk address")
88 assert_raises_jsonrpc(-29, "Node not found in connected nodes", self
.nodes
[0].disconnectnode
, address
="221B Baker Street")
90 self
.log
.info("disconnectnode: successfully disconnect node by address")
91 address1
= self
.nodes
[0].getpeerinfo()[0]['addr']
92 self
.nodes
[0].disconnectnode(address
=address1
)
93 assert wait_until(lambda: len(self
.nodes
[0].getpeerinfo()) == 1, timeout
=10)
94 assert not [node
for node
in self
.nodes
[0].getpeerinfo() if node
['addr'] == address1
]
96 self
.log
.info("disconnectnode: successfully reconnect node")
97 connect_nodes_bi(self
.nodes
, 0, 1) # reconnect the node
98 assert_equal(len(self
.nodes
[0].getpeerinfo()), 2)
99 assert [node
for node
in self
.nodes
[0].getpeerinfo() if node
['addr'] == address1
]
101 self
.log
.info("disconnectnode: successfully disconnect node by node id")
102 id1
= self
.nodes
[0].getpeerinfo()[0]['id']
103 self
.nodes
[0].disconnectnode(nodeid
=id1
)
104 assert wait_until(lambda: len(self
.nodes
[0].getpeerinfo()) == 1, timeout
=10)
105 assert not [node
for node
in self
.nodes
[0].getpeerinfo() if node
['id'] == id1
]
107 if __name__
== '__main__':
108 DisconnectBanTest().main()