pack: Add test with backend.NewReadSeeker

This uses the new backend ReadSeeker with the unpacker.
This commit is contained in:
Alexander Neumann 2016-02-21 00:21:06 +01:00
parent 21a99397ff
commit 090920039f
1 changed files with 35 additions and 11 deletions

View File

@ -11,6 +11,7 @@ import (
"testing" "testing"
"restic/backend" "restic/backend"
"restic/backend/mem"
"restic/crypto" "restic/crypto"
"restic/pack" "restic/pack"
. "restic/test" . "restic/test"
@ -18,12 +19,12 @@ import (
var lengths = []int{23, 31650, 25860, 10928, 13769, 19862, 5211, 127, 13690, 30231} var lengths = []int{23, 31650, 25860, 10928, 13769, 19862, 5211, 127, 13690, 30231}
func TestCreatePack(t *testing.T) { type Buf struct {
type Buf struct { data []byte
data []byte id backend.ID
id backend.ID }
}
func newPack(t testing.TB, k *crypto.Key) ([]Buf, []byte, uint) {
bufs := []Buf{} bufs := []Buf{}
for _, l := range lengths { for _, l := range lengths {
@ -34,9 +35,6 @@ func TestCreatePack(t *testing.T) {
bufs = append(bufs, Buf{data: b, id: h}) bufs = append(bufs, Buf{data: b, id: h})
} }
// create random keys
k := crypto.NewRandomKey()
// pack blobs // pack blobs
p := pack.NewPacker(k, nil) p := pack.NewPacker(k, nil)
for _, b := range bufs { for _, b := range bufs {
@ -46,6 +44,10 @@ func TestCreatePack(t *testing.T) {
packData, err := p.Finalize() packData, err := p.Finalize()
OK(t, err) OK(t, err)
return bufs, packData, p.Size()
}
func verifyBlobs(t testing.TB, bufs []Buf, k *crypto.Key, rd io.ReadSeeker, packSize uint) {
written := 0 written := 0
for _, l := range lengths { for _, l := range lengths {
written += l written += l
@ -58,11 +60,9 @@ func TestCreatePack(t *testing.T) {
written += crypto.Extension written += crypto.Extension
// check length // check length
Equals(t, written, len(packData)) Equals(t, uint(written), packSize)
Equals(t, uint(written), p.Size())
// read and parse it again // read and parse it again
rd := bytes.NewReader(packData)
np, err := pack.NewUnpacker(k, rd) np, err := pack.NewUnpacker(k, rd)
OK(t, err) OK(t, err)
Equals(t, len(np.Entries), len(bufs)) Equals(t, len(np.Entries), len(bufs))
@ -81,6 +81,15 @@ func TestCreatePack(t *testing.T) {
} }
} }
func TestCreatePack(t *testing.T) {
// create random keys
k := crypto.NewRandomKey()
bufs, packData, packSize := newPack(t, k)
Equals(t, uint(len(packData)), packSize)
verifyBlobs(t, bufs, k, bytes.NewReader(packData), packSize)
}
var blobTypeJSON = []struct { var blobTypeJSON = []struct {
t pack.BlobType t pack.BlobType
res string res string
@ -103,3 +112,18 @@ func TestBlobTypeJSON(t *testing.T) {
Equals(t, test.t, v) Equals(t, test.t, v)
} }
} }
func TestUnpackReadSeeker(t *testing.T) {
// create random keys
k := crypto.NewRandomKey()
bufs, packData, packSize := newPack(t, k)
b := mem.New()
id := backend.Hash(packData)
handle := backend.Handle{Type: backend.Data, Name: id.String()}
OK(t, b.Save(handle, packData))
rd := backend.NewReadSeeker(b, handle)
verifyBlobs(t, bufs, k, rd, packSize)
}