2010-08-30 49 views
41

我現在正在對我的應用程序再次損壞的文件進行一些測試。但是我發現很難找到測試文件。是否有將隨機垃圾字節寫入文件的命令?

所以我想知道是否有一些現有的工具,它可以將隨機/垃圾字節寫入某種格式的文件。

基本上,我需要這個工具:

  1. 它隨機寫入的字節垃圾入檔。
  2. 它不需要知道文件的格式,只需寫隨機字節對我來說就OK了。
  3. 最好寫在目標文件的隨機位置。
  4. 批處理也是一種獎勵。

謝謝。

回答

75

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

它是在一個基本水平測試,但你會發現有需要被照顧的邊緣錯誤情況。用它來做什麼。

+0

+1正確的 - 儘管你可能想解釋隨機和urandom設備之間的區別... – Konerak 2010-08-30 07:38:29

+0

其實我有使用10M的dd arg'bs' – Benoit 2012-07-13 13:55:59

+0

'urandom'沒有我想要的那麼快。 urandom產生足夠好的序列來創建私鑰。我不需要這樣的保證,但想要更快的速度。 – akostadinov 2014-11-03 09:24:08

5

你可以從/dev/random閱讀:

# generate a 50MB file named `random.stuff` filled with random stuff ... 
dd if=/dev/random of=random.stuff bs=1000000 count=50 

你也可以指定在人類可讀的方式大小:

# generate just 2MB ... 
dd if=/dev/random of=random.stuff bs=1M count=2 
+2

我認爲這是最好的答案,因爲問題被標記爲「linux」。然而,在另一個Unix上,/ dev/urandom可能被記錄如此:「/ dev/urandom是對Linux的兼容性指向。在Linux上,如果熵池耗盡,/ dev/urandom將產生較低質量的輸出,而/ dev /寧願阻止並等待額外的熵被收集,對Yarrow來說,這種選擇和區分是不必要的,兩個設備的行爲是相同的,你也可以使用。 – 2010-08-30 07:42:32

+2

'/ dev/random'太慢了。不是作者想要的。 -1 – akostadinov 2014-11-03 09:22:31

19

只是爲了完整性,這裏是另一種方式來做到這一點:

shred -s 10 - > my-file 

將10個隨機字節寫入標準輸出並將其重定向到一個文件。 shred通常用於銷燬(安全覆蓋)數據,但也可用於創建新的隨機文件。 所以,如果你已經有一個要填充用隨機數據的文件,使用此:

shred my-existing-file 
+0

有意思,謝謝。這比'urandom'快嗎? – akostadinov 2014-11-03 09:22:54