Skip to content

Commit

Permalink
Make ReadFooterDataFromBlob test-only
Browse files Browse the repository at this point in the history
It has no non-test users any more, so decrease the
size of this package (relevant to non-c/storage
callers of c/image).

Should not change behavior.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Apr 22, 2024
1 parent 9fbd0e0 commit e7a3eae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
23 changes: 0 additions & 23 deletions pkg/chunked/internal/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"archive/tar"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -251,25 +250,3 @@ func ReadFooterDataFromAnnotations(annotations map[string]string) (ZstdChunkedFo
}
return footerData, nil
}

// ReadFooterDataFromBlob reads the zstd:chunked footer from the binary buffer.
func ReadFooterDataFromBlob(footer []byte) (ZstdChunkedFooterData, error) {
var footerData ZstdChunkedFooterData

if len(footer) < FooterSizeSupported {
return footerData, errors.New("blob too small")
}
footerData.Offset = binary.LittleEndian.Uint64(footer[0:8])
footerData.LengthCompressed = binary.LittleEndian.Uint64(footer[8:16])
footerData.LengthUncompressed = binary.LittleEndian.Uint64(footer[16:24])
footerData.ManifestType = binary.LittleEndian.Uint64(footer[24:32])
footerData.OffsetTarSplit = binary.LittleEndian.Uint64(footer[32:40])
footerData.LengthCompressedTarSplit = binary.LittleEndian.Uint64(footer[40:48])
footerData.LengthUncompressedTarSplit = binary.LittleEndian.Uint64(footer[48:56])

// the magic number is stored in the last 8 bytes
if !bytes.Equal(ZstdChunkedFrameMagic, footer[len(footer)-len(ZstdChunkedFrameMagic):]) {
return footerData, errors.New("invalid magic number")
}
return footerData, nil
}
27 changes: 26 additions & 1 deletion pkg/chunked/internal/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package internal

import (
"bytes"
"encoding/binary"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -23,10 +26,32 @@ func TestGenerateAndReadFooter(t *testing.T) {
b := footerDataToBlob(footer)
assert.Len(t, b, FooterSizeSupported)

footer2, err := ReadFooterDataFromBlob(b)
footer2, err := readFooterDataFromBlob(b)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, footer, footer2)
}

// readFooterDataFromBlob reads the zstd:chunked footer from the binary buffer.
func readFooterDataFromBlob(footer []byte) (ZstdChunkedFooterData, error) {
var footerData ZstdChunkedFooterData

if len(footer) < FooterSizeSupported {
return footerData, errors.New("blob too small")
}
footerData.Offset = binary.LittleEndian.Uint64(footer[0:8])
footerData.LengthCompressed = binary.LittleEndian.Uint64(footer[8:16])
footerData.LengthUncompressed = binary.LittleEndian.Uint64(footer[16:24])
footerData.ManifestType = binary.LittleEndian.Uint64(footer[24:32])
footerData.OffsetTarSplit = binary.LittleEndian.Uint64(footer[32:40])
footerData.LengthCompressedTarSplit = binary.LittleEndian.Uint64(footer[40:48])
footerData.LengthUncompressedTarSplit = binary.LittleEndian.Uint64(footer[48:56])

// the magic number is stored in the last 8 bytes
if !bytes.Equal(ZstdChunkedFrameMagic, footer[len(footer)-len(ZstdChunkedFrameMagic):]) {
return footerData, errors.New("invalid magic number")
}
return footerData, nil
}

0 comments on commit e7a3eae

Please sign in to comment.