mirror of https://github.com/restic/restic.git
Add tests and benchmarks for encrypt and decrypt
This commit is contained in:
parent
8277daa9e1
commit
2ac9d28c8d
81
key_test.go
81
key_test.go
|
@ -2,6 +2,7 @@ package khepri_test
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
@ -10,22 +11,20 @@ import (
|
|||
"github.com/fd0/khepri/backend"
|
||||
)
|
||||
|
||||
var test_password = "foobar"
|
||||
var testPassword = "foobar"
|
||||
var testCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)")
|
||||
|
||||
func setupBackend(t *testing.T) *backend.Local {
|
||||
func setupBackend(t testing.TB) *backend.Local {
|
||||
tempdir, err := ioutil.TempDir("", "khepri-test-")
|
||||
ok(t, err)
|
||||
|
||||
b, err := backend.CreateLocal(tempdir)
|
||||
ok(t, err)
|
||||
|
||||
t.Logf("created local backend at %s", tempdir)
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
func teardownBackend(t *testing.T, b *backend.Local) {
|
||||
func teardownBackend(t testing.TB, b *backend.Local) {
|
||||
if !*testCleanup {
|
||||
t.Logf("leaving local backend at %s\n", b.Location())
|
||||
return
|
||||
|
@ -34,17 +33,75 @@ func teardownBackend(t *testing.T, b *backend.Local) {
|
|||
ok(t, os.RemoveAll(b.Location()))
|
||||
}
|
||||
|
||||
func setupKey(t *testing.T, be backend.Server, password string) *khepri.Key {
|
||||
c, err := khepri.CreateKey(be, password)
|
||||
func setupKey(t testing.TB, be backend.Server, password string) *khepri.Key {
|
||||
k, err := khepri.CreateKey(be, password)
|
||||
ok(t, err)
|
||||
|
||||
t.Logf("created Safe at %s", be.Location())
|
||||
|
||||
return c
|
||||
return k
|
||||
}
|
||||
|
||||
func TestSafe(t *testing.T) {
|
||||
func TestRepo(t *testing.T) {
|
||||
be := setupBackend(t)
|
||||
defer teardownBackend(t, be)
|
||||
_ = setupKey(t, be, test_password)
|
||||
_ = setupKey(t, be, testPassword)
|
||||
}
|
||||
|
||||
func TestEncryptDecrypt(t *testing.T) {
|
||||
be := setupBackend(t)
|
||||
defer teardownBackend(t, be)
|
||||
k := setupKey(t, be, testPassword)
|
||||
|
||||
for _, size := range []int{5, 23, 1 << 20, 10<<20 + 123} {
|
||||
data := make([]byte, size)
|
||||
f, err := os.Open("/dev/urandom")
|
||||
ok(t, err)
|
||||
|
||||
_, err = io.ReadFull(f, data)
|
||||
ok(t, err)
|
||||
|
||||
ciphertext, err := k.Encrypt(data)
|
||||
ok(t, err)
|
||||
|
||||
plaintext, err := k.Decrypt(ciphertext)
|
||||
ok(t, err)
|
||||
|
||||
equals(t, plaintext, data)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkEncrypt(b *testing.B) {
|
||||
size := 16 << 20 // 16MiB
|
||||
data := make([]byte, size)
|
||||
|
||||
be := setupBackend(b)
|
||||
defer teardownBackend(b, be)
|
||||
k := setupKey(b, be, testPassword)
|
||||
|
||||
b.ResetTimer()
|
||||
b.SetBytes(int64(size))
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := k.Encrypt(data)
|
||||
ok(b, err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDecrypt(b *testing.B) {
|
||||
size := 16 << 20 // 16MiB
|
||||
data := make([]byte, size)
|
||||
|
||||
be := setupBackend(b)
|
||||
defer teardownBackend(b, be)
|
||||
k := setupKey(b, be, testPassword)
|
||||
|
||||
ciphertext, err := k.Encrypt(data)
|
||||
ok(b, err)
|
||||
|
||||
b.ResetTimer()
|
||||
b.SetBytes(int64(size))
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := k.Decrypt(ciphertext)
|
||||
ok(b, err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,8 +16,8 @@ var testFiles = []struct {
|
|||
{"bar/bla/blubb", []byte("This is just a test!\n")},
|
||||
}
|
||||
|
||||
// prepare directory and return temporary path
|
||||
func prepare_dir(t *testing.T) string {
|
||||
// prepareDir creates a temporary directory and returns it.
|
||||
func prepareDir(t *testing.T) string {
|
||||
tempdir, err := ioutil.TempDir("", "khepri-test-")
|
||||
ok(t, err)
|
||||
|
||||
|
@ -39,13 +39,11 @@ func prepare_dir(t *testing.T) string {
|
|||
ok(t, err)
|
||||
}
|
||||
|
||||
t.Logf("tempdir prepared at %s", tempdir)
|
||||
|
||||
return tempdir
|
||||
}
|
||||
|
||||
func TestTree(t *testing.T) {
|
||||
dir := prepare_dir(t)
|
||||
dir := prepareDir(t)
|
||||
defer func() {
|
||||
if *testCleanup {
|
||||
ok(t, os.RemoveAll(dir))
|
||||
|
|
Loading…
Reference in New Issue