2019-12-28 12:50:20 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2020 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Test asmap config argument for ASN-based IP bucketing.
|
|
|
|
|
|
|
|
Verify node behaviour and debug log when launching bitcoind in these cases:
|
|
|
|
|
|
|
|
1. `bitcoind` with no -asmap arg, using /16 prefix for IP bucketing
|
|
|
|
|
2019-12-29 18:45:59 +01:00
|
|
|
2. `bitcoind -asmap=<absolute path>`, using the unit test skeleton asmap
|
2019-12-28 12:50:20 +01:00
|
|
|
|
2019-12-29 18:45:59 +01:00
|
|
|
3. `bitcoind -asmap=<relative path>`, using the unit test skeleton asmap
|
2019-12-28 12:50:20 +01:00
|
|
|
|
2019-12-29 18:45:59 +01:00
|
|
|
4. `bitcoind -asmap/-asmap=` with no file specified, using the default asmap
|
|
|
|
|
|
|
|
5. `bitcoind -asmap` with no file specified and a missing default asmap file
|
2019-12-28 12:50:20 +01:00
|
|
|
|
2019-12-29 19:51:04 +01:00
|
|
|
6. `bitcoind -asmap` with an empty (unparsable) default asmap file
|
|
|
|
|
2020-01-29 16:38:25 +01:00
|
|
|
The tests are order-independent.
|
2019-12-28 12:50:20 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
|
|
|
|
DEFAULT_ASMAP_FILENAME = 'ip_asn.map' # defined in src/init.cpp
|
|
|
|
ASMAP = '../../src/test/data/asmap.raw' # path to unit test skeleton asmap
|
|
|
|
VERSION = 'fec61fa21a9f46f3b17bdcd660d7f4cd90b966aad3aec593c99b35f0aca15853'
|
|
|
|
|
|
|
|
def expected_messages(filename):
|
2021-06-11 07:33:51 +02:00
|
|
|
return [f'Opened asmap file "{filename}" (59 bytes) from disk',
|
|
|
|
f'Using asmap version {VERSION} for IP bucketing']
|
2019-12-28 12:50:20 +01:00
|
|
|
|
|
|
|
class AsmapTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.num_nodes = 1
|
|
|
|
|
|
|
|
def test_without_asmap_arg(self):
|
|
|
|
self.log.info('Test bitcoind with no -asmap arg passed')
|
|
|
|
self.stop_node(0)
|
|
|
|
with self.node.assert_debug_log(['Using /16 prefix for IP bucketing']):
|
|
|
|
self.start_node(0)
|
|
|
|
|
2019-12-29 18:45:59 +01:00
|
|
|
def test_asmap_with_absolute_path(self):
|
|
|
|
self.log.info('Test bitcoind -asmap=<absolute path>')
|
|
|
|
self.stop_node(0)
|
|
|
|
filename = os.path.join(self.datadir, 'my-map-file.map')
|
|
|
|
shutil.copyfile(self.asmap_raw, filename)
|
|
|
|
with self.node.assert_debug_log(expected_messages(filename)):
|
2021-06-11 07:33:51 +02:00
|
|
|
self.start_node(0, [f'-asmap={filename}'])
|
2019-12-29 18:45:59 +01:00
|
|
|
os.remove(filename)
|
|
|
|
|
2019-12-28 12:50:20 +01:00
|
|
|
def test_asmap_with_relative_path(self):
|
|
|
|
self.log.info('Test bitcoind -asmap=<relative path>')
|
|
|
|
self.stop_node(0)
|
|
|
|
name = 'ASN_map'
|
|
|
|
filename = os.path.join(self.datadir, name)
|
|
|
|
shutil.copyfile(self.asmap_raw, filename)
|
|
|
|
with self.node.assert_debug_log(expected_messages(filename)):
|
2021-06-11 07:33:51 +02:00
|
|
|
self.start_node(0, [f'-asmap={name}'])
|
2019-12-28 12:50:20 +01:00
|
|
|
os.remove(filename)
|
|
|
|
|
|
|
|
def test_default_asmap(self):
|
|
|
|
shutil.copyfile(self.asmap_raw, self.default_asmap)
|
|
|
|
for arg in ['-asmap', '-asmap=']:
|
2021-06-11 07:33:51 +02:00
|
|
|
self.log.info(f'Test bitcoind {arg} (using default map file)')
|
2019-12-28 12:50:20 +01:00
|
|
|
self.stop_node(0)
|
|
|
|
with self.node.assert_debug_log(expected_messages(self.default_asmap)):
|
|
|
|
self.start_node(0, [arg])
|
|
|
|
os.remove(self.default_asmap)
|
|
|
|
|
|
|
|
def test_default_asmap_with_missing_file(self):
|
|
|
|
self.log.info('Test bitcoind -asmap with missing default map file')
|
|
|
|
self.stop_node(0)
|
2021-06-11 07:33:51 +02:00
|
|
|
msg = f"Error: Could not find asmap file \"{self.default_asmap}\""
|
2019-12-28 12:50:20 +01:00
|
|
|
self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg)
|
|
|
|
|
2019-12-29 19:51:04 +01:00
|
|
|
def test_empty_asmap(self):
|
|
|
|
self.log.info('Test bitcoind -asmap with empty map file')
|
|
|
|
self.stop_node(0)
|
|
|
|
with open(self.default_asmap, "w", encoding="utf-8") as f:
|
|
|
|
f.write("")
|
2021-06-11 07:33:51 +02:00
|
|
|
msg = f"Error: Could not parse asmap file \"{self.default_asmap}\""
|
2019-12-29 19:51:04 +01:00
|
|
|
self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg)
|
|
|
|
os.remove(self.default_asmap)
|
|
|
|
|
2019-12-28 12:50:20 +01:00
|
|
|
def run_test(self):
|
|
|
|
self.node = self.nodes[0]
|
|
|
|
self.datadir = os.path.join(self.node.datadir, self.chain)
|
|
|
|
self.default_asmap = os.path.join(self.datadir, DEFAULT_ASMAP_FILENAME)
|
|
|
|
self.asmap_raw = os.path.join(os.path.dirname(os.path.realpath(__file__)), ASMAP)
|
|
|
|
|
|
|
|
self.test_without_asmap_arg()
|
2019-12-29 18:45:59 +01:00
|
|
|
self.test_asmap_with_absolute_path()
|
2019-12-28 12:50:20 +01:00
|
|
|
self.test_asmap_with_relative_path()
|
|
|
|
self.test_default_asmap()
|
|
|
|
self.test_default_asmap_with_missing_file()
|
2019-12-29 19:51:04 +01:00
|
|
|
self.test_empty_asmap()
|
2019-12-28 12:50:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
AsmapTest().main()
|