2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 03:18:55 +00:00

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

View File

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