-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
62 lines (56 loc) · 1.45 KB
/
main_test.go
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
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()
}
}
func TestCli(t *testing.T) {
assets := os.Getenv("TEST_ASSETS")
if assets == "" {
assets = "test-assets"
}
tempdir, err := ioutil.TempDir(TempRoot(), "testdir")
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(0o700)))
out := path.Join(tempdir, "out")
privKey := path.Join(assets, "id_rsa")
cmds := [][]string{
{"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},
}
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
ok(t, cmd.Run())
}
}