2014-12-05 20:45:49 +00:00
|
|
|
package restic_test
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2014-11-15 18:34:42 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2014-11-15 18:34:42 +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-11-15 18:34:42 +00:00
|
|
|
func setupBackend(t testing.TB) *backend.Local {
|
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)
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-11-15 18:34:42 +00:00
|
|
|
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()))
|
|
|
|
}
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
func setupKey(t testing.TB, be backend.Server, password string) *restic.Key {
|
|
|
|
k, err := restic.CreateKey(be, password)
|
2014-09-23 20:39:12 +00:00
|
|
|
ok(t, err)
|
|
|
|
|
2014-11-15 18:34:42 +00:00
|
|
|
return k
|
|
|
|
}
|
2014-09-23 20:39:12 +00:00
|
|
|
|
2014-11-15 18:34:42 +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
|
|
|
}
|
|
|
|
|
2014-11-15 18:34:42 +00:00
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
2014-09-23 20:39:12 +00:00
|
|
|
be := setupBackend(t)
|
|
|
|
defer teardownBackend(t, be)
|
2014-11-15 18:34:42 +00:00
|
|
|
k := setupKey(t, be, testPassword)
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
for _, size := range []int{5, 23, 1 << 20, 7<<20 + 123} {
|
2014-11-15 18:34:42 +00:00
|
|
|
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")
|
2014-11-23 15:48:00 +00:00
|
|
|
n, err := k.Encrypt(ciphertext, data)
|
2014-11-15 18:34:42 +00:00
|
|
|
ok(t, err)
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
plaintext, err := k.Decrypt(ciphertext[:n])
|
2014-11-15 18:34:42 +00:00
|
|
|
ok(t, err)
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
restic.FreeChunkBuf("TestEncryptDecrypt", ciphertext)
|
2014-11-23 15:48:00 +00:00
|
|
|
|
2014-11-15 18:34:42 +00:00
|
|
|
equals(t, plaintext, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-23 21:58:28 +00:00
|
|
|
func TestLargeEncrypt(t *testing.T) {
|
|
|
|
be := setupBackend(t)
|
|
|
|
defer teardownBackend(t, be)
|
|
|
|
k := setupKey(t, be, 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)
|
2014-11-23 21:58:28 +00:00
|
|
|
n, err := k.Encrypt(ciphertext, data)
|
|
|
|
ok(t, err)
|
|
|
|
|
|
|
|
plaintext, err := k.Decrypt(ciphertext[:n])
|
|
|
|
ok(t, err)
|
|
|
|
|
|
|
|
equals(t, plaintext, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 18:34:42 +00:00
|
|
|
func BenchmarkEncrypt(b *testing.B) {
|
2014-11-23 15:48:00 +00:00
|
|
|
size := 8 << 20 // 8MiB
|
2014-11-15 18:34:42 +00:00
|
|
|
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")
|
2014-11-15 18:34:42 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2014-11-23 15:48:00 +00:00
|
|
|
_, err := k.Encrypt(buf, data)
|
2014-11-15 18:34:42 +00:00
|
|
|
ok(b, err)
|
|
|
|
}
|
2014-12-05 20:45:49 +00:00
|
|
|
restic.FreeChunkBuf("BenchmarkEncrypt", buf)
|
2014-11-15 18:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDecrypt(b *testing.B) {
|
2014-11-23 15:48:00 +00:00
|
|
|
size := 8 << 20 // 8MiB
|
2014-11-15 18:34:42 +00:00
|
|
|
data := make([]byte, size)
|
|
|
|
|
|
|
|
be := setupBackend(b)
|
|
|
|
defer teardownBackend(b, be)
|
|
|
|
k := setupKey(b, be, testPassword)
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
ciphertext := restic.GetChunkBuf("BenchmarkDecrypt")
|
2014-11-23 15:48:00 +00:00
|
|
|
n, err := k.Encrypt(ciphertext, data)
|
2014-11-15 18:34:42 +00:00
|
|
|
ok(b, err)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2014-11-23 15:48:00 +00:00
|
|
|
_, err := k.Decrypt(ciphertext[:n])
|
2014-11-15 18:34:42 +00:00
|
|
|
ok(b, err)
|
|
|
|
}
|
2014-12-05 20:45:49 +00:00
|
|
|
restic.FreeChunkBuf("BenchmarkDecrypt", ciphertext)
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|