restic/internal/backend/utils_test.go

92 lines
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"
2016-08-31 20:51:35 +00:00
"restic"
2016-01-23 22:41:55 +00:00
"testing"
"restic/backend"
"restic/backend/mem"
. "restic/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++ {
data := 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))
2016-01-23 22:41:55 +00:00
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()})
2016-01-23 22:41:55 +00:00
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 TestLoadSmallBuffer(t *testing.T) {
b := mem.New()
for i := 0; i < 20; i++ {
data := 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))
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()})
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++ {
data := 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))
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()})
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
}
}
}