From bcab5486171e0dd09f8fc4dcf87b58031fc8f13a Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 29 Apr 2022 23:41:03 +0200 Subject: [PATCH] pack: slightly expand testing of compressed blobs --- internal/pack/pack.go | 16 ++++++++--- internal/pack/pack_internal_test.go | 42 +++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/internal/pack/pack.go b/internal/pack/pack.go index 697a14a5d..2d7a5c3fb 100644 --- a/internal/pack/pack.go +++ b/internal/pack/pack.go @@ -54,10 +54,18 @@ var plainEntrySize = uint(binary.Size(restic.BlobType(0)) + headerLengthSize + l // headerEntry describes the format of header entries. It serves only as // documentation. type headerEntry struct { - Type uint8 - Length uint32 - ID restic.ID - CompressedLength uint32 + Type uint8 + Length uint32 + ID restic.ID +} + +// compressedHeaderEntry describes the format of header entries for compressed blobs. +// It serves only as documentation. +type compressedHeaderEntry struct { + Type uint8 + Length uint32 + UncompressedLength uint32 + ID restic.ID } // Finalize writes the header for all added blobs and finalizes the pack. diff --git a/internal/pack/pack_internal_test.go b/internal/pack/pack_internal_test.go index 93d04f18e..c1a4867ea 100644 --- a/internal/pack/pack_internal_test.go +++ b/internal/pack/pack_internal_test.go @@ -13,7 +13,7 @@ import ( func TestParseHeaderEntry(t *testing.T) { h := headerEntry{ - Type: 0, // Blob. + Type: 0, // Blob Length: 100, } for i := range h.ID { @@ -28,21 +28,53 @@ func TestParseHeaderEntry(t *testing.T) { rtest.Equals(t, restic.DataBlob, b.Type) rtest.Equals(t, plainEntrySize, size) t.Logf("%v %v", h.ID, b.ID) - rtest.Assert(t, bytes.Equal(h.ID[:], b.ID[:]), "id mismatch") + rtest.Equals(t, h.ID[:], b.ID[:]) rtest.Equals(t, uint(h.Length), b.Length) + rtest.Equals(t, uint(0), b.UncompressedLength) + + c := compressedHeaderEntry{ + Type: 2, // compressed Blob + Length: 100, + UncompressedLength: 200, + } + for i := range c.ID { + c.ID[i] = byte(i) + } + + buf = new(bytes.Buffer) + _ = binary.Write(buf, binary.LittleEndian, &c) + + b, size, err = parseHeaderEntry(buf.Bytes()) + rtest.OK(t, err) + rtest.Equals(t, restic.DataBlob, b.Type) + rtest.Equals(t, entrySize, size) + t.Logf("%v %v", c.ID, b.ID) + rtest.Equals(t, c.ID[:], b.ID[:]) + rtest.Equals(t, uint(c.Length), b.Length) + rtest.Equals(t, uint(c.UncompressedLength), b.UncompressedLength) +} + +func TestParseHeaderEntryErrors(t *testing.T) { + h := headerEntry{ + Type: 0, // Blob + Length: 100, + } + for i := range h.ID { + h.ID[i] = byte(i) + } h.Type = 0xae - buf.Reset() + buf := new(bytes.Buffer) _ = binary.Write(buf, binary.LittleEndian, &h) - b, _, err = parseHeaderEntry(buf.Bytes()) + _, _, err := parseHeaderEntry(buf.Bytes()) rtest.Assert(t, err != nil, "no error for invalid type") h.Type = 0 buf.Reset() _ = binary.Write(buf, binary.LittleEndian, &h) - b, _, err = parseHeaderEntry(buf.Bytes()[:plainEntrySize-1]) + _, _, err = parseHeaderEntry(buf.Bytes()[:plainEntrySize-1]) rtest.Assert(t, err != nil, "no error for short input") }