...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/remote/signatures_test.go

Documentation: github.com/sigstore/cosign/v2/pkg/oci/remote

     1  //
     2  // Copyright 2021 The Sigstore 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  package remote
    17  
    18  import (
    19  	"errors"
    20  	"net/http"
    21  	"testing"
    22  
    23  	"github.com/google/go-containerregistry/pkg/name"
    24  	v1 "github.com/google/go-containerregistry/pkg/v1"
    25  	"github.com/google/go-containerregistry/pkg/v1/fake"
    26  	"github.com/google/go-containerregistry/pkg/v1/remote"
    27  	"github.com/google/go-containerregistry/pkg/v1/remote/transport"
    28  )
    29  
    30  func TestSignaturesErrors(t *testing.T) {
    31  	ri := remote.Image
    32  	t.Cleanup(func() {
    33  		remoteImage = ri
    34  	})
    35  
    36  	t.Run("404 returns empty", func(t *testing.T) {
    37  		remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
    38  			return nil, &transport.Error{
    39  				StatusCode: http.StatusNotFound,
    40  			}
    41  		}
    42  
    43  		sigs, err := Signatures(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
    44  		if err != nil {
    45  			t.Fatalf("Signatures() = %v", err)
    46  		}
    47  		if sl, err := sigs.Get(); err != nil {
    48  			t.Fatalf("Get() = %v", err)
    49  		} else if len(sl) != 0 {
    50  			t.Fatalf("len(Get()) = %d, wanted 0", len(sl))
    51  		}
    52  	})
    53  
    54  	t.Run("other transport errors propagate", func(t *testing.T) {
    55  		want := &transport.Error{
    56  			StatusCode: http.StatusInternalServerError,
    57  		}
    58  		remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
    59  			return nil, want
    60  		}
    61  
    62  		_, err := Signatures(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
    63  		if !errors.Is(err, want) {
    64  			t.Fatalf("Signatures() = %v, wanted %v", err, want)
    65  		}
    66  	})
    67  
    68  	t.Run("other errors propagate", func(t *testing.T) {
    69  		want := errors.New("it's my error, I can cry if I want to")
    70  		remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
    71  			return nil, want
    72  		}
    73  
    74  		_, err := Signatures(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
    75  		if !errors.Is(err, want) {
    76  			t.Fatalf("Signatures() = %v, wanted %v", err, want)
    77  		}
    78  	})
    79  
    80  	t.Run("too many layers", func(t *testing.T) {
    81  		remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
    82  			return &fake.FakeImage{
    83  				ManifestStub: func() (*v1.Manifest, error) {
    84  					return &v1.Manifest{
    85  						Layers: make([]v1.Descriptor, 10000),
    86  					}, nil
    87  				},
    88  			}, nil
    89  		}
    90  		sigs, err := Signatures(name.MustParseReference("gcr.io/distroless/static:sha256-deadbeef.sig"))
    91  		if err != nil {
    92  			t.Fatalf("Signatures() = %v", err)
    93  		}
    94  		want := errors.New("number of layers (10000) exceeded the limit (1000)")
    95  		_, err = sigs.Get()
    96  		if err == nil || want.Error() != err.Error() {
    97  			t.Fatalf("Get() = %v", err)
    98  		}
    99  	})
   100  }
   101  

View as plain text