Skip to content

Commit 380130d

Browse files
test: add coverage to feature_addrman.py
I added two new tests that will cover the nNew and nTried tests which add coverage to the if block by checking values larger than our range since we only check for negative values now Co-authored-by: ismaelsadeeq <ask4ismailsadiq@gmail.com>
1 parent 78a983f commit 380130d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

test/functional/feature_addrman.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import struct
1010

1111
from test_framework.messages import ser_uint256, hash256
12+
from test_framework.netutil import ADDRMAN_NEW_BUCKET_COUNT, ADDRMAN_TRIED_BUCKET_COUNT, ADDRMAN_BUCKET_SIZE
1213
from test_framework.p2p import MAGIC_BYTES
1314
from test_framework.test_framework import BitcoinTestFramework
1415
from test_framework.test_node import ErrorMatch
1516
from test_framework.util import assert_equal
1617

17-
1818
def serialize_addrman(
1919
*,
2020
format=1,
@@ -117,17 +117,34 @@ def run_test(self):
117117

118118
self.log.info("Check that corrupt addrman cannot be read (len_tried)")
119119
self.stop_node(0)
120+
max_len_tried = ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE
120121
write_addrman(peers_dat, len_tried=-1)
121122
self.nodes[0].assert_start_raises_init_error(
122-
expected_msg=init_error("Corrupt AddrMan serialization: nTried=-1, should be in \\[0, 16384\\]:.*"),
123+
expected_msg=init_error(f"Corrupt AddrMan serialization: nTried=-1, should be in \\[0, {max_len_tried}\\]:.*"),
124+
match=ErrorMatch.FULL_REGEX,
125+
)
126+
127+
self.log.info("Check that corrupt addrman cannot be read (large len_tried)")
128+
write_addrman(peers_dat, len_tried=max_len_tried + 1)
129+
self.nodes[0].assert_start_raises_init_error(
130+
expected_msg=init_error(f"Corrupt AddrMan serialization: nTried={max_len_tried + 1}, should be in \\[0, {max_len_tried}\\]:.*"),
123131
match=ErrorMatch.FULL_REGEX,
124132
)
125133

126134
self.log.info("Check that corrupt addrman cannot be read (len_new)")
127135
self.stop_node(0)
136+
max_len_new = ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE
128137
write_addrman(peers_dat, len_new=-1)
129138
self.nodes[0].assert_start_raises_init_error(
130-
expected_msg=init_error("Corrupt AddrMan serialization: nNew=-1, should be in \\[0, 65536\\]:.*"),
139+
expected_msg=init_error(f"Corrupt AddrMan serialization: nNew=-1, should be in \\[0, {max_len_new}\\]:.*"),
140+
match=ErrorMatch.FULL_REGEX,
141+
)
142+
143+
self.log.info("Check that corrupt addrman cannot be read (large len_new)")
144+
self.stop_node(0)
145+
write_addrman(peers_dat, len_new=max_len_new + 1)
146+
self.nodes[0].assert_start_raises_init_error(
147+
expected_msg=init_error(f"Corrupt AddrMan serialization: nNew={max_len_new + 1}, should be in \\[0, {max_len_new}\\]:.*"),
131148
match=ErrorMatch.FULL_REGEX,
132149
)
133150

test/functional/test_framework/netutil.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
STATE_LISTEN = '0A'
2626
# STATE_CLOSING = '0B'
2727

28+
# Address manager size constants as defined in addrman_impl.h
29+
ADDRMAN_NEW_BUCKET_COUNT = 1 << 10
30+
ADDRMAN_TRIED_BUCKET_COUNT = 1 << 8
31+
ADDRMAN_BUCKET_SIZE = 1 << 6
32+
2833
def get_socket_inodes(pid):
2934
'''
3035
Get list of socket inodes for process pid.

0 commit comments

Comments
 (0)