Skip to content

Commit 2cdbbd7

Browse files
committed
Add tests for symmetric GPG encryption
1 parent 4d3d6da commit 2cdbbd7

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

main_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,10 @@ func cmdMkGPGConfig(ts *testscript.TestScript, neg bool, args []string) {
233233
if neg {
234234
ts.Fatalf("unupported: ! mkgpgconfig")
235235
}
236-
if len(args) > 0 {
237-
ts.Fatalf("usage: mkgpgconfig")
236+
if len(args) > 1 || len(args) == 1 && args[0] != "-symmetric" {
237+
ts.Fatalf("usage: mkgpgconfig [-symmetric]")
238238
}
239+
symmetric := len(args) == 1 && args[0] == "-symmetric"
239240

240241
// Create a new directory for GPG. We can't use a subdirectory of the
241242
// testscript's working directory because on darwin the absolute path can
@@ -260,17 +261,22 @@ func cmdMkGPGConfig(ts *testscript.TestScript, neg bool, args []string) {
260261

261262
configFile := filepath.Join(ts.Getenv("HOME"), ".config", "chezmoi", "chezmoi.toml")
262263
ts.Check(os.MkdirAll(filepath.Dir(configFile), 0o777))
263-
ts.Check(os.WriteFile(configFile, []byte(fmt.Sprintf(chezmoitest.JoinLines(
264+
lines := []string{
264265
`encryption = "gpg"`,
265266
`[gpg]`,
266267
` args = [`,
267-
` "--homedir", %q,`,
268+
` "--homedir", ` + quote(gpgHomeDir) + `,`,
268269
` "--no-tty",`,
269-
` "--passphrase", %q,`,
270+
` "--passphrase", ` + quote(passphrase) + `,`,
270271
` "--pinentry-mode", "loopback",`,
271272
` ]`,
272-
` recipient = %q`,
273-
), gpgHomeDir, passphrase, key)), 0o666))
273+
}
274+
if symmetric {
275+
lines = append(lines, ` symmetric = true`)
276+
} else {
277+
lines = append(lines, ` recipient = "`+key+`"`)
278+
}
279+
ts.Check(os.WriteFile(configFile, []byte(chezmoitest.JoinLines(lines...)), 0o666))
274280
}
275281

276282
// cmdMkHomeDir makes and populates a home directory.
@@ -430,6 +436,10 @@ func prependDirToPath(dir, path string) string {
430436
return strings.Join(append([]string{dir}, filepath.SplitList(path)...), string(os.PathListSeparator))
431437
}
432438

439+
func quote(s string) string {
440+
return fmt.Sprintf("%q", s)
441+
}
442+
433443
func setup(env *testscript.Env) error {
434444
var (
435445
binDir = filepath.Join(env.WorkDir, "bin")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[!exec:gpg] stop
2+
3+
mkhomedir
4+
mkgpgconfig -symmetric
5+
6+
# test that chezmoi add --encrypt encrypts
7+
cp golden/.encrypted $HOME
8+
chezmoi add --encrypt $HOME${/}.encrypted
9+
exists $CHEZMOISOURCEDIR/encrypted_dot_encrypted.asc
10+
! grep plaintext $CHEZMOISOURCEDIR/encrypted_dot_encrypted.asc
11+
12+
# test that chezmoi apply decrypts
13+
rm $HOME/.encrypted
14+
chezmoi apply --force
15+
cmp golden/.encrypted $HOME/.encrypted
16+
17+
# test that chezmoi apply --exclude=encrypted does not apply encrypted files
18+
rm $HOME/.encrypted
19+
chezmoi apply --exclude=encrypted --force
20+
! exists $HOME/.encrypted
21+
chezmoi apply --force
22+
cmp $HOME/.encrypted golden/.encrypted
23+
24+
# test that chezmoi detects gpg encryption if gpg is configured but encryption = "gpg" is not set
25+
removeline $CHEZMOICONFIGDIR/chezmoi.toml 'encryption = "gpg"'
26+
chezmoi cat $HOME${/}.encrypted
27+
cmp stdout golden/.encrypted
28+
29+
# test that chezmoi edit --apply transparently decrypts and re-encrypts
30+
chezmoi edit --apply --force $HOME${/}.encrypted
31+
grep '# edited' $HOME/.encrypted
32+
33+
# test that chezmoi files in subdirectories can be encrypted and that suffix can be set
34+
appendline $CHEZMOICONFIGDIR/chezmoi.toml ' suffix = ".gpg"'
35+
mkdir $HOME/.dir
36+
cp golden/.encrypted $HOME/.dir
37+
chezmoi add --encrypt $HOME${/}.dir${/}.encrypted
38+
! grep plaintext $CHEZMOISOURCEDIR/dot_dir/encrypted_dot_encrypted.gpg
39+
chezmoi edit --apply $HOME${/}.dir${/}.encrypted
40+
grep '# edited' $HOME/.dir/.encrypted
41+
42+
-- golden/.encrypted --
43+
plaintext

0 commit comments

Comments
 (0)