summaryrefslogtreecommitdiffstats
path: root/integrations/gpg_git_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'integrations/gpg_git_test.go')
-rw-r--r--integrations/gpg_git_test.go66
1 files changed, 37 insertions, 29 deletions
diff --git a/integrations/gpg_git_test.go b/integrations/gpg_git_test.go
index a31dec29bb..e01f0f31c1 100644
--- a/integrations/gpg_git_test.go
+++ b/integrations/gpg_git_test.go
@@ -10,7 +10,6 @@ import (
"io/ioutil"
"net/url"
"os"
- "path/filepath"
"testing"
"code.gitea.io/gitea/models"
@@ -40,8 +39,11 @@ func TestGPGGit(t *testing.T) {
defer os.Setenv("GNUPGHOME", oldGNUPGHome)
// Need to create a root key
- rootKeyPair, err := createGPGKey(tmpDir, "gitea", "gitea@fake.local")
+ rootKeyPair, err := importTestingKey(tmpDir, "gitea", "gitea@fake.local")
assert.NoError(t, err)
+ if err != nil {
+ assert.FailNow(t, "Unable to import rootKeyPair")
+ }
rootKeyID := rootKeyPair.PrimaryKey.KeyIdShortString()
@@ -125,6 +127,11 @@ func TestGPGGit(t *testing.T) {
testCtx := NewAPITestContext(t, username, "initial-unsigned")
t.Run("CreateCRUDFile-Always", crudActionCreateFile(
t, testCtx, user, "master", "always", "signed-always.txt", func(t *testing.T, response api.FileResponse) {
+ assert.NotNil(t, response.Verification)
+ if response.Verification == nil {
+ assert.FailNow(t, "no verification provided with response! %v", response)
+ return
+ }
assert.True(t, response.Verification.Verified)
if !response.Verification.Verified {
t.FailNow()
@@ -134,6 +141,11 @@ func TestGPGGit(t *testing.T) {
}))
t.Run("CreateCRUDFile-ParentSigned-always", crudActionCreateFile(
t, testCtx, user, "parentsigned", "parentsigned-always", "signed-parent2.txt", func(t *testing.T, response api.FileResponse) {
+ assert.NotNil(t, response.Verification)
+ if response.Verification == nil {
+ assert.FailNow(t, "no verification provided with response! %v", response)
+ return
+ }
assert.True(t, response.Verification.Verified)
if !response.Verification.Verified {
t.FailNow()
@@ -152,6 +164,11 @@ func TestGPGGit(t *testing.T) {
testCtx := NewAPITestContext(t, username, "initial-unsigned")
t.Run("CreateCRUDFile-Always-ParentSigned", crudActionCreateFile(
t, testCtx, user, "always", "always-parentsigned", "signed-always-parentsigned.txt", func(t *testing.T, response api.FileResponse) {
+ assert.NotNil(t, response.Verification)
+ if response.Verification == nil {
+ assert.FailNow(t, "no verification provided with response! %v", response)
+ return
+ }
assert.True(t, response.Verification.Verified)
if !response.Verification.Verified {
t.FailNow()
@@ -171,7 +188,15 @@ func TestGPGGit(t *testing.T) {
t.Run("CreateRepository", doAPICreateRepository(testCtx, false))
t.Run("CheckMasterBranchSigned", doAPIGetBranch(testCtx, "master", func(t *testing.T, branch api.Branch) {
assert.NotNil(t, branch.Commit)
+ if branch.Commit == nil {
+ assert.FailNow(t, "no commit provided with branch! %v", branch)
+ return
+ }
assert.NotNil(t, branch.Commit.Verification)
+ if branch.Commit.Verification == nil {
+ assert.FailNow(t, "no verification provided with branch commit! %v", branch.Commit)
+ return
+ }
assert.True(t, branch.Commit.Verification.Verified)
if !branch.Commit.Verification.Verified {
t.FailNow()
@@ -318,43 +343,26 @@ func crudActionCreateFile(t *testing.T, ctx APITestContext, user *models.User, f
}, callback...)
}
-func createGPGKey(tmpDir, name, email string) (*openpgp.Entity, error) {
- keyPair, err := openpgp.NewEntity(name, "test", email, nil)
- if err != nil {
+func importTestingKey(tmpDir, name, email string) (*openpgp.Entity, error) {
+ if _, _, err := process.GetManager().Exec("gpg --import integrations/private-testing.key", "gpg", "--import", "integrations/private-testing.key"); err != nil {
return nil, err
}
-
- for _, id := range keyPair.Identities {
- err := id.SelfSignature.SignUserId(id.UserId.Id, keyPair.PrimaryKey, keyPair.PrivateKey, nil)
- if err != nil {
- return nil, err
- }
- }
-
- keyFile := filepath.Join(tmpDir, "temporary.key")
- keyWriter, err := os.Create(keyFile)
+ keyringFile, err := os.Open("integrations/private-testing.key")
if err != nil {
return nil, err
}
- defer keyWriter.Close()
- defer os.Remove(keyFile)
+ defer keyringFile.Close()
- w, err := armor.Encode(keyWriter, openpgp.PrivateKeyType, nil)
+ block, err := armor.Decode(keyringFile)
if err != nil {
return nil, err
}
- defer w.Close()
- keyPair.SerializePrivate(w, nil)
- if err := w.Close(); err != nil {
- return nil, err
- }
- if err := keyWriter.Close(); err != nil {
- return nil, err
+ keyring, err := openpgp.ReadKeyRing(block.Body)
+ if err != nil {
+ return nil, fmt.Errorf("Keyring access failed: '%v'", err)
}
- if _, _, err := process.GetManager().Exec("gpg --import temporary.key", "gpg", "--import", keyFile); err != nil {
- return nil, err
- }
- return keyPair, nil
+ // There should only be one entity in this file.
+ return keyring[0], nil
}