91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package keyring
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
ExpectedListKeysRegex = "sec: [[:alnum:]]{8}:[[:space:]]*foo[[:space:]]*\\(pm\\)[[:space:]]*<bar>\npub: [[:alnum:]]{8}:[[:space:]]*foo[[:space:]]*\\(pm\\)[[:space:]]*<bar>"
|
|
)
|
|
|
|
func TestNewKeyPair(t *testing.T) {
|
|
tmpdir, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
t.Fatalf("Couldn't create tmpdir")
|
|
}
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
var newKeyPair = []struct {
|
|
r, n, e string
|
|
expected bool
|
|
}{
|
|
{"", "", "", false},
|
|
{"", "foo", "", false},
|
|
{"", "foo", "bad<>email", false},
|
|
{tmpdir, "foo", "bar", true},
|
|
}
|
|
for _, rt := range newKeyPair {
|
|
actual := NewKeyPair(rt.r, rt.n, rt.e)
|
|
if (actual == nil) != rt.expected {
|
|
t.Errorf(
|
|
"failed NewKeyPair with an error: %v\n\troot: %s\n\tname: %s\n\temail:%s\n\texpected: %t\n\t actual: %t",
|
|
actual,
|
|
rt.r,
|
|
rt.n,
|
|
rt.e,
|
|
(actual == nil),
|
|
rt.expected,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestListKeys(t *testing.T) {
|
|
tmpdir, err := ioutil.TempDir("", "")
|
|
if err != nil {
|
|
t.Fatalf("Couldn't create tmpdir")
|
|
}
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
err = NewKeyPair(tmpdir, "foo", "bar")
|
|
if err != nil {
|
|
t.Fatalf("Couldn't create New Key Pair")
|
|
}
|
|
|
|
var newKeyPair = []struct {
|
|
r string
|
|
regex string
|
|
w *bytes.Buffer
|
|
expected bool
|
|
}{
|
|
{"", "", bytes.NewBuffer(nil), false},
|
|
{tmpdir, ExpectedListKeysRegex, bytes.NewBuffer(nil), true},
|
|
}
|
|
for _, rt := range newKeyPair {
|
|
actual := ListKeys(rt.r, rt.w)
|
|
if (actual == nil) != rt.expected {
|
|
t.Errorf(
|
|
"failed ListKeys with an error: %v\n\troot: %s\n\texpected: %t\n\t actual: %t",
|
|
actual,
|
|
rt.r,
|
|
(actual == nil),
|
|
rt.expected,
|
|
)
|
|
}
|
|
if rt.expected {
|
|
matched, err := regexp.MatchString(rt.regex, rt.w.String())
|
|
if err != nil {
|
|
t.Fatalf("error%v, ", err)
|
|
}
|
|
if !matched {
|
|
t.Errorf("did not match")
|
|
t.Errorf("ListKeys did not match expected regex; wanted: [%q], got: [%s]", rt.regex, rt.w.String())
|
|
}
|
|
}
|
|
}
|
|
}
|