我现在正在对我的应用程序进行一些测试,再次测试损坏的文件.但我发现很难找到测试文件.
所以我想知道是否有一些现有的工具,可以将随机/垃圾字节写入某种格式的文件.
基本上,我需要这个工具来:
- 它将随机垃圾字节写入文件.
- 它不需要知道文件的格式,只需要写随机字节就可以了.
- 最好在目标文件的任意位置写入.
- 批量处理也是一个好处.
谢谢
我现在正在对我的应用程序进行一些测试,再次测试损坏的文件.但我发现很难找到测试文件.
所以我想知道是否有一些现有的工具,可以将随机/垃圾字节写入某种格式的文件.
基本上,我需要这个工具来:
谢谢
/dev/urandom
伪设备和dd
可以为您做到这一点:
dd if=/dev/urandom of=newfile bs=1M count=10
这将创建一个大小为10M的文件newfile
.
如果没有足够的随机性,/dev/random
设备通常会阻塞,而urandom
设备则不会阻塞.如果你把随机性用于加密级别的东西,你可以避开urandom
.对于其他任何事情,它应该足够了,而且很可能更快.
如果只想损坏文件的一部分(而不是整个文件),只需使用C风格的随机函数即可.只需使用rnd()
计算偏移量和长度n
,然后使用它n
次获取随机字节以覆盖文件.
下面的Perl脚本展示了如何做到这一点(无需担心编译C代码):
use strict;
use warnings;
sub corrupt ($$$$) {
# Get parameters, names should be self-explanatory.
my $filespec = shift;
my $mincount = shift;
my $maxcount = shift;
my $charset = shift;
# Work out position and size of corruption.
my @fstat = stat ($filespec);
my $size = $fstat[7];
my $count = $mincount + int (rand ($maxcount + 1 - $mincount));
my $pos = 0;
if ($count >= $size) {
$count = $size;
} else {
$pos = int (rand ($size - $count));
}
# Output for debugging purposes.
my $last = $pos + $count - 1;
print "'$filespec', $size bytes, corrupting $pos through $last\n";
# Open file, seek to position, corrupt and close.
open (my $fh, "+<$filespec") || die "Can't open $filespec: $!";
seek ($fh, $pos, 0);
while ($count-- > 0) {
my $newval = substr ($charset, int (rand (length ($charset) + 1)), 1);
print $fh $newval;
}
close ($fh);
}
# Test harness.
system ("echo =========="); #DEBUG
system ("cp base-testfile testfile"); #DEBUG
system ("cat testfile"); #DEBUG
system ("echo =========="); #DEBUG
corrupt ("testfile", 8, 16, "ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
system ("echo =========="); #DEBUG
system ("cat testfile"); #DEBUG
system ("echo =========="); #DEBUG
它由调用的corrupt
函数组成,其中包含文件名、最小和最大损坏大小,以及用于绘制损坏的字符集.底部的代码只是单元测试代码.下面是一些示例输出,您可以看到文件的一部分已损坏:
==========
this is a file with nothing in it except for lowercase
letters (and spaces and punctuation and newlines).
that will make it easy to detect corruptions from the
test program since the character range there is from
uppercase a through z.
i have to make it big enough so that the random stuff
will work nicely, which is why i am waffling on a bit.
==========
'testfile', 344 bytes, corrupting 122 through 135
==========
this is a file with nothing in it except for lowercase
letters (and spaces and punctuation and newlines).
that will make iFHCGZF VJ GZDYct corruptions from the
test program since the character range there is from
uppercase a through z.
i have to make it big enough so that the random stuff
will work nicely, which is why i am waffling on a bit.
==========
它是在一个基本的水平上测试的,但是你可能会发现有一些边缘错误的情况需要处理.你想怎么做就怎么做.