2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 19:08:55 +00:00
restic/key_test.go

108 lines
2.0 KiB
Go
Raw Normal View History

2014-09-23 20:39:12 +00:00
package khepri_test
import (
"flag"
"io"
2014-09-23 20:39:12 +00:00
"io/ioutil"
"os"
"testing"
"github.com/fd0/khepri"
"github.com/fd0/khepri/backend"
)
var testPassword = "foobar"
2014-09-23 20:39:12 +00:00
var testCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)")
func setupBackend(t testing.TB) *backend.Local {
2014-09-23 20:39:12 +00:00
tempdir, err := ioutil.TempDir("", "khepri-test-")
ok(t, err)
b, err := backend.CreateLocal(tempdir)
ok(t, err)
return b
}
func teardownBackend(t testing.TB, b *backend.Local) {
2014-09-23 20:39:12 +00:00
if !*testCleanup {
t.Logf("leaving local backend at %s\n", b.Location())
return
}
ok(t, os.RemoveAll(b.Location()))
}
func setupKey(t testing.TB, be backend.Server, password string) *khepri.Key {
k, err := khepri.CreateKey(be, password)
2014-09-23 20:39:12 +00:00
ok(t, err)
return k
}
2014-09-23 20:39:12 +00:00
func TestRepo(t *testing.T) {
be := setupBackend(t)
defer teardownBackend(t, be)
_ = setupKey(t, be, testPassword)
2014-09-23 20:39:12 +00:00
}
func TestEncryptDecrypt(t *testing.T) {
2014-09-23 20:39:12 +00:00
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)
}
2014-09-23 20:39:12 +00:00
}