From 6d9c53c8f8c9561c29c0ce8bce207234d2524fd1 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 20 Apr 2017 00:19:35 +0200 Subject: Naming Scheme --- cmd/signify/main.go | 17 +++++++++++++++++ cmd/signify/main_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 cmd/signify/main_test.go (limited to 'cmd') diff --git a/cmd/signify/main.go b/cmd/signify/main.go index 9cdb6cd..58706ee 100644 --- a/cmd/signify/main.go +++ b/cmd/signify/main.go @@ -1,11 +1,13 @@ package main import ( + "errors" "flag" "fmt" "io/ioutil" "log" "path" + "path/filepath" "dim13.org/signify" ) @@ -35,6 +37,10 @@ var ( gzip = flag.Bool("z", false, "Sign and verify gzip archives") ) +var ( + ErrNamingScheme = errors.New("please use naming scheme of keyname.pub and keyname.sec") +) + func main() { flag.Parse() @@ -61,6 +67,9 @@ func main() { } func Generate(pubFile, secFile, comment string, rounds int) error { + if !NamingScheme(pubFile, secFile) { + return ErrNamingScheme + } pubKey, encKey, err := signify.NewKey() if err != nil { return err @@ -203,3 +212,11 @@ func Verify(msgFile, pubFile string) error { log.Println("Signature Verfied") return nil } + +func NamingScheme(pubFile, secFile string) bool { + pubFile = filepath.Base(pubFile) + secFile = filepath.Base(secFile) + pubExt := filepath.Ext(pubFile) + secExt := filepath.Ext(secFile) + return pubExt == ".pub" && secExt == ".sec" && pubFile[:len(pubExt)] == secFile[:len(secExt)] +} diff --git a/cmd/signify/main_test.go b/cmd/signify/main_test.go new file mode 100644 index 0000000..1e8d0e5 --- /dev/null +++ b/cmd/signify/main_test.go @@ -0,0 +1,24 @@ +package main + +import "testing" + +func TestNamingScheme(t *testing.T) { + testCases := []struct { + pub, sec string + ok bool + }{ + {"key.pub", "key.sec", true}, + {"testdata/key.pub", "key.sec", true}, + {"key.pub", "testdata/key.sec", true}, + {"foo.pub", "bar.sec", false}, + {"key.foo", "key.bar", false}, + } + for _, tc := range testCases { + t.Run(tc.pub+"+"+tc.sec, func(t *testing.T) { + ok := NamingScheme(tc.pub, tc.sec) + if ok != tc.ok { + t.Errorf("got %v, want %v", ok, tc.ok) + } + }) + } +} -- cgit v1.2.3