-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper_test.go
225 lines (214 loc) · 6.27 KB
/
helper_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
// SPDX-FileCopyrightText: 2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
package openpgp
import (
"os"
"strings"
"testing"
"github.com/ProtonMail/gopenpgp/v2/helper"
)
func TestMiddleware_reArmorMessage(t *testing.T) {
ts := "This is the test message"
mc, err := NewConfig(privKey, pubKey, WithPrivKeyPass(os.Getenv("PRIV_KEY_PASS")))
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
ct, err := helper.EncryptMessageArmored(mw.config.PublicKey, ts)
if err != nil {
t.Errorf("failed to encrypt message: %s", err)
}
ra, err := mw.reArmorMessage(ct)
if err != nil {
t.Errorf("reArmorMessage failed: %s", err)
}
if !strings.Contains(ra, armorComment) || !strings.Contains(ra, armorVersion) {
t.Errorf("reArmorMessage failed. Expected version/comment but didn't find it")
}
pt, err := helper.DecryptMessageArmored(mw.config.PrivKey, []byte(mw.config.passphrase), ra)
if err != nil {
t.Errorf("reArmorMessage failed. Decryption of re-armored message failed: %s", err)
}
if pt != ts {
t.Errorf("reArmorMessage failed. Expected: %q, got: %q", ts, pt)
}
}
func TestMiddleware_reArmorMessage_failed(t *testing.T) {
ts := "This is the test message"
mc, err := NewConfig(privKey, pubKey)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
_, err = mw.reArmorMessage(ts)
if err == nil {
t.Errorf("reArmorMessage with no armored message was supposed to fail, but didn't")
}
}
func TestMiddleware_processPlain(t *testing.T) {
tests := []struct {
n string
a Action
}{
{"Encrypt-only", ActionEncrypt},
{"Encrypt/Sign", ActionEncryptAndSign},
{"Sign-only", ActionSign},
}
ts := "This is the test message"
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
mc, err := NewConfig(privKey, pubKey,
WithPrivKeyPass(os.Getenv("PRIV_KEY_PASS")),
WithAction(tt.a),
)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
ct, err := mw.processPlain(ts)
if err != nil {
t.Errorf("processPlain failed: %s", err)
}
if tt.a == ActionEncrypt {
pt, err := helper.DecryptMessageArmored(mw.config.PrivKey, []byte(mw.config.passphrase), ct)
if err != nil {
t.Errorf("processPlain failed. Decryption of message failed: %s", err)
}
if pt != ts {
t.Errorf("processPlain failed. Expected: %q, got: %q", ts, pt)
}
}
if tt.a == ActionEncryptAndSign {
pt, err := helper.DecryptVerifyMessageArmored(mw.config.PublicKey, mw.config.PrivKey,
[]byte(mw.config.passphrase), ct)
if err != nil {
t.Errorf("processPlain failed. Decryption of message failed: %s", err)
}
if pt != ts {
t.Errorf("processPlain failed. Expected: %q, got: %q", ts, pt)
}
}
if tt.a == ActionSign {
pt, err := helper.VerifyCleartextMessageArmored(mw.config.PublicKey, ct, 0)
if err != nil {
t.Errorf("processPlain failed. Verification of message failed: %s", err)
}
if pt != ts {
t.Errorf("processPlain failed. Expected: %q, got: %q", ts, pt)
}
}
})
}
}
func TestMiddleware_processPlain_fail(t *testing.T) {
ts := "This is the test message"
mc, err := NewConfig(privKey, pubKey,
WithPrivKeyPass(os.Getenv("PRIV_KEY_PASS")),
WithAction(999),
)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
_, err = mw.processPlain(ts)
if err == nil {
t.Errorf("processPlain with unknown action was supposed to fail, but didn't")
}
mc, err = NewConfig(privKey, pubKey,
WithPrivKeyPass(os.Getenv("PRIV_KEY_PASS")),
WithAction(ActionEncrypt),
)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw = NewMiddleware(mc)
mw.config.PublicKey = ""
_, err = mw.processPlain(ts)
if err == nil {
t.Errorf("processPlain with empty pubkey was supposed to fail, but didn't")
}
}
func TestMiddleware_processBinary(t *testing.T) {
tests := []struct {
n string
a Action
}{
{"Encrypt-only", ActionEncrypt},
{"Encrypt/Sign", ActionEncryptAndSign},
{"Sign-only", ActionSign},
}
ts := []byte("This is the test message")
for _, tt := range tests {
t.Run(tt.n, func(t *testing.T) {
mc, err := NewConfig(privKey, pubKey,
WithPrivKeyPass(os.Getenv("PRIV_KEY_PASS")),
WithAction(tt.a),
)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
ct, err := mw.processBinary(ts)
if err != nil {
t.Errorf("processBinary failed: %s", err)
}
if tt.a == ActionEncrypt {
pt, err := helper.DecryptMessageArmored(mw.config.PrivKey, []byte(mw.config.passphrase), ct)
if err != nil {
t.Errorf("processBinary failed. Decryption of message failed: %s", err)
}
if pt != string(ts) {
t.Errorf("processBinary failed. Expected: %q, got: %q", ts, pt)
}
}
if tt.a == ActionEncryptAndSign {
pt, err := helper.DecryptVerifyMessageArmored(mw.config.PublicKey, mw.config.PrivKey,
[]byte(mw.config.passphrase), ct)
if err != nil {
t.Errorf("processBinary failed. Decryption of message failed: %s", err)
}
if pt != string(ts) {
t.Errorf("processBinary failed. Expected: %q, got: %q", ts, pt)
}
}
if tt.a == ActionSign {
pt, err := helper.VerifyCleartextMessageArmored(mw.config.PublicKey, ct, 0)
if err != nil {
t.Errorf("processBinary failed. Verification of message failed: %s", err)
}
if pt != string(ts) {
t.Errorf("processBinary failed. Expected: %q, got: %q", ts, pt)
}
}
})
}
}
func TestMiddleware_processBinary_fail(t *testing.T) {
ts := []byte("This is the test message")
mc, err := NewConfig(privKey, pubKey,
WithPrivKeyPass(os.Getenv("PRIV_KEY_PASS")),
WithAction(999),
)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw := NewMiddleware(mc)
_, err = mw.processBinary(ts)
if err == nil {
t.Errorf("processBinary with unknown action was supposed to fail, but didn't")
}
mc, err = NewConfig(privKey, pubKey,
WithPrivKeyPass(os.Getenv("PRIV_KEY_PASS")),
WithAction(ActionEncrypt),
)
if err != nil {
t.Errorf("failed to create new config: %s", err)
}
mw = NewMiddleware(mc)
mw.config.PublicKey = ""
_, err = mw.processBinary(ts)
if err == nil {
t.Errorf("processBinary with empty pubkey was supposed to fail, but didn't")
}
}