tests: added NewKeyPair and ListKeys

Signed-off-by: Derek McQuay <derekmcquay@gmail.com>
This commit is contained in:
Derek McQuay 2018-02-27 20:10:59 -08:00
parent c4d67105f2
commit efbe79e786
No known key found for this signature in database
GPG Key ID: 50CA472F40A5AFED
1 changed files with 90 additions and 0 deletions

90
keyring/keyring_test.go Normal file
View File

@ -0,0 +1,90 @@
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())
}
}
}
}