Changeset View
Changeset View
Standalone View
Standalone View
test/functional/feature_asmap.py
Show All 22 Lines | |||||
The tests are order-independent. | The tests are order-independent. | ||||
""" | """ | ||||
import os | import os | ||||
import shutil | import shutil | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
DEFAULT_ASMAP_FILENAME = 'ip_asn.map' # defined in src/init.cpp | DEFAULT_ASMAP_FILENAME = "ip_asn.map" # defined in src/init.cpp | ||||
ASMAP = '../../src/test/data/asmap.raw' # path to unit test skeleton asmap | ASMAP = "../../src/test/data/asmap.raw" # path to unit test skeleton asmap | ||||
VERSION = 'fec61fa21a9f46f3b17bdcd660d7f4cd90b966aad3aec593c99b35f0aca15853' | VERSION = "fec61fa21a9f46f3b17bdcd660d7f4cd90b966aad3aec593c99b35f0aca15853" | ||||
def expected_messages(filename): | def expected_messages(filename): | ||||
return [f'Opened asmap file "{filename}" (59 bytes) from disk', | return [ | ||||
f'Using asmap version {VERSION} for IP bucketing'] | f'Opened asmap file "{filename}" (59 bytes) from disk', | ||||
f"Using asmap version {VERSION} for IP bucketing", | |||||
] | |||||
class AsmapTest(BitcoinTestFramework): | class AsmapTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 1 | self.num_nodes = 1 | ||||
self.extra_args = [[ | self.extra_args = [ | ||||
[ | |||||
# Do addrman checks on all operations. | # Do addrman checks on all operations. | ||||
"-checkaddrman=1" | "-checkaddrman=1" | ||||
]] | ] | ||||
] | |||||
def fill_addrman(self, node_id): | def fill_addrman(self, node_id): | ||||
"""Add 1 tried address to the addrman, followed by 1 new address.""" | """Add 1 tried address to the addrman, followed by 1 new address.""" | ||||
for addr, tried in [[0, True], [1, False]]: | for addr, tried in [[0, True], [1, False]]: | ||||
self.nodes[node_id].addpeeraddress( | self.nodes[node_id].addpeeraddress( | ||||
address=f"101.{addr}.0.0", tried=tried, port=8333) | address=f"101.{addr}.0.0", tried=tried, port=8333 | ||||
) | |||||
def test_without_asmap_arg(self): | def test_without_asmap_arg(self): | ||||
self.log.info('Test bitcoind with no -asmap arg passed') | self.log.info("Test bitcoind with no -asmap arg passed") | ||||
self.stop_node(0) | self.stop_node(0) | ||||
with self.node.assert_debug_log(['Using /16 prefix for IP bucketing']): | with self.node.assert_debug_log(["Using /16 prefix for IP bucketing"]): | ||||
self.start_node(0) | self.start_node(0) | ||||
def test_asmap_with_absolute_path(self): | def test_asmap_with_absolute_path(self): | ||||
self.log.info('Test bitcoind -asmap=<absolute path>') | self.log.info("Test bitcoind -asmap=<absolute path>") | ||||
self.stop_node(0) | self.stop_node(0) | ||||
filename = os.path.join(self.datadir, 'my-map-file.map') | filename = os.path.join(self.datadir, "my-map-file.map") | ||||
shutil.copyfile(self.asmap_raw, filename) | shutil.copyfile(self.asmap_raw, filename) | ||||
with self.node.assert_debug_log(expected_messages(filename)): | with self.node.assert_debug_log(expected_messages(filename)): | ||||
self.start_node(0, [f'-asmap={filename}']) | self.start_node(0, [f"-asmap={filename}"]) | ||||
os.remove(filename) | os.remove(filename) | ||||
def test_asmap_with_relative_path(self): | def test_asmap_with_relative_path(self): | ||||
self.log.info('Test bitcoind -asmap=<relative path>') | self.log.info("Test bitcoind -asmap=<relative path>") | ||||
self.stop_node(0) | self.stop_node(0) | ||||
name = 'ASN_map' | name = "ASN_map" | ||||
filename = os.path.join(self.datadir, name) | filename = os.path.join(self.datadir, name) | ||||
shutil.copyfile(self.asmap_raw, filename) | shutil.copyfile(self.asmap_raw, filename) | ||||
with self.node.assert_debug_log(expected_messages(filename)): | with self.node.assert_debug_log(expected_messages(filename)): | ||||
self.start_node(0, [f'-asmap={name}']) | self.start_node(0, [f"-asmap={name}"]) | ||||
os.remove(filename) | os.remove(filename) | ||||
def test_default_asmap(self): | def test_default_asmap(self): | ||||
shutil.copyfile(self.asmap_raw, self.default_asmap) | shutil.copyfile(self.asmap_raw, self.default_asmap) | ||||
for arg in ['-asmap', '-asmap=']: | for arg in ["-asmap", "-asmap="]: | ||||
self.log.info(f'Test bitcoind {arg} (using default map file)') | self.log.info(f"Test bitcoind {arg} (using default map file)") | ||||
self.stop_node(0) | self.stop_node(0) | ||||
with self.node.assert_debug_log(expected_messages(self.default_asmap)): | with self.node.assert_debug_log(expected_messages(self.default_asmap)): | ||||
self.start_node(0, [arg]) | self.start_node(0, [arg]) | ||||
os.remove(self.default_asmap) | os.remove(self.default_asmap) | ||||
def test_asmap_interaction_with_addrman_containing_entries(self): | def test_asmap_interaction_with_addrman_containing_entries(self): | ||||
self.log.info( | self.log.info( | ||||
"Test bitcoind -asmap restart with addrman containing new and tried entries") | "Test bitcoind -asmap restart with addrman containing new and tried entries" | ||||
) | |||||
self.stop_node(0) | self.stop_node(0) | ||||
shutil.copyfile(self.asmap_raw, self.default_asmap) | shutil.copyfile(self.asmap_raw, self.default_asmap) | ||||
self.start_node(0, ["-asmap", "-checkaddrman=1"]) | self.start_node(0, ["-asmap", "-checkaddrman=1"]) | ||||
self.fill_addrman(node_id=0) | self.fill_addrman(node_id=0) | ||||
self.restart_node(0, ["-asmap", "-checkaddrman=1"]) | self.restart_node(0, ["-asmap", "-checkaddrman=1"]) | ||||
with self.node.assert_debug_log( | with self.node.assert_debug_log( | ||||
expected_msgs=[ | expected_msgs=[ | ||||
"CheckAddrman: new 1, tried 1, total 2 started", | "CheckAddrman: new 1, tried 1, total 2 started", | ||||
"CheckAddrman: completed", | "CheckAddrman: completed", | ||||
] | ] | ||||
): | ): | ||||
# getnodeaddresses re-runs the addrman checks | # getnodeaddresses re-runs the addrman checks | ||||
self.node.getnodeaddresses() | self.node.getnodeaddresses() | ||||
os.remove(self.default_asmap) | os.remove(self.default_asmap) | ||||
def test_default_asmap_with_missing_file(self): | def test_default_asmap_with_missing_file(self): | ||||
self.log.info('Test bitcoind -asmap with missing default map file') | self.log.info("Test bitcoind -asmap with missing default map file") | ||||
self.stop_node(0) | self.stop_node(0) | ||||
msg = f'Error: Could not find asmap file "{self.default_asmap}"' | msg = f'Error: Could not find asmap file "{self.default_asmap}"' | ||||
self.node.assert_start_raises_init_error( | self.node.assert_start_raises_init_error( | ||||
extra_args=['-asmap'], expected_msg=msg) | extra_args=["-asmap"], expected_msg=msg | ||||
) | |||||
def test_empty_asmap(self): | def test_empty_asmap(self): | ||||
self.log.info('Test bitcoind -asmap with empty map file') | self.log.info("Test bitcoind -asmap with empty map file") | ||||
self.stop_node(0) | self.stop_node(0) | ||||
with open(self.default_asmap, "w", encoding="utf-8") as f: | with open(self.default_asmap, "w", encoding="utf-8") as f: | ||||
f.write("") | f.write("") | ||||
msg = f'Error: Could not parse asmap file "{self.default_asmap}"' | msg = f'Error: Could not parse asmap file "{self.default_asmap}"' | ||||
self.node.assert_start_raises_init_error( | self.node.assert_start_raises_init_error( | ||||
extra_args=['-asmap'], expected_msg=msg) | extra_args=["-asmap"], expected_msg=msg | ||||
) | |||||
os.remove(self.default_asmap) | os.remove(self.default_asmap) | ||||
def run_test(self): | def run_test(self): | ||||
self.node = self.nodes[0] | self.node = self.nodes[0] | ||||
self.datadir = os.path.join(self.node.datadir, self.chain) | self.datadir = os.path.join(self.node.datadir, self.chain) | ||||
self.default_asmap = os.path.join(self.datadir, DEFAULT_ASMAP_FILENAME) | self.default_asmap = os.path.join(self.datadir, DEFAULT_ASMAP_FILENAME) | ||||
self.asmap_raw = os.path.join( | self.asmap_raw = os.path.join( | ||||
os.path.dirname( | os.path.dirname(os.path.realpath(__file__)), ASMAP | ||||
os.path.realpath(__file__)), ASMAP) | ) | ||||
self.test_without_asmap_arg() | self.test_without_asmap_arg() | ||||
self.test_asmap_with_absolute_path() | self.test_asmap_with_absolute_path() | ||||
self.test_asmap_with_relative_path() | self.test_asmap_with_relative_path() | ||||
self.test_default_asmap() | self.test_default_asmap() | ||||
self.test_asmap_interaction_with_addrman_containing_entries() | self.test_asmap_interaction_with_addrman_containing_entries() | ||||
self.test_default_asmap_with_missing_file() | self.test_default_asmap_with_missing_file() | ||||
self.test_empty_asmap() | self.test_empty_asmap() | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
AsmapTest().main() | AsmapTest().main() |