Skip to content

Commit 71c6d7f

Browse files
fanquakePastaPastaPasta
authored andcommitted
Merge bitcoin#26653: test, init: perturb file to ensure failure instead of only deleting them
c371cae test, init: perturb file to ensure failure instead of only deleting them (brunoerg) Pull request description: In `feature_init.py` there is a TODO about perturbing the files instead of only testing by deleting them. ```py # TODO: at some point, we should test perturbing the files instead of removing # them, e.g. # # contents = target_file.read_bytes() # tweaked_contents = bytearray(contents) # tweaked_contents[50:250] = b'1' * 200 # target_file.write_bytes(bytes(tweaked_contents)) # # At the moment I can't get this to work (bitcoind loads successfully?) so # investigate doing this later. ``` This PR adds it by writing into the file random bytes and checking whether it throws an error when starting. ACKs for top commit: MarcoFalke: lgtm ACK c371cae Tree-SHA512: d691eee60b91dd9d1b200588608f56b0a10dccd9761a75254b69e0ba5e5866cae14d2f90cb2bd7ec0f95b0617c2562cd33f20892ffd16355b6df770d3806a0ff
1 parent 417f71a commit 71c6d7f

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

test/functional/feature_init.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def sigterm_node():
4242
node.process.terminate()
4343
node.process.wait()
4444

45+
def start_expecting_error(err_fragment):
46+
node.assert_start_raises_init_error(
47+
extra_args=['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1'],
48+
expected_msg=err_fragment,
49+
match=ErrorMatch.PARTIAL_REGEX,
50+
)
51+
4552
def check_clean_start():
4653
"""Ensure that node restarts successfully after various interrupts."""
4754
node.start()
@@ -85,36 +92,27 @@ def check_clean_start():
8592

8693
self.log.info("Test startup errors after removing certain essential files")
8794

88-
files_to_disturb = {
95+
files_to_delete = {
8996
'blocks/index/*.ldb': 'Error opening block database.',
9097
'chainstate/*.ldb': 'Error opening block database.',
9198
'blocks/blk*.dat': 'Error loading block database.',
9299
}
93100

94-
for file_patt, err_fragment in files_to_disturb.items():
101+
files_to_perturb = {
102+
'blocks/index/*.ldb': 'Error opening block database.',
103+
'chainstate/*.ldb': 'Error opening block database.',
104+
'blocks/blk*.dat': 'Error opening block database.',
105+
}
106+
107+
for file_patt, err_fragment in files_to_delete.items():
95108
target_files = list(node.chain_path.glob(file_patt))
96109

97110
for target_file in target_files:
98-
self.log.info(f"Tweaking file to ensure failure {target_file}")
111+
self.log.info(f"Deleting file to ensure failure {target_file}")
99112
bak_path = str(target_file) + ".bak"
100113
target_file.rename(bak_path)
101114

102-
# TODO: at some point, we should test perturbing the files instead of removing
103-
# them, e.g.
104-
#
105-
# contents = target_file.read_bytes()
106-
# tweaked_contents = bytearray(contents)
107-
# tweaked_contents[50:250] = b'1' * 200
108-
# target_file.write_bytes(bytes(tweaked_contents))
109-
#
110-
# At the moment I can't get this to work (bitcoind loads successfully?) so
111-
# investigate doing this later.
112-
113-
node.assert_start_raises_init_error(
114-
extra_args=['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1'],
115-
expected_msg=err_fragment,
116-
match=ErrorMatch.PARTIAL_REGEX,
117-
)
115+
start_expecting_error(err_fragment)
118116

119117
for target_file in target_files:
120118
bak_path = str(target_file) + ".bak"
@@ -124,6 +122,18 @@ def check_clean_start():
124122
check_clean_start()
125123
self.stop_node(0)
126124

125+
for file_patt, err_fragment in files_to_perturb.items():
126+
target_files = list(node.chain_path.glob(file_patt))
127+
128+
for target_file in target_files:
129+
self.log.info(f"Perturbing file to ensure failure {target_file}")
130+
with open(target_file, "rb") as tf_read, open(target_file, "wb") as tf_write:
131+
contents = tf_read.read()
132+
tweaked_contents = bytearray(contents)
133+
tweaked_contents[50:250] = b'1' * 200
134+
tf_write.write(bytes(tweaked_contents))
135+
136+
start_expecting_error(err_fragment)
127137

128138
if __name__ == '__main__':
129139
InitStressTest().main()

0 commit comments

Comments
 (0)