From 08b992675cf8d946db19b7bea747fa1085fdb2a2 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 28 Dec 2019 12:50:20 +0100 Subject: test: add feature_asmap functional tests to verify node behaviour and debug log when launching bitcoind in these cases: 1. `bitcoind` with no -asmap arg, using /16 prefix for IP bucketing 2. `bitcoind -asmap=`, using the unit test skeleton asmap 3. `bitcoind -asmap/-asmap=` with no file specified, using the default asmap 4. `bitcoind -asmap` with no file specified, and a missing default asmap file The tests are order-independent. The slowest test (missing default asmap file) is placed last. --- test/functional/feature_asmap.py | 83 ++++++++++++++++++++++++++++++++++++++++ test/functional/test_runner.py | 1 + 2 files changed, 84 insertions(+) create mode 100755 test/functional/feature_asmap.py (limited to 'test') diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py new file mode 100755 index 0000000000..a196d7cc9d --- /dev/null +++ b/test/functional/feature_asmap.py @@ -0,0 +1,83 @@ +#!/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 + +2. `bitcoind -asmap=`, using the unit test skeleton asmap + +3. `bitcoind -asmap/-asmap=` with no file specified, using the default asmap + +4. `bitcoind -asmap` with no file specified, and a missing default asmap file + +The tests are order-independent. The slowest test (missing default asmap file) +is placed last. + +""" +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): + return ['Opened asmap file "{}" (59 bytes) from disk.'.format(filename), + 'Using asmap version {} for IP bucketing.'.format(VERSION)] + +class AsmapTest(BitcoinTestFramework): + def set_test_params(self): + self.setup_clean_chain = False + 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) + + def test_asmap_with_relative_path(self): + self.log.info('Test bitcoind -asmap=') + 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)): + self.start_node(0, ['-asmap={}'.format(name)]) + os.remove(filename) + + def test_default_asmap(self): + shutil.copyfile(self.asmap_raw, self.default_asmap) + for arg in ['-asmap', '-asmap=']: + self.log.info('Test bitcoind {} (using default map file)'.format(arg)) + 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) + msg = "Error: Could not find or parse specified asmap: '\"{}\"'".format(self.default_asmap) + self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg) + + 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() + self.test_asmap_with_relative_path() + self.test_default_asmap() + self.test_default_asmap_with_missing_file() + + +if __name__ == '__main__': + AsmapTest().main() diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index 06d939afb7..ac746ea9bf 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -101,6 +101,7 @@ BASE_SCRIPTS = [ 'rpc_txoutproof.py', 'wallet_listreceivedby.py', 'wallet_abandonconflict.py', + 'feature_asmap.py', 'feature_csv_activation.py', 'rpc_rawtransaction.py', 'wallet_address_types.py', -- cgit v1.2.3 From 81c38a24975f34e5894efe3d1aaf45ff6a8cee4a Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 29 Dec 2019 18:45:59 +0100 Subject: config: enable passing -asmap an absolute file path - allow passing an absolute file path to the -asmap config arg - update the -asmap config help - add a functional test in feature_asmap.py --- test/functional/feature_asmap.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index a196d7cc9d..07dde84775 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -8,11 +8,13 @@ Verify node behaviour and debug log when launching bitcoind in these cases: 1. `bitcoind` with no -asmap arg, using /16 prefix for IP bucketing -2. `bitcoind -asmap=`, using the unit test skeleton asmap +2. `bitcoind -asmap=`, using the unit test skeleton asmap -3. `bitcoind -asmap/-asmap=` with no file specified, using the default asmap +3. `bitcoind -asmap=`, using the unit test skeleton asmap -4. `bitcoind -asmap` with no file specified, and a missing default asmap file +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 The tests are order-independent. The slowest test (missing default asmap file) is placed last. @@ -42,6 +44,15 @@ class AsmapTest(BitcoinTestFramework): with self.node.assert_debug_log(['Using /16 prefix for IP bucketing']): self.start_node(0) + def test_asmap_with_absolute_path(self): + self.log.info('Test bitcoind -asmap=') + 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)): + self.start_node(0, ['-asmap={}'.format(filename)]) + os.remove(filename) + def test_asmap_with_relative_path(self): self.log.info('Test bitcoind -asmap=') self.stop_node(0) @@ -74,6 +85,7 @@ class AsmapTest(BitcoinTestFramework): self.asmap_raw = os.path.join(os.path.dirname(os.path.realpath(__file__)), ASMAP) self.test_without_asmap_arg() + self.test_asmap_with_absolute_path() self.test_asmap_with_relative_path() self.test_default_asmap() self.test_default_asmap_with_missing_file() -- cgit v1.2.3 From b8d0412b213df18f23bf8677ab94068c6cca9f51 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 29 Dec 2019 18:52:10 +0100 Subject: config: separate the asmap finding and parsing checks and update the tests. --- test/functional/feature_asmap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index 07dde84775..6b9d90960a 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -75,7 +75,7 @@ class AsmapTest(BitcoinTestFramework): def test_default_asmap_with_missing_file(self): self.log.info('Test bitcoind -asmap with missing default map file') self.stop_node(0) - msg = "Error: Could not find or parse specified asmap: '\"{}\"'".format(self.default_asmap) + msg = "Error: Could not find asmap file '\"{}\"'".format(self.default_asmap) self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg) def run_test(self): -- cgit v1.2.3 From dcaf543ba0241f9219cea70b67c7b066d4c9ca9b Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 29 Dec 2019 19:51:04 +0100 Subject: test: add functional test for an empty, unparsable asmap This is now testable after separating the asmap finding and parsing checks in the previous commit. --- test/functional/feature_asmap.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index 6b9d90960a..8f5a77c92d 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -16,8 +16,10 @@ Verify node behaviour and debug log when launching bitcoind in these cases: 5. `bitcoind -asmap` with no file specified and a missing default asmap file -The tests are order-independent. The slowest test (missing default asmap file) -is placed last. +6. `bitcoind -asmap` with an empty (unparsable) default asmap file + +The tests are order-independent. The slowest tests (missing default asmap and +empty asmap) are placed last. """ import os @@ -78,6 +80,15 @@ class AsmapTest(BitcoinTestFramework): msg = "Error: Could not find asmap file '\"{}\"'".format(self.default_asmap) self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg) + 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("") + msg = "Error: Could not parse asmap file \"{}\"".format(self.default_asmap) + self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg) + os.remove(self.default_asmap) + def run_test(self): self.node = self.nodes[0] self.datadir = os.path.join(self.node.datadir, self.chain) @@ -89,6 +100,7 @@ class AsmapTest(BitcoinTestFramework): self.test_asmap_with_relative_path() self.test_default_asmap() self.test_default_asmap_with_missing_file() + self.test_empty_asmap() if __name__ == '__main__': -- cgit v1.2.3 From 819fb5549b0d02477f47b3c40338071f37b6d885 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 29 Dec 2019 18:54:33 +0100 Subject: logging: asmap logging and #include fixups - move asmap #includes to sorted positions in addrman and init (move-only) - remove redundant quotes in asmap InitError, update test - remove full stops from asmap logging to be consistent with debug logging, update tests --- test/functional/feature_asmap.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index 8f5a77c92d..dd12633f50 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -32,8 +32,8 @@ ASMAP = '../../src/test/data/asmap.raw' # path to unit test skeleton asmap VERSION = 'fec61fa21a9f46f3b17bdcd660d7f4cd90b966aad3aec593c99b35f0aca15853' def expected_messages(filename): - return ['Opened asmap file "{}" (59 bytes) from disk.'.format(filename), - 'Using asmap version {} for IP bucketing.'.format(VERSION)] + return ['Opened asmap file "{}" (59 bytes) from disk'.format(filename), + 'Using asmap version {} for IP bucketing'.format(VERSION)] class AsmapTest(BitcoinTestFramework): def set_test_params(self): @@ -77,7 +77,7 @@ class AsmapTest(BitcoinTestFramework): def test_default_asmap_with_missing_file(self): self.log.info('Test bitcoind -asmap with missing default map file') self.stop_node(0) - msg = "Error: Could not find asmap file '\"{}\"'".format(self.default_asmap) + msg = "Error: Could not find asmap file \"{}\"".format(self.default_asmap) self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg) def test_empty_asmap(self): -- cgit v1.2.3 From 1ba3e1cc21150abe632a5b82a1a38998b33815dc Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Wed, 29 Jan 2020 16:38:25 +0100 Subject: init: move asmap code earlier in init process and update feature_asmap.py and test_runner.py This commit moves the asmap init.cpp code from the end of "Step 12: start node" to "Step 6: network initialization" to provide feedback on passing an -asmap config arg much more quickly. This change speeds up the feature_asmap.py functional test file from 60 to 5 seconds by accelerating the 2 tests that use `assert_start_raises_init_error`. Credit to Wladimir J. van der Laan for the suggestion. --- test/functional/feature_asmap.py | 3 +-- test/functional/test_runner.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index dd12633f50..2c6553fbe2 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -18,8 +18,7 @@ Verify node behaviour and debug log when launching bitcoind in these cases: 6. `bitcoind -asmap` with an empty (unparsable) default asmap file -The tests are order-independent. The slowest tests (missing default asmap and -empty asmap) are placed last. +The tests are order-independent. """ import os diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index ac746ea9bf..2036d20852 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -101,7 +101,6 @@ BASE_SCRIPTS = [ 'rpc_txoutproof.py', 'wallet_listreceivedby.py', 'wallet_abandonconflict.py', - 'feature_asmap.py', 'feature_csv_activation.py', 'rpc_rawtransaction.py', 'wallet_address_types.py', @@ -207,6 +206,7 @@ BASE_SCRIPTS = [ 'p2p_dos_header_tree.py', 'p2p_unrequested_blocks.py', 'feature_includeconf.py', + 'feature_asmap.py', 'rpc_deriveaddresses.py', 'rpc_deriveaddresses.py --usecli', 'rpc_scantxoutset.py', -- cgit v1.2.3