Fix Unpacker for packs < 2048 byte

This commit is contained in:
Alexander Neumann 2016-08-07 16:24:02 +02:00
parent a5cbbb8b5a
commit ef33cf12ca
2 changed files with 27 additions and 7 deletions

View file

@ -241,6 +241,12 @@ func NewUnpacker(k *crypto.Key, ldr Loader) (*Unpacker, error) {
// we do not need another round trip. // we do not need another round trip.
buf := make([]byte, preloadHeaderSize) buf := make([]byte, preloadHeaderSize)
n, err := ldr.Load(buf, -int64(len(buf))) n, err := ldr.Load(buf, -int64(len(buf)))
if err == io.ErrUnexpectedEOF {
err = nil
buf = buf[:n]
}
if err != nil { if err != nil {
return nil, fmt.Errorf("Load at -%d failed: %v", len(buf), err) return nil, fmt.Errorf("Load at -%d failed: %v", len(buf), err)
} }

View file

@ -16,14 +16,14 @@ import (
. "restic/test" . "restic/test"
) )
var lengths = []int{23, 31650, 25860, 10928, 13769, 19862, 5211, 127, 13690, 30231} var testLens = []int{23, 31650, 25860, 10928, 13769, 19862, 5211, 127, 13690, 30231}
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) { func newPack(t testing.TB, k *crypto.Key, lengths []int) ([]Buf, []byte, uint) {
bufs := []Buf{} bufs := []Buf{}
for _, l := range lengths { for _, l := range lengths {
@ -49,13 +49,13 @@ func newPack(t testing.TB, k *crypto.Key) ([]Buf, []byte, uint) {
func verifyBlobs(t testing.TB, bufs []Buf, k *crypto.Key, ldr pack.Loader, packSize uint) { func verifyBlobs(t testing.TB, bufs []Buf, k *crypto.Key, ldr pack.Loader, packSize uint) {
written := 0 written := 0
for _, l := range lengths { for _, buf := range bufs {
written += l written += len(buf.data)
} }
// header length // header length
written += binary.Size(uint32(0)) written += binary.Size(uint32(0))
// header // header
written += len(lengths) * (binary.Size(pack.BlobType(0)) + binary.Size(uint32(0)) + backend.IDSize) written += len(bufs) * (binary.Size(pack.BlobType(0)) + binary.Size(uint32(0)) + backend.IDSize)
// header crypto // header crypto
written += crypto.Extension written += crypto.Extension
@ -89,7 +89,7 @@ func TestCreatePack(t *testing.T) {
// create random keys // create random keys
k := crypto.NewRandomKey() k := crypto.NewRandomKey()
bufs, packData, packSize := newPack(t, k) bufs, packData, packSize := newPack(t, k, testLens)
Equals(t, uint(len(packData)), packSize) Equals(t, uint(len(packData)), packSize)
verifyBlobs(t, bufs, k, pack.BufferLoader(packData), packSize) verifyBlobs(t, bufs, k, pack.BufferLoader(packData), packSize)
} }
@ -121,7 +121,21 @@ func TestUnpackReadSeeker(t *testing.T) {
// create random keys // create random keys
k := crypto.NewRandomKey() k := crypto.NewRandomKey()
bufs, packData, packSize := newPack(t, k) 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() b := mem.New()
id := backend.Hash(packData) id := backend.Hash(packData)