restic/src/restic/pack/pack_test.go

148 lines
3.1 KiB
Go
Raw Normal View History

2015-04-26 13:36:49 +00:00
package pack_test
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"encoding/json"
"io"
"testing"
"restic/backend"
"restic/backend/mem"
"restic/crypto"
"restic/pack"
. "restic/test"
2015-04-26 13:36:49 +00:00
)
2016-08-07 14:24:02 +00:00
var testLens = []int{23, 31650, 25860, 10928, 13769, 19862, 5211, 127, 13690, 30231}
2015-04-26 13:36:49 +00:00
type Buf struct {
data []byte
id backend.ID
}
2015-04-26 13:36:49 +00:00
2016-08-07 14:24:02 +00:00
func newPack(t testing.TB, k *crypto.Key, lengths []int) ([]Buf, []byte, uint) {
2015-04-26 13:36:49 +00:00
bufs := []Buf{}
for _, l := range lengths {
b := make([]byte, l)
_, err := io.ReadFull(rand.Reader, b)
OK(t, err)
h := sha256.Sum256(b)
bufs = append(bufs, Buf{data: b, id: h})
2015-04-26 13:36:49 +00:00
}
// pack blobs
2016-01-24 18:30:14 +00:00
p := pack.NewPacker(k, nil)
2015-04-26 13:36:49 +00:00
for _, b := range bufs {
p.Add(pack.Tree, b.id, b.data)
2015-04-26 13:36:49 +00:00
}
_, err := p.Finalize()
2015-04-26 13:36:49 +00:00
OK(t, err)
packData := p.Writer().(*bytes.Buffer).Bytes()
return bufs, packData, p.Size()
}
2016-08-07 12:50:24 +00:00
func verifyBlobs(t testing.TB, bufs []Buf, k *crypto.Key, ldr pack.Loader, packSize uint) {
2015-04-26 13:36:49 +00:00
written := 0
2016-08-07 14:24:02 +00:00
for _, buf := range bufs {
written += len(buf.data)
2015-04-26 13:36:49 +00:00
}
// header length
written += binary.Size(uint32(0))
// header
2016-08-07 14:24:02 +00:00
written += len(bufs) * (binary.Size(pack.BlobType(0)) + binary.Size(uint32(0)) + backend.IDSize)
2015-04-26 13:36:49 +00:00
// header crypto
written += crypto.Extension
// check length
Equals(t, uint(written), packSize)
2015-04-26 13:36:49 +00:00
// read and parse it again
2016-08-07 12:50:24 +00:00
np, err := pack.NewUnpacker(k, ldr)
2015-04-26 13:36:49 +00:00
OK(t, err)
Equals(t, len(np.Entries), len(bufs))
2016-08-07 12:50:24 +00:00
var buf []byte
2015-04-26 13:36:49 +00:00
for i, b := range bufs {
e := np.Entries[i]
Equals(t, b.id, e.ID)
2016-08-07 12:50:24 +00:00
if len(buf) < int(e.Length) {
buf = make([]byte, int(e.Length))
}
buf = buf[:int(e.Length)]
n, err := ldr.Load(buf, int64(e.Offset))
2015-04-26 13:36:49 +00:00
OK(t, err)
2016-08-07 12:50:24 +00:00
buf = buf[:n]
2015-04-26 13:36:49 +00:00
2016-08-07 12:50:24 +00:00
Assert(t, bytes.Equal(b.data, buf),
2015-04-26 13:36:49 +00:00
"data for blob %v doesn't match", i)
}
}
func TestCreatePack(t *testing.T) {
// create random keys
k := crypto.NewRandomKey()
2016-08-07 14:24:02 +00:00
bufs, packData, packSize := newPack(t, k, testLens)
Equals(t, uint(len(packData)), packSize)
2016-08-07 12:50:24 +00:00
verifyBlobs(t, bufs, k, pack.BufferLoader(packData), packSize)
}
2015-10-25 17:21:48 +00:00
var blobTypeJSON = []struct {
2015-04-26 13:36:49 +00:00
t pack.BlobType
res string
}{
{pack.Data, `"data"`},
{pack.Tree, `"tree"`},
}
func TestBlobTypeJSON(t *testing.T) {
2015-10-25 17:21:48 +00:00
for _, test := range blobTypeJSON {
2015-04-26 13:36:49 +00:00
// test serialize
buf, err := json.Marshal(test.t)
OK(t, err)
Equals(t, test.res, string(buf))
// test unserialize
var v pack.BlobType
err = json.Unmarshal([]byte(test.res), &v)
OK(t, err)
Equals(t, test.t, v)
}
}
func TestUnpackReadSeeker(t *testing.T) {
// create random keys
k := crypto.NewRandomKey()
2016-08-07 14:24:02 +00:00
bufs, packData, packSize := newPack(t, k, testLens)
b := mem.New()
id := backend.Hash(packData)
handle := backend.Handle{Type: backend.Data, Name: id.String()}
OK(t, b.Save(handle, packData))
ldr := pack.BackendLoader{Backend: b, Handle: handle}
verifyBlobs(t, bufs, k, ldr, packSize)
}
func TestShortPack(t *testing.T) {
k := crypto.NewRandomKey()
bufs, packData, packSize := newPack(t, k, []int{23})
b := mem.New()
id := backend.Hash(packData)
handle := backend.Handle{Type: backend.Data, Name: id.String()}
OK(t, b.Save(handle, packData))
2016-08-07 12:50:24 +00:00
ldr := pack.BackendLoader{Backend: b, Handle: handle}
verifyBlobs(t, bufs, k, ldr, packSize)
}