-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig_test.go
384 lines (363 loc) · 11.9 KB
/
config_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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// SPDX-FileCopyrightText: 2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
package openpgp
import (
"fmt"
"os"
"testing"
"github.com/wneessen/go-mail-middleware/log"
)
func TestNewConfig(t *testing.T) {
mc, err := NewConfig(privKey, pubKey, nil)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.Scheme != SchemePGPInline {
t.Errorf("NewConfig failed. Expected Scheme %d, got: %d", SchemePGPInline, mc.Scheme)
}
if mc.Logger == nil {
t.Errorf("NewConfig failed. Expected log logger but got nil")
}
if mc.PublicKey == "" {
t.Errorf("NewConfig failed. Expected public key but got empty string")
}
if mc.PublicKey != pubKey {
t.Errorf("NewConfig failed. Public key does not match")
}
if mc.PrivKey == "" {
t.Errorf("NewConfig failed. Expected private key but got empty string")
}
if mc.PrivKey != privKey {
t.Errorf("NewConfig failed. Private key does not match")
}
}
func TestNewConfigFromPubKeyBytes(t *testing.T) {
mc, err := NewConfigFromPubKeyByteSlice([]byte(pubKey))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.Scheme != SchemePGPInline {
t.Errorf("NewConfigFromPubKeyByteSlice failed. Expected Scheme %d, got: %d", SchemePGPInline, mc.Scheme)
}
if mc.Logger == nil {
t.Errorf("NewConfigFromPubKeyByteSlice failed. Expected log logger but got nil")
}
if mc.PublicKey == "" {
t.Errorf("NewConfigFromPubKeyByteSlice failed. Expected public key but got empty string")
}
if mc.PublicKey != pubKey {
t.Errorf("NewConfigFromPubKeyByteSlice failed. Public key does not match")
}
}
func TestNewConfigFromPrivKeyBytes(t *testing.T) {
mc, err := NewConfigFromPrivKeyByteSlice([]byte(privKey), WithAction(ActionSign))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.Scheme != SchemePGPInline {
t.Errorf("NewConfigFromPrivKeyByteSlice failed. Expected Scheme %d, got: %d", SchemePGPInline, mc.Scheme)
}
if mc.Logger == nil {
t.Errorf("NewConfigFromPrivKeyByteSlice failed. Expected log logger but got nil")
}
if mc.PrivKey == "" {
t.Errorf("NewConfigFromPrivKeyByteSlice failed. Expected public key but got empty string")
}
if mc.PrivKey != privKey {
t.Errorf("NewConfigFromPrivKeyByteSlice failed. Private key does not match")
}
}
func TestNewConfigFromKeysBytes(t *testing.T) {
mc, err := NewConfigFromKeysByteSlices([]byte(privKey), []byte(pubKey))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.Scheme != SchemePGPInline {
t.Errorf("NewConfigFromPubKeyByteSlice failed. Expected Scheme %d, got: %d", SchemePGPInline, mc.Scheme)
}
if mc.Logger == nil {
t.Errorf("NewConfigFromPubKeyByteSlice failed. Expected log logger but got nil")
}
if mc.PublicKey == "" {
t.Errorf("NewConfigFromPubKeyByteSlice failed. Expected public key but got empty string")
}
if mc.PublicKey != pubKey {
t.Errorf("NewConfigFromPubKeyByteSlice failed. Public key does not match")
}
if mc.PrivKey == "" {
t.Errorf("NewConfigFromKeysByteSlices failed. Expected private key but got empty string")
}
if mc.PrivKey != privKey {
t.Errorf("NewConfigFromKeysByteSlices failed. Private key does not match")
}
}
func TestNewConfigFromPubKeyFile(t *testing.T) {
tmp, err := os.MkdirTemp(os.TempDir(), "go-mail-middleware-openpgp_")
if err != nil {
t.Errorf("failed to create temporary directory for key file")
return
}
defer func() { _ = os.RemoveAll(tmp) }()
file := fmt.Sprintf("%s/%s", tmp, "pubkey.asc")
if err := os.WriteFile(file, []byte(pubKey), 0o700); err != nil {
t.Errorf("failed to write public key to temporary file %q: %s", file, err)
return
}
mc, err := NewConfigFromPubKeyFile(file)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.Scheme != SchemePGPInline {
t.Errorf("NewConfigFromPubKeyFile failed. Expected Scheme %d, got: %d", SchemePGPInline, mc.Scheme)
}
if mc.Logger == nil {
t.Errorf("NewConfigFromPubKeyFile failed. Expected log logger but got nil")
}
if mc.PublicKey == "" {
t.Errorf("NewConfigFromPubKeyFile failed. Expected public key but got empty string")
}
if mc.PublicKey != pubKey {
t.Errorf("NewConfigFromPubKeyFile failed. Public key does not match")
}
}
func TestNewConfigFromPrivKeyFile(t *testing.T) {
tmp, err := os.MkdirTemp(os.TempDir(), "go-mail-middleware-openpgp_")
if err != nil {
t.Errorf("failed to create temporary directory for key file")
return
}
defer func() { _ = os.RemoveAll(tmp) }()
file := fmt.Sprintf("%s/%s", tmp, "privkey.asc")
if err := os.WriteFile(file, []byte(privKey), 0o700); err != nil {
t.Errorf("failed to write public key to temporary file %q: %s", file, err)
return
}
mc, err := NewConfigFromPrivKeyFile(file, WithAction(ActionSign))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.Scheme != SchemePGPInline {
t.Errorf("NewConfigFromPrivKeyFile failed. Expected Scheme %d, got: %d", SchemePGPInline, mc.Scheme)
}
if mc.Logger == nil {
t.Errorf("NewConfigFromPrivKeyFile failed. Expected log logger but got nil")
}
if mc.PrivKey == "" {
t.Errorf("NewConfigFromPrivKeyFile failed. Expected public key but got empty string")
}
if mc.PrivKey != privKey {
t.Errorf("NewConfigFromPrivKeyFile failed. Private key does not match")
}
}
func TestNewConfigFromKeysFiles(t *testing.T) {
tmp, err := os.MkdirTemp(os.TempDir(), "go-mail-middleware-openpgp_")
if err != nil {
t.Errorf("failed to create temporary directory for key file")
return
}
defer func() { _ = os.RemoveAll(tmp) }()
pubfile := fmt.Sprintf("%s/%s", tmp, "pubkey.asc")
if err := os.WriteFile(pubfile, []byte(pubKey), 0o700); err != nil {
t.Errorf("failed to write public key to temporary file %q: %s", pubfile, err)
return
}
privfile := fmt.Sprintf("%s/%s", tmp, "privkey.asc")
if err := os.WriteFile(privfile, []byte(privKey), 0o700); err != nil {
t.Errorf("failed to write private key to temporary file %q: %s", privfile, err)
return
}
mc, err := NewConfigFromKeyFiles(privfile, pubfile)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.Scheme != SchemePGPInline {
t.Errorf("NewConfigFromKeyFiles failed. Expected Scheme %d, got: %d", SchemePGPInline, mc.Scheme)
}
if mc.Logger == nil {
t.Errorf("NewConfigFromKeyFiles failed. Expected log logger but got nil")
}
if mc.PublicKey == "" {
t.Errorf("NewConfigFromKeyFiles failed. Expected public key but got empty string")
}
if mc.PublicKey != pubKey {
t.Errorf("NewConfigFromKeyFiles failed. Public key does not match")
}
if mc.PrivKey == "" {
t.Errorf("NewConfigFromKeyFiles failed. Expected private key but got empty string")
}
if mc.PrivKey != privKey {
t.Errorf("NewConfigFromKeyFiles failed. Private key does not match")
}
}
func TestNewConfigFromFiles_failed(t *testing.T) {
const f = "/file/does/not/exist/at/all.pgp"
tmp, err := os.MkdirTemp(os.TempDir(), "go-mail-middleware-openpgp_")
if err != nil {
t.Errorf("failed to create temporary directory for key file")
return
}
defer func() { _ = os.RemoveAll(tmp) }()
ex := fmt.Sprintf("%s/%s", tmp, "exists.asc")
if err := os.WriteFile(ex, []byte("file exists"), 0o700); err != nil {
t.Errorf("failed to write to temporary file %q: %s", ex, err)
return
}
_, err = NewConfigFromPubKeyFile(f)
if err == nil {
t.Errorf("reading from non existing file(s) should have failed, but didn't")
}
_, err = NewConfigFromPrivKeyFile(f)
if err == nil {
t.Errorf("reading from non existing file(s) should have failed, but didn't")
}
_, err = NewConfigFromKeyFiles(f, f)
if err == nil {
t.Errorf("reading from non existing file(s) should have failed, but didn't")
}
_, err = NewConfigFromKeyFiles(ex, f)
if err == nil {
t.Errorf("reading from non existing file(s) should have failed, but didn't")
}
_, err = NewConfigFromKeyFiles(f, ex)
if err == nil {
t.Errorf("reading from non existing file(s) should have failed, but didn't")
}
}
func TestNewConfig_WithLogger(t *testing.T) {
l := log.New(os.Stderr, "[openpgp-custom]", log.LevelWarn)
mc, err := NewConfig(privKey, pubKey, WithLogger(l))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.Logger == nil {
t.Errorf("NewConfig_WithLogger failed. Expected slog logger but got empty field")
}
}
func TestNewConfig_WithPrivKeyPass(t *testing.T) {
p := "sup3rS3cret!"
mc, err := NewConfig(privKey, pubKey, WithPrivKeyPass(p))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
if mc.passphrase == "" {
t.Errorf("NewConfig_WithPrivKeyPass failed. Expected value but got empty string")
}
if mc.passphrase != p {
t.Errorf("NewConfig_WithPrivKeyPass failed. Expected: %s, got: %s", p, mc.passphrase)
}
}
func TestNewConfig_WithScheme(t *testing.T) {
tests := []struct {
n string
s PGPScheme
}{
{"PGP/Inline", SchemePGPInline},
{"PGP/MIME", SchemePGPMIME},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
mc, err := NewConfig(privKey, pubKey, WithScheme(tt.s))
if err != nil {
t.Errorf("NewConfig_WithScheme %q failed: %s", tt.s, err)
}
if mc.Scheme != tt.s {
t.Errorf("NewConfig_WithScheme failed. Expected %s, got %s", tt.s, mc.Scheme)
}
if mc.Scheme.String() == "unknown" {
t.Errorf("NewConfig_WithScheme failed. Received unknown type")
}
})
}
}
func TestNewConfig_WithAction(t *testing.T) {
tests := []struct {
n string
a Action
}{
{"Encrypt-only", ActionEncrypt},
{"Encrypt/Sign", ActionEncryptAndSign},
{"Sign-only", ActionSign},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
mc, err := NewConfig(privKey, pubKey, WithAction(tt.a))
if err != nil {
t.Errorf("NewConfig_WithAction %q failed: %s", tt.a, err)
}
if mc.Action != tt.a {
t.Errorf("NewConfig_WithAction failed. Expected %s, got %s", tt.a, mc.Action)
}
if mc.Action.String() == "unknown" {
t.Errorf("NewConfig_WithAction failed. Received unknown type")
}
})
}
}
func TestNewConfig_WithAction_fails(t *testing.T) {
tests := []struct {
n string
a Action
pr string
pu string
f bool
}{
{"Encrypt-only, PubKey, PrivKey", ActionEncrypt, privKey, pubKey, false},
{"Encrypt-only, PubKey, NoPrivKey", ActionEncrypt, "", pubKey, false},
{"Encrypt-only, NoPubKey, PrivKey", ActionEncrypt, privKey, "", true},
{"Encrypt-only, NoPubKey, NoPrivKey", ActionEncrypt, "", "", true},
{"Encrypt/Sign, PubKey, PrivKey", ActionEncryptAndSign, privKey, pubKey, false},
{"Encrypt/Sign, PubKey, NoPrivKey", ActionEncryptAndSign, "", pubKey, true},
{"Encrypt/Sign, NoPubKey, PrivKey", ActionEncryptAndSign, privKey, "", true},
{"Encrypt/Sign, NoPubKey, NoPrivKey", ActionEncryptAndSign, "", "", true},
{"Sign-only, PubKey, PrivKey", ActionSign, privKey, pubKey, false},
{"Sign-only, PubKey, NoPrivKey", ActionSign, "", pubKey, true},
{"Sign-only, NoPubKey, PrivKey", ActionSign, privKey, "", false},
{"Sign-only, NoPubKey, NoPrivKey", ActionSign, "", "", true},
}
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
_, err := NewConfig(tt.pr, tt.pu, WithAction(tt.a))
if err != nil && !tt.f {
t.Errorf("NewConfig_WithAction %q failed: %s", tt.a, err)
}
})
}
}
func TestPGPSchemeString(t *testing.T) {
tests := []struct {
name string
s PGPScheme
want string
}{
{"inline", SchemePGPInline, "PGP/Inline"},
{"mime", SchemePGPMIME, "PGP/MIME"},
{"unknown", PGPScheme(3), "unknown"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.s.String(); got != tt.want {
t.Errorf("PGPScheme.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestActionString(t *testing.T) {
tests := []struct {
name string
a Action
want string
}{
{"encrypt", ActionEncrypt, "Encrypt-only"},
{"encrypt-sign", ActionEncryptAndSign, "Encrypt/Sign"},
{"sign", ActionSign, "Sign-only"},
{"unknown", Action(3), "unknown"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.a.String(); got != tt.want {
t.Errorf("Action.String() = %v, want %v", got, tt.want)
}
})
}
}