aboutsummaryrefslogtreecommitdiff
path: root/b64file
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-04 23:41:08 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-04 23:41:08 +0200
commit245a41f1fa992fefb396fc4591a2bbdd6858e525 (patch)
tree7f5d0ca929f2af891205f49f918d2222f0c8a7ce /b64file
parent8084465e13ee600c733b709059dfbaa753603674 (diff)
Split names
Diffstat (limited to 'b64file')
-rw-r--r--b64file/file.go9
-rw-r--r--b64file/names.go72
-rw-r--r--b64file/names_test.go65
3 files changed, 2 insertions, 144 deletions
diff --git a/b64file/file.go b/b64file/file.go
index ebca8fe..9254303 100644
--- a/b64file/file.go
+++ b/b64file/file.go
@@ -1,4 +1,4 @@
-// Package file implements signify file format
+// Package b64file implements signify file format
package b64file
import (
@@ -14,12 +14,7 @@ import (
"strings"
)
-const (
- ModeSec os.FileMode = 0600
- ModePub os.FileMode = 0644
- ModeSig os.FileMode = 0644
- untrusted = "untrusted comment: "
-)
+const untrusted = "untrusted comment: "
// Original Error: "invalid comment in %s; must start with 'untrusted comment: '"
var ErrUntrusted = errors.New("comment must start with 'untrusted comment: '")
diff --git a/b64file/names.go b/b64file/names.go
deleted file mode 100644
index 83d73d2..0000000
--- a/b64file/names.go
+++ /dev/null
@@ -1,72 +0,0 @@
-package b64file
-
-import (
- "errors"
- "os"
- "path"
- "path/filepath"
- "strings"
-)
-
-var ErrNames = errors.New("please use naming scheme of keyname.pub and keyname.sec")
-
-const (
- extPub = ".pub"
- extSec = ".sec"
- extSig = ".sig"
- verifyWith = "verify with "
-)
-
-func splitNameExt(fname string) (string, string) {
- _, file := path.Split(fname)
- ext := filepath.Ext(file)
- return file[:len(file)-len(ext)], ext
-}
-
-func ValidateNames(pubFile, secFile string) error {
- pubName, pubExt := splitNameExt(pubFile)
- secName, secExt := splitNameExt(secFile)
- if pubExt != extPub || secExt != extSec || pubName != secName {
- return ErrNames
- }
- return nil
-}
-
-func PubName(secFile string) string {
- ext := filepath.Ext(secFile)
- return filepath.Base(secFile[:len(ext)-1] + extPub)
-}
-
-func SigName(msgFile string) string {
- return msgFile + extSig
-}
-
-func PubFile(comment string) string {
- if strings.HasPrefix(comment, verifyWith) {
- file := comment[len(verifyWith):]
- if strings.HasSuffix(file, extPub) {
- return FindFile(file)
- }
- }
- return ""
-}
-
-func VerifyWith(secFile string) string {
- return verifyWith + PubName(secFile)
-}
-
-var safePath = []string{
- "/etc/signify",
- "$HOME/.signify",
-}
-
-// FindFile locates keys in safe path. Falls back to current dir.
-func FindFile(fname string) string {
- for _, v := range safePath {
- p := path.Join(os.Expand(v, os.Getenv), fname)
- if _, err := os.Stat(p); err == nil {
- return p
- }
- }
- return fname
-}
diff --git a/b64file/names_test.go b/b64file/names_test.go
deleted file mode 100644
index 0acabed..0000000
--- a/b64file/names_test.go
+++ /dev/null
@@ -1,65 +0,0 @@
-package b64file
-
-import "testing"
-
-func TestCheckNames(t *testing.T) {
- testCases := []struct {
- pub, sec string
- err error
- }{
- {"key.pub", "key.sec", nil},
- {"testdata/key.pub", "key.sec", nil},
- {"key.pub", "testdata/key.sec", nil},
- {"foo.pub", "bar.sec", ErrNames},
- {"key.foo", "key.bar", ErrNames},
- }
- for _, tc := range testCases {
- t.Run(tc.pub+"+"+tc.sec, func(t *testing.T) {
- err := ValidateNames(tc.pub, tc.sec)
- if err != tc.err {
- t.Errorf("got %v, want %v", err, tc.err)
- }
- })
- }
-}
-
-func TestVerify(t *testing.T) {
- testCases := []struct {
- comment string
- file string
- }{
- {"verify with key.pub", "key.pub"},
- {"verify with s p a c e s.pub", "s p a c e s.pub"},
- {"verify with key.sec", ""},
- {"whatever", ""},
- }
- for _, tc := range testCases {
- t.Run(tc.comment, func(t *testing.T) {
- file := PubFile(tc.comment)
- if file != tc.file {
- t.Errorf("got %v, want %v", file, tc.file)
- }
- })
- }
-}
-
-func TestSplit(t *testing.T) {
- testCases := []struct {
- fname, name, ext string
- }{
- {"testkey.pub", "testkey", ".pub"},
- {"testkey", "testkey", ""},
- {".pub", "", ".pub"},
- {".testkey.pub", ".testkey", ".pub"},
- {"", "", ""},
- {"path/key.pub", "key", ".pub"},
- }
- for _, tc := range testCases {
- t.Run(tc.fname, func(t *testing.T) {
- name, ext := splitNameExt(tc.fname)
- if name != tc.name || ext != tc.ext {
- t.Errorf("got %q %q, want %q %q", name, tc.name, ext, tc.ext)
- }
- })
- }
-}