...

Source file src/github.com/sigstore/rekor/pkg/events/newentry/new_entry_test.go

Documentation: github.com/sigstore/rekor/pkg/events/newentry

     1  // Copyright 2023 The Sigstore Authors.
     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 newentry
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"github.com/sigstore/rekor/pkg/events"
    24  	"google.golang.org/protobuf/proto"
    25  	"google.golang.org/protobuf/testing/protocmp"
    26  	"google.golang.org/protobuf/types/known/anypb"
    27  	"google.golang.org/protobuf/types/known/timestamppb"
    28  
    29  	events_pb "github.com/sigstore/protobuf-specs/gen/pb-go/events/v1"
    30  	rekor_pb "github.com/sigstore/protobuf-specs/gen/pb-go/rekor/v1"
    31  )
    32  
    33  func TestBuildNewEntryEvent(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	decamillennium := time.Date(9999, 12, 31, 24, 59, 59, math.MaxInt, time.UTC).Unix()
    37  	testEntry := &rekor_pb.TransparencyLogEntry{
    38  		IntegratedTime: decamillennium,
    39  		KindVersion: &rekor_pb.KindVersion{
    40  			Kind: "test_kind",
    41  		},
    42  	}
    43  	marshalledEntry, err := proto.Marshal(testEntry)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	testCases := []struct {
    49  		desc     string
    50  		entryID  string
    51  		subjects []string
    52  		entry    *rekor_pb.TransparencyLogEntry
    53  
    54  		wantError bool
    55  		want      *events_pb.CloudEvent
    56  	}{
    57  		{
    58  			desc:      "missing ID",
    59  			subjects:  []string{"test@rekor.dev"},
    60  			entry:     testEntry,
    61  			wantError: true,
    62  		},
    63  		{
    64  			desc:     "valid",
    65  			entryID:  "test-id",
    66  			subjects: []string{"test@rekor.dev", "foo@bar.baz"}, // Output should be sorted.
    67  			entry:    testEntry,
    68  			want: &events_pb.CloudEvent{
    69  				SpecVersion: events.CloudEventsSpecVersion,
    70  				Id:          "test-id",
    71  				Source:      Source,
    72  				Type:        Name,
    73  				Attributes: map[string]*events_pb.CloudEvent_CloudEventAttributeValue{
    74  					"time": {Attr: &events_pb.CloudEvent_CloudEventAttributeValue_CeTimestamp{
    75  						CeTimestamp: &timestamppb.Timestamp{Seconds: decamillennium},
    76  					}},
    77  					"rekor_entry_kind": {Attr: &events_pb.CloudEvent_CloudEventAttributeValue_CeString{
    78  						CeString: "test_kind",
    79  					}},
    80  					"rekor_signing_subjects": {Attr: &events_pb.CloudEvent_CloudEventAttributeValue_CeString{
    81  						CeString: "foo@bar.baz,test@rekor.dev",
    82  					}},
    83  				},
    84  				Data: &events_pb.CloudEvent_ProtoData{
    85  					ProtoData: &anypb.Any{
    86  						Value:   marshalledEntry,
    87  						TypeUrl: string(testEntry.ProtoReflect().Descriptor().FullName()),
    88  					},
    89  				},
    90  			},
    91  		},
    92  	}
    93  	for _, tc := range testCases {
    94  		tc := tc
    95  		t.Run(tc.desc, func(t *testing.T) {
    96  			t.Parallel()
    97  			event, err := New(tc.entryID, tc.entry, tc.subjects)
    98  			gotErr := err != nil
    99  			if gotErr != tc.wantError {
   100  				t.Fatalf("New() err = %v, want %v", gotErr, tc.wantError)
   101  			}
   102  			if err != nil {
   103  				return
   104  			}
   105  			msg, err := event.Proto()
   106  			if err != nil {
   107  				t.Fatal(err)
   108  			}
   109  			if diff := cmp.Diff(msg, tc.want, protocmp.Transform()); diff != "" {
   110  				t.Errorf("New() unexpected diff:\n%s", diff)
   111  			}
   112  		})
   113  	}
   114  }
   115  

View as plain text