...
1
16
17 package externaltoc
18
19 import (
20 "bytes"
21 "compress/gzip"
22 "fmt"
23 "testing"
24
25 "github.com/containerd/stargz-snapshotter/estargz"
26 )
27
28
29 func TestGzipEStargz(t *testing.T) {
30 estargz.CompressionTestSuite(t,
31 gzipControllerWithLevel(gzip.NoCompression),
32 gzipControllerWithLevel(gzip.BestSpeed),
33 gzipControllerWithLevel(gzip.BestCompression),
34 gzipControllerWithLevel(gzip.DefaultCompression),
35 gzipControllerWithLevel(gzip.HuffmanOnly),
36 )
37 }
38
39 func gzipControllerWithLevel(compressionLevel int) estargz.TestingControllerFactory {
40 return func() estargz.TestingController {
41 compressor := NewGzipCompressorWithLevel(compressionLevel)
42 decompressor := NewGzipDecompressor(func() ([]byte, error) {
43 buf := new(bytes.Buffer)
44 if _, err := compressor.WriteTOCTo(buf); err != nil {
45 return nil, err
46 }
47 return buf.Bytes(), nil
48 })
49 return &gzipController{compressor, decompressor}
50 }
51 }
52
53 type gzipController struct {
54 *GzipCompressor
55 *GzipDecompressor
56 }
57
58 func (gc *gzipController) String() string {
59 return fmt.Sprintf("externaltoc_gzip_compression_level=%v", gc.GzipCompressor.compressionLevel)
60 }
61
62
63 func (gc *gzipController) TestStreams(t *testing.T, b []byte, streams []int64) {
64 estargz.CheckGzipHasStreams(t, b, streams)
65 }
66
67 func (gc *gzipController) DiffIDOf(t *testing.T, b []byte) string {
68 return estargz.GzipDiffIDOf(t, b)
69 }
70
71
72 func TestGzipFooter(t *testing.T) {
73 footer, err := gzipFooterBytes()
74 if err != nil {
75 t.Fatalf("failed gzipFooterBytes: %v", err)
76 }
77 if len(footer) != FooterSize {
78 t.Fatalf("footer length was %d, not expected %d. got bytes: %q", len(footer), FooterSize, footer)
79 }
80 _, gotTOCOffset, _, err := (&GzipDecompressor{}).ParseFooter(footer)
81 if err != nil {
82 t.Fatalf("failed to parse footer, footer: %x: err: %v", footer, err)
83 }
84 if gotTOCOffset != -1 {
85 t.Fatalf("ParseFooter(footerBytes) must return -1 for external toc but got %d", gotTOCOffset)
86 }
87 }
88
View as plain text