diff options
author | merge-script <falke.marco@gmail.com> | 2021-09-21 09:28:45 +0200 |
---|---|---|
committer | merge-script <falke.marco@gmail.com> | 2021-09-21 09:28:45 +0200 |
commit | 0c1a39390f67ca2ab89354147dbba2637ece298d (patch) | |
tree | 600ad82c691eb9f94309653c64d56e006edf0470 /test | |
parent | 89447a63b9ccf427bdd45562fc1ec67a8f4266ab (diff) | |
parent | faa81f948663a7155335986b028fb2ce19049a79 (diff) |
Merge bitcoin/bitcoin#23041: test: Add addrman deserialization error tests
faa81f948663a7155335986b028fb2ce19049a79 test: Add addrman deserialization error tests (MarcoFalke)
Pull request description:
Add missing test coverage
ACKs for top commit:
jonatack:
Light code review ACK faa81f948663a7155335986b028fb2ce19049a79 and ran the test
Tree-SHA512: 8b254ba912c83473125faaf7df02a33a99840b40460bdce1486991a01de9ba6371c053354318f09b69fdc18c823bca3f2f7d341db0f8950e22d8435acbaa9cf5
Diffstat (limited to 'test')
-rwxr-xr-x | test/functional/feature_addrman.py | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/test/functional/feature_addrman.py b/test/functional/feature_addrman.py index ee421c89b5..42afd74ac9 100755 --- a/test/functional/feature_addrman.py +++ b/test/functional/feature_addrman.py @@ -14,22 +14,30 @@ from test_framework.test_node import ErrorMatch from test_framework.util import assert_equal -def serialize_addrman(*, format=1, lowest_compatible=3): +def serialize_addrman( + *, + format=1, + lowest_compatible=3, + net_magic="regtest", + len_new=None, + len_tried=None, + mock_checksum=None, +): new = [] tried = [] INCOMPATIBILITY_BASE = 32 - r = MAGIC_BYTES["regtest"] + r = MAGIC_BYTES[net_magic] r += struct.pack("B", format) r += struct.pack("B", INCOMPATIBILITY_BASE + lowest_compatible) r += ser_uint256(1) - r += struct.pack("i", len(new)) - r += struct.pack("i", len(tried)) + r += struct.pack("i", len_new or len(new)) + r += struct.pack("i", len_tried or len(tried)) ADDRMAN_NEW_BUCKET_COUNT = 1 << 10 r += struct.pack("i", ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30)) for _ in range(ADDRMAN_NEW_BUCKET_COUNT): r += struct.pack("i", 0) checksum = hash256(r) - r += checksum + r += mock_checksum or checksum return r @@ -70,7 +78,7 @@ class AddrmanTest(BitcoinTestFramework): match=ErrorMatch.FULL_REGEX, ) - self.log.info("Check that corrupt addrman cannot be read") + self.log.info("Check that corrupt addrman cannot be read (EOF)") self.stop_node(0) with open(peers_dat, "wb") as f: f.write(serialize_addrman()[:-1]) @@ -79,6 +87,38 @@ class AddrmanTest(BitcoinTestFramework): match=ErrorMatch.FULL_REGEX, ) + self.log.info("Check that corrupt addrman cannot be read (magic)") + self.stop_node(0) + write_addrman(peers_dat, net_magic="signet") + self.nodes[0].assert_start_raises_init_error( + expected_msg=init_error("Invalid network magic number"), + match=ErrorMatch.FULL_REGEX, + ) + + self.log.info("Check that corrupt addrman cannot be read (checksum)") + self.stop_node(0) + write_addrman(peers_dat, mock_checksum=b"ab" * 32) + self.nodes[0].assert_start_raises_init_error( + expected_msg=init_error("Checksum mismatch, data corrupted"), + match=ErrorMatch.FULL_REGEX, + ) + + self.log.info("Check that corrupt addrman cannot be read (len_tried)") + self.stop_node(0) + write_addrman(peers_dat, len_tried=-1) + self.nodes[0].assert_start_raises_init_error( + expected_msg=init_error("Corrupt CAddrMan serialization: nTried=-1, should be in \\[0, 16384\\]:.*"), + match=ErrorMatch.FULL_REGEX, + ) + + self.log.info("Check that corrupt addrman cannot be read (len_new)") + self.stop_node(0) + write_addrman(peers_dat, len_new=-1) + self.nodes[0].assert_start_raises_init_error( + expected_msg=init_error("Corrupt CAddrMan serialization: nNew=-1, should be in \\[0, 65536\\]:.*"), + match=ErrorMatch.FULL_REGEX, + ) + self.log.info("Check that missing addrman is recreated") self.stop_node(0) os.remove(peers_dat) |