...

Source file src/github.com/google/go-containerregistry/internal/gzip/zip_test.go

Documentation: github.com/google/go-containerregistry/internal/gzip

     1  // Copyright 2020 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gzip
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  func TestReader(t *testing.T) {
    26  	want := "This is the input string."
    27  	buf := bytes.NewBufferString(want)
    28  	zipped := ReadCloser(io.NopCloser(buf))
    29  	unzipped, err := UnzipReadCloser(zipped)
    30  	if err != nil {
    31  		t.Error("UnzipReadCloser() =", err)
    32  	}
    33  
    34  	b, err := io.ReadAll(unzipped)
    35  	if err != nil {
    36  		t.Error("ReadAll() =", err)
    37  	}
    38  	if got := string(b); got != want {
    39  		t.Errorf("ReadAll(); got %q, want %q", got, want)
    40  	}
    41  	if err := unzipped.Close(); err != nil {
    42  		t.Error("Close() =", err)
    43  	}
    44  }
    45  
    46  func TestIs(t *testing.T) {
    47  	tests := []struct {
    48  		in  []byte
    49  		out bool
    50  		err error
    51  	}{
    52  		{[]byte{}, false, nil},
    53  		{[]byte{'\x00', '\x00', '\x00'}, false, nil},
    54  		{[]byte{'\x1f', '\x8b', '\x1b'}, true, nil},
    55  	}
    56  	for _, test := range tests {
    57  		reader := bytes.NewReader(test.in)
    58  		got, err := Is(reader)
    59  		if got != test.out {
    60  			t.Errorf("Is; n: got %v, wanted %v\n", got, test.out)
    61  		}
    62  		if err != test.err {
    63  			t.Errorf("Is; err: got %v, wanted %v\n", err, test.err)
    64  		}
    65  	}
    66  }
    67  
    68  var (
    69  	errRead = fmt.Errorf("Read failed")
    70  )
    71  
    72  type failReader struct{}
    73  
    74  func (f failReader) Read(_ []byte) (int, error) {
    75  	return 0, errRead
    76  }
    77  
    78  func TestReadErrors(t *testing.T) {
    79  	fr := failReader{}
    80  	if _, err := Is(fr); err != errRead {
    81  		t.Error("Is: expected errRead, got", err)
    82  	}
    83  
    84  	frc := io.NopCloser(fr)
    85  	if _, err := UnzipReadCloser(frc); err != errRead {
    86  		t.Error("UnzipReadCloser: expected errRead, got", err)
    87  	}
    88  
    89  	zr := ReadCloser(io.NopCloser(fr))
    90  	if _, err := zr.Read(nil); err != errRead {
    91  		t.Error("ReadCloser: expected errRead, got", err)
    92  	}
    93  
    94  	zr = ReadCloserLevel(io.NopCloser(strings.NewReader("zip me")), -10)
    95  	if _, err := zr.Read(nil); err == nil {
    96  		t.Error("Expected invalid level error, got:", err)
    97  	}
    98  }
    99  

View as plain text