1
2
3
4
5
6
7
8
9
10
11
12
13
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"},
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: ×tamppb.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