测试-使用'dd'故意损坏.Z文件 [英] testing - intentionally corrupt a .Z file using 'dd'

查看:150
本文介绍了测试-使用'dd'故意损坏.Z文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试我的Python程序,该程序可以接收.zip或.Z文件,并使用Python的 zipfile 模块或Unix的解压缩它们。 gzip 。在尝试执行任何操作之前,请确保文件类型为.zip或.Z(在后一种情况下,使用Unix的 file 命令)。我想在极少数情况下测试我的错误处理,在这种情况下,经过验证的存档文件在解压缩时会出错。因此,基本上,我想向它提供一个损坏的.Z文件。



有人建议我可以使用Unix的 dd 命令来弄乱一个好的.Z文件并将其用作我不好的输入。我找不到在此用例中使用 dd 的任何示例,希望有人可以提供一个简单的示例。我知道我不应该弄乱标题,因为元数据告诉我们它是一个.Z文件。所以我知道我需要弄混一些中间和一些末尾...谢谢您的帮助。

解决方案

您可以使用十六进制编辑器,例如 hexedit 。 / p>

由于您一直询问,

  dd if = / dev / urandom of = yourfile.z bs = 1024 seek = $((RANDOM%10))count = 1 conv = notrunc 

会将垃圾重写为文件中前十个随机的1024b块。


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.

解决方案

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.

这篇关于测试-使用'dd'故意损坏.Z文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆