2019-08-16 16:17:20 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-12-31 18:50:11 +01:00
|
|
|
# Copyright (c) 2015-2020 The Bitcoin Core developers
|
2019-08-16 16:17:20 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Test the setban rpc call."""
|
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
p2p_port
|
|
|
|
)
|
|
|
|
|
|
|
|
class SetBanTests(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.num_nodes = 2
|
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.extra_args = [[],[]]
|
|
|
|
|
2021-01-04 18:37:52 +01:00
|
|
|
def is_banned(self, node, addr):
|
|
|
|
return any(e['address'] == addr for e in node.listbanned())
|
|
|
|
|
2019-08-16 16:17:20 +02:00
|
|
|
def run_test(self):
|
|
|
|
# Node 0 connects to Node 1, check that the noban permission is not granted
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(0, 1)
|
2019-08-16 16:17:20 +02:00
|
|
|
peerinfo = self.nodes[1].getpeerinfo()[0]
|
2023-09-10 15:49:43 +02:00
|
|
|
assert not "noban" in peerinfo["permissions"]
|
2019-08-16 16:17:20 +02:00
|
|
|
|
|
|
|
# Node 0 get banned by Node 1
|
|
|
|
self.nodes[1].setban("127.0.0.1", "add")
|
|
|
|
|
|
|
|
# Node 0 should not be able to reconnect
|
2020-01-25 17:20:14 +01:00
|
|
|
with self.nodes[1].assert_debug_log(expected_msgs=['dropped (banned)\n'], timeout=50):
|
2019-08-16 16:17:20 +02:00
|
|
|
self.restart_node(1, [])
|
|
|
|
self.nodes[0].addnode("127.0.0.1:" + str(p2p_port(1)), "onetry")
|
|
|
|
|
|
|
|
# However, node 0 should be able to reconnect if it has noban permission
|
|
|
|
self.restart_node(1, ['-whitelist=127.0.0.1'])
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(0, 1)
|
2019-08-16 16:17:20 +02:00
|
|
|
peerinfo = self.nodes[1].getpeerinfo()[0]
|
2023-09-10 15:49:43 +02:00
|
|
|
assert "noban" in peerinfo["permissions"]
|
2019-08-16 16:17:20 +02:00
|
|
|
|
|
|
|
# If we remove the ban, Node 0 should be able to reconnect even without noban permission
|
|
|
|
self.nodes[1].setban("127.0.0.1", "remove")
|
|
|
|
self.restart_node(1, [])
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(0, 1)
|
2019-08-16 16:17:20 +02:00
|
|
|
peerinfo = self.nodes[1].getpeerinfo()[0]
|
2023-09-10 15:49:43 +02:00
|
|
|
assert not "noban" in peerinfo["permissions"]
|
2019-08-16 16:17:20 +02:00
|
|
|
|
2021-01-04 18:37:52 +01:00
|
|
|
self.log.info("Test that a non-IP address can be banned/unbanned")
|
|
|
|
node = self.nodes[1]
|
|
|
|
tor_addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"
|
|
|
|
ip_addr = "1.2.3.4"
|
2023-09-10 15:49:43 +02:00
|
|
|
assert not self.is_banned(node, tor_addr)
|
|
|
|
assert not self.is_banned(node, ip_addr)
|
|
|
|
|
2021-01-04 18:37:52 +01:00
|
|
|
node.setban(tor_addr, "add")
|
2023-09-10 15:49:43 +02:00
|
|
|
assert self.is_banned(node, tor_addr)
|
|
|
|
assert not self.is_banned(node, ip_addr)
|
|
|
|
|
|
|
|
self.log.info("Test the ban list is preserved through restart")
|
|
|
|
|
|
|
|
self.restart_node(1)
|
|
|
|
assert self.is_banned(node, tor_addr)
|
|
|
|
assert not self.is_banned(node, ip_addr)
|
|
|
|
|
2021-01-04 18:37:52 +01:00
|
|
|
node.setban(tor_addr, "remove")
|
2023-09-10 15:49:43 +02:00
|
|
|
assert not self.is_banned(self.nodes[1], tor_addr)
|
|
|
|
assert not self.is_banned(node, ip_addr)
|
|
|
|
|
|
|
|
self.restart_node(1)
|
|
|
|
assert not self.is_banned(node, tor_addr)
|
|
|
|
assert not self.is_banned(node, ip_addr)
|
|
|
|
|
2021-01-04 18:37:52 +01:00
|
|
|
|
2019-08-16 16:17:20 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
SetBanTests().main()
|