pack: slightly expand testing of compressed blobs

This commit is contained in:
Michael Eischer 2022-04-29 23:41:03 +02:00
parent ec2b25565a
commit bcab548617
2 changed files with 49 additions and 9 deletions

View file

@ -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 // headerEntry describes the format of header entries. It serves only as
// documentation. // documentation.
type headerEntry struct { type headerEntry struct {
Type uint8 Type uint8
Length uint32 Length uint32
ID restic.ID ID restic.ID
CompressedLength uint32 }
// 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. // Finalize writes the header for all added blobs and finalizes the pack.

View file

@ -13,7 +13,7 @@ import (
func TestParseHeaderEntry(t *testing.T) { func TestParseHeaderEntry(t *testing.T) {
h := headerEntry{ h := headerEntry{
Type: 0, // Blob. Type: 0, // Blob
Length: 100, Length: 100,
} }
for i := range h.ID { for i := range h.ID {
@ -28,21 +28,53 @@ func TestParseHeaderEntry(t *testing.T) {
rtest.Equals(t, restic.DataBlob, b.Type) rtest.Equals(t, restic.DataBlob, b.Type)
rtest.Equals(t, plainEntrySize, size) rtest.Equals(t, plainEntrySize, size)
t.Logf("%v %v", h.ID, b.ID) 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(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 h.Type = 0xae
buf.Reset() buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.LittleEndian, &h) _ = 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") rtest.Assert(t, err != nil, "no error for invalid type")
h.Type = 0 h.Type = 0
buf.Reset() buf.Reset()
_ = binary.Write(buf, binary.LittleEndian, &h) _ = 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") rtest.Assert(t, err != nil, "no error for short input")
} }