-
Notifications
You must be signed in to change notification settings - Fork 1
/
yage_test.go
387 lines (370 loc) · 13.7 KB
/
yage_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
385
386
387
// Copyright 2021 Google LLC
// Copyright 2021 Sylvain Rabot
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package main
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"filippo.io/age"
"sylr.dev/yage/v2/cmd/decrypt"
"sylr.dev/yage/v2/cmd/encrypt"
"sylr.dev/yage/v2/utils"
)
func TestVectors(t *testing.T) {
var defaultIDs []age.Identity
password, err := os.ReadFile("testdata/default_password.txt")
if err != nil {
t.Fatal(err)
}
p := strings.TrimSpace(string(password))
i, err := age.NewScryptIdentity(p)
if err != nil {
t.Fatal(err)
}
defaultIDs = append(defaultIDs, i)
ids, err := utils.ParseIdentitiesFile("testdata/default_key.txt", false)
if err != nil {
t.Fatal(err)
}
defaultIDs = append(defaultIDs, ids...)
files, _ := filepath.Glob("testdata/*.age")
for _, f := range files {
_, name := filepath.Split(f)
name = strings.TrimSuffix(name, ".age")
expectPass := strings.HasPrefix(name, "good_")
expectFailure := strings.HasPrefix(name, "fail_")
expectNoMatch := strings.HasPrefix(name, "nomatch_")
t.Run(name, func(t *testing.T) {
identities := defaultIDs
ids, err := utils.ParseIdentitiesFile("testdata/"+name+"_key.txt", false)
if err == nil {
identities = ids
}
password, err := os.ReadFile("testdata/" + name + "_password.txt")
if err == nil {
p := strings.TrimSpace(string(password))
i, err := age.NewScryptIdentity(p)
if err != nil {
t.Fatal(err)
}
identities = []age.Identity{i}
}
in, err := os.Open("testdata/" + name + ".age")
if err != nil {
t.Fatal(err)
}
r, err := age.Decrypt(in, identities...)
if expectFailure {
if err == nil {
t.Fatal("expected Decrypt failure")
}
if e := new(age.NoIdentityMatchError); errors.As(err, &e) {
t.Errorf("got ErrIncorrectIdentity, expected more specific error")
}
} else if expectNoMatch {
if err == nil {
t.Fatal("expected Decrypt failure")
}
if e := new(age.NoIdentityMatchError); !errors.As(err, &e) {
t.Errorf("expected ErrIncorrectIdentity, got %v", err)
}
} else if expectPass {
if err != nil {
t.Fatal(err)
}
out, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
t.Logf("%s", out)
} else {
t.Fatal("invalid test vector")
}
})
}
}
func TestYAML(t *testing.T) {
tests := []struct {
Description string
Input string
Expected string
DiscardNoTag bool
}{
{
Description: "Not style defined",
Input: `password: !crypto/age |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: !crypto/age ThisIsMyReallyEncryptedPassword`),
},
{
Description: "Double quoted",
Input: `password: !crypto/age:DoubleQuoted |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: !crypto/age:DoubleQuoted "ThisIsMyReallyEncryptedPassword"`),
},
{
Description: "Single quoted",
Input: `password: !crypto/age:SingleQuoted |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: !crypto/age:SingleQuoted 'ThisIsMyReallyEncryptedPassword'`),
},
{
Description: "Literal",
Input: `password: !crypto/age:Literal |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: !crypto/age:Literal |-
ThisIsMyReallyEncryptedPassword`),
},
{
Description: "Folded",
Input: `password: !crypto/age:Folded |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: !crypto/age:Folded >-
ThisIsMyReallyEncryptedPassword`),
},
{
Description: "Flow",
Input: `password: !crypto/age:Flow |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: !crypto/age:Flow ThisIsMyReallyEncryptedPassword`),
},
{
Description: "No tag",
Input: `password: !crypto/age:NoTag |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: ThisIsMyReallyEncryptedPassword`),
},
{
Description: "Double quoted, No Tag",
Input: `password: !crypto/age:DoubleQuoted,NoTag |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: "ThisIsMyReallyEncryptedPassword"`),
},
{
Description: "Single quoted, No Tag",
Input: `password: !crypto/age:SingleQuoted,NoTag |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: 'ThisIsMyReallyEncryptedPassword'`),
},
{
Description: "Literal, No Tag",
Input: `password: !crypto/age:Literal,NoTag |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: |-
ThisIsMyReallyEncryptedPassword`),
},
{
Description: "Folded, No Tag",
Input: `password: !crypto/age:Folded,NoTag |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: >-
ThisIsMyReallyEncryptedPassword`),
},
{
Description: "Flow, No Tag",
Input: `password: !crypto/age:Flow,NoTag |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----`,
Expected: fmt.Sprintln(`password: ThisIsMyReallyEncryptedPassword`),
},
{
Description: "Anchor",
Input: `password: &password !crypto/age |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----
dup: *password`,
Expected: fmt.Sprintln(`password: &password !crypto/age ThisIsMyReallyEncryptedPassword
dup: *password`),
},
{
Description: "Anchor, No Tag",
Input: `db_password: &password !crypto/age:NoTag |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----
dup: *password`,
Expected: fmt.Sprintln(`db_password: &password ThisIsMyReallyEncryptedPassword
dup: *password`),
},
{
Description: "Comment, Anchor, No Tag",
Input: `db:
# this is a head comment
password: &password !crypto/age:NoTag | # this is a line comment
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----
# this is a footer comment
dup: *password # alias comment`,
Expected: fmt.Sprintln(`db:
# this is a head comment
password: &password ThisIsMyReallyEncryptedPassword # this is a line comment
# this is a footer comment
dup: *password # alias comment`),
},
{
Description: "Documents, Comment, Anchor, No Tag",
Input: `db:
# this is a head comment
password: &password !crypto/age | # this is a line comment
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----
# this is a footer comment
dup: *password # alias comment
---
db:
# this is a head comment
password: &password !crypto/age:NoTag | # this is a line comment
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBpTmZNODFnSlAzM0F2TEs0
OU9iYk54T0tPN2E5OGdvVkZhVGw1anFyVEV3CjlyaE5RUkh6cStLT2V6aFJua0VD
amlzc3lyS09sVjZKV0FjUjZzMmVTWm8KLS0tIFFHeURlKzB4QW91WE5GZnNNdGdn
alEvdW5oaGVocUp5bVVTNzlQRmduZmcK66z0fR47miRVT/0t8obsCRfacNgy5T6C
gLJ+Nu91e/apOC85VBL/rDgbakSmfHPsCo486rDB0N3Ul0qtHT1m
-----END AGE ENCRYPTED FILE-----
# this is a footer comment
dup: *password # alias comment`,
Expected: fmt.Sprintln(`db:
# this is a head comment
password: &password !crypto/age ThisIsMyReallyEncryptedPassword # this is a line comment
# this is a footer comment
dup: *password # alias comment
---
db:
# this is a head comment
password: &password ThisIsMyReallyEncryptedPassword # this is a line comment
# this is a footer comment
dup: *password # alias comment`),
},
}
recFile, err := os.Open("./testdata/yaml.pub")
if err != nil {
t.Fatal(err)
}
recs, err := age.ParseRecipients(recFile)
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
input := test.Input
for i := 0; i < 2; i++ {
in := bytes.NewBufferString(input)
decryptOut := bytes.NewBuffer(nil)
encryptOut := bytes.NewBuffer(nil)
// decrypt
err := decrypt.DecryptYAML([]string{"./testdata/yaml.key"}, in, decryptOut, test.DiscardNoTag, false, false)
if err != nil {
t.Fatal(err)
}
// compare decrypted with expected
if decryptOut.String() != test.Expected {
t.Errorf("Test \"%s\" failed:\nExpected:\n%sActual:\n%s", test.Description, test.Expected, decryptOut.String())
}
// re-encrypt data for second pass
err = encrypt.EncryptYAML(recs, decryptOut, encryptOut)
if err != nil {
t.Fatal(err)
}
// assign encrypted result to test input
input = encryptOut.String()
}
}
}