...

Source file src/github.com/google/go-containerregistry/internal/zstd/zstd_test.go

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

     1  // Copyright 2022 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 zstd
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"testing"
    22  )
    23  
    24  func TestReader(t *testing.T) {
    25  	want := "This is the input string."
    26  	buf := bytes.NewBufferString(want)
    27  	zipped := ReadCloser(io.NopCloser(buf))
    28  	unzipped, err := UnzipReadCloser(zipped)
    29  	if err != nil {
    30  		t.Error("UnzipReadCloser() =", err)
    31  	}
    32  
    33  	b, err := io.ReadAll(unzipped)
    34  	if err != nil {
    35  		t.Error("ReadAll() =", err)
    36  	}
    37  	if got := string(b); got != want {
    38  		t.Errorf("ReadAll(); got %q, want %q", got, want)
    39  	}
    40  	if err := unzipped.Close(); err != nil {
    41  		t.Error("Close() =", err)
    42  	}
    43  }
    44  
    45  func TestIs(t *testing.T) {
    46  	tests := []struct {
    47  		in  []byte
    48  		out bool
    49  		err error
    50  	}{
    51  		{[]byte{}, false, nil},
    52  		{[]byte{'\x00', '\x00', '\x00', '\x00', '\x00'}, false, nil},
    53  		{[]byte{'\x28', '\xb5', '\x2f', '\xfd', '\x1b'}, true, nil},
    54  	}
    55  	for _, test := range tests {
    56  		reader := bytes.NewReader(test.in)
    57  		got, err := Is(reader)
    58  		if got != test.out {
    59  			t.Errorf("Is; n: got %v, wanted %v\n", got, test.out)
    60  		}
    61  		if err != test.err {
    62  			t.Errorf("Is; err: got %v, wanted %v\n", err, test.err)
    63  		}
    64  	}
    65  }
    66  
    67  var (
    68  	errRead = fmt.Errorf("read failed")
    69  )
    70  
    71  type failReader struct{}
    72  
    73  func (f failReader) Read(_ []byte) (int, error) {
    74  	return 0, errRead
    75  }
    76  
    77  func TestReadErrors(t *testing.T) {
    78  	fr := failReader{}
    79  	if _, err := Is(fr); err != errRead {
    80  		t.Error("Is: expected errRead, got", err)
    81  	}
    82  
    83  	frc := io.NopCloser(fr)
    84  	if r, err := UnzipReadCloser(frc); err != errRead {
    85  		data := make([]byte, 100)
    86  		_, err := r.Read(data)
    87  		if err != errRead {
    88  			t.Error("UnzipReadCloser: expected errRead, got", err)
    89  		}
    90  	}
    91  
    92  	zr := ReadCloser(io.NopCloser(fr))
    93  	if _, err := zr.Read(nil); err != errRead {
    94  		t.Error("ReadCloser: expected errRead, got", err)
    95  	}
    96  }
    97  

View as plain text