1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package gcp
16
17 import (
18 "testing"
19 "time"
20
21 "github.com/google/go-cmp/cmp"
22
23 "github.com/sigstore/rekor/pkg/events"
24 "google.golang.org/protobuf/types/known/emptypb"
25 )
26
27 func TestParseRef(t *testing.T) {
28 t.Parallel()
29 testCases := []struct {
30 desc string
31 ref string
32
33 wantProject string
34 wantTopic string
35 wantErr bool
36 }{
37
38 {
39 desc: "Valid example",
40 ref: "gcppubsub://projects/project-foo/topics/topic-bar",
41 wantProject: "project-foo",
42 wantTopic: "topic-bar",
43 },
44 {
45 desc: "Empty ref",
46 wantErr: true,
47 },
48 {
49 desc: "Missing topic",
50 ref: "gcppubsub://projects/project-foo/topics/",
51 wantErr: true,
52 },
53 {
54 desc: "Wrong scheme",
55 ref: "foo://projects/project-foo/topics/topic-bar",
56 wantErr: true,
57 },
58 }
59
60 for _, tc := range testCases {
61 tc := tc
62 t.Run(tc.desc, func(t *testing.T) {
63 t.Parallel()
64 project, topic, err := parseRef(tc.ref)
65 gotErr := err != nil
66 if gotErr != tc.wantErr {
67 t.Errorf("parseRef(%s) error = %v, wantErr %v", tc.ref, gotErr, tc.wantErr)
68 return
69 }
70 if project != tc.wantProject {
71 t.Errorf("parseRef(%s) project = %s, want %s", tc.ref, project, tc.wantProject)
72 }
73 if topic != tc.wantTopic {
74 t.Errorf("parseRef(%s) topic = %s, want %s", tc.ref, topic, tc.wantTopic)
75 }
76 })
77 }
78 }
79
80 func TestGCPAttrs(t *testing.T) {
81 t.Parallel()
82
83 empty := &emptypb.Empty{}
84 ty := events.RegisterType("gcpAttrsTestEvent", "/source", empty.ProtoReflect().Descriptor())
85
86 coreEvent, err := ty.New("A123-456", &emptypb.Empty{}, nil)
87 if err != nil {
88 t.Fatal(err)
89 }
90 attrs := map[string]any{
91 "attr_string": "string",
92 "attr_bool": true,
93 "attr_int": 123,
94 "attr_bytes": []byte("hello"),
95 "attr_timestamp": time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Second),
96 }
97 attrsEvent, err := ty.New("A456-789", &emptypb.Empty{}, attrs)
98 if err != nil {
99 t.Fatal(err)
100 }
101
102 testCases := []struct {
103 desc string
104 event *events.Event
105 want map[string]string
106 }{
107 {
108 desc: "Core attrs only",
109 event: coreEvent,
110 want: map[string]string{
111 "datacontenttype": "application/fake-test-mime",
112 "source": "/source",
113 "type": "gcpAttrsTestEvent",
114 },
115 },
116 {
117 desc: "With optional attrs",
118 event: attrsEvent,
119 want: map[string]string{
120 "datacontenttype": "application/fake-test-mime",
121 "source": "/source",
122 "type": "gcpAttrsTestEvent",
123 "attr_string": "string",
124 "attr_int": "123",
125 "attr_bool": "true",
126 "attr_bytes": "aGVsbG8=",
127 "attr_timestamp": time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Second).Format(time.RFC3339),
128 },
129 },
130 }
131 for _, tc := range testCases {
132 tc := tc
133 t.Run(tc.desc, func(t *testing.T) {
134 t.Parallel()
135 got := gcpAttrs(tc.event, "application/fake-test-mime")
136 if diff := cmp.Diff(got, tc.want); diff != "" {
137 t.Errorf("unexpected diff:\n%s", diff)
138 }
139 })
140 }
141 }
142
View as plain text