Debian Codegraph @

pkg/signature/kms/gcp/gcp_test.go

raw
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 gcp
17
18 import (
19 "context"
20 "testing"
21
22 "golang.org/x/oauth2"
23 "google.golang.org/api/option"
24 )
25
26 func TestParseReference(t *testing.T) {
27 tests := []struct {
28 in string
29 wantProjectID string
30 wantLocationID string
31 wantKeyRing string
32 wantKeyName string
33 wantKeyVersion string
34 wantErr bool
35 }{
36 {
37 in: "gcpkms://projects/pp/locations/ll/keyRings/rr/cryptoKeys/kk",
38 wantProjectID: "pp",
39 wantLocationID: "ll",
40 wantKeyRing: "rr",
41 wantKeyName: "kk",
42 wantErr: false,
43 },
44 {
45 in: "gcpkms://projects/pp/locations/ll/keyRings/rr/cryptoKeys/kk/versions/1",
46 wantProjectID: "pp",
47 wantLocationID: "ll",
48 wantKeyRing: "rr",
49 wantKeyName: "kk",
50 wantKeyVersion: "1",
51 wantErr: false,
52 },
53 {
54 in: "gcpkms://projects/pp/locations/ll/keyRings/rr/cryptoKeys/kk/cryptoKeyVersions/1",
55 wantProjectID: "pp",
56 wantLocationID: "ll",
57 wantKeyRing: "rr",
58 wantKeyName: "kk",
59 wantKeyVersion: "1",
60 wantErr: false,
61 },
62 {
63 in: "gcpkms://projects/p1/p2/locations/l1/l2/keyRings/r1/r2/cryptoKeys/k1/k2",
64 wantErr: true,
65 },
66 {
67 in: "foo://bar",
68 wantErr: true,
69 },
70 {
71 in: "",
72 wantErr: true,
73 },
74 {
75 in: "gcpkms://projects/p1/p2/locations/l1/l2/keyRings/r1/r2/cryptoKeys/k1/versions",
76 wantErr: true,
77 },
78 }
79 for _, tt := range tests {
80 t.Run(tt.in, func(t *testing.T) {
81 gotProjectID, gotLocationID, gotKeyRing, gotKeyName, gotKeyVersion, err := parseReference(tt.in)
82 if (err != nil) != tt.wantErr {
83 t.Errorf("parseReference() error = %v, wantErr %v", err, tt.wantErr)
84 return
85 }
86 if gotProjectID != tt.wantProjectID {
87 t.Errorf("parseReference() gotProjectID = %v, want %v", gotProjectID, tt.wantProjectID)
88 }
89 if gotLocationID != tt.wantLocationID {
90 t.Errorf("parseReference() gotLocationID = %v, want %v", gotLocationID, tt.wantLocationID)
91 }
92 if gotKeyRing != tt.wantKeyRing {
93 t.Errorf("parseReference() gotKeyRing = %v, want %v", gotKeyRing, tt.wantKeyRing)
94 }
95 if gotKeyName != tt.wantKeyName {
96 t.Errorf("parseReference() gotKeyName = %v, want %v", gotKeyName, tt.wantKeyName)
97 }
98 if gotKeyVersion != tt.wantKeyVersion {
99 t.Errorf("parseReference() gotKeyVersion = %v, want %v", gotKeyVersion, tt.wantKeyVersion)
100 }
101 })
102 }
103 }
104
105 func TestOptionsWork(_ *testing.T) {
106 // Check that we can pass options into LoadSignerVerifier
107 // (this is mostly a compile-time check)
108 ts := oauth2.StaticTokenSource(&oauth2.Token{})
109 LoadSignerVerifier(context.Background(), "gcpkms://projects/a-project/locations/global/keyRings/a-keyring/cryptoKeys/key-name", option.WithTokenSource(ts))
110 }