问题
I am trying to test my Python program, which takes in either .zip or .Z files and decompresses them using Python's zipfile
module or Unix's gzip
, respectively. It makes sure that the filetype is either .zip or .Z (in the latter case, using Unix's file
command) before trying to do anything. I wanted to test my error handling in the very rare case in which a verified archive file errors out while being decompressed. So basically, I want to feed it a corrupt .Z file.
Someone suggested that I could use Unix's dd
command to just mess up a good .Z file and use that as my bad input. I couldn't find any examples for using dd
for this use case and was hoping someone could provide a simple example. I know I shouldn't mess with the header, since that's where the metadata is that tells us it is a .Z file. So I know I need to mess up some of the middle and some of the end... Thanks for any help.
回答1:
You could just have used a hex editor like hexedit
.
Since you ask though,
dd if=/dev/urandom of=yourfile.z bs=1024 seek=$((RANDOM%10)) count=1 conv=notrunc
will rewrite garbage to a random 1024b block out of the first ten in a file.
来源:https://stackoverflow.com/questions/24130538/testing-intentionally-corrupt-a-z-file-using-dd