-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopenpgp_test.go
310 lines (295 loc) · 13.6 KB
/
openpgp_test.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// SPDX-FileCopyrightText: 2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
package openpgp
import (
"bufio"
"bytes"
"encoding/base64"
"strings"
"testing"
"github.com/wneessen/go-mail"
)
// pubkey is a dedicated OpenPGP key for testing this go-middleware. This key is
// not used in any actual environment. Please don't use it to send any encrypted
// mails
const pubKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBGPT4R8BEAC77qxjyWmshngRUrA2dVBD+/N8lBqxeMq/ZvGQJhhId9KJGDe5
X/lWUqr5Gx0b4eTSOv7Uqc4wSg0Ji7bSqzenvgQIvfKdbDs82kZ8V9pBiRo02bbP
BwP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IBYhBF66D4P0wZTvfrZHXgeEFhOdGoyzBQJj0+Ef
AhsMAAoJEAeEFhOdGoyzIaIP/13274pbYyoTFK6mNbfQQJ+qb1OkQBHH/LKNE+Sm
Xod8SvBy/e65p1aJMjcJOT52NQfAeDv5bpcWUOcodmwNvpDYT6hpMfkOv05sNOec
qnoki+rwVOEQnL/ZEN9ruQRkcFVcr4MXk18ex1qhkLxF46DKnsq6aEz1vgNfaEBu
o43X63MJ6vz4V69oEk+37Bpwg7aJBRAOBOZCaM9ubfCT42S5q60lDOx4pae1uRA/
jbwfNAyscpqs3BDmqLlUQArb5mr7YvOchFFZzLk9eWZu6ZlbaAr3/MEW/9CMgc8l
I7MmLr7CNs6qavo6wTQWhKErQ6ljVLd+0gdUCNb5ljHeATcR2HEdlx+fCR7MCNGN
+IhCgz4EKDSZEKFzgxORfV5es+Fpqq+uotEchp3h7TMcLsGBZzbZRbpUS7De7ysV
BLdAiUChctzXCcmJiPsiDr5BJehA3WHOamp2I/QVcfZCTTea5G6LukLgMUWAPKYe
xTHXTPpAVMkhnkNzm/0vmO/x1FmyNXGFto/v17DxxNEi180qCajmjldadnND2JO2
lDGmTvNf/IY2qnsn12qnHUyegtWgoz+urSi6CdfpgttwCJEqGYC15D2Gt9ryskj6
aEhxoA7tp6gsmDCFZvoBJ3C1tPiu3Hkqku7QfPsAs/3692tl4vIPFasO2KmbcVcb
avSf
=JhVL
-----END PGP PUBLIC KEY BLOCK-----`
// privkey is a dedicated OpenPGP key for testing this go-middleware. This key is
// not used in any actual environment. Please don't use it to send any encrypted
// mails
const privKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
lQdGBGPT4R8BEAC77qxjyWmshngRUrA2dVBD+/N8lBqxeMq/ZvGQJhhId9KJGDe5
X/lWUqr5Gx0b4eTSOv7Uqc4wSg0Ji7bSqzenvgQIvfKdbDs82kZ8V9pBiRo02bbP
BwPJK+zIVDSFJfiFYNRVYl7OCGvfE7RRGfMpF8HJFU3mnt2l8CPxfTEI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tp6gsmDCFZvoB
J3C1tPiu3Hkqku7QfPsAs/3692tl4vIPFasO2KmbcVcbavSf
=JfM9
-----END PGP PRIVATE KEY BLOCK-----`
func TestNewMiddleware(t *testing.T) {
mc, err := NewConfig(privKey, pubKey)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
if mw.config == nil {
t.Errorf("NewMiddleware failed. Expected config but got empty field")
}
}
func TestMiddleware_HandlePGPInline(t *testing.T) {
mc, err := NewConfig(privKey, pubKey, WithScheme(SchemePGPInline))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
m := mail.NewMsg(mail.WithMiddleware(mw))
m.Subject("This is a subject")
m.SetDate()
m.SetBodyString(mail.TypeTextPlain, "This is the mail body")
buf := bytes.Buffer{}
_, err = m.WriteTo(&buf)
if err != nil {
t.Errorf("failed writing message to memory: %s", err)
}
br := bufio.NewScanner(&buf)
fb := false
body := ""
for br.Scan() {
l := br.Text()
if l == "" {
fb = true
}
if fb {
body += l + "\n"
}
}
bb, err := base64.StdEncoding.DecodeString(body)
if err != nil {
t.Errorf("failed to base64 decode message body: %s", err)
}
if !strings.Contains(string(bb), `-----BEGIN PGP MESSAGE-----`) ||
!strings.Contains(string(bb), `-----END PGP MESSAGE-----`) {
t.Errorf("mail encryption failed. Unable to find PGP notation in mail body")
}
}
func TestMiddleware_HandlePGPMIME(t *testing.T) {
t.Skip("PGP/MIME not supported yet")
mc, err := NewConfig(privKey, pubKey, WithScheme(SchemePGPMIME))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
m := mail.NewMsg(mail.WithMiddleware(mw))
m.Subject("This is a subject")
m.SetDate()
m.SetBodyString(mail.TypeTextPlain, "This is the mail body")
buf := bytes.Buffer{}
_, err = m.WriteTo(&buf)
if err != nil {
t.Errorf("failed writing message to memory: %s", err)
}
br := bufio.NewScanner(&buf)
fb := false
body := ""
for br.Scan() {
l := br.Text()
if l == "" {
fb = true
}
if fb {
body += l + "\n"
}
}
bb, err := base64.StdEncoding.DecodeString(body)
if err != nil {
t.Errorf("failed to base64 decode message body: %s", err)
}
if !strings.Contains(string(bb), `-----BEGIN PGP MESSAGE-----`) ||
!strings.Contains(string(bb), `-----END PGP MESSAGE-----`) {
t.Errorf("mail encryption failed. Unable to find PGP notation in mail body")
}
}
func TestMiddleware_HandleUnknown(t *testing.T) {
mc, err := NewConfig(privKey, pubKey, WithScheme(999))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
m := mail.NewMsg(mail.WithMiddleware(mw))
m.Subject("This is a subject")
m.SetDate()
m.SetBodyString(mail.TypeTextPlain, "This is the mail body")
buf := bytes.Buffer{}
_, err = m.WriteTo(&buf)
if err != nil {
t.Errorf("failed writing message to memory: %s", err)
}
if len(m.GetParts()) != 1 {
t.Errorf("Handle with unknown scheme failed. Mails parts seems modified")
return
}
pc, err := m.GetParts()[0].GetContent()
if err != nil {
t.Errorf("failed to get message body part content")
}
if string(pc) != "This is the mail body" {
t.Errorf("Handle with unknown scheme failed. Mails parts seems modified")
}
}
func TestMiddleware_Type(t *testing.T) {
mc, err := NewConfig(privKey, pubKey, WithScheme(999))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
if mw.Type() != Type {
t.Errorf("Type() failed. Expected: %s, got: %s", Type, mw.Type())
}
}