Skip to content

Commit 5e6ab4d

Browse files
committed
cleans up tests and removed duplicate test files
Signed-off-by: Ricardo Bartels <ricardo.bartels@telekom.de>
1 parent ad3e595 commit 5e6ab4d

13 files changed

Lines changed: 553 additions & 1423 deletions

git/signatures/gpg_signature_test.go

Lines changed: 49 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -191,40 +191,50 @@ func TestVerifyPGPSignature(t *testing.T) {
191191
}
192192
}
193193

194-
func TestVerifyPGPSignatureWithFixturesForTags(t *testing.T) {
194+
func TestVerifyPGPSignatureForCommitsAndTags(t *testing.T) {
195195
testDataDir := filepath.Join("testdata", "gpg_signatures")
196196

197197
// Test cases for each key type using fixtures
198198
keyTypes := []struct {
199-
name string
200-
sigFile string
201-
keyFile string
202-
wantErr bool
199+
name string
200+
commitFile string
201+
tagFile string
202+
keyFile string
203+
wantErr bool
203204
}{
204-
{"rsa_2048 valid tag signature", "tag_rsa_2048_signed.txt", "key_rsa_2048.pub", false},
205-
{"rsa_4096 valid tag signature", "tag_rsa_4096_signed.txt", "key_rsa_4096.pub", false},
206-
{"ed25519 valid tag signature", "tag_ed25519_signed.txt", "key_ed25519.pub", false},
207-
{"ecdsa_p256 valid tag signature", "tag_ecdsa_p256_signed.txt", "key_ecdsa_p256.pub", false},
208-
{"ecdsa_p384 valid tag signature", "tag_ecdsa_p384_signed.txt", "key_ecdsa_p384.pub", false},
209-
{"ecdsa_p521 valid tag signature", "tag_ecdsa_p521_signed.txt", "key_ecdsa_p521.pub", false},
210-
{"dsa_2048 valid tag signature", "tag_dsa_2048_signed.txt", "key_dsa_2048.pub", false},
211-
{"brainpool_p256 valid tag signature", "tag_brainpool_p256_signed.txt", "key_brainpool_p256.pub", false},
212-
{"brainpool_p384 valid tag signature", "tag_brainpool_p384_signed.txt", "key_brainpool_p384.pub", false},
213-
{"brainpool_p512 valid tag signature", "tag_brainpool_p512_signed.txt", "key_brainpool_p512.pub", false},
205+
{"rsa_2048 valid signature", "commit_rsa_2048_signed.txt", "tag_rsa_2048_signed.txt", "key_rsa_2048.pub", false},
206+
{"rsa_4096 valid signature", "commit_rsa_4096_signed.txt", "tag_rsa_4096_signed.txt", "key_rsa_4096.pub", false},
207+
{"ed25519 valid signature", "commit_ed25519_signed.txt", "tag_ed25519_signed.txt", "key_ed25519.pub", false},
208+
{"ecdsa_p256 valid signature", "commit_ecdsa_p256_signed.txt", "tag_ecdsa_p256_signed.txt", "key_ecdsa_p256.pub", false},
209+
{"ecdsa_p384 valid signature", "commit_ecdsa_p384_signed.txt", "tag_ecdsa_p384_signed.txt", "key_ecdsa_p384.pub", false},
210+
{"ecdsa_p521 valid signature", "commit_ecdsa_p521_signed.txt", "tag_ecdsa_p521_signed.txt", "key_ecdsa_p521.pub", false},
211+
{"dsa_2048 valid signature", "commit_dsa_2048_signed.txt", "tag_dsa_2048_signed.txt", "key_dsa_2048.pub", false},
212+
{"brainpool_p256 valid signature", "commit_brainpool_p256_signed.txt", "tag_brainpool_p256_signed.txt", "key_brainpool_p256.pub", false},
213+
{"brainpool_p384 valid signature", "commit_brainpool_p384_signed.txt", "tag_brainpool_p384_signed.txt", "key_brainpool_p384.pub", false},
214+
{"brainpool_p512 valid signature", "commit_brainpool_p512_signed.txt", "tag_brainpool_p512_signed.txt", "key_brainpool_p512.pub", false},
214215

215216
// ed448 test fails because the key was created with OpenPGP version 5,
216217
// which is not supported by github.com/ProtonMail/go-crypto (only version 4 is supported).
217218
// The error occurs when trying to read the armored key ring:
218219
// "unable to read armored key ring: openpgp: invalid data: first packet was not a public/private key"
219-
{"ed448 valid tag signature", "tag_ed448_signed.txt", "key_ed448.pub", true},
220+
{"ed448 valid signature", "commit_ed448_signed.txt", "tag_ed448_signed.txt", "key_ed448.pub", true},
220221
}
221222

223+
var allKeysRing []string
222224
for _, kt := range keyTypes {
223-
t.Run(kt.name, func(t *testing.T) {
225+
publicKey, err := os.ReadFile(filepath.Join(testDataDir, kt.keyFile))
226+
if err != nil {
227+
t.Fatalf("failed to read public key file %s: %v", kt.keyFile, err)
228+
}
229+
allKeysRing = append(allKeysRing, string(publicKey))
230+
}
231+
232+
for _, kt := range keyTypes {
233+
t.Run(kt.name+" tag", func(t *testing.T) {
224234
g := NewWithT(t)
225235

226236
// Parse the tag from the fixture file
227-
tagObj, err := testutils.ParseTagFromFixture(filepath.Join(testDataDir, kt.sigFile))
237+
tagObj, err := testutils.ParseTagFromFixture(filepath.Join(testDataDir, kt.tagFile))
228238
g.Expect(err).ToNot(HaveOccurred())
229239

230240
// Build a git.Tag using BuildTag
@@ -245,44 +255,27 @@ func TestVerifyPGPSignatureWithFixturesForTags(t *testing.T) {
245255

246256
g.Expect(err).ToNot(HaveOccurred())
247257
g.Expect(fingerprint).ToNot(BeEmpty())
248-
})
249-
}
250-
}
251258

252-
func TestVerifyPGPSignatureWithFixtures(t *testing.T) {
253-
testDataDir := filepath.Join("testdata", "gpg_signatures")
259+
// Verify the signature using the multi-key keyring
260+
fingerprint, err = signatures.VerifyPGPSignature(gitTag.Signature, gitTag.Encoded, allKeysRing...)
261+
if kt.wantErr {
262+
g.Expect(err).To(HaveOccurred())
263+
g.Expect(fingerprint).To(BeEmpty())
264+
return
265+
}
254266

255-
// Test cases for each key type using fixtures
256-
keyTypes := []struct {
257-
name string
258-
sigFile string
259-
keyFile string
260-
wantErr bool
261-
}{
262-
{"rsa_2048 valid signature", "commit_rsa_2048_signed.txt", "key_rsa_2048.pub", false},
263-
{"rsa_4096 valid signature", "commit_rsa_4096_signed.txt", "key_rsa_4096.pub", false},
264-
{"ed25519 valid signature", "commit_ed25519_signed.txt", "key_ed25519.pub", false},
265-
{"ecdsa_p256 valid signature", "commit_ecdsa_p256_signed.txt", "key_ecdsa_p256.pub", false},
266-
{"ecdsa_p384 valid signature", "commit_ecdsa_p384_signed.txt", "key_ecdsa_p384.pub", false},
267-
{"ecdsa_p521 valid signature", "commit_ecdsa_p521_signed.txt", "key_ecdsa_p521.pub", false},
268-
{"dsa_2048 valid signature", "commit_dsa_2048_signed.txt", "key_dsa_2048.pub", false},
269-
{"brainpool_p256 valid signature", "commit_brainpool_p256_signed.txt", "key_brainpool_p256.pub", false},
270-
{"brainpool_p384 valid signature", "commit_brainpool_p384_signed.txt", "key_brainpool_p384.pub", false},
271-
{"brainpool_p512 valid signature", "commit_brainpool_p512_signed.txt", "key_brainpool_p512.pub", false},
267+
g.Expect(err).ToNot(HaveOccurred())
268+
g.Expect(fingerprint).ToNot(BeEmpty())
272269

273-
// ed448 test fails because the key was created with OpenPGP version 5,
274-
// which is not supported by github.com/ProtonMail/go-crypto (only version 4 is supported).
275-
// The error occurs when trying to read the armored key ring:
276-
// "unable to read armored key ring: openpgp: invalid data: first packet was not a public/private key"
277-
{"ed448 valid signature", "commit_ed448_signed.txt", "key_ed448.pub", true},
270+
})
278271
}
279272

280273
for _, kt := range keyTypes {
281-
t.Run(kt.name, func(t *testing.T) {
274+
t.Run(kt.name+" commit", func(t *testing.T) {
282275
g := NewWithT(t)
283276

284277
// Parse the commit from the fixture file
285-
commitObj, err := testutils.ParseCommitFromFixture(filepath.Join(testDataDir, kt.sigFile))
278+
commitObj, err := testutils.ParseCommitFromFixture(filepath.Join(testDataDir, kt.commitFile))
286279
g.Expect(err).ToNot(HaveOccurred())
287280

288281
// Build a git.Commit using BuildCommitWithRef
@@ -303,90 +296,9 @@ func TestVerifyPGPSignatureWithFixtures(t *testing.T) {
303296

304297
g.Expect(err).ToNot(HaveOccurred())
305298
g.Expect(fingerprint).ToNot(BeEmpty())
306-
})
307-
}
308-
309-
// Test error cases
310-
t.Run("unsigned commit", func(t *testing.T) {
311-
g := NewWithT(t)
312-
313-
// Parse the unsigned commit from the fixture file
314-
commitObj, err := testutils.ParseCommitFromFixture(filepath.Join(testDataDir, "commit_unsigned.txt"))
315-
g.Expect(err).ToNot(HaveOccurred())
316-
317-
// Build a git.Commit using BuildCommitWithRef
318-
gitCommit, err := gogit.BuildCommitWithRef(commitObj, nil, plumbing.ReferenceName("refs/heads/main"))
319-
g.Expect(err).ToNot(HaveOccurred())
320-
321-
// Read a public key
322-
publicKey, err := os.ReadFile(filepath.Join(testDataDir, "key_rsa_2048.pub"))
323-
g.Expect(err).ToNot(HaveOccurred())
324-
325-
// Verify the signature - should fail as the commit is unsigned
326-
fingerprint, err := signatures.VerifyPGPSignature(gitCommit.Signature, gitCommit.Encoded, string(publicKey))
327-
g.Expect(err).To(HaveOccurred())
328-
g.Expect(fingerprint).To(BeEmpty())
329-
})
330-
}
331-
332-
func TestVerifyPGPSignatureWithMultipleKeyRing(t *testing.T) {
333-
testDataDir := filepath.Join("testdata", "gpg_signatures")
334-
335-
// Read multiple public keys to create a multi-key keyring
336-
keyFiles := []string{
337-
"key_rsa_2048.pub",
338-
"key_rsa_4096.pub",
339-
"key_ed25519.pub",
340-
"key_ecdsa_p256.pub",
341-
"key_ecdsa_p384.pub",
342-
"key_ecdsa_p521.pub",
343-
"key_dsa_2048.pub",
344-
"key_brainpool_p256.pub",
345-
"key_brainpool_p384.pub",
346-
"key_brainpool_p512.pub",
347-
}
348-
349-
var keyRings []string
350-
for _, keyFile := range keyFiles {
351-
publicKey, err := os.ReadFile(filepath.Join(testDataDir, keyFile))
352-
if err != nil {
353-
t.Fatalf("failed to read public key file %s: %v", keyFile, err)
354-
}
355-
keyRings = append(keyRings, string(publicKey))
356-
}
357-
358-
// Test cases for each key type using the multi-key keyring
359-
keyTypes := []struct {
360-
name string
361-
sigFile string
362-
wantErr bool
363-
}{
364-
{"rsa_2048 valid signature with multi-key keyring", "commit_rsa_2048_signed.txt", false},
365-
{"rsa_4096 valid signature with multi-key keyring", "commit_rsa_4096_signed.txt", false},
366-
{"ed25519 valid signature with multi-key keyring", "commit_ed25519_signed.txt", false},
367-
{"ecdsa_p256 valid signature with multi-key keyring", "commit_ecdsa_p256_signed.txt", false},
368-
{"ecdsa_p384 valid signature with multi-key keyring", "commit_ecdsa_p384_signed.txt", false},
369-
{"ecdsa_p521 valid signature with multi-key keyring", "commit_ecdsa_p521_signed.txt", false},
370-
{"dsa_2048 valid signature with multi-key keyring", "commit_dsa_2048_signed.txt", false},
371-
{"brainpool_p256 valid signature with multi-key keyring", "commit_brainpool_p256_signed.txt", false},
372-
{"brainpool_p384 valid signature with multi-key keyring", "commit_brainpool_p384_signed.txt", false},
373-
{"brainpool_p512 valid signature with multi-key keyring", "commit_brainpool_p512_signed.txt", false},
374-
}
375-
376-
for _, kt := range keyTypes {
377-
t.Run(kt.name, func(t *testing.T) {
378-
g := NewWithT(t)
379-
380-
// Parse the commit from the fixture file
381-
commitObj, err := testutils.ParseCommitFromFixture(filepath.Join(testDataDir, kt.sigFile))
382-
g.Expect(err).ToNot(HaveOccurred())
383-
384-
// Build a git.Commit using BuildCommitWithRef
385-
gitCommit, err := gogit.BuildCommitWithRef(commitObj, nil, plumbing.ReferenceName("refs/heads/main"))
386-
g.Expect(err).ToNot(HaveOccurred())
387299

388300
// Verify the signature using the multi-key keyring
389-
fingerprint, err := signatures.VerifyPGPSignature(gitCommit.Signature, gitCommit.Encoded, keyRings...)
301+
fingerprint, err = signatures.VerifyPGPSignature(gitCommit.Signature, gitCommit.Encoded, allKeysRing...)
390302
if kt.wantErr {
391303
g.Expect(err).To(HaveOccurred())
392304
g.Expect(fingerprint).To(BeEmpty())
@@ -395,11 +307,12 @@ func TestVerifyPGPSignatureWithMultipleKeyRing(t *testing.T) {
395307

396308
g.Expect(err).ToNot(HaveOccurred())
397309
g.Expect(fingerprint).ToNot(BeEmpty())
310+
398311
})
399312
}
400313

401-
// Test that an unsigned commit fails with multi-key keyring
402-
t.Run("unsigned commit with multi-key keyring", func(t *testing.T) {
314+
// Test error cases
315+
t.Run("unsigned commit", func(t *testing.T) {
403316
g := NewWithT(t)
404317

405318
// Parse the unsigned commit from the fixture file
@@ -410,8 +323,12 @@ func TestVerifyPGPSignatureWithMultipleKeyRing(t *testing.T) {
410323
gitCommit, err := gogit.BuildCommitWithRef(commitObj, nil, plumbing.ReferenceName("refs/heads/main"))
411324
g.Expect(err).ToNot(HaveOccurred())
412325

326+
// Read a public key
327+
publicKey, err := os.ReadFile(filepath.Join(testDataDir, "key_rsa_2048.pub"))
328+
g.Expect(err).ToNot(HaveOccurred())
329+
413330
// Verify the signature - should fail as the commit is unsigned
414-
fingerprint, err := signatures.VerifyPGPSignature(gitCommit.Signature, gitCommit.Encoded, keyRings...)
331+
fingerprint, err := signatures.VerifyPGPSignature(gitCommit.Signature, gitCommit.Encoded, string(publicKey))
415332
g.Expect(err).To(HaveOccurred())
416333
g.Expect(fingerprint).To(BeEmpty())
417334
})

git/signatures/signature.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func IsSSHSignature(signature string) bool {
6666

6767
// IsX509Signature tests if the given signature is of type x509.
6868
// It returns true if the signature starts with the x509 signature prefix.
69+
// This is a place holder / compatibility implementation to embed the signature
70+
// type into the error message to inform the user about the wrong type of signature
6971
func IsX509Signature(signature string) bool {
7072
return startsWithStrings(signature, X509SignaturePrefix)
7173
}

git/signatures/ssh_signature.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,20 @@ func VerifySSHSignature(signature string, payload []byte, authorizedKeys ...stri
8989
for _, pubKey := range publicKeys {
9090
// Verify the signature using sshsig library
9191
// The namespace for Git is "git"
92-
// Git supports both SHA256 and SHA512, so we try both
93-
for _, hashAlgo := range []sshsig.HashAlgorithm{sshsig.HashSHA256, sshsig.HashSHA512} {
94-
err := sshsig.Verify(bytes.NewReader(payload), sig, pubKey, hashAlgo, "git")
95-
if err == nil {
96-
// Signature verified successfully
97-
return GetPublicKeyFingerprint(pubKey), nil
98-
}
92+
err := sshsig.Verify(bytes.NewReader(payload), sig, pubKey, sig.HashAlgorithm, "git")
93+
if err == nil {
94+
// Signature verified successfully
95+
return getPublicKeyFingerprint(pubKey), nil
9996
}
10097
}
10198
}
10299

103100
return "", fmt.Errorf("unable to verify payload with any of the given authorized keys")
104101
}
105102

106-
// GetPublicKeyFingerprint returns the SHA256 fingerprint of the public key
103+
// getPublicKeyFingerprint returns the SHA256 fingerprint of the public key
107104
// in the format used by SSH (e.g., "SHA256:abc123...").
108-
func GetPublicKeyFingerprint(pubKey gossh.PublicKey) string {
105+
func getPublicKeyFingerprint(pubKey gossh.PublicKey) string {
109106
hash := sha256.Sum256(pubKey.Marshal())
110107
return "SHA256:" + base64.RawStdEncoding.EncodeToString(hash[:])
111108
}

0 commit comments

Comments
 (0)