aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/gpg/reader_test.go
diff options
context:
space:
mode:
authorTim Culverhouse <tim@timculverhouse.com>2022-05-24 15:12:17 -0500
committerRobin Jarry <robin@jarry.cc>2022-05-25 10:09:04 +0200
commit0cc992b4e3ed26c0f9f8dab94024985c87853a64 (patch)
tree6f790f1b09cca049208417f6af7d9e658355fc50 /lib/crypto/gpg/reader_test.go
parent600913015d23a3d322ac10d6bbfde425e7f68606 (diff)
downloadaerc-0cc992b4e3ed26c0f9f8dab94024985c87853a64.tar.gz
gpg: refactor tests for macos compatibility
Refactor lib/crypto/gpg tests to facilitate unit test runs on macos. Macos creates temporary directories with names too long to call gpg-agent (108 characters). Additionally, too many concurrent test calls created IPC errors to gpg-agent. To get around this, tests were given shorter names and refactored into subtests to create fewer concurrent tests Tested on Linux and MacOS. Signed-off-by: Tim Culverhouse <tim@timculverhouse.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/crypto/gpg/reader_test.go')
-rw-r--r--lib/crypto/gpg/reader_test.go209
1 files changed, 86 insertions, 123 deletions
diff --git a/lib/crypto/gpg/reader_test.go b/lib/crypto/gpg/reader_test.go
index 06cf7a3f..957a727d 100644
--- a/lib/crypto/gpg/reader_test.go
+++ b/lib/crypto/gpg/reader_test.go
@@ -1,8 +1,6 @@
package gpg
import (
- "bytes"
- "io"
"strings"
"testing"
@@ -20,135 +18,100 @@ func importPublicKey() {
gpgbin.Import(r)
}
-func TestReader_encryptedSignedPGPMIME(t *testing.T) {
- initGPGtest(t)
-
- var expect = models.MessageDetails{
- IsEncrypted: true,
- IsSigned: true,
- SignedBy: "John Doe (This is a test key) <john.doe@example.org>",
- SignedByKeyId: 3490876580878068068,
- SignatureError: "",
- DecryptedWith: "John Doe (This is a test key) <john.doe@example.org>",
- DecryptedWithKeyId: 3490876580878068068,
- Body: strings.NewReader(testEncryptedBody),
- Micalg: "pgp-sha512",
- }
-
- importSecretKey()
- sr := strings.NewReader(testPGPMIMEEncryptedSigned)
- r, err := Read(sr)
- if err != nil {
- t.Fatalf("pgpmail.Read() = %v", err)
- }
-
- deepEqual(t, r.MessageDetails, &expect)
-}
-
-func TestReader_signedPGPMIME(t *testing.T) {
- initGPGtest(t)
-
- var expect = models.MessageDetails{
- IsEncrypted: false,
- IsSigned: true,
- SignedBy: "John Doe (This is a test key) <john.doe@example.org>",
- SignedByKeyId: 3490876580878068068,
- SignatureError: "",
- DecryptedWith: "",
- DecryptedWithKeyId: 0,
- Body: strings.NewReader(testSignedBody),
- Micalg: "pgp-sha256",
- }
-
- importSecretKey()
- importPublicKey()
- sr := strings.NewReader(testPGPMIMESigned)
- r, err := Read(sr)
- if err != nil {
- t.Fatalf("pgpmail.Read() = %v", err)
- }
-
- deepEqual(t, r.MessageDetails, &expect)
+type readerTestCase struct {
+ name string
+ want models.MessageDetails
+ input string
}
-func TestReader_encryptedSignedEncapsulatedPGPMIME(t *testing.T) {
+func TestReader(t *testing.T) {
initGPGtest(t)
-
- var expect = models.MessageDetails{
- IsEncrypted: true,
- IsSigned: true,
- SignedBy: "John Doe (This is a test key) <john.doe@example.org>",
- SignedByKeyId: 3490876580878068068,
- SignatureError: "",
- DecryptedWith: "John Doe (This is a test key) <john.doe@example.org>",
- DecryptedWithKeyId: 3490876580878068068,
- Body: strings.NewReader(testSignedBody),
- Micalg: "pgp-sha256",
- }
-
- importSecretKey()
importPublicKey()
- sr := strings.NewReader(testPGPMIMEEncryptedSignedEncapsulated)
- r, err := Read(sr)
- if err != nil {
- t.Fatalf("pgpmail.Read() = %v", err)
- }
-
- deepEqual(t, r.MessageDetails, &expect)
-
- var buf bytes.Buffer
- if _, err := io.Copy(&buf, r.MessageDetails.Body); err != nil {
- t.Fatalf("io.Copy() = %v", err)
- }
-}
-func TestReader_signedPGPMIMEInvalid(t *testing.T) {
- initGPGtest(t)
-
- var expect = models.MessageDetails{
- IsEncrypted: false,
- IsSigned: true,
- SignedBy: "John Doe (This is a test key) <john.doe@example.org>",
- SignedByKeyId: 3490876580878068068,
- SignatureError: "gpg: invalid signature",
- DecryptedWith: "",
- DecryptedWithKeyId: 0,
- Body: strings.NewReader(testSignedInvalidBody),
- Micalg: "",
- }
-
importSecretKey()
- importPublicKey()
- sr := strings.NewReader(testPGPMIMESignedInvalid)
- r, err := Read(sr)
- if err != nil {
- t.Fatalf("pgpmail.Read() = %v", err)
- }
- deepEqual(t, r.MessageDetails, &expect)
-}
-
-func TestReader_plaintext(t *testing.T) {
- initGPGtest(t)
- sr := strings.NewReader(testPlaintext)
- r, err := Read(sr)
- if err != nil {
- t.Fatalf("pgpmail.Read() = %v", err)
- }
-
- var buf bytes.Buffer
- if _, err := io.Copy(&buf, r.MessageDetails.Body); err != nil {
- t.Fatalf("io.Copy() = %v", err)
- }
-
- if r.MessageDetails.IsEncrypted {
- t.Errorf("MessageDetails.IsEncrypted != false")
- }
- if r.MessageDetails.IsSigned {
- t.Errorf("MessageDetails.IsSigned != false")
+ testCases := []readerTestCase{
+ {
+ name: "Encrypted and Signed",
+ input: testPGPMIMEEncryptedSigned,
+ want: models.MessageDetails{
+ IsEncrypted: true,
+ IsSigned: true,
+ SignedBy: "John Doe (This is a test key) <john.doe@example.org>",
+ SignedByKeyId: 3490876580878068068,
+ SignatureValidity: 0,
+ SignatureError: "",
+ DecryptedWith: "John Doe (This is a test key) <john.doe@example.org>",
+ DecryptedWithKeyId: 3490876580878068068,
+ Body: strings.NewReader(testEncryptedBody),
+ Micalg: "pgp-sha512",
+ },
+ },
+ {
+ name: "Signed",
+ input: testPGPMIMESigned,
+ want: models.MessageDetails{
+ IsEncrypted: false,
+ IsSigned: true,
+ SignedBy: "John Doe (This is a test key) <john.doe@example.org>",
+ SignedByKeyId: 3490876580878068068,
+ SignatureValidity: 0,
+ SignatureError: "",
+ DecryptedWith: "",
+ DecryptedWithKeyId: 0,
+ Body: strings.NewReader(testSignedBody),
+ Micalg: "pgp-sha256",
+ },
+ },
+ {
+ name: "Encapsulated Signature",
+ input: testPGPMIMEEncryptedSignedEncapsulated,
+ want: models.MessageDetails{
+ IsEncrypted: true,
+ IsSigned: true,
+ SignedBy: "John Doe (This is a test key) <john.doe@example.org>",
+ SignedByKeyId: 3490876580878068068,
+ SignatureValidity: 0,
+ SignatureError: "",
+ DecryptedWith: "John Doe (This is a test key) <john.doe@example.org>",
+ DecryptedWithKeyId: 3490876580878068068,
+ Body: strings.NewReader(testSignedBody),
+ },
+ },
+ {
+ name: "Invalid Signature",
+ input: testPGPMIMESignedInvalid,
+ want: models.MessageDetails{
+ IsEncrypted: false,
+ IsSigned: true,
+ SignedBy: "John Doe (This is a test key) <john.doe@example.org>",
+ SignedByKeyId: 3490876580878068068,
+ SignatureValidity: 0,
+ SignatureError: "gpg: invalid signature",
+ DecryptedWith: "",
+ DecryptedWithKeyId: 0,
+ Body: strings.NewReader(testSignedInvalidBody),
+ Micalg: "",
+ },
+ },
+ {
+ name: "Plain text",
+ input: testPlaintext,
+ want: models.MessageDetails{
+ IsEncrypted: false,
+ IsSigned: false,
+ Body: strings.NewReader(testPlaintext),
+ },
+ },
}
- if s := buf.String(); s != testPlaintext {
- t.Errorf("MessagesDetails.UnverifiedBody = \n%v\n but want \n%v", s, testPlaintext)
+ for _, tc := range testCases {
+ t.Logf("Test case: %s", tc.name)
+ sr := strings.NewReader(tc.input)
+ r, err := Read(sr)
+ if err != nil {
+ t.Fatalf("gpg.Read() = %v", err)
+ }
+ deepEqual(t, tc.name, r.MessageDetails, &tc.want)
}
}