2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 22:27:35 +00:00
restic/key_test.go

141 lines
2.9 KiB
Go
Raw Normal View History

2014-12-05 20:45:49 +00:00
package restic_test
2014-09-23 20:39:12 +00:00
import (
"flag"
"io"
2014-09-23 20:39:12 +00:00
"io/ioutil"
"os"
"testing"
2014-12-05 20:45:49 +00:00
"github.com/restic/restic"
"github.com/restic/restic/backend"
"github.com/restic/restic/chunker"
2014-09-23 20:39:12 +00:00
)
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)")
2014-12-21 16:02:49 +00:00
func setupBackend(t testing.TB) restic.Server {
2014-12-05 20:45:49 +00:00
tempdir, err := ioutil.TempDir("", "restic-test-")
2014-09-23 20:39:12 +00:00
ok(t, err)
b, err := backend.CreateLocal(tempdir)
ok(t, err)
2014-12-21 16:02:49 +00:00
return restic.NewServer(b)
2014-09-23 20:39:12 +00:00
}
2014-12-21 16:02:49 +00:00
func teardownBackend(t testing.TB, s restic.Server) {
2014-09-23 20:39:12 +00:00
if !*testCleanup {
2014-12-21 16:02:49 +00:00
l := s.Backend().(*backend.Local)
t.Logf("leaving local backend at %s\n", l.Location())
2014-09-23 20:39:12 +00:00
return
}
2014-12-21 16:02:49 +00:00
ok(t, s.Delete())
2014-09-23 20:39:12 +00:00
}
2014-12-21 16:02:49 +00:00
func setupKey(t testing.TB, s restic.Server, password string) *restic.Key {
k, err := restic.CreateKey(s, 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) {
2014-12-21 16:02:49 +00:00
s := setupBackend(t)
defer teardownBackend(t, s)
_ = setupKey(t, s, testPassword)
2014-09-23 20:39:12 +00:00
}
func TestEncryptDecrypt(t *testing.T) {
2014-12-21 16:02:49 +00:00
s := setupBackend(t)
defer teardownBackend(t, s)
k := setupKey(t, s, testPassword)
for _, size := range []int{5, 23, 1 << 20, 7<<20 + 123} {
data := make([]byte, size)
f, err := os.Open("/dev/urandom")
ok(t, err)
_, err = io.ReadFull(f, data)
ok(t, err)
2014-12-05 20:45:49 +00:00
ciphertext := restic.GetChunkBuf("TestEncryptDecrypt")
n, err := k.Encrypt(ciphertext, data)
ok(t, err)
plaintext, err := k.Decrypt(ciphertext[:n])
ok(t, err)
2014-12-05 20:45:49 +00:00
restic.FreeChunkBuf("TestEncryptDecrypt", ciphertext)
equals(t, plaintext, data)
}
}
func TestLargeEncrypt(t *testing.T) {
2014-12-21 16:02:49 +00:00
s := setupBackend(t)
defer teardownBackend(t, s)
k := setupKey(t, s, testPassword)
for _, size := range []int{chunker.MaxSize, chunker.MaxSize + 1} {
data := make([]byte, size)
f, err := os.Open("/dev/urandom")
ok(t, err)
_, err = io.ReadFull(f, data)
ok(t, err)
2014-12-05 20:45:49 +00:00
ciphertext := make([]byte, size+restic.CiphertextExtension)
n, err := k.Encrypt(ciphertext, data)
ok(t, err)
plaintext, err := k.Decrypt(ciphertext[:n])
ok(t, err)
equals(t, plaintext, data)
}
}
func BenchmarkEncrypt(b *testing.B) {
size := 8 << 20 // 8MiB
data := make([]byte, size)
be := setupBackend(b)
defer teardownBackend(b, be)
k := setupKey(b, be, testPassword)
b.ResetTimer()
b.SetBytes(int64(size))
2014-12-05 20:45:49 +00:00
buf := restic.GetChunkBuf("BenchmarkEncrypt")
for i := 0; i < b.N; i++ {
_, err := k.Encrypt(buf, data)
ok(b, err)
}
2014-12-05 20:45:49 +00:00
restic.FreeChunkBuf("BenchmarkEncrypt", buf)
}
func BenchmarkDecrypt(b *testing.B) {
size := 8 << 20 // 8MiB
data := make([]byte, size)
2014-12-21 16:02:49 +00:00
s := setupBackend(b)
defer teardownBackend(b, s)
k := setupKey(b, s, testPassword)
2014-12-05 20:45:49 +00:00
ciphertext := restic.GetChunkBuf("BenchmarkDecrypt")
n, err := k.Encrypt(ciphertext, data)
ok(b, err)
b.ResetTimer()
b.SetBytes(int64(size))
for i := 0; i < b.N; i++ {
_, err := k.Decrypt(ciphertext[:n])
ok(b, err)
}
2014-12-05 20:45:49 +00:00
restic.FreeChunkBuf("BenchmarkDecrypt", ciphertext)
2014-09-23 20:39:12 +00:00
}