|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2020 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 asmap config argument for ASN-based IP bucketing. |
| 6 | +
|
| 7 | +Verify node behaviour and debug log when launching bitcoind in these cases: |
| 8 | +
|
| 9 | +1. `bitcoind` with no -asmap arg, using /16 prefix for IP bucketing |
| 10 | +
|
| 11 | +2. `bitcoind -asmap=<relative path>`, using the unit test skeleton asmap |
| 12 | +
|
| 13 | +3. `bitcoind -asmap/-asmap=` with no file specified, using the default asmap |
| 14 | +
|
| 15 | +4. `bitcoind -asmap` with no file specified, and a missing default asmap file |
| 16 | +
|
| 17 | +The tests are order-independent. The slowest test (missing default asmap file) |
| 18 | +is placed last. |
| 19 | +
|
| 20 | +""" |
| 21 | +import os |
| 22 | +import shutil |
| 23 | + |
| 24 | +from test_framework.test_framework import PivxTestFramework |
| 25 | + |
| 26 | +DEFAULT_ASMAP_FILENAME = 'ip_asn.map' # defined in src/init.cpp |
| 27 | +ASMAP = '../../src/test/data/asmap.raw' # path to unit test skeleton asmap |
| 28 | +VERSION = 'fec61fa21a9f46f3b17bdcd660d7f4cd90b966aad3aec593c99b35f0aca15853' |
| 29 | + |
| 30 | +def expected_messages(filename): |
| 31 | + return ['Opened asmap file "{}" (59 bytes) from disk.'.format(filename), |
| 32 | + 'Using asmap version {} for IP bucketing.'.format(VERSION)] |
| 33 | + |
| 34 | +class AsmapTest(PivxTestFramework): |
| 35 | + def set_test_params(self): |
| 36 | + self.setup_clean_chain = False |
| 37 | + self.num_nodes = 1 |
| 38 | + |
| 39 | + def test_without_asmap_arg(self): |
| 40 | + self.log.info('Test bitcoind with no -asmap arg passed') |
| 41 | + self.stop_node(0) |
| 42 | + with self.node.assert_debug_log(['Using /16 prefix for IP bucketing']): |
| 43 | + self.start_node(0) |
| 44 | + |
| 45 | + def test_asmap_with_relative_path(self): |
| 46 | + self.log.info('Test bitcoind -asmap=<relative path>') |
| 47 | + self.stop_node(0) |
| 48 | + name = 'ASN_map' |
| 49 | + filename = os.path.join(self.datadir, name) |
| 50 | + shutil.copyfile(self.asmap_raw, filename) |
| 51 | + with self.node.assert_debug_log(expected_messages(filename)): |
| 52 | + self.start_node(0, ['-asmap={}'.format(name)]) |
| 53 | + os.remove(filename) |
| 54 | + |
| 55 | + def test_default_asmap(self): |
| 56 | + shutil.copyfile(self.asmap_raw, self.default_asmap) |
| 57 | + for arg in ['-asmap', '-asmap=']: |
| 58 | + self.log.info('Test bitcoind {} (using default map file)'.format(arg)) |
| 59 | + self.stop_node(0) |
| 60 | + with self.node.assert_debug_log(expected_messages(self.default_asmap)): |
| 61 | + self.start_node(0, [arg]) |
| 62 | + os.remove(self.default_asmap) |
| 63 | + |
| 64 | + def test_default_asmap_with_missing_file(self): |
| 65 | + self.log.info('Test bitcoind -asmap with missing default map file') |
| 66 | + self.stop_node(0) |
| 67 | + msg = "Error: Could not find or parse specified asmap: '\"{}\"'".format(self.default_asmap) |
| 68 | + self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg) |
| 69 | + |
| 70 | + def run_test(self): |
| 71 | + self.node = self.nodes[0] |
| 72 | + self.datadir = os.path.join(self.node.datadir, 'regtest') |
| 73 | + self.default_asmap = os.path.join(self.datadir, DEFAULT_ASMAP_FILENAME) |
| 74 | + self.asmap_raw = os.path.join(os.path.dirname(os.path.realpath(__file__)), ASMAP) |
| 75 | + |
| 76 | + self.test_without_asmap_arg() |
| 77 | + self.test_asmap_with_relative_path() |
| 78 | + self.test_default_asmap() |
| 79 | + #self.test_default_asmap_with_missing_file() // fixme |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + AsmapTest().main() |
0 commit comments