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

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

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

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

它是在一个基本的水平上测试的,但是你可能会发现有一些边缘错误的情况需要处理.你想怎么做就怎么做.

Linux相关问答推荐

使用awk以相同的时间戳重新排列

空字符串和空文件的区别

没有发现运行时依赖关系,但它在S的运行时路径中有吗?

如何判断程序是在终端上运行还是在后台运行?

JSON 转义 CURL/JQ 输出

Linux 中大型 TSV 的条件编辑

从另一个文件中的大文件中查找行的最快方法

如何在 DolphinDB 中递归查找目录中的所有文件?

从 curl 命令输出中获取值

如何使用 awk 重新排列列?

从 Ansible 中的 shell 命令输出中提取特定数据

如何在守护进程中使用 popen() 和 pclose() 获取通过管道执行的 shell 命令的正确退出代码?

在 SLURM 作业(job)脚本中设置和传递字符串变量

如果 bash 中已经存在文件名,则创建新文件但添加数字

增加 mysql docker 中的 max_allowed_pa​​cket 大小

打印当前一周的星期一的日期(在 bash 中)

NGINX:connect() 到 unix:/var/run/php7.0-fpm.sock 失败(2:没有这样的文件或目录)

比较linux中两个未排序的列表,列出第二个文件中的唯一性

linux根据文件名模式搜索文件

如何找到只对所有者具有特定权限的文件?