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
|
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"roseh.moe/cmd/sym/internal/sym"
)
func mustWriteFile(t *testing.T, path string, content []byte) {
t.Helper()
if err := os.WriteFile(path, content, 0600); err != nil {
t.Fatalf("Failed to write test file: %s", err)
}
}
func mustReadFile(t *testing.T, path string) []byte {
t.Helper()
content, err := os.ReadFile(path)
if err != nil {
t.Fatalf("Failed to read file: %s", err)
}
return content
}
func TestEnc(t *testing.T) {
t.Parallel()
const password = "asdf"
fileContent := []byte("test file content")
fileName := filepath.Join(t.TempDir(), "file")
mustWriteFile(t, fileName, fileContent)
if err := (&options{password: password}).enc(fileName); err != nil {
t.Fatalf("enc failed: %s", err)
}
if err := sym.DecryptFile(fileName+".enc", password, sym.Force(true)); err != nil {
t.Fatalf("Failed to decrypt encrypted file: %s", err)
}
gotFileContents := mustReadFile(t, fileName)
if !bytes.Equal(gotFileContents, fileContent) {
t.Errorf("encrypt round trip returned incorrect contents %q, want %q", gotFileContents, fileContent)
}
}
func TestEnc_UsageError(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
desc string
generatePassword bool
password string
files []string
}{{
desc: "GeneratePasswordAndPassword",
generatePassword: true,
password: "asdf",
}, {
desc: "MissingPasswordStdin",
generatePassword: false,
password: "",
}, {
desc: "NonexistentFile",
password: "asdf",
files: []string{"my-nonexistent-file.txt"},
}} {
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
opts := &options{
generatePassword: tc.generatePassword,
password: tc.password,
}
if err := opts.enc(tc.files...); err == nil {
t.Errorf("enc(%+v) succeeded, want error", opts)
}
})
}
}
func TestEnc_GeneratePassword(t *testing.T) {
t.Parallel()
fileContent := []byte("test file content")
fileName := filepath.Join(t.TempDir(), "file")
mustWriteFile(t, fileName, fileContent)
password := new(strings.Builder)
opts := &options{
generatePassword: true,
passwordOut: password,
}
if err := opts.enc(fileName); err != nil {
t.Fatalf("enc(%+v) failed: %s", opts, err)
}
pw := password.String()
if err := sym.DecryptFile(fileName+".enc", pw, sym.Force(true)); err != nil {
t.Fatalf("Failed to decrypt encrypted file with generated password %q: %s", pw, err)
}
gotFileContents := mustReadFile(t, fileName)
if !bytes.Equal(gotFileContents, fileContent) {
t.Errorf("encrypt round trip returned incorrect contents %q, want %q", gotFileContents, fileContent)
}
}
func TestEnc_Stdin(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
desc string
ascii bool
}{{
desc: "Binary",
ascii: false,
}, {
desc: "ASCII",
ascii: true,
}} {
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
const (
input = "test input"
password = "asdf"
)
stdout := new(strings.Builder)
opts := &options{
password: password,
asciiOutput: tc.ascii,
stdin: strings.NewReader(input),
stdout: stdout,
}
if err := opts.enc(); err != nil {
t.Errorf("enc(+%v) failed: %s", opts, err)
}
got := new(strings.Builder)
if err := sym.Decrypt(got, strings.NewReader(stdout.String()), password); err != nil {
t.Errorf("Failed to decrypt stdout content: %s", err)
}
if got, want := got.String(), input; got != want {
t.Errorf("Encrypt round-trip to stdout returned incorrect contents: %q, want %q", got, want)
}
})
}
}
|