| 1 |
package cosign |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/rand" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/opencontainers/go-digest" |
| 10 |
v1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 11 |
) |
| 12 |
|
| 13 |
func TestPayload(t *testing.T) { |
| 14 |
testCases := []struct { |
| 15 |
name string |
| 16 |
blob []byte |
| 17 |
wantDigest digest.Digest |
| 18 |
wantPayload []byte |
| 19 |
wantPass bool |
| 20 |
}{ |
| 21 |
{ |
| 22 |
name: "cosign signature Payload() success", |
| 23 |
blob: []byte(`{"critical":{"identity":{"docker-reference":"us-docker.pkg.dev/confidential-space-images-dev/cs-cosign-tests/base"},"image":{"docker-manifest-digest":"sha256:9494e567c7c44e8b9f8808c1658a47c9b7979ef3cceef10f48754fc2706802ba"},"type":"cosign container image signature"},"optional":null}`), |
| 24 |
wantDigest: "sha256:d1e44a76902409836227b982beb920189949927c2011f196594bd34c5bb8f8b1", |
| 25 |
wantPayload: []byte(`{"critical":{"identity":{"docker-reference":"us-docker.pkg.dev/confidential-space-images-dev/cs-cosign-tests/base"},"image":{"docker-manifest-digest":"sha256:9494e567c7c44e8b9f8808c1658a47c9b7979ef3cceef10f48754fc2706802ba"},"type":"cosign container image signature"},"optional":null}`), |
| 26 |
wantPass: true, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "cosign signature Payload() failed with unmatched digest", |
| 30 |
blob: []byte(`{"critical":{"identity":{"docker-reference":"us-docker.pkg.dev/confidential-space-images-dev/cs-cosign-tests/base"},"image":{"docker-manifest-digest":"sha256:9494e567c7c44e8b9f8808c1658a47c9b7979ef3cceef10f48754fc2706802ba"},"type":"cosign container image signature"},"optional":null}`), |
| 31 |
wantDigest: "sha256:unmatched digest", |
| 32 |
wantPayload: []byte{}, |
| 33 |
wantPass: false, |
| 34 |
}, |
| 35 |
} |
| 36 |
|
| 37 |
for _, tc := range testCases { |
| 38 |
t.Run(tc.name, func(t *testing.T) { |
| 39 |
sig := &Sig{ |
| 40 |
Layer: v1.Descriptor{ |
| 41 |
Digest: tc.wantDigest, |
| 42 |
}, |
| 43 |
Blob: tc.blob, |
| 44 |
} |
| 45 |
gotPayload, err := sig.Payload() |
| 46 |
if err != nil && tc.wantPass { |
| 47 |
t.Errorf("Payload() failed for test case %v: %v", tc.name, err) |
| 48 |
} |
| 49 |
if !bytes.Equal(gotPayload, tc.wantPayload) { |
| 50 |
t.Errorf("Payload() failed for test case %v: got %v, but want %v", tc.name, gotPayload, tc.wantPayload) |
| 51 |
} |
| 52 |
}) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestBase64Encoded(t *testing.T) { |
| 57 |
testCases := []struct { |
| 58 |
name string |
| 59 |
wantSignatureKey string |
| 60 |
wantSignature string |
| 61 |
wantPass bool |
| 62 |
}{ |
| 63 |
{ |
| 64 |
name: "cosign signature Base64Encoded() success", |
| 65 |
wantSignatureKey: CosignSigKey, |
| 66 |
wantSignature: randomBase64EncodedString(32), |
| 67 |
wantPass: true, |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "cosign signature Base64Encoded() failed with mismatched signature key", |
| 71 |
wantSignatureKey: "mismatched signature key", |
| 72 |
wantSignature: "", |
| 73 |
wantPass: false, |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "cosign signature Base64Encoded() failed with invalid base64 encoded signature", |
| 77 |
wantSignatureKey: CosignSigKey, |
| 78 |
wantSignature: "", |
| 79 |
wantPass: false, |
| 80 |
}, |
| 81 |
} |
| 82 |
|
| 83 |
for _, tc := range testCases { |
| 84 |
t.Run(tc.name, func(t *testing.T) { |
| 85 |
sig := &Sig{ |
| 86 |
Layer: v1.Descriptor{ |
| 87 |
Annotations: map[string]string{ |
| 88 |
tc.wantSignatureKey: tc.wantSignature, |
| 89 |
}, |
| 90 |
}, |
| 91 |
} |
| 92 |
gotSignature, err := sig.Base64Encoded() |
| 93 |
if err != nil && tc.wantPass { |
| 94 |
t.Errorf("Base64Encoded() failed for test case %v: %v", tc.name, err) |
| 95 |
} |
| 96 |
if gotSignature != tc.wantSignature { |
| 97 |
t.Errorf("Base64Encoded() failed for test case %v: got %v, but want %v", tc.name, gotSignature, tc.wantSignature) |
| 98 |
} |
| 99 |
}) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func TestWorkflow(t *testing.T) { |
| 104 |
wantSig := randomBase64EncodedString(32) |
| 105 |
blob := []byte(`{"critical":{"identity":{"docker-reference":"us-docker.pkg.dev/confidential-space-images-dev/cs-cosign-tests/base"},"image":{"docker-manifest-digest":"sha256:9494e567c7c44e8b9f8808c1658a47c9b7979ef3cceef10f48754fc2706802ba"},"type":"cosign container image signature"},"optional":null}`) |
| 106 |
|
| 107 |
sig := &Sig{ |
| 108 |
Layer: v1.Descriptor{ |
| 109 |
Digest: digest.FromBytes(blob), |
| 110 |
Annotations: map[string]string{ |
| 111 |
CosignSigKey: wantSig, |
| 112 |
}, |
| 113 |
}, |
| 114 |
Blob: blob, |
| 115 |
} |
| 116 |
|
| 117 |
gotPayload, err := sig.Payload() |
| 118 |
if err != nil { |
| 119 |
t.Errorf("Payload() failed: %v", err) |
| 120 |
} |
| 121 |
if !bytes.Equal(gotPayload, blob) { |
| 122 |
t.Errorf("Payload() failed: got %v, but want %v", gotPayload, blob) |
| 123 |
} |
| 124 |
|
| 125 |
gotSig, err := sig.Base64Encoded() |
| 126 |
if err != nil { |
| 127 |
t.Errorf("Base64Encoded() failed: %v", err) |
| 128 |
} |
| 129 |
if gotSig != wantSig { |
| 130 |
t.Errorf("Base64Encoded() failed, got %s, but want %s", gotSig, wantSig) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestString(t *testing.T) { |
| 135 |
testCases := []struct { |
| 136 |
name string |
| 137 |
sourceRepo string |
| 138 |
b64Sig string |
| 139 |
wantString string |
| 140 |
}{ |
| 141 |
{ |
| 142 |
name: "successful signature details", |
| 143 |
sourceRepo: "gcr.io/hello_world", |
| 144 |
b64Sig: "aGVsbG8gd29ybGQ=", // base64 encoded "hello world" |
| 145 |
wantString: `signature: "aGVsbG8gd29ybGQ=", sourceRepo: "gcr.io/hello_world"`, |
| 146 |
}, |
| 147 |
{ |
| 148 |
name: "erronous signature details", |
| 149 |
sourceRepo: "gcr.io/hello_world", |
| 150 |
b64Sig: "invalid", |
| 151 |
wantString: `signature error: invalid base64 encoded signature`, |
| 152 |
}, |
| 153 |
} |
| 154 |
|
| 155 |
for _, tc := range testCases { |
| 156 |
t.Run(tc.name, func(t *testing.T) { |
| 157 |
sig := &Sig{ |
| 158 |
Layer: v1.Descriptor{ |
| 159 |
Annotations: map[string]string{ |
| 160 |
CosignSigKey: tc.b64Sig, |
| 161 |
}, |
| 162 |
}, |
| 163 |
SourceRepo: tc.sourceRepo, |
| 164 |
} |
| 165 |
gotString := sig.String() |
| 166 |
if !strings.Contains(gotString, tc.wantString) { |
| 167 |
t.Errorf("String() failed, got %s, but want %s", gotString, tc.wantString) |
| 168 |
} |
| 169 |
}) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func randomBase64EncodedString(n int) string { |
| 174 |
b := make([]byte, n) |
| 175 |
_, err := rand.Read(b) |
| 176 |
if err != nil { |
| 177 |
return "" |
| 178 |
} |
| 179 |
return encoding.EncodeToString(b) |
| 180 |
} |