2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-11 21:57:58 +00:00
restic/internal/backend/utils_test.go

92 lines
2.2 KiB
Go
Raw Normal View History

2016-01-23 22:41:55 +00:00
package backend_test
import (
"bytes"
2017-06-03 15:39:57 +00:00
"context"
2016-01-23 22:41:55 +00:00
"math/rand"
"testing"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/mem"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-10-02 13:06:39 +00:00
rtest "github.com/restic/restic/internal/test"
2016-01-23 22:41:55 +00:00
)
const KiB = 1 << 10
const MiB = 1 << 20
func TestLoadAll(t *testing.T) {
b := mem.New()
for i := 0; i < 20; i++ {
2017-10-02 13:06:39 +00:00
data := rtest.Random(23+i, rand.Intn(MiB)+500*KiB)
2016-01-23 22:41:55 +00:00
2016-08-31 20:51:35 +00:00
id := restic.Hash(data)
2017-06-03 15:39:57 +00:00
err := b.Save(context.TODO(), restic.Handle{Name: id.String(), Type: restic.DataFile}, bytes.NewReader(data))
2017-10-02 13:06:39 +00:00
rtest.OK(t, err)
2016-01-23 22:41:55 +00:00
2017-06-03 15:39:57 +00:00
buf, err := backend.LoadAll(context.TODO(), b, restic.Handle{Type: restic.DataFile, Name: id.String()})
2017-10-02 13:06:39 +00:00
rtest.OK(t, err)
2016-01-23 22:41:55 +00:00
if len(buf) != len(data) {
t.Errorf("length of returned buffer does not match, want %d, got %d", len(data), len(buf))
continue
}
if !bytes.Equal(buf, data) {
t.Errorf("wrong data returned")
continue
}
}
}
func TestLoadSmallBuffer(t *testing.T) {
b := mem.New()
for i := 0; i < 20; i++ {
2017-10-02 13:06:39 +00:00
data := rtest.Random(23+i, rand.Intn(MiB)+500*KiB)
2016-08-31 20:51:35 +00:00
id := restic.Hash(data)
2017-06-03 15:39:57 +00:00
err := b.Save(context.TODO(), restic.Handle{Name: id.String(), Type: restic.DataFile}, bytes.NewReader(data))
2017-10-02 13:06:39 +00:00
rtest.OK(t, err)
2017-06-03 15:39:57 +00:00
buf, err := backend.LoadAll(context.TODO(), b, restic.Handle{Type: restic.DataFile, Name: id.String()})
2017-10-02 13:06:39 +00:00
rtest.OK(t, err)
if len(buf) != len(data) {
t.Errorf("length of returned buffer does not match, want %d, got %d", len(data), len(buf))
continue
}
if !bytes.Equal(buf, data) {
t.Errorf("wrong data returned")
continue
}
}
}
func TestLoadLargeBuffer(t *testing.T) {
b := mem.New()
for i := 0; i < 20; i++ {
2017-10-02 13:06:39 +00:00
data := rtest.Random(23+i, rand.Intn(MiB)+500*KiB)
2016-08-31 20:51:35 +00:00
id := restic.Hash(data)
2017-06-03 15:39:57 +00:00
err := b.Save(context.TODO(), restic.Handle{Name: id.String(), Type: restic.DataFile}, bytes.NewReader(data))
2017-10-02 13:06:39 +00:00
rtest.OK(t, err)
2017-06-03 15:39:57 +00:00
buf, err := backend.LoadAll(context.TODO(), b, restic.Handle{Type: restic.DataFile, Name: id.String()})
2017-10-02 13:06:39 +00:00
rtest.OK(t, err)
if len(buf) != len(data) {
t.Errorf("length of returned buffer does not match, want %d, got %d", len(data), len(buf))
continue
}
if !bytes.Equal(buf, data) {
t.Errorf("wrong data returned")
continue
}
}
}