tests: Simplify key handling

This commit is contained in:
Alexander Neumann 2015-05-03 16:36:52 +02:00
parent 062c328f2d
commit 1cedff0e9e
8 changed files with 20 additions and 61 deletions

View file

@ -55,9 +55,8 @@ func BenchmarkChunkEncrypt(b *testing.B) {
data := Random(23, 10<<20) // 10MiB data := Random(23, 10<<20) // 10MiB
rd := bytes.NewReader(data) rd := bytes.NewReader(data)
be := SetupBackend(b) s := SetupBackend(b)
defer TeardownBackend(b, be) defer TeardownBackend(b, s)
key := SetupKey(b, be, "geheim")
buf := restic.GetChunkBuf("BenchmarkChunkEncrypt") buf := restic.GetChunkBuf("BenchmarkChunkEncrypt")
buf2 := restic.GetChunkBuf("BenchmarkChunkEncrypt") buf2 := restic.GetChunkBuf("BenchmarkChunkEncrypt")
@ -66,7 +65,7 @@ func BenchmarkChunkEncrypt(b *testing.B) {
b.SetBytes(int64(len(data))) b.SetBytes(int64(len(data)))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
benchmarkChunkEncrypt(b, buf, buf2, rd, key) benchmarkChunkEncrypt(b, buf, buf2, rd, s.Key())
} }
restic.FreeChunkBuf("BenchmarkChunkEncrypt", buf) restic.FreeChunkBuf("BenchmarkChunkEncrypt", buf)
@ -94,9 +93,8 @@ func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *server.Key)
} }
func BenchmarkChunkEncryptParallel(b *testing.B) { func BenchmarkChunkEncryptParallel(b *testing.B) {
be := SetupBackend(b) s := SetupBackend(b)
defer TeardownBackend(b, be) defer TeardownBackend(b, s)
key := SetupKey(b, be, "geheim")
data := Random(23, 10<<20) // 10MiB data := Random(23, 10<<20) // 10MiB
@ -108,7 +106,7 @@ func BenchmarkChunkEncryptParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
rd := bytes.NewReader(data) rd := bytes.NewReader(data)
benchmarkChunkEncryptP(pb, buf, rd, key) benchmarkChunkEncryptP(pb, buf, rd, s.Key())
} }
}) })
@ -118,8 +116,6 @@ func BenchmarkChunkEncryptParallel(b *testing.B) {
func archiveDirectory(b testing.TB) { func archiveDirectory(b testing.TB) {
server := SetupBackend(b) server := SetupBackend(b)
defer TeardownBackend(b, server) defer TeardownBackend(b, server)
key := SetupKey(b, server, "geheim")
server.SetKey(key)
arch := restic.NewArchiver(server) arch := restic.NewArchiver(server)
@ -154,8 +150,6 @@ func archiveWithDedup(t testing.TB) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
var cnt struct { var cnt struct {
before, after, after2 struct { before, after, after2 struct {
@ -236,8 +230,6 @@ func BenchmarkLoadTree(t *testing.B) {
s := SetupBackend(t) s := SetupBackend(t)
defer TeardownBackend(t, s) defer TeardownBackend(t, s)
key := SetupKey(t, s, "geheim")
s.SetKey(key)
// archive a few files // archive a few files
arch := restic.NewArchiver(s) arch := restic.NewArchiver(s)

View file

@ -10,8 +10,6 @@ import (
func TestCache(t *testing.T) { func TestCache(t *testing.T) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
_, err := restic.NewCache(server) _, err := restic.NewCache(server)
OK(t, err) OK(t, err)

View file

@ -1,13 +0,0 @@
package server_test
import (
"testing"
. "github.com/restic/restic/test"
)
func TestRepo(t *testing.T) {
s := SetupBackend(t)
defer TeardownBackend(t, s)
_ = SetupKey(t, s, TestPassword)
}

View file

@ -32,10 +32,6 @@ func NewServer(be backend.Backend) *Server {
} }
} }
func (s *Server) SetKey(k *Key) {
s.key = k
}
// ChunkerPolynomial returns the secret polynomial used for content defined chunking. // ChunkerPolynomial returns the secret polynomial used for content defined chunking.
func (s *Server) ChunkerPolynomial() chunker.Pol { func (s *Server) ChunkerPolynomial() chunker.Pol {
return chunker.Pol(s.key.Master().ChunkerPolynomial) return chunker.Pol(s.key.Master().ChunkerPolynomial)
@ -532,7 +528,16 @@ func (s *Server) SearchKey(password string) error {
} }
s.key = key s.key = key
return nil
}
func (s *Server) CreateKey(password string) error {
key, err := CreateKey(s, password)
if err != nil {
return err
}
s.key = key
return nil return nil
} }

View file

@ -30,8 +30,6 @@ var serverTests = []testJSONStruct{
func TestSaveJSON(t *testing.T) { func TestSaveJSON(t *testing.T) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
for _, obj := range serverTests { for _, obj := range serverTests {
data, err := json.Marshal(obj) data, err := json.Marshal(obj)
@ -51,8 +49,6 @@ func TestSaveJSON(t *testing.T) {
func BenchmarkSaveJSON(t *testing.B) { func BenchmarkSaveJSON(t *testing.B) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
obj := serverTests[0] obj := serverTests[0]
@ -78,8 +74,6 @@ var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20}
func TestSave(t *testing.T) { func TestSave(t *testing.T) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
for _, size := range testSizes { for _, size := range testSizes {
data := make([]byte, size) data := make([]byte, size)
@ -112,8 +106,6 @@ func TestSave(t *testing.T) {
func TestSaveFrom(t *testing.T) { func TestSaveFrom(t *testing.T) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
for _, size := range testSizes { for _, size := range testSizes {
data := make([]byte, size) data := make([]byte, size)
@ -144,8 +136,6 @@ func TestSaveFrom(t *testing.T) {
func BenchmarkSaveFrom(t *testing.B) { func BenchmarkSaveFrom(t *testing.B) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
size := 4 << 20 // 4MiB size := 4 << 20 // 4MiB
@ -172,8 +162,6 @@ func TestLoadJSONPack(t *testing.T) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
// archive a few files // archive a few files
sn := SnapshotDir(t, server, *benchTestDir, nil) sn := SnapshotDir(t, server, *benchTestDir, nil)
@ -191,8 +179,6 @@ func TestLoadJSONEncrypted(t *testing.T) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
// archive a snapshot // archive a snapshot
sn := restic.Snapshot{} sn := restic.Snapshot{}

View file

@ -13,7 +13,7 @@ import (
"github.com/restic/restic/server" "github.com/restic/restic/server"
) )
var TestPassword = "foobar" var TestPassword = flag.String("test.password", "", `use this password for repositories created during tests (default: "geheim")`)
var TestCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)") var TestCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)")
var TestTempDir = flag.String("test.tempdir", "", "use this directory for temporary storage (default: system temp dir)") var TestTempDir = flag.String("test.tempdir", "", "use this directory for temporary storage (default: system temp dir)")
@ -25,11 +25,13 @@ func SetupBackend(t testing.TB) *server.Server {
b, err := local.Create(filepath.Join(tempdir, "repo")) b, err := local.Create(filepath.Join(tempdir, "repo"))
OK(t, err) OK(t, err)
// set cache dir // set cache dir below temp dir
err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache")) err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache"))
OK(t, err) OK(t, err)
return server.NewServer(b) s := server.NewServer(b)
OK(t, s.CreateKey(*TestPassword))
return s
} }
func TeardownBackend(t testing.TB, s *server.Server) { func TeardownBackend(t testing.TB, s *server.Server) {
@ -42,13 +44,6 @@ func TeardownBackend(t testing.TB, s *server.Server) {
OK(t, s.Delete()) OK(t, s.Delete())
} }
func SetupKey(t testing.TB, s *server.Server, password string) *server.Key {
k, err := server.CreateKey(s, password)
OK(t, err)
return k
}
func SnapshotDir(t testing.TB, server *server.Server, path string, parent backend.ID) *restic.Snapshot { func SnapshotDir(t testing.TB, server *server.Server, path string, parent backend.ID) *restic.Snapshot {
arch := restic.NewArchiver(server) arch := restic.NewArchiver(server)
sn, _, err := arch.Snapshot(nil, []string{path}, parent) sn, _, err := arch.Snapshot(nil, []string{path}, parent)

View file

@ -95,8 +95,6 @@ func TestNodeComparison(t *testing.T) {
func TestLoadTree(t *testing.T) { func TestLoadTree(t *testing.T) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
// save tree // save tree
tree := restic.NewTree() tree := restic.NewTree()

View file

@ -18,8 +18,6 @@ func TestWalkTree(t *testing.T) {
server := SetupBackend(t) server := SetupBackend(t)
defer TeardownBackend(t, server) defer TeardownBackend(t, server)
key := SetupKey(t, server, "geheim")
server.SetKey(key)
// archive a few files // archive a few files
arch := restic.NewArchiver(server) arch := restic.NewArchiver(server)