...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/remote/image_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  	"testing"
    20  
    21  	"github.com/google/go-containerregistry/pkg/name"
    22  	v1 "github.com/google/go-containerregistry/pkg/v1"
    23  	"github.com/google/go-containerregistry/pkg/v1/random"
    24  	"github.com/google/go-containerregistry/pkg/v1/remote"
    25  )
    26  
    27  func TestSignedImage(t *testing.T) {
    28  	ri := remote.Image
    29  	t.Cleanup(func() {
    30  		remoteImage = ri
    31  	})
    32  	wantLayers := int64(7)
    33  
    34  	remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
    35  		// Only called for signature images
    36  		return random.Image(300 /* byteSize */, wantLayers)
    37  	}
    38  
    39  	ref, err := name.ParseReference("gcr.io/distroless/static:nonroot")
    40  	if err != nil {
    41  		t.Fatalf("ParseRef() = %v", err)
    42  	}
    43  
    44  	si, err := SignedImage(ref)
    45  	if err != nil {
    46  		t.Fatalf("Signatures() = %v", err)
    47  	}
    48  
    49  	sigs, err := si.Signatures()
    50  	if err != nil {
    51  		t.Fatalf("Signatures() = %v", err)
    52  	}
    53  
    54  	if sl, err := sigs.Get(); err != nil {
    55  		t.Errorf("Get() = %v", err)
    56  	} else if got := int64(len(sl)); got != wantLayers {
    57  		t.Errorf("len(Get()) = %d, wanted %d", got, wantLayers)
    58  	}
    59  
    60  	atts, err := si.Attestations()
    61  	if err != nil {
    62  		t.Fatalf("Signatures() = %v", err)
    63  	}
    64  
    65  	if al, err := atts.Get(); err != nil {
    66  		t.Errorf("Get() = %v", err)
    67  	} else if got := int64(len(al)); got != wantLayers {
    68  		t.Errorf("len(Get()) = %d, wanted %d", got, wantLayers)
    69  	}
    70  }
    71  
    72  func TestSignedImageWithAttachment(t *testing.T) {
    73  	ri := remote.Image
    74  	t.Cleanup(func() {
    75  		remoteImage = ri
    76  	})
    77  	wantLayers := int64(1) // File must have a single layer
    78  
    79  	remoteImage = func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
    80  		// Only called for signature images
    81  		return random.Image(300 /* byteSize */, wantLayers)
    82  	}
    83  
    84  	ref, err := name.ParseReference("gcr.io/distroless/static:nonroot")
    85  	if err != nil {
    86  		t.Fatalf("ParseRef() = %v", err)
    87  	}
    88  
    89  	si, err := SignedImage(ref)
    90  	if err != nil {
    91  		t.Fatalf("Signatures() = %v", err)
    92  	}
    93  
    94  	file, err := si.Attachment("sbom")
    95  	if err != nil {
    96  		t.Fatalf("Signatures() = %v", err)
    97  	}
    98  
    99  	payload, err := file.Payload()
   100  	if err != nil {
   101  		t.Errorf("Payload() = %v", err)
   102  	}
   103  	// We check greater than because it's wrapped in a tarball with `random.Layer`
   104  	if len(payload) < 300 {
   105  		t.Errorf("Payload() = %d bytes, wanted %d", len(payload), 300)
   106  	}
   107  }
   108  

View as plain text