...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/empty/signed_test.go

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

     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 empty
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/google/go-containerregistry/pkg/name"
    22  )
    23  
    24  func TestSignedImage(t *testing.T) {
    25  	tests := []struct {
    26  		ref       string
    27  		digestStr string
    28  		digestErr string
    29  	}{
    30  		{
    31  			ref:       "hello-world:latest",
    32  			digestErr: "digest not available",
    33  		},
    34  		{
    35  			ref:       "hello-world@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a",
    36  			digestStr: "sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a",
    37  		},
    38  	}
    39  	for _, test := range tests {
    40  		ref, err := name.ParseReference(test.ref)
    41  		if err != nil {
    42  			t.Errorf("failed to parse ref \"%s\": %v", test.ref, err)
    43  			continue
    44  		}
    45  		se, err := SignedImage(ref)
    46  		if err != nil {
    47  			t.Errorf("failed to create signed image for \"%s\": %v", test.ref, err)
    48  			continue
    49  		}
    50  		d, err := se.Digest()
    51  		if (err == nil && test.digestErr != "") ||
    52  			(err != nil && test.digestErr == "") ||
    53  			(err != nil && test.digestErr != "" && err.Error() != test.digestErr) {
    54  			t.Errorf("digest error mismatch for \"%s\": expected %s, saw %v", test.ref, test.digestErr, err)
    55  		}
    56  		if test.digestStr != "" && d.String() != test.digestStr {
    57  			t.Errorf("digest mismatch for \"%s\": expected %s, saw %s", test.ref, test.digestStr, d.String())
    58  		}
    59  		_, err = se.Signatures()
    60  		if err != nil {
    61  			t.Errorf("failed to get signatures for %s: %v", test.ref, err)
    62  		}
    63  		_, err = se.Attestations()
    64  		if err != nil {
    65  			t.Errorf("failed to get attestations for %s: %v", test.ref, err)
    66  		}
    67  	}
    68  }
    69  

View as plain text