66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
|
|
package filex
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestFillWithRandomAndCryptoRandom(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
pseudoPath := filepath.Join(dir, "pseudo.bin")
|
||
|
|
securePath := filepath.Join(dir, "secure.bin")
|
||
|
|
|
||
|
|
if err := FillWithRandom(pseudoPath, 2048, 128, 4, nil); err != nil {
|
||
|
|
t.Fatalf("FillWithRandom failed: %v", err)
|
||
|
|
}
|
||
|
|
if err := FillWithCryptoRandom(securePath, 2048, 128, nil); err != nil {
|
||
|
|
t.Fatalf("FillWithCryptoRandom failed: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
pseudoInfo, err := os.Stat(pseudoPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("stat pseudo file failed: %v", err)
|
||
|
|
}
|
||
|
|
if pseudoInfo.Size() != 2048 {
|
||
|
|
t.Fatalf("unexpected pseudo size: %d", pseudoInfo.Size())
|
||
|
|
}
|
||
|
|
|
||
|
|
secureInfo, err := os.Stat(securePath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("stat secure file failed: %v", err)
|
||
|
|
}
|
||
|
|
if secureInfo.Size() != 2048 {
|
||
|
|
t.Fatalf("unexpected secure size: %d", secureInfo.Size())
|
||
|
|
}
|
||
|
|
|
||
|
|
pseudo, err := os.ReadFile(pseudoPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("read pseudo file failed: %v", err)
|
||
|
|
}
|
||
|
|
secure, err := os.ReadFile(securePath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("read secure file failed: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if bytes.Equal(secure, make([]byte, len(secure))) {
|
||
|
|
t.Fatalf("secure random output should not be all zero")
|
||
|
|
}
|
||
|
|
if bytes.Equal(pseudo, secure) {
|
||
|
|
t.Fatalf("pseudo and secure random outputs unexpectedly identical")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFillWithRandomInvalidArgs(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
path := filepath.Join(dir, "bad.bin")
|
||
|
|
|
||
|
|
if err := FillWithRandom(path, -1, 16, 1, nil); err == nil {
|
||
|
|
t.Fatalf("expected FillWithRandom negative filesize error")
|
||
|
|
}
|
||
|
|
if err := FillWithCryptoRandom(path, -1, 16, nil); err == nil {
|
||
|
|
t.Fatalf("expected FillWithCryptoRandom negative filesize error")
|
||
|
|
}
|
||
|
|
}
|