14
14
from test_framework .util import assert_equal
15
15
16
16
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
+ ):
18
26
new = []
19
27
tried = []
20
28
INCOMPATIBILITY_BASE = 32
21
- r = MAGIC_BYTES ["regtest" ]
29
+ r = MAGIC_BYTES [net_magic ]
22
30
r += struct .pack ("B" , format )
23
31
r += struct .pack ("B" , INCOMPATIBILITY_BASE + lowest_compatible )
24
32
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 ))
27
35
ADDRMAN_NEW_BUCKET_COUNT = 1 << 10
28
36
r += struct .pack ("i" , ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30 ))
29
37
for _ in range (ADDRMAN_NEW_BUCKET_COUNT ):
30
38
r += struct .pack ("i" , 0 )
31
39
checksum = hash256 (r )
32
- r += checksum
40
+ r += mock_checksum or checksum
33
41
return r
34
42
35
43
@@ -70,7 +78,7 @@ def run_test(self):
70
78
match = ErrorMatch .FULL_REGEX ,
71
79
)
72
80
73
- self .log .info ("Check that corrupt addrman cannot be read" )
81
+ self .log .info ("Check that corrupt addrman cannot be read (EOF) " )
74
82
self .stop_node (0 )
75
83
with open (peers_dat , "wb" ) as f :
76
84
f .write (serialize_addrman ()[:- 1 ])
@@ -79,6 +87,38 @@ def run_test(self):
79
87
match = ErrorMatch .FULL_REGEX ,
80
88
)
81
89
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
+
82
122
self .log .info ("Check that missing addrman is recreated" )
83
123
self .stop_node (0 )
84
124
os .remove (peers_dat )
0 commit comments