| 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 |
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 |
// TestGzipEStargz tests gzip-based external TOC eStargz |
| 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 |
// TestStream tests the passed estargz blob contains the specified list of streams. |
| 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 |
// Tests footer encoding, size, and parsing of gzip-based eStargz. |
| 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 |
} |