You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
5.2 KiB
108 lines
5.2 KiB
9 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
# Copyright (c) 2014-2017 The Bitcoin Core developers
|
||
10 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
8 years ago
|
"""Test node disconnect and ban behavior"""
|
||
8 years ago
|
import time
|
||
10 years ago
|
|
||
|
from test_framework.test_framework import BitcoinTestFramework
|
||
7 years ago
|
from test_framework.util import (
|
||
|
assert_equal,
|
||
8 years ago
|
assert_raises_rpc_error,
|
||
7 years ago
|
connect_nodes_bi,
|
||
|
wait_until,
|
||
|
)
|
||
10 years ago
|
|
||
8 years ago
|
class DisconnectBanTest(BitcoinTestFramework):
|
||
8 years ago
|
def set_test_params(self):
|
||
8 years ago
|
self.num_nodes = 2
|
||
9 years ago
|
|
||
10 years ago
|
def run_test(self):
|
||
8 years ago
|
self.log.info("Test setban and listbanned RPCs")
|
||
|
|
||
|
self.log.info("setban: successfully ban single IP address")
|
||
8 years ago
|
assert_equal(len(self.nodes[1].getpeerinfo()), 2) # node1 should have 2 connections to node0 at this point
|
||
|
self.nodes[1].setban("127.0.0.1", "add")
|
||
7 years ago
|
wait_until(lambda: len(self.nodes[1].getpeerinfo()) == 0, timeout=10)
|
||
8 years ago
|
assert_equal(len(self.nodes[1].getpeerinfo()), 0) # all nodes must be disconnected at this point
|
||
|
assert_equal(len(self.nodes[1].listbanned()), 1)
|
||
8 years ago
|
|
||
|
self.log.info("clearbanned: successfully clear ban list")
|
||
8 years ago
|
self.nodes[1].clearbanned()
|
||
|
assert_equal(len(self.nodes[1].listbanned()), 0)
|
||
|
self.nodes[1].setban("127.0.0.0/24", "add")
|
||
8 years ago
|
|
||
|
self.log.info("setban: fail to ban an already banned subnet")
|
||
8 years ago
|
assert_equal(len(self.nodes[1].listbanned()), 1)
|
||
8 years ago
|
assert_raises_rpc_error(-23, "IP/Subnet already banned", self.nodes[1].setban, "127.0.0.1", "add")
|
||
8 years ago
|
|
||
|
self.log.info("setban: fail to ban an invalid subnet")
|
||
8 years ago
|
assert_raises_rpc_error(-30, "Error: Invalid IP/Subnet", self.nodes[1].setban, "127.0.0.1/42", "add")
|
||
8 years ago
|
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
|
||
8 years ago
|
|
||
|
self.log.info("setban remove: fail to unban a non-banned subnet")
|
||
8 years ago
|
assert_raises_rpc_error(-30, "Error: Unban failed", self.nodes[1].setban, "127.0.0.1", "remove")
|
||
8 years ago
|
assert_equal(len(self.nodes[1].listbanned()), 1)
|
||
8 years ago
|
|
||
|
self.log.info("setban remove: successfully unban subnet")
|
||
8 years ago
|
self.nodes[1].setban("127.0.0.0/24", "remove")
|
||
|
assert_equal(len(self.nodes[1].listbanned()), 0)
|
||
|
self.nodes[1].clearbanned()
|
||
|
assert_equal(len(self.nodes[1].listbanned()), 0)
|
||
10 years ago
|
|
||
8 years ago
|
self.log.info("setban: test persistence across node restart")
|
||
8 years ago
|
self.nodes[1].setban("127.0.0.0/32", "add")
|
||
|
self.nodes[1].setban("127.0.0.0/24", "add")
|
||
8 years ago
|
# Set the mocktime so we can control when bans expire
|
||
|
old_time = int(time.time())
|
||
|
self.nodes[1].setmocktime(old_time)
|
||
8 years ago
|
self.nodes[1].setban("192.168.0.1", "add", 1) # ban for 1 seconds
|
||
|
self.nodes[1].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) # ban for 1000 seconds
|
||
|
listBeforeShutdown = self.nodes[1].listbanned()
|
||
8 years ago
|
assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address'])
|
||
8 years ago
|
# Move time forward by 3 seconds so the third ban has expired
|
||
|
self.nodes[1].setmocktime(old_time + 3)
|
||
|
assert_equal(len(self.nodes[1].listbanned()), 3)
|
||
10 years ago
|
|
||
8 years ago
|
self.stop_node(1)
|
||
8 years ago
|
self.start_node(1)
|
||
10 years ago
|
|
||
8 years ago
|
listAfterShutdown = self.nodes[1].listbanned()
|
||
10 years ago
|
assert_equal("127.0.0.0/24", listAfterShutdown[0]['address'])
|
||
|
assert_equal("127.0.0.0/32", listAfterShutdown[1]['address'])
|
||
|
assert_equal("/19" in listAfterShutdown[2]['address'], True)
|
||
10 years ago
|
|
||
8 years ago
|
# Clear ban lists
|
||
|
self.nodes[1].clearbanned()
|
||
|
connect_nodes_bi(self.nodes, 0, 1)
|
||
|
|
||
8 years ago
|
self.log.info("Test disconnectnode RPCs")
|
||
8 years ago
|
|
||
8 years ago
|
self.log.info("disconnectnode: fail to disconnect when calling with address and nodeid")
|
||
|
address1 = self.nodes[0].getpeerinfo()[0]['addr']
|
||
|
node1 = self.nodes[0].getpeerinfo()[0]['addr']
|
||
8 years ago
|
assert_raises_rpc_error(-32602, "Only one of address and nodeid should be provided.", self.nodes[0].disconnectnode, address=address1, nodeid=node1)
|
||
8 years ago
|
|
||
|
self.log.info("disconnectnode: fail to disconnect when calling with junk address")
|
||
8 years ago
|
assert_raises_rpc_error(-29, "Node not found in connected nodes", self.nodes[0].disconnectnode, address="221B Baker Street")
|
||
8 years ago
|
|
||
8 years ago
|
self.log.info("disconnectnode: successfully disconnect node by address")
|
||
|
address1 = self.nodes[0].getpeerinfo()[0]['addr']
|
||
|
self.nodes[0].disconnectnode(address=address1)
|
||
7 years ago
|
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10)
|
||
8 years ago
|
assert not [node for node in self.nodes[0].getpeerinfo() if node['addr'] == address1]
|
||
10 years ago
|
|
||
8 years ago
|
self.log.info("disconnectnode: successfully reconnect node")
|
||
8 years ago
|
connect_nodes_bi(self.nodes, 0, 1) # reconnect the node
|
||
8 years ago
|
assert_equal(len(self.nodes[0].getpeerinfo()), 2)
|
||
8 years ago
|
assert [node for node in self.nodes[0].getpeerinfo() if node['addr'] == address1]
|
||
10 years ago
|
|
||
8 years ago
|
self.log.info("disconnectnode: successfully disconnect node by node id")
|
||
|
id1 = self.nodes[0].getpeerinfo()[0]['id']
|
||
|
self.nodes[0].disconnectnode(nodeid=id1)
|
||
7 years ago
|
wait_until(lambda: len(self.nodes[0].getpeerinfo()) == 1, timeout=10)
|
||
8 years ago
|
assert not [node for node in self.nodes[0].getpeerinfo() if node['id'] == id1]
|
||
|
|
||
10 years ago
|
if __name__ == '__main__':
|
||
8 years ago
|
DisconnectBanTest().main()
|