gpg: make tests more robust

Skip the tests if gpg is not installed.
Avoid interference with the global ~/.gnupg.
Automatically delete GNUPGHOME at the end of tests.

Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
Robin Jarry 2022-05-10 09:58:54 +02:00
parent 43a79acdfa
commit b65f5649c8
4 changed files with 31 additions and 24 deletions

View File

@ -58,6 +58,10 @@ checkfmt:
exit 1; \ exit 1; \
fi fi
.PHONY: tests
tests:
$(GO) test $(GOFLAGS) -v ./...
.PHONY: debug .PHONY: debug
debug: aerc.debug debug: aerc.debug
@echo 'Run `./aerc.debug` and use this command in another terminal to attach a debugger:' @echo 'Run `./aerc.debug` and use this command in another terminal to attach a debugger:'

View File

@ -2,9 +2,8 @@ package gpg
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"log" "os"
"os/exec" "os/exec"
"strings" "strings"
"testing" "testing"
@ -12,22 +11,20 @@ import (
"git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/models"
) )
func TestHasGpg(t *testing.T) { func initGPGtest(t *testing.T) {
gpgmail := new(Mail) if _, err := exec.LookPath("gpg"); err != nil {
hasGpg := gpgmail.Init(new(log.Logger)) t.Skipf("%s", err)
if hasGpg != nil {
t.Errorf("System does not have GPG")
} }
} // temp dir is automatically deleted by the test runtime
dir := t.TempDir()
func CleanUp() { // t.Setenv is only available since go 1.17
cmd := exec.Command("gpg", "--batch", "--yes", "--delete-secret-and-public-keys", testKeyId) if err := os.Setenv("GNUPGHOME", dir); err != nil {
err := cmd.Run() t.Fatalf("failed to set GNUPGHOME: %s", err)
if err != nil {
fmt.Println("Test cleanup failed: you may need to delete the test keys from your GPG keyring")
return
} }
t.Cleanup(func() {
os.Unsetenv("GNUPGHOME")
})
t.Logf("using GNUPGHOME = %s", dir)
} }
func toCRLF(s string) string { func toCRLF(s string) string {

View File

@ -21,6 +21,8 @@ func importPublicKey() {
} }
func TestReader_encryptedSignedPGPMIME(t *testing.T) { func TestReader_encryptedSignedPGPMIME(t *testing.T) {
initGPGtest(t)
var expect = models.MessageDetails{ var expect = models.MessageDetails{
IsEncrypted: true, IsEncrypted: true,
IsSigned: true, IsSigned: true,
@ -41,11 +43,11 @@ func TestReader_encryptedSignedPGPMIME(t *testing.T) {
} }
deepEqual(t, r.MessageDetails, &expect) deepEqual(t, r.MessageDetails, &expect)
t.Cleanup(CleanUp)
} }
func TestReader_signedPGPMIME(t *testing.T) { func TestReader_signedPGPMIME(t *testing.T) {
initGPGtest(t)
var expect = models.MessageDetails{ var expect = models.MessageDetails{
IsEncrypted: false, IsEncrypted: false,
IsSigned: true, IsSigned: true,
@ -67,11 +69,11 @@ func TestReader_signedPGPMIME(t *testing.T) {
} }
deepEqual(t, r.MessageDetails, &expect) deepEqual(t, r.MessageDetails, &expect)
t.Cleanup(CleanUp)
} }
func TestReader_encryptedSignedEncapsulatedPGPMIME(t *testing.T) { func TestReader_encryptedSignedEncapsulatedPGPMIME(t *testing.T) {
initGPGtest(t)
var expect = models.MessageDetails{ var expect = models.MessageDetails{
IsEncrypted: true, IsEncrypted: true,
IsSigned: true, IsSigned: true,
@ -100,6 +102,8 @@ func TestReader_encryptedSignedEncapsulatedPGPMIME(t *testing.T) {
} }
} }
func TestReader_signedPGPMIMEInvalid(t *testing.T) { func TestReader_signedPGPMIMEInvalid(t *testing.T) {
initGPGtest(t)
var expect = models.MessageDetails{ var expect = models.MessageDetails{
IsEncrypted: false, IsEncrypted: false,
IsSigned: true, IsSigned: true,
@ -120,11 +124,11 @@ func TestReader_signedPGPMIMEInvalid(t *testing.T) {
t.Fatalf("pgpmail.Read() = %v", err) t.Fatalf("pgpmail.Read() = %v", err)
} }
deepEqual(t, r.MessageDetails, &expect) deepEqual(t, r.MessageDetails, &expect)
t.Cleanup(CleanUp)
} }
func TestReader_plaintext(t *testing.T) { func TestReader_plaintext(t *testing.T) {
initGPGtest(t)
sr := strings.NewReader(testPlaintext) sr := strings.NewReader(testPlaintext)
r, err := Read(sr) r, err := Read(sr)
if err != nil { if err != nil {

View File

@ -16,6 +16,8 @@ func init() {
} }
func TestEncrypt(t *testing.T) { func TestEncrypt(t *testing.T) {
initGPGtest(t)
importPublicKey() importPublicKey()
importSecretKey() importSecretKey()
var h textproto.Header var h textproto.Header
@ -55,11 +57,11 @@ func TestEncrypt(t *testing.T) {
if s := body.String(); s != wantEncrypted { if s := body.String(); s != wantEncrypted {
t.Errorf("Encrypt() = \n%v\n but want \n%v", s, wantEncrypted) t.Errorf("Encrypt() = \n%v\n but want \n%v", s, wantEncrypted)
} }
t.Cleanup(CleanUp)
} }
func TestSign(t *testing.T) { func TestSign(t *testing.T) {
initGPGtest(t)
importPublicKey() importPublicKey()
importSecretKey() importSecretKey()
var h textproto.Header var h textproto.Header