[WIP] Add Tests for Keyring Func #2

Draft
dm wants to merge 2 commits from dm/pm:keyring into master
2 changed files with 110 additions and 12 deletions

View File

@ -78,8 +78,8 @@ func NewKeyPair(root, name, email string) error {
// ListKeys prints keyring information to w. // ListKeys prints keyring information to w.
func ListKeys(root string, w io.Writer) error { func ListKeys(root string, w io.Writer) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
secs, pubs, err := getELs(srn, prn) secs, pubs, err := getELs(srn, prn)
@ -105,8 +105,8 @@ func ListKeys(root string, w io.Writer) error {
// Export prints pubkey information associated with email to w. // Export prints pubkey information associated with email to w.
func Export(root string, w io.Writer, email string) error { func Export(root string, w io.Writer, email string) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
_, pubs, err := getELs(srn, prn) _, pubs, err := getELs(srn, prn)
@ -142,8 +142,8 @@ func Import(root string, w io.Reader) error {
return errors.Wrap(err, "reading keyring") return errors.Wrap(err, "reading keyring")
} }
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
_, pubs, err := getELs(srn, prn) _, pubs, err := getELs(srn, prn)
@ -185,8 +185,8 @@ func Import(root string, w io.Reader) error {
// Sign takes an id and a reader and writes the signature for that id to sig. // Sign takes an id and a reader and writes the signature for that id to sig.
func Sign(root, id string, in io.Reader, sig io.Writer) error { func Sign(root, id string, in io.Reader, sig io.Writer) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
secs, _, err := getELs(srn, prn) secs, _, err := getELs(srn, prn)
@ -206,8 +206,8 @@ func Sign(root, id string, in io.Reader, sig io.Writer) error {
// Verify verifies a file's deatched signature. // Verify verifies a file's deatched signature.
func Verify(root string, file, sig io.Reader) error { func Verify(root string, file, sig io.Reader) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
_, pubs, err := getELs(srn, prn) _, pubs, err := getELs(srn, prn)
@ -225,8 +225,8 @@ func Verify(root string, file, sig io.Reader) error {
// It skips public keys that have matching secret keys, and does not effect // It skips public keys that have matching secret keys, and does not effect
// private keys. // private keys.
func Remove(root string, id string) error { func Remove(root string, id string) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
secs, pubs, err := getELs(srn, prn) secs, pubs, err := getELs(srn, prn)
@ -266,6 +266,14 @@ func pGPDir(root string) string {
return filepath.Join(root, "var", "lib", "pm", "pgp") return filepath.Join(root, "var", "lib", "pm", "pgp")
} }
func checkDir(root string) error {
d := pGPDir(root)
if !fs.Exists(d) {
return fmt.Errorf("pgp dir does not exist")
}
return nil
}
func ensureDir(root string) error { func ensureDir(root string) error {
d := pGPDir(root) d := pGPDir(root)
if !fs.Exists(d) { if !fs.Exists(d) {

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())
}
}
}
}