aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/gpg/writer_test.go
blob: e8defc1b34233f405fbab31ca02bbdccf227460b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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) {
	initGPGtest(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)
	}
}

func TestSign(t *testing.T) {
	initGPGtest(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",
}