2022-04-25 15:30:44 +02:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:12:17 +02:00
|
|
|
type writerTestCase struct {
|
|
|
|
name string
|
|
|
|
method string
|
|
|
|
body string
|
|
|
|
}
|
2022-05-10 09:58:54 +02:00
|
|
|
|
2022-05-24 22:12:17 +02:00
|
|
|
func TestWriter(t *testing.T) {
|
|
|
|
initGPGtest(t)
|
2022-04-25 15:30:44 +02:00
|
|
|
importPublicKey()
|
|
|
|
importSecretKey()
|
2022-05-24 22:12:17 +02:00
|
|
|
|
|
|
|
testCases := []writerTestCase{
|
|
|
|
{
|
|
|
|
name: "Encrypt",
|
|
|
|
method: "encrypt",
|
|
|
|
body: "This is an encrypted message!\r\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Sign",
|
|
|
|
method: "sign",
|
|
|
|
body: "This is a signed message!\r\n",
|
|
|
|
},
|
|
|
|
}
|
2022-04-25 15:30:44 +02:00
|
|
|
var h textproto.Header
|
|
|
|
h.Set("From", "John Doe <john.doe@example.org>")
|
|
|
|
h.Set("To", "John Doe <john.doe@example.org>")
|
|
|
|
|
2022-05-24 22:12:17 +02:00
|
|
|
var header textproto.Header
|
|
|
|
header.Set("Content-Type", "text/plain")
|
2022-04-25 15:30:44 +02:00
|
|
|
|
|
|
|
to := []string{"john.doe@example.org"}
|
|
|
|
from := "john.doe@example.org"
|
|
|
|
|
2022-05-24 22:12:17 +02:00
|
|
|
var err error
|
|
|
|
for _, tc := range testCases {
|
|
|
|
var (
|
|
|
|
buf bytes.Buffer
|
|
|
|
cleartext io.WriteCloser
|
|
|
|
)
|
|
|
|
switch tc.method {
|
|
|
|
case "encrypt":
|
|
|
|
cleartext, err = Encrypt(&buf, h, to, from)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Encrypt() = %v", err)
|
|
|
|
}
|
|
|
|
case "sign":
|
|
|
|
cleartext, err = Sign(&buf, h, from)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Encrypt() = %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = textproto.WriteHeader(cleartext, header); err != nil {
|
|
|
|
t.Fatalf("textproto.WriteHeader() = %v", err)
|
|
|
|
}
|
|
|
|
if _, err = io.WriteString(cleartext, tc.body); err != nil {
|
|
|
|
t.Fatalf("io.WriteString() = %v", err)
|
|
|
|
}
|
|
|
|
if err = cleartext.Close(); err != nil {
|
|
|
|
t.Fatalf("ciphertext.Close() = %v", err)
|
|
|
|
}
|
|
|
|
switch tc.method {
|
|
|
|
case "encrypt":
|
|
|
|
validateEncrypt(t, buf)
|
|
|
|
case "sign":
|
|
|
|
validateSign(t, buf)
|
|
|
|
}
|
2022-04-25 15:30:44 +02:00
|
|
|
}
|
2022-05-24 22:12:17 +02:00
|
|
|
}
|
2022-04-25 15:30:44 +02:00
|
|
|
|
2022-05-24 22:12:17 +02:00
|
|
|
func validateEncrypt(t *testing.T, buf bytes.Buffer) {
|
2022-04-25 15:30:44 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:12:17 +02:00
|
|
|
func validateSign(t *testing.T, buf bytes.Buffer) {
|
2022-04-25 15:30:44 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-05-24 22:12:17 +02:00
|
|
|
deepEqual(t, "Sign", md, &wantSigned)
|
2022-04-25 15:30:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
}
|