aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/gpg/writer_test.go
diff options
context:
space:
mode:
authorTim Culverhouse <tim@timculverhouse.com>2022-04-25 08:30:44 -0500
committerRobin Jarry <robin@jarry.cc>2022-04-27 09:46:25 +0200
commit57699b1fa6367a42d5877afcfdb1504e52835ed9 (patch)
treeb5000bfad3d62f01127f5831d64d27aac07872e1 /lib/crypto/gpg/writer_test.go
parentd09636ee0b9957ed60fc01224ddfbb03c4f4b7fa (diff)
downloadaerc-57699b1fa6367a42d5877afcfdb1504e52835ed9.tar.gz
feat: add gpg integration
This commit adds gpg system integration. This is done through two new packages: gpgbin, which handles the system calls and parsing; and gpg which is mostly a copy of emersion/go-pgpmail with modifications to interface with package gpgbin. gpg includes tests for many cases, and by it's nature also tests package gpgbin. I separated these in case an external dependency is ever used for the gpg sys-calls/parsing (IE we mirror how go-pgpmail+openpgp currently are dependencies) Two new config options are introduced: * pgp-provider. If it is not explicitly set to "gpg", aerc will default to it's internal pgp provider * pgp-key-id: (Optionally) specify a key by short or long keyId Signed-off-by: Tim Culverhouse <tim@timculverhouse.com> Acked-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/crypto/gpg/writer_test.go')
-rw-r--r--lib/crypto/gpg/writer_test.go122
1 files changed, 122 insertions, 0 deletions
diff --git a/lib/crypto/gpg/writer_test.go b/lib/crypto/gpg/writer_test.go
new file mode 100644
index 00000000..0f9ab109
--- /dev/null
+++ b/lib/crypto/gpg/writer_test.go
@@ -0,0 +1,122 @@
+package gpg
+
+import (
+ "bytes"
+ "io"
+ "strings"
+ "testing"
+
+ "git.sr.ht/~rjarry/aerc/lib/crypto/gpg/gpgbin"
+ "git.sr.ht/~rjarry/aerc/models"
+ "github.com/emersion/go-message/textproto"
+)
+
+func init() {
+ forceBoundary = "foo"
+}
+
+func TestEncrypt(t *testing.T) {
+ importPublicKey()
+ importSecretKey()
+ var h textproto.Header
+ h.Set("From", "John Doe <john.doe@example.org>")
+ h.Set("To", "John Doe <john.doe@example.org>")
+
+ var encryptedHeader textproto.Header
+ encryptedHeader.Set("Content-Type", "text/plain")
+
+ var encryptedBody = "This is an encrypted message!\r\n"
+
+ to := []string{"john.doe@example.org"}
+ from := "john.doe@example.org"
+
+ var buf bytes.Buffer
+ cleartext, err := Encrypt(&buf, h, to, from)
+ if err != nil {
+ t.Fatalf("Encrypt() = %v", err)
+ }
+
+ if err = textproto.WriteHeader(cleartext, encryptedHeader); err != nil {
+ t.Fatalf("textproto.WriteHeader() = %v", err)
+ }
+ if _, err = io.WriteString(cleartext, encryptedBody); err != nil {
+ t.Fatalf("io.WriteString() = %v", err)
+ }
+ if err = cleartext.Close(); err != nil {
+ t.Fatalf("ciphertext.Close() = %v", err)
+ }
+
+ md, err := gpgbin.Decrypt(&buf)
+ if err != nil {
+ t.Errorf("Encrypt error: could not decrypt test encryption")
+ }
+ var body bytes.Buffer
+ io.Copy(&body, md.Body)
+ if s := body.String(); s != wantEncrypted {
+ t.Errorf("Encrypt() = \n%v\n but want \n%v", s, wantEncrypted)
+ }
+
+ t.Cleanup(CleanUp)
+}
+
+func TestSign(t *testing.T) {
+ importPublicKey()
+ importSecretKey()
+ var h textproto.Header
+ h.Set("From", "John Doe <john.doe@example.org>")
+ h.Set("To", "John Doe <john.doe@example.org>")
+
+ var signedHeader textproto.Header
+ signedHeader.Set("Content-Type", "text/plain")
+
+ var signedBody = "This is a signed message!\r\n"
+
+ var buf bytes.Buffer
+ cleartext, err := Sign(&buf, h, "john.doe@example.org")
+ if err != nil {
+ t.Fatalf("Encrypt() = %v", err)
+ }
+
+ if err = textproto.WriteHeader(cleartext, signedHeader); err != nil {
+ t.Fatalf("textproto.WriteHeader() = %v", err)
+ }
+ if _, err = io.WriteString(cleartext, signedBody); err != nil {
+ t.Fatalf("io.WriteString() = %v", err)
+ }
+
+ if err = cleartext.Close(); err != nil {
+ t.Fatalf("ciphertext.Close() = %v", err)
+ }
+
+ parts := strings.Split(buf.String(), "\r\n--foo\r\n")
+ msg := strings.NewReader(parts[1])
+ sig := strings.NewReader(parts[2])
+ md, err := gpgbin.Verify(msg, sig)
+ if err != nil {
+ t.Fatalf("gpg.Verify() = %v", err)
+ }
+
+ deepEqual(t, md, &wantSigned)
+}
+
+var wantEncrypted = toCRLF(`Content-Type: text/plain
+
+This is an encrypted message!
+`)
+
+var wantSignedBody = toCRLF(`Content-Type: text/plain
+
+This is a signed message!
+`)
+
+var wantSigned = 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(wantSignedBody),
+ Micalg: "pgp-sha256",
+}