1
0
Fork 0
mirror of https://github.com/Mic92/sops-nix.git synced 2025-03-06 16:47:41 +00:00
sops-nix/pkgs/ssh-to-pgp/main_test.go

61 lines
1.5 KiB
Go
Raw Normal View History

2020-07-12 13:50:55 +01:00
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"testing"
)
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
func TempRoot() string {
if runtime.GOOS == "darwin" {
// macOS make its TEMPDIR long enough for unix socket to break
return "/tmp"
} else {
return os.TempDir()
}
}
2020-07-12 13:50:55 +01:00
func TestCli(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
assets := path.Join(path.Dir(filename), "test-assets")
tempdir, err := ioutil.TempDir(TempRoot(), "testdir")
2020-07-12 13:50:55 +01:00
ok(t, err)
defer os.RemoveAll(tempdir)
gpgHome := path.Join(tempdir, "gpg-home")
gpgEnv := append(os.Environ(), fmt.Sprintf("GNUPGHOME=%s", gpgHome))
ok(t, os.Mkdir(gpgHome, os.FileMode(0700)))
2020-07-12 13:50:55 +01:00
out := path.Join(tempdir, "out")
privKey := path.Join(assets, "id_rsa")
cmds := [][]string{
2020-07-13 09:12:47 +01:00
{"ssh-to-pgp", "-i", privKey, "-o", out},
{"ssh-to-pgp", "-format=binary", "-i", privKey, "-o", out},
{"ssh-to-pgp", "-private-key", "-i", privKey, "-o", out},
{"ssh-to-pgp", "-format=binary", "-private-key", "-i", privKey, "-o", out},
2020-07-12 13:50:55 +01:00
}
for _, cmd := range cmds {
err = convertKeys(cmd)
ok(t, err)
cmd := exec.Command("gpg", "--with-fingerprint", "--show-key", out)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = gpgEnv
2020-07-12 13:50:55 +01:00
ok(t, cmd.Run())
}
}