2015-04-26 12:46:15 +00:00
|
|
|
package server_test
|
2015-02-16 18:32:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-02-16 19:00:23 +00:00
|
|
|
"crypto/rand"
|
2015-02-16 18:32:36 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
2015-04-26 12:46:15 +00:00
|
|
|
"flag"
|
2015-02-16 19:00:23 +00:00
|
|
|
"io"
|
2015-02-16 18:32:36 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/backend"
|
2015-04-26 15:44:38 +00:00
|
|
|
"github.com/restic/restic/pack"
|
2015-04-09 19:15:48 +00:00
|
|
|
. "github.com/restic/restic/test"
|
2015-02-16 18:32:36 +00:00
|
|
|
)
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
var benchTestDir = flag.String("test.dir", ".", "dir used in benchmarks (default: .)")
|
|
|
|
|
2015-02-16 18:32:36 +00:00
|
|
|
type testJSONStruct struct {
|
|
|
|
Foo uint32
|
|
|
|
Bar string
|
|
|
|
Baz []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var serverTests = []testJSONStruct{
|
|
|
|
testJSONStruct{Foo: 23, Bar: "Teststring", Baz: []byte("xx")},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveJSON(t *testing.T) {
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
2015-02-16 18:32:36 +00:00
|
|
|
|
|
|
|
for _, obj := range serverTests {
|
|
|
|
data, err := json.Marshal(obj)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-16 18:32:36 +00:00
|
|
|
data = append(data, '\n')
|
|
|
|
h := sha256.Sum256(data)
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
id, err := server.SaveJSON(pack.Tree, obj)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-16 18:32:36 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
Assert(t, bytes.Equal(h[:], id),
|
2015-02-16 18:32:36 +00:00
|
|
|
"TestSaveJSON: wrong plaintext ID: expected %02x, got %02x",
|
2015-04-26 15:44:38 +00:00
|
|
|
h, id)
|
2015-02-16 18:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSaveJSON(t *testing.B) {
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
2015-02-16 18:32:36 +00:00
|
|
|
|
|
|
|
obj := serverTests[0]
|
|
|
|
|
|
|
|
data, err := json.Marshal(obj)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-16 18:32:36 +00:00
|
|
|
data = append(data, '\n')
|
|
|
|
h := sha256.Sum256(data)
|
|
|
|
|
2015-02-16 19:00:23 +00:00
|
|
|
t.ResetTimer()
|
|
|
|
|
2015-02-16 18:32:36 +00:00
|
|
|
for i := 0; i < t.N; i++ {
|
2015-04-26 15:44:38 +00:00
|
|
|
id, err := server.SaveJSON(pack.Tree, obj)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-16 18:32:36 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
Assert(t, bytes.Equal(h[:], id),
|
2015-02-16 18:32:36 +00:00
|
|
|
"TestSaveJSON: wrong plaintext ID: expected %02x, got %02x",
|
2015-04-26 15:44:38 +00:00
|
|
|
h, id)
|
2015-02-16 18:32:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-16 19:00:23 +00:00
|
|
|
|
|
|
|
var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestSave(t *testing.T) {
|
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
|
|
|
|
|
|
|
for _, size := range testSizes {
|
|
|
|
data := make([]byte, size)
|
|
|
|
_, err := io.ReadFull(rand.Reader, data)
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
id := backend.Hash(data)
|
|
|
|
|
|
|
|
// save
|
|
|
|
sid, err := server.Save(pack.Data, data, nil)
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
Equals(t, id, sid)
|
|
|
|
|
|
|
|
OK(t, server.Flush())
|
|
|
|
|
|
|
|
// read back
|
|
|
|
buf, err := server.LoadBlob(pack.Data, id)
|
|
|
|
|
|
|
|
Assert(t, len(buf) == len(data),
|
|
|
|
"number of bytes read back does not match: expected %d, got %d",
|
|
|
|
len(data), len(buf))
|
|
|
|
|
|
|
|
Assert(t, bytes.Equal(buf, data),
|
|
|
|
"data does not match: expected %02x, got %02x",
|
|
|
|
data, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 19:00:23 +00:00
|
|
|
func TestSaveFrom(t *testing.T) {
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
2015-02-16 19:00:23 +00:00
|
|
|
|
|
|
|
for _, size := range testSizes {
|
|
|
|
data := make([]byte, size)
|
|
|
|
_, err := io.ReadFull(rand.Reader, data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-16 19:00:23 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
id := backend.Hash(data)
|
2015-02-16 19:00:23 +00:00
|
|
|
|
|
|
|
// save
|
2015-04-26 15:44:38 +00:00
|
|
|
err = server.SaveFrom(pack.Data, id[:], uint(size), bytes.NewReader(data))
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-16 19:00:23 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
OK(t, server.Flush())
|
|
|
|
|
2015-02-16 19:00:23 +00:00
|
|
|
// read back
|
2015-04-26 15:44:38 +00:00
|
|
|
buf, err := server.LoadBlob(pack.Data, id[:])
|
2015-02-16 19:00:23 +00:00
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, len(buf) == len(data),
|
2015-02-16 19:00:23 +00:00
|
|
|
"number of bytes read back does not match: expected %d, got %d",
|
|
|
|
len(data), len(buf))
|
|
|
|
|
2015-04-09 19:15:48 +00:00
|
|
|
Assert(t, bytes.Equal(buf, data),
|
2015-02-16 19:00:23 +00:00
|
|
|
"data does not match: expected %02x, got %02x",
|
|
|
|
data, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSaveFrom(t *testing.B) {
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
2015-02-16 19:00:23 +00:00
|
|
|
|
|
|
|
size := 4 << 20 // 4MiB
|
|
|
|
|
|
|
|
data := make([]byte, size)
|
|
|
|
_, err := io.ReadFull(rand.Reader, data)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-16 19:00:23 +00:00
|
|
|
|
|
|
|
id := sha256.Sum256(data)
|
|
|
|
|
|
|
|
t.ResetTimer()
|
|
|
|
t.SetBytes(int64(size))
|
|
|
|
|
|
|
|
for i := 0; i < t.N; i++ {
|
|
|
|
// save
|
2015-04-26 15:44:38 +00:00
|
|
|
err = server.SaveFrom(pack.Data, id[:], uint(size), bytes.NewReader(data))
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-16 19:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 22:05:23 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestLoadJSONPack(t *testing.T) {
|
2015-04-26 12:46:15 +00:00
|
|
|
if *benchTestDir == "" {
|
2015-02-18 21:46:09 +00:00
|
|
|
t.Skip("benchdir not set, skipping TestServerStats")
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
2015-02-18 21:46:09 +00:00
|
|
|
|
|
|
|
// archive a few files
|
2015-04-26 12:46:15 +00:00
|
|
|
sn := SnapshotDir(t, server, *benchTestDir, nil)
|
2015-04-26 15:44:38 +00:00
|
|
|
OK(t, server.Flush())
|
2015-03-28 14:07:08 +00:00
|
|
|
|
2015-02-18 21:46:09 +00:00
|
|
|
tree := restic.NewTree()
|
2015-04-26 15:44:38 +00:00
|
|
|
err := server.LoadJSONPack(pack.Tree, sn.Tree, &tree)
|
2015-04-09 19:15:48 +00:00
|
|
|
OK(t, err)
|
2015-02-18 21:46:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestLoadJSONEncrypted(t *testing.T) {
|
2015-04-26 12:46:15 +00:00
|
|
|
if *benchTestDir == "" {
|
2015-02-18 21:46:09 +00:00
|
|
|
t.Skip("benchdir not set, skipping TestServerStats")
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
server := SetupBackend(t)
|
|
|
|
defer TeardownBackend(t, server)
|
2015-02-18 21:46:09 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// archive a snapshot
|
|
|
|
sn := restic.Snapshot{}
|
|
|
|
sn.Hostname = "foobar"
|
|
|
|
sn.Username = "test!"
|
2015-02-18 21:46:09 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
id, err := server.SaveJSONUnpacked(backend.Snapshot, &sn)
|
|
|
|
OK(t, err)
|
2015-02-18 21:46:09 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
var sn2 restic.Snapshot
|
|
|
|
|
|
|
|
// restore
|
|
|
|
err = server.LoadJSONEncrypted(backend.Snapshot, id, &sn2)
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
Equals(t, sn.Hostname, sn2.Hostname)
|
|
|
|
Equals(t, sn.Username, sn2.Username)
|
2015-02-18 21:46:09 +00:00
|
|
|
}
|