aerc/lib/crypto/gpg/gpg_test.go

168 lines
7.3 KiB
Go

package gpg
import (
"bytes"
"fmt"
"io"
"log"
"os/exec"
"strings"
"testing"
"git.sr.ht/~rjarry/aerc/models"
)
func TestHasGpg(t *testing.T) {
gpgmail := new(Mail)
hasGpg := gpgmail.Init(new(log.Logger))
if hasGpg != nil {
t.Errorf("System does not have GPG")
}
}
func CleanUp() {
cmd := exec.Command("gpg", "--batch", "--yes", "--delete-secret-and-public-keys", testKeyId)
err := cmd.Run()
if err != nil {
fmt.Println("Test cleanup failed: you may need to delete the test keys from your GPG keyring")
return
}
}
func toCRLF(s string) string {
return strings.ReplaceAll(s, "\n", "\r\n")
}
func deepEqual(t *testing.T, r *models.MessageDetails, expect *models.MessageDetails) {
var resBuf bytes.Buffer
if _, err := io.Copy(&resBuf, r.Body); err != nil {
t.Fatalf("io.Copy() = %v", err)
}
var expBuf bytes.Buffer
if _, err := io.Copy(&expBuf, expect.Body); err != nil {
t.Fatalf("io.Copy() = %v", err)
}
if resBuf.String() != expBuf.String() {
t.Errorf("MessagesDetails.Body = \n%v\n but want \n%v", resBuf.String(), expBuf.String())
}
if r.IsEncrypted != expect.IsEncrypted {
t.Errorf("IsEncrypted = \n%v\n but want \n%v", r.IsEncrypted, expect.IsEncrypted)
}
if r.IsSigned != expect.IsSigned {
t.Errorf("IsSigned = \n%v\n but want \n%v", r.IsSigned, expect.IsSigned)
}
if r.SignedBy != expect.SignedBy {
t.Errorf("SignedBy = \n%v\n but want \n%v", r.SignedBy, expect.SignedBy)
}
if r.SignedByKeyId != expect.SignedByKeyId {
t.Errorf("SignedByKeyId = \n%v\n but want \n%v", r.SignedByKeyId, expect.SignedByKeyId)
}
if r.SignatureError != expect.SignatureError {
t.Errorf("SignatureError = \n%v\n but want \n%v", r.SignatureError, expect.SignatureError)
}
if r.DecryptedWith != expect.DecryptedWith {
t.Errorf("DecryptedWith = \n%v\n but want \n%v", r.DecryptedWith, expect.DecryptedWith)
}
if r.DecryptedWithKeyId != expect.DecryptedWithKeyId {
t.Errorf("DecryptedWithKeyId = \n%v\n but want \n%v", r.DecryptedWithKeyId, expect.DecryptedWithKeyId)
}
}
const testKeyId = `B1A8669354153B799F2217BF307215C13DF7A964`
const testPrivateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
lQOYBF5FJf8BCACvlKhSSsv4P8C3Wbv391SrNUBtFquoMuWKtuCr/Ks6KHuofGLn
bM55uBSQp908aITBDPkaOPsQ3OvwgF7SM8bNIDVpO7FHzCEg2Ysp99iPET/+LsbY
ugc8oYSuvA5aFFIOMYbAbI+HmbIBuCs+xp0AcU1cemAPzPBDCZs4xl5Y+/ce2yQz
ZGK9O/tQQIKoBUOWLo/0byAWyD6Gwn/Le3fVxxK6RPeeizDV6VfzHLxhxBNkMgmd
QUkBkvqF154wYxhzsHn72ushbJpspKz1LQN7d5u6QOq3h2sLwcLbT457qbMfZsZs
HLhoOibOd+yJ7C6TRbbyC4sQRr+K1CNGcvhJABEBAAEAB/sGyvoOIP2uL409qreW
eteoPgmtjsR6X+m4iaW8kaxwNhO+q31KFdARLnmBNTVeem60Z1OV26F/AAUSy2yf
tkgZNIdMeHY94FxhwHjdWUzkEBdJNrcTuHLCOj9/YSAvBP09tlXPyQNujBgyb9Ug
ex+k3j1PeB6STev3s/3w3t/Ukm6GvPpRSUac1i0yazGOJhGeVjBn34vqJA+D+JxP
odlCZnBGaFlj86sQs+2qlrITGCZLeLlFGXo6GEEDipCBJ94ETcpHEEZLZxoZAcdp
9iQhCK/BNpUO7H7GRs9DxiiWgV2GAeFwgt35kIwuf9X0/3Zt/23KaW/h7xe8G+0e
C0rfBADGZt5tT+5g7vsdgMCGKqi0jCbHpeLDkPbLjlYKOiWQZntLi+i6My4hjZbh
sFpWHUfc5SqBe+unClwXKO084UIzFQU5U7v9JKP+s1lCAXf1oNziDeE8p/71O0Np
J1DQ0WdjPFPH54IzLIbpUwoqha+f/4HERo2/pyIC8RMLNVcVYwQA4o27fAyLePwp
8Zc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