...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/static/options_test.go

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

     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 static
    17  
    18  import (
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"github.com/google/go-containerregistry/pkg/v1/types"
    24  	cbundle "github.com/sigstore/cosign/v2/pkg/cosign/bundle"
    25  	ctypes "github.com/sigstore/cosign/v2/pkg/types"
    26  )
    27  
    28  func TestOptions(t *testing.T) {
    29  	bundle := &cbundle.RekorBundle{}
    30  	rfc3161Timestamp := &cbundle.RFC3161Timestamp{}
    31  
    32  	tests := []struct {
    33  		name string
    34  		opts []Option
    35  		want *options
    36  	}{{
    37  		name: "no options",
    38  		want: &options{
    39  			LayerMediaType:  ctypes.SimpleSigningMediaType,
    40  			ConfigMediaType: types.OCIConfigJSON,
    41  			Annotations:     make(map[string]string),
    42  		},
    43  	}, {
    44  		name: "with layer media type",
    45  		opts: []Option{WithLayerMediaType("foo")},
    46  		want: &options{
    47  			LayerMediaType:  "foo",
    48  			ConfigMediaType: types.OCIConfigJSON,
    49  			Annotations:     make(map[string]string),
    50  		},
    51  	}, {
    52  		name: "with config media type",
    53  		opts: []Option{WithConfigMediaType("bar")},
    54  		want: &options{
    55  			LayerMediaType:  ctypes.SimpleSigningMediaType,
    56  			ConfigMediaType: "bar",
    57  			Annotations:     make(map[string]string),
    58  		},
    59  	}, {
    60  		name: "with annotations",
    61  		opts: []Option{WithAnnotations(map[string]string{
    62  			"foo": "bar",
    63  		})},
    64  		want: &options{
    65  			LayerMediaType:  ctypes.SimpleSigningMediaType,
    66  			ConfigMediaType: types.OCIConfigJSON,
    67  			Annotations: map[string]string{
    68  				"foo": "bar",
    69  			},
    70  		},
    71  	}, {
    72  		name: "with cert chain",
    73  		opts: []Option{WithCertChain([]byte("a"), []byte("b"))},
    74  		want: &options{
    75  			LayerMediaType:  ctypes.SimpleSigningMediaType,
    76  			ConfigMediaType: types.OCIConfigJSON,
    77  			Annotations: map[string]string{
    78  				CertificateAnnotationKey: "a",
    79  				ChainAnnotationKey:       "b",
    80  			},
    81  			Cert:  []byte("a"),
    82  			Chain: []byte("b"),
    83  		},
    84  	}, {
    85  		name: "with bundle",
    86  		opts: []Option{WithBundle(bundle)},
    87  		want: &options{
    88  			LayerMediaType:  ctypes.SimpleSigningMediaType,
    89  			ConfigMediaType: types.OCIConfigJSON,
    90  			Annotations: map[string]string{
    91  				BundleAnnotationKey: "{\"SignedEntryTimestamp\":null,\"Payload\":{\"body\":null,\"integratedTime\":0,\"logIndex\":0,\"logID\":\"\"}}",
    92  			},
    93  			Bundle: bundle,
    94  		},
    95  	}, {
    96  		name: "with RFC3161 timestamp bundle",
    97  		opts: []Option{WithRFC3161Timestamp(rfc3161Timestamp)},
    98  		want: &options{
    99  			LayerMediaType:  ctypes.SimpleSigningMediaType,
   100  			ConfigMediaType: types.OCIConfigJSON,
   101  			Annotations: map[string]string{
   102  				RFC3161TimestampAnnotationKey: "{\"SignedRFC3161Timestamp\":null}",
   103  			},
   104  			RFC3161Timestamp: rfc3161Timestamp,
   105  		},
   106  	}, {
   107  		name: "with RFC3161Timestamp and Rekor bundle",
   108  		opts: []Option{WithRFC3161Timestamp(rfc3161Timestamp), WithBundle(bundle)},
   109  		want: &options{
   110  			LayerMediaType:  ctypes.SimpleSigningMediaType,
   111  			ConfigMediaType: types.OCIConfigJSON,
   112  			Annotations: map[string]string{
   113  				RFC3161TimestampAnnotationKey: "{\"SignedRFC3161Timestamp\":null}",
   114  				BundleAnnotationKey:           "{\"SignedEntryTimestamp\":null,\"Payload\":{\"body\":null,\"integratedTime\":0,\"logIndex\":0,\"logID\":\"\"}}",
   115  			},
   116  			RFC3161Timestamp: rfc3161Timestamp,
   117  			Bundle:           bundle,
   118  		},
   119  	}, {
   120  		name: "with RFC3161Timestamp and Rekor bundle",
   121  		opts: []Option{WithRFC3161Timestamp(rfc3161Timestamp), WithBundle(bundle)},
   122  		want: &options{
   123  			LayerMediaType:  ctypes.SimpleSigningMediaType,
   124  			ConfigMediaType: types.OCIConfigJSON,
   125  			Annotations: map[string]string{
   126  				RFC3161TimestampAnnotationKey: "{\"SignedRFC3161Timestamp\":null}",
   127  				BundleAnnotationKey:           "{\"SignedEntryTimestamp\":null,\"Payload\":{\"body\":null,\"integratedTime\":0,\"logIndex\":0,\"logID\":\"\"}}",
   128  			},
   129  			RFC3161Timestamp: rfc3161Timestamp,
   130  			Bundle:           bundle,
   131  		},
   132  	}}
   133  
   134  	for _, test := range tests {
   135  		t.Run(test.name, func(t *testing.T) {
   136  			got, err := makeOptions(test.opts...)
   137  			if err != nil {
   138  				t.Fatalf("makeOptions() = %v", err)
   139  			}
   140  
   141  			if !reflect.DeepEqual(got, test.want) {
   142  				t.Errorf("makeOptions() = %s", cmp.Diff(got, test.want))
   143  			}
   144  		})
   145  	}
   146  }
   147  

View as plain text