-
Notifications
You must be signed in to change notification settings - Fork 0
/
awwan_encrypt_test.go
131 lines (107 loc) · 2.73 KB
/
awwan_encrypt_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
// 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 TestAwwanEncrypt(t *testing.T) {
type testCase struct {
baseDir string
file string
passphrase string
expError string
}
var cases = []testCase{{
baseDir: filepath.Join(`testdata`, `encrypt-with-passphrase`),
file: `.awwan.env`,
passphrase: "s3cret\r",
}, {
baseDir: filepath.Join(`testdata`, `encrypt-with-passphrase`),
file: `.awwan.env`,
expError: `Encrypt: private key is missing or not loaded`,
}, {
baseDir: filepath.Join(`testdata`, `encrypt-with-passphrase`),
file: `.awwan.env`,
passphrase: "invalids3cret\r",
expError: `Encrypt: LoadPrivateKeyInteractive: x509: decryption password incorrect`,
}, {
baseDir: filepath.Join(`testdata`, `encrypt-without-rsa`),
file: `.awwan.env`,
passphrase: "s3cret\r",
expError: `Encrypt: the private key type must be RSA, got *ed25519.PrivateKey`,
}, {
baseDir: filepath.Join(`testdata`, `encrypt-without-passphrase`),
file: `.awwan.env`,
}}
var (
mockrw = mock.ReadWriter{}
c testCase
err error
filePlain string
fileVault string
)
for _, c = range cases {
var aww = Awwan{}
filePlain = filepath.Join(c.baseDir, c.file)
err = aww.init(c.baseDir)
if err != nil {
test.Assert(t, `Encrypt`, c.expError, err.Error())
continue
}
// Write the passphrase to standard input to be read
// interactively.
mockrw.BufRead.WriteString(c.passphrase)
aww.cryptoc.termrw = &mockrw
fileVault, err = aww.Encrypt(filePlain)
if err != nil {
test.Assert(t, `Encrypt`, c.expError, err.Error())
continue
}
_, err = os.Stat(fileVault)
if err != nil {
test.Assert(t, `os.Stat`, c.expError, err.Error())
}
}
}
func TestAwwanEncryptDecrypt_withPassFile(t *testing.T) {
var (
baseDir = filepath.Join(`testdata`, `encrypt-with-passfile`)
tdata *test.Data
err error
)
tdata, err = test.LoadData(filepath.Join(baseDir, `test.data`))
if err != nil {
t.Fatal(err)
}
var aww *Awwan
aww, err = New(baseDir)
if err != nil {
t.Fatal(err)
}
var (
filePlain = filepath.Join(baseDir, `plain.txt`)
fileVault string
)
fileVault, err = aww.Encrypt(filePlain)
if err != nil {
t.Fatal(err)
}
filePlain, err = aww.Decrypt(fileVault)
if err != nil {
t.Fatal(err)
}
var (
expContent = tdata.Output[`plain.txt`]
gotContent []byte
)
gotContent, err = os.ReadFile(filePlain)
if err != nil {
t.Fatal(err)
}
test.Assert(t, `content`, string(expContent), string(gotContent))
}