From a0df376348d2ad1d3e557ea221e75c78a5d9fd96 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sat, 17 Dec 2022 12:12:05 +0100 Subject: pkg/image: make Decompress easier to use Change DecompressWriter to DecompressCheck: checking validity of the image is the only useful use of DecompressWriter. Change Decompress to MustDecompress which does not return an error. We check validity during program deserialization, so all other uses already panic on errors. Also add dtor return value in preparation for subsequent changes. --- pkg/image/compression_test.go | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'pkg/image/compression_test.go') diff --git a/pkg/image/compression_test.go b/pkg/image/compression_test.go index cf18ed340..0107f82aa 100644 --- a/pkg/image/compression_test.go +++ b/pkg/image/compression_test.go @@ -13,32 +13,33 @@ import ( ) func TestCompress(t *testing.T) { + t.Parallel() r := rand.New(testutil.RandSource(t)) - err := testRoundTrip(r, Compress, Decompress) - if err != nil { - t.Fatalf("compress/decompress %v", err) + for i := 0; i < testutil.IterCount(); i++ { + t.Run(fmt.Sprint(i), func(t *testing.T) { + randBytes := testutil.RandMountImage(r) + resultBytes := Compress(randBytes) + resultBytes, dtor := MustDecompress(resultBytes) + defer dtor() + if !bytes.Equal(randBytes, resultBytes) { + t.Fatalf("roundtrip changes data (length %v->%v)", len(randBytes), len(resultBytes)) + } + }) } } func TestEncode(t *testing.T) { + t.Parallel() r := rand.New(testutil.RandSource(t)) - err := testRoundTrip(r, EncodeB64, DecodeB64) - if err != nil { - t.Fatalf("encode/decode Base64 %v", err) - } -} - -func testRoundTrip(r *rand.Rand, transform func([]byte) []byte, inverse func([]byte) ([]byte, error)) error { for i := 0; i < testutil.IterCount(); i++ { randBytes := testutil.RandMountImage(r) - resultBytes := transform(randBytes) - resultBytes, err := inverse(resultBytes) + resultBytes := EncodeB64(randBytes) + resultBytes, err := DecodeB64(resultBytes) if err != nil { - return err + t.Fatalf("decoding failed: %v", err) } if !bytes.Equal(randBytes, resultBytes) { - return fmt.Errorf("roundtrip changes data (original length %d)", len(randBytes)) + t.Fatalf("roundtrip changes data (original length %d)", len(randBytes)) } } - return nil } -- cgit mrf-deployment