Debian Codegraph @

estargz/gzip_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 /*
18 Copyright 2019 The Go Authors. All rights reserved.
19 Use of this source code is governed by a BSD-style
20 license that can be found in the LICENSE file.
21 */
22
23 package estargz
24
25 import (
26 "bytes"
27 "compress/gzip"
28 "fmt"
29 "testing"
30 )
31
32 // TestGzipEStargz tests gzip-based eStargz
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 // TestStream tests the passed estargz blob contains the specified list of streams.
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 // Tests footer encoding, size, and parsing of gzip-based eStargz.
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 // TODO: check fallback
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 }