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
|
package main
import (
"bytes"
"context"
"flag"
"os"
"path/filepath"
"testing"
"github.com/google/subcommands"
)
func init() {
argon2Memory = 1
}
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 mustRename(t *testing.T, src, dst string) {
t.Helper()
if err := os.Rename(src, dst); err != nil {
t.Fatalf("Failed to rename: %s", err)
}
}
func mustRemove(t *testing.T, path string) {
t.Helper()
if err := os.Remove(path); err != nil {
t.Fatalf("Failed to remove file: %s", err)
}
}
func mustChmod(t *testing.T, path string, mod os.FileMode) {
t.Helper()
if err := os.Chmod(path, mod); err != nil {
t.Fatalf("Failed to chmod: %s", err)
}
}
func TestEncryptDecrypt(t *testing.T) {
t.Parallel()
buf := make([]byte, 12*1024*1024)
for i := range buf {
buf[i] = byte(i)
}
fileName := filepath.Join(t.TempDir(), "file")
mustWriteFile(t, fileName, buf)
const password = "karp cache tidal mars fed rajah uses graze pobox flew"
if err := (&encCmd{}).encryptFile(fileName, password); err != nil {
t.Fatalf("EncryptFile failed: %s", err)
}
mustRemove(t, fileName)
if err := (&decCmd{}).decryptFile(fileName+".enc", password); err != nil {
t.Fatalf("DecryptFile failed: %s", err)
}
gotContents := mustReadFile(t, fileName)
if !bytes.Equal(gotContents, buf) {
t.Errorf("contents differ")
}
}
func run(ctx context.Context, t *testing.T, cmd ...string) subcommands.ExitStatus {
t.Helper()
fs := flag.NewFlagSet("test", flag.ContinueOnError)
commander := subcommands.NewCommander(fs, "test")
registerCommands(commander, nil, nil, nil, nil)
if err := fs.Parse(cmd); err != nil {
t.Fatalf("Failed to parse command %q: %s", cmd, err)
}
return commander.Execute(ctx)
}
func TestCommander(t *testing.T) {
t.Parallel()
ctx := t.Context()
fileName := filepath.Join(t.TempDir(), "file.txt")
const fileContent = "test file content"
mustWriteFile(t, fileName, []byte(fileContent))
const password = "asdf"
if st := run(ctx, t, "enc", "-p="+password, fileName); st != subcommands.ExitSuccess {
t.Fatalf("enc failed: status %d", st)
}
mustRemove(t, fileName)
if st := run(ctx, t, "dec", "-p="+password, fileName+".enc"); st != subcommands.ExitSuccess {
t.Fatalf("dec failed: status %d", st)
}
gotContents := mustReadFile(t, fileName)
if !bytes.Equal(gotContents, []byte(fileContent)) {
t.Errorf("dec returned invalid content, got %q, want %q", gotContents, fileContent)
}
}
func TestCommander_Errors(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
desc string
cmd []string
wantStatus subcommands.ExitStatus
}{{
desc: "EncUsageError",
cmd: []string{"enc", "-g", "-p=asdf", "file.txt"},
wantStatus: subcommands.ExitUsageError,
}, {
desc: "EncNoSuchFile",
cmd: []string{"enc", "-p=asdf", "nonexistent-file.txt"},
wantStatus: subcommands.ExitFailure,
}, {
desc: "DecUsageError",
cmd: []string{"dec"},
wantStatus: subcommands.ExitUsageError,
}, {
desc: "DecNoSuchFile",
cmd: []string{"dec", "-p=asdf", "nonexistent-file.txt.enc"},
wantStatus: subcommands.ExitFailure,
}} {
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
st := run(ctx, t, tc.cmd...)
if st != tc.wantStatus {
t.Errorf("command %q returned status %d, want %d", tc.cmd, st, tc.wantStatus)
}
})
}
}
func TestUsage(t *testing.T) {
t.Parallel()
ctx := t.Context()
run(ctx, t, "help")
run(ctx, t, "enc", "-h")
run(ctx, t, "dec", "-h")
}
|