1
16
17
22
23 package estargz
24
25 import (
26 "bytes"
27 "compress/gzip"
28 "fmt"
29 "testing"
30 )
31
32
33 func TestGzipEStargz(t *testing.T) {
34 CompressionTestSuite(t,
35 gzipControllerWithLevel(gzip.NoCompression),
36 gzipControllerWithLevel(gzip.BestSpeed),
37 gzipControllerWithLevel(gzip.BestCompression),
38 gzipControllerWithLevel(gzip.DefaultCompression),
39 gzipControllerWithLevel(gzip.HuffmanOnly),
40 )
41 }
42
43 func gzipControllerWithLevel(compressionLevel int) TestingControllerFactory {
44 return func() TestingController {
45 return &gzipController{&GzipCompressor{compressionLevel}, &GzipDecompressor{}}
46 }
47 }
48
49 type gzipController struct {
50 *GzipCompressor
51 *GzipDecompressor
52 }
53
54 func (gc *gzipController) String() string {
55 return fmt.Sprintf("gzip_compression_level=%v", gc.GzipCompressor.compressionLevel)
56 }
57
58
59 func (gc *gzipController) TestStreams(t *testing.T, b []byte, streams []int64) {
60 CheckGzipHasStreams(t, b, streams)
61 }
62
63 func (gc *gzipController) DiffIDOf(t *testing.T, b []byte) string {
64 return GzipDiffIDOf(t, b)
65 }
66
67
68 func TestGzipFooter(t *testing.T) {
69 for off := int64(0); off <= 200000; off += 1023 {
70 checkFooter(t, off)
71 checkLegacyFooter(t, off)
72 }
73 }
74
75
76 func checkFooter(t *testing.T, off int64) {
77 footer := gzipFooterBytes(off)
78 if len(footer) != FooterSize {
79 t.Fatalf("for offset %v, footer length was %d, not expected %d. got bytes: %q", off, len(footer), FooterSize, footer)
80 }
81 _, got, _, err := (&GzipDecompressor{}).ParseFooter(footer)
82 if err != nil {
83 t.Fatalf("failed to parse footer for offset %d, footer: %x: err: %v",
84 off, footer, err)
85 }
86 if got != off {
87 t.Fatalf("ParseFooter(footerBytes(offset %d)) = %d; want %d", off, got, off)
88 }
89 }
90
91 func checkLegacyFooter(t *testing.T, off int64) {
92 footer := legacyFooterBytes(off)
93 if len(footer) != legacyFooterSize {
94 t.Fatalf("for offset %v, footer length was %d, not expected %d. got bytes: %q", off, len(footer), legacyFooterSize, footer)
95 }
96 _, got, _, err := (&LegacyGzipDecompressor{}).ParseFooter(footer)
97 if err != nil {
98 t.Fatalf("failed to parse legacy footer for offset %d, footer: %x: err: %v",
99 off, footer, err)
100 }
101 if got != off {
102 t.Fatalf("ParseFooter(legacyFooterBytes(offset %d)) = %d; want %d", off, got, off)
103 }
104 }
105
106 func legacyFooterBytes(tocOff int64) []byte {
107 buf := bytes.NewBuffer(make([]byte, 0, legacyFooterSize))
108 gz, _ := gzip.NewWriterLevel(buf, gzip.NoCompression)
109 gz.Header.Extra = []byte(fmt.Sprintf("%016xSTARGZ", tocOff))
110 gz.Close()
111 if buf.Len() != legacyFooterSize {
112 panic(fmt.Sprintf("footer buffer = %d, not %d", buf.Len(), legacyFooterSize))
113 }
114 return buf.Bytes()
115 }
116
View as plain text