From 1cedff0e9ea5d04817ec2b357a41d6be60eccdf2 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 3 May 2015 16:36:52 +0200 Subject: [PATCH] tests: Simplify key handling --- archiver_test.go | 20 ++++++-------------- cache_test.go | 2 -- server/key_test.go | 13 ------------- server/server.go | 13 +++++++++---- server/server_test.go | 14 -------------- test/backend.go | 15 +++++---------- tree_test.go | 2 -- walk_test.go | 2 -- 8 files changed, 20 insertions(+), 61 deletions(-) delete mode 100644 server/key_test.go diff --git a/archiver_test.go b/archiver_test.go index e0fbae084..dce0e0b7e 100644 --- a/archiver_test.go +++ b/archiver_test.go @@ -55,9 +55,8 @@ func BenchmarkChunkEncrypt(b *testing.B) { data := Random(23, 10<<20) // 10MiB rd := bytes.NewReader(data) - be := SetupBackend(b) - defer TeardownBackend(b, be) - key := SetupKey(b, be, "geheim") + s := SetupBackend(b) + defer TeardownBackend(b, s) buf := restic.GetChunkBuf("BenchmarkChunkEncrypt") buf2 := restic.GetChunkBuf("BenchmarkChunkEncrypt") @@ -66,7 +65,7 @@ func BenchmarkChunkEncrypt(b *testing.B) { b.SetBytes(int64(len(data))) for i := 0; i < b.N; i++ { - benchmarkChunkEncrypt(b, buf, buf2, rd, key) + benchmarkChunkEncrypt(b, buf, buf2, rd, s.Key()) } 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) { - be := SetupBackend(b) - defer TeardownBackend(b, be) - key := SetupKey(b, be, "geheim") + s := SetupBackend(b) + defer TeardownBackend(b, s) data := Random(23, 10<<20) // 10MiB @@ -108,7 +106,7 @@ func BenchmarkChunkEncryptParallel(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { 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) { server := SetupBackend(b) defer TeardownBackend(b, server) - key := SetupKey(b, server, "geheim") - server.SetKey(key) arch := restic.NewArchiver(server) @@ -154,8 +150,6 @@ func archiveWithDedup(t testing.TB) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) var cnt struct { before, after, after2 struct { @@ -236,8 +230,6 @@ func BenchmarkLoadTree(t *testing.B) { s := SetupBackend(t) defer TeardownBackend(t, s) - key := SetupKey(t, s, "geheim") - s.SetKey(key) // archive a few files arch := restic.NewArchiver(s) diff --git a/cache_test.go b/cache_test.go index 9948dbbfc..8540e1839 100644 --- a/cache_test.go +++ b/cache_test.go @@ -10,8 +10,6 @@ import ( func TestCache(t *testing.T) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) _, err := restic.NewCache(server) OK(t, err) diff --git a/server/key_test.go b/server/key_test.go deleted file mode 100644 index 468e09c65..000000000 --- a/server/key_test.go +++ /dev/null @@ -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) -} diff --git a/server/server.go b/server/server.go index e2a84650c..accc1c40c 100644 --- a/server/server.go +++ b/server/server.go @@ -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. func (s *Server) ChunkerPolynomial() chunker.Pol { return chunker.Pol(s.key.Master().ChunkerPolynomial) @@ -532,7 +528,16 @@ func (s *Server) SearchKey(password string) error { } 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 } diff --git a/server/server_test.go b/server/server_test.go index 51556ee94..55c22760c 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -30,8 +30,6 @@ var serverTests = []testJSONStruct{ func TestSaveJSON(t *testing.T) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) for _, obj := range serverTests { data, err := json.Marshal(obj) @@ -51,8 +49,6 @@ func TestSaveJSON(t *testing.T) { func BenchmarkSaveJSON(t *testing.B) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) obj := serverTests[0] @@ -78,8 +74,6 @@ var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20} func TestSave(t *testing.T) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) for _, size := range testSizes { data := make([]byte, size) @@ -112,8 +106,6 @@ func TestSave(t *testing.T) { func TestSaveFrom(t *testing.T) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) for _, size := range testSizes { data := make([]byte, size) @@ -144,8 +136,6 @@ func TestSaveFrom(t *testing.T) { func BenchmarkSaveFrom(t *testing.B) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) size := 4 << 20 // 4MiB @@ -172,8 +162,6 @@ func TestLoadJSONPack(t *testing.T) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) // archive a few files sn := SnapshotDir(t, server, *benchTestDir, nil) @@ -191,8 +179,6 @@ func TestLoadJSONEncrypted(t *testing.T) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) // archive a snapshot sn := restic.Snapshot{} diff --git a/test/backend.go b/test/backend.go index 31be3f0ab..89bcb7b10 100644 --- a/test/backend.go +++ b/test/backend.go @@ -13,7 +13,7 @@ import ( "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 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")) OK(t, err) - // set cache dir + // set cache dir below temp dir err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache")) 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) { @@ -42,13 +44,6 @@ func TeardownBackend(t testing.TB, s *server.Server) { 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 { arch := restic.NewArchiver(server) sn, _, err := arch.Snapshot(nil, []string{path}, parent) diff --git a/tree_test.go b/tree_test.go index ef50e1b93..ecfefa620 100644 --- a/tree_test.go +++ b/tree_test.go @@ -95,8 +95,6 @@ func TestNodeComparison(t *testing.T) { func TestLoadTree(t *testing.T) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) // save tree tree := restic.NewTree() diff --git a/walk_test.go b/walk_test.go index 88e2914b2..048af71c0 100644 --- a/walk_test.go +++ b/walk_test.go @@ -18,8 +18,6 @@ func TestWalkTree(t *testing.T) { server := SetupBackend(t) defer TeardownBackend(t, server) - key := SetupKey(t, server, "geheim") - server.SetKey(key) // archive a few files arch := restic.NewArchiver(server)