Fan*_*Fan 53 linux testing command-line
我现在正在做我的应用程序的一些测试再次损坏的文件.但我发现很难找到测试文件.
所以我想知道是否有一些现有的工具,它们可以将随机/垃圾字节写入某种格式的文件中.
基本上,我需要这个工具:
谢谢.
pax*_*blo 93
该/dev/urandom伪设备,连同dd,能为你做到这一点:
dd if=/dev/urandom of=newfile bs=1M count=10
Run Code Online (Sandbox Code Playgroud)
这将创建一个newfile大小为10M 的文件.
/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";
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)
它包含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.
==========
Run Code Online (Sandbox Code Playgroud)
它在基本级别进行了测试,但您可能会发现存在需要处理的边缘错误情况.用它做你想做的事.
jkr*_*mer 25
为了完整起见,这是另一种方法:
shred -s 10 - > my-file
Run Code Online (Sandbox Code Playgroud)
将10个随机字节写入stdout并将其重定向到文件.shred通常用于销毁(安全覆盖)数据,但它也可用于创建新的随机文件.因此,如果您已经有一个要填充随机数据的文件,请使用:
shred my-existing-file
Run Code Online (Sandbox Code Playgroud)
你可以阅读/dev/random:
# generate a 50MB file named `random.stuff` filled with random stuff ...
dd if=/dev/random of=random.stuff bs=1000000 count=50
Run Code Online (Sandbox Code Playgroud)
您也可以用人类可读的方式指定大小:
# generate just 2MB ...
dd if=/dev/random of=random.stuff bs=1M count=2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46848 次 |
| 最近记录: |