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
|
package main
import (
"bytes"
"errors"
"flag"
"path/filepath"
"strings"
"testing"
)
func TestEncryptFile_Force(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
desc string
force bool
wantErr bool
}{{
desc: "OutputExists",
force: false,
wantErr: true,
}, {
desc: "Force",
force: true,
wantErr: false,
}} {
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
fileName := filepath.Join(t.TempDir(), "file")
mustWriteFile(t, fileName, []byte("test file content"))
mustWriteFile(t, fileName+".enc", []byte("file already exists"))
err := (&encryptOptions{encryptFlags: encryptFlags{force: tc.force}}).encryptFile(fileName, "asdf")
if gotErr := err != nil; gotErr != tc.wantErr {
t.Errorf("EncryptFile(force=%t) returned returned error %v when output file exists, want error? %t", tc.force, err, tc.wantErr)
}
})
}
}
func TestEncryptFile_NotFound(t *testing.T) {
t.Parallel()
err := (&encryptOptions{}).encryptFile("my-nonexistent-file.txt", "asdf")
if err == nil {
t.Fatal("encryptFile succeeded for nonexistent file, want error")
}
}
func TestEncryptFile_NoPermission(t *testing.T) {
t.Parallel()
fileName := filepath.Join(t.TempDir(), "file")
mustWriteFile(t, fileName, []byte("test file content"))
mustWriteFile(t, fileName+".enc", nil)
mustChmod(t, fileName+".enc", 0400)
err := (&encryptOptions{encryptFlags: encryptFlags{force: true}}).encryptFile(fileName, "asdf")
if err == nil {
t.Fatal("encryptFile succeeded for unwritable file, want error")
}
}
func TestEncryptOptions_RegisterFlags(t *testing.T) {
t.Parallel()
var o encryptOptions
fs := flag.NewFlagSet("test", flag.ContinueOnError)
o.registerFlags(fs)
const cmd = "-g -p asdf -f"
fs.Parse(strings.Split(cmd, " "))
want := encryptFlags{
generatePassword: true,
password: "asdf",
force: true,
}
if o.encryptFlags != want {
t.Errorf("Command line %q parsed incorrect encryptOptions, got %+v, want %+v", cmd, o, want)
}
}
func TestEncryptOptions_Run(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 := (&encryptOptions{encryptFlags: encryptFlags{password: password}}).run(fileName); err != nil {
t.Fatalf("enc failed: %s", err)
}
mustRemove(t, fileName)
if err := (&decryptOptions{}).decryptFile(fileName+".enc", password); 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 TestEncryptOptions_Run_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 := &encryptOptions{encryptFlags: encryptFlags{
generatePassword: tc.generatePassword,
password: tc.password,
}}
if err := opts.run(tc.files...); err == nil {
t.Errorf("encryptOptions.run(%+v) succeeded, want error", opts)
}
})
}
}
func TestEncryptOptions_Run_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 := &encryptOptions{
encryptFlags: encryptFlags{generatePassword: true},
passwordOut: password,
}
if err := opts.run(fileName); err != nil {
t.Fatalf("encryptOptions.run(%+v) failed: %s", opts, err)
}
pw := password.String()
mustRemove(t, fileName)
if err := (&decryptOptions{}).decryptFile(fileName+".enc", pw); 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 TestEncryptOptions_Run_Stdin(t *testing.T) {
t.Parallel()
const (
input = "test input"
password = "asdf"
)
stdout := new(strings.Builder)
if err := (&encryptOptions{
encryptFlags: encryptFlags{password: password},
stdin: strings.NewReader(input),
stdout: stdout,
}).run(); err != nil {
t.Errorf("encryptOptions.run failed: %s", err)
}
got := new(strings.Builder)
if err := (&decryptOptions{}).decrypt(got, strings.NewReader(stdout.String()), password); err != nil {
t.Errorf("Failed to decrypt stdout content %q: %s", stdout, err)
}
if got, want := got.String(), input; got != want {
t.Errorf("Encrypt round-trip to stdout returned incorrect contents: %q, want %q", got, want)
}
}
func TestEncryptOptions_Run_ReadPassword(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
desc string
passwords []string
err error
wantErr bool
}{{
desc: "Ok",
passwords: []string{"asdf"},
}, {
desc: "EmptyPassword",
passwords: []string{""},
wantErr: true,
}, {
desc: "PasswordsDoNotMatch",
passwords: []string{"asdf", "jkl"},
wantErr: true,
}, {
desc: "ReadPasswordErr",
err: errors.New("test error"),
wantErr: true,
}, {
desc: "RepeatPasswordErr",
passwords: []string{"asdf"},
err: errors.New("test error"),
wantErr: true,
}} {
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
fileName := filepath.Join(t.TempDir(), "file")
mustWriteFile(t, fileName, []byte("test file content"))
passwordI := 0
err := (&encryptOptions{
passwordIn: func() (string, error) {
if passwordI == len(tc.passwords) && tc.err != nil {
return "", tc.err
}
pw := tc.passwords[passwordI%len(tc.passwords)]
passwordI++
return pw, nil
},
}).run(fileName)
if gotErr := err != nil; gotErr != tc.wantErr {
t.Errorf("encryptOptions.run returned error %v, want error? %t", err, tc.wantErr)
}
})
}
}
|