-
Notifications
You must be signed in to change notification settings - Fork 0
/
awwan_decrypt_test.go
85 lines (72 loc) · 2.03 KB
/
awwan_decrypt_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
// SPDX-FileCopyrightText: 2023 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
//go:build !integration
package awwan
import (
"os"
"path/filepath"
"testing"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
"git.sr.ht/~shulhan/pakakeh.go/lib/test/mock"
)
func TestAwwanDecrypt(t *testing.T) {
type testCase struct {
baseDir string
fileVault string
passphrase string
expError string
}
var cases = []testCase{{
baseDir: filepath.Join(`testdata`, `decrypt-with-passphrase`),
fileVault: `.awwan.env`,
passphrase: "s3cret\r",
expError: `Decrypt: invalid extension, expecting .vault, got .env`,
}, {
baseDir: filepath.Join(`testdata`, `decrypt-with-passphrase`),
fileVault: `.awwan.env.vault`,
passphrase: "invalidpassphrase\r",
expError: `LoadPrivateKeyInteractive: x509: decryption password incorrect`,
}, {
baseDir: filepath.Join(`testdata`, `decrypt-with-passphrase`),
fileVault: `.awwan.env.vault`,
passphrase: "s3cret\r",
}, {
baseDir: filepath.Join(`testdata`, `decrypt-wrong-privatekey`),
fileVault: `.awwan.env.vault`,
passphrase: "news3cret\r",
expError: `Decrypt: DecryptOaep: crypto/rsa: decryption error`,
}, {
baseDir: filepath.Join(`testdata`, `decrypt-with-passphrase`),
fileVault: `.awwan.env.vault`,
expError: `Decrypt: private key is missing or not loaded`,
}}
var (
mockrw = mock.ReadWriter{}
c testCase
err error
filePlain string
fileVault string
)
for _, c = range cases {
var aww = Awwan{}
fileVault = filepath.Join(c.baseDir, c.fileVault)
err = aww.init(c.baseDir)
if err != nil {
test.Assert(t, `Decrypt`, c.expError, err.Error())
continue
}
// Write the passphrase to standard input to be read
// interactively.
mockrw.BufRead.WriteString(c.passphrase)
aww.cryptoc.termrw = &mockrw
filePlain, err = aww.Decrypt(fileVault)
if err != nil {
test.Assert(t, `Decrypt`, c.expError, err.Error())
continue
}
_, err = os.Stat(filePlain)
if err != nil {
t.Fatal(err)
}
}
}