1414from test_framework .util import assert_equal
1515
1616
17- def serialize_addrman (* , format = 1 , lowest_compatible = 3 ):
17+ def serialize_addrman (
18+ * ,
19+ format = 1 ,
20+ lowest_compatible = 3 ,
21+ net_magic = "regtest" ,
22+ len_new = None ,
23+ len_tried = None ,
24+ mock_checksum = None ,
25+ ):
1826 new = []
1927 tried = []
2028 INCOMPATIBILITY_BASE = 32
21- r = MAGIC_BYTES ["regtest" ]
29+ r = MAGIC_BYTES [net_magic ]
2230 r += struct .pack ("B" , format )
2331 r += struct .pack ("B" , INCOMPATIBILITY_BASE + lowest_compatible )
2432 r += ser_uint256 (1 )
25- r += struct .pack ("i" , len (new ))
26- r += struct .pack ("i" , len (tried ))
33+ r += struct .pack ("i" , len_new or len (new ))
34+ r += struct .pack ("i" , len_tried or len (tried ))
2735 ADDRMAN_NEW_BUCKET_COUNT = 1 << 10
2836 r += struct .pack ("i" , ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30 ))
2937 for _ in range (ADDRMAN_NEW_BUCKET_COUNT ):
3038 r += struct .pack ("i" , 0 )
3139 checksum = hash256 (r )
32- r += checksum
40+ r += mock_checksum or checksum
3341 return r
3442
3543
@@ -70,7 +78,7 @@ def run_test(self):
7078 match = ErrorMatch .FULL_REGEX ,
7179 )
7280
73- self .log .info ("Check that corrupt addrman cannot be read" )
81+ self .log .info ("Check that corrupt addrman cannot be read (EOF) " )
7482 self .stop_node (0 )
7583 with open (peers_dat , "wb" ) as f :
7684 f .write (serialize_addrman ()[:- 1 ])
@@ -79,6 +87,38 @@ def run_test(self):
7987 match = ErrorMatch .FULL_REGEX ,
8088 )
8189
90+ self .log .info ("Check that corrupt addrman cannot be read (magic)" )
91+ self .stop_node (0 )
92+ write_addrman (peers_dat , net_magic = "signet" )
93+ self .nodes [0 ].assert_start_raises_init_error (
94+ expected_msg = init_error ("Invalid network magic number" ),
95+ match = ErrorMatch .FULL_REGEX ,
96+ )
97+
98+ self .log .info ("Check that corrupt addrman cannot be read (checksum)" )
99+ self .stop_node (0 )
100+ write_addrman (peers_dat , mock_checksum = b"ab" * 32 )
101+ self .nodes [0 ].assert_start_raises_init_error (
102+ expected_msg = init_error ("Checksum mismatch, data corrupted" ),
103+ match = ErrorMatch .FULL_REGEX ,
104+ )
105+
106+ self .log .info ("Check that corrupt addrman cannot be read (len_tried)" )
107+ self .stop_node (0 )
108+ write_addrman (peers_dat , len_tried = - 1 )
109+ self .nodes [0 ].assert_start_raises_init_error (
110+ expected_msg = init_error ("Corrupt CAddrMan serialization: nTried=-1, should be in \\ [0, 16384\\ ]:.*" ),
111+ match = ErrorMatch .FULL_REGEX ,
112+ )
113+
114+ self .log .info ("Check that corrupt addrman cannot be read (len_new)" )
115+ self .stop_node (0 )
116+ write_addrman (peers_dat , len_new = - 1 )
117+ self .nodes [0 ].assert_start_raises_init_error (
118+ expected_msg = init_error ("Corrupt CAddrMan serialization: nNew=-1, should be in \\ [0, 65536\\ ]:.*" ),
119+ match = ErrorMatch .FULL_REGEX ,
120+ )
121+
82122 self .log .info ("Check that missing addrman is recreated" )
83123 self .stop_node (0 )
84124 os .remove (peers_dat )
0 commit comments