linux - 是否有将随机垃圾字节写入文件的命令?

我现在正在对我的应用程序再次损坏文件进行一些测试。但是我发现很难找到测试文件。

所以我想知道是否有一些现有的工具可以将随机/垃圾字节写入某种格式的文件中。

基本上,我需要这个工具来:

  1. 它将随机垃圾字节写入文件。
  2. 它不需要知道文件的格式,我只写随机字节就可以了。
  3. 最好在目标文件的任意位置写入。
  4. 批处理也是一个好处。

谢谢。

最佳答案

/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.
==========

它在基本级别上进行了测试,但您可能会发现需要注意边缘错误情况。随心所欲地使用它。

https://stackoverflow.com/questions/3598622/

相关文章:

linux - 未生成核心转储文件

python - 如何在 Python 中进行并行编程?

php - 如何在 CentOS 6.2 上安装 PHP mbstring

linux - 如何找到今天在 Unix/Linux 中创建的所有文件?

linux - Linux 上的 NuGet : Error getting response st

python - 我什么时候应该使用 ugettext_lazy?

linux - Crontab 每 15 分钟运行一次,除了凌晨 3 点?

linux - 如何给 Linux 用户 sudo 访问权限?

python - 排除 os.walk 中的目录

python - 如何在当前模块上调用 setattr()?