lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 21c629f4eb401b0c8103176b6ff7d1567a2fc5c1
parent 6d0bab6b9ba60c2e942e088b6dcf2b470dd5f033
Author: Sean Enck <sean@ttypty.com>
Date:   Mon,  3 Oct 2022 18:35:19 -0400

more tests

Diffstat:
Minternal/backend/core.go | 2+-
Minternal/backend/query_test.go | 7+++++++
Minternal/inputs/env.go | 22+++++++++-------------
Minternal/inputs/env_test.go | 83++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
4 files changed, 68 insertions(+), 46 deletions(-)

diff --git a/internal/backend/core.go b/internal/backend/core.go @@ -22,7 +22,7 @@ func loadFile(file string, must bool) (*Transaction, error) { exists := pathExists(file) if must { if !exists { - return nil, errors.New("invalid file, does not exists") + return nil, errors.New("invalid file, does not exist") } } ro, err := inputs.IsReadOnly() diff --git a/internal/backend/query_test.go b/internal/backend/query_test.go @@ -118,3 +118,10 @@ func TestEntityDir(t *testing.T) { t.Error("invalid query directory") } } + +func TestNewPath(t *testing.T) { + p := backend.NewPath("abc", "xyz") + if p != filepath.Join("abc", "xyz") { + t.Error("invalid new path") + } +} diff --git a/internal/inputs/env.go b/internal/inputs/env.go @@ -22,10 +22,10 @@ const ( readOnlyEnv = prefixKey + "READONLY" fieldTOTPEnv = prefixKey + "TOTP" formatTOTPEnv = fieldTOTPEnv + "_FORMAT" - // KeyModeEnv indicates what the KEY value is (e.g. command, plaintext). - KeyModeEnv = prefixKey + "KEYMODE" - // KeyEnv is the key value used by the lockbox store. - KeyEnv = prefixKey + "KEY" + keyModeEnv = prefixKey + "KEYMODE" + keyEnv = prefixKey + "KEY" + plainKeyMode = "plaintext" + commandKeyMode = "command" // PlatformEnv is the platform lb is running on. PlatformEnv = prefixKey + "PLATFORM" // StoreEnv is the location of the filesystem store that lb is operating on. @@ -34,10 +34,6 @@ const ( ClipMaxEnv = prefixKey + "CLIPMAX" // ColorBetweenEnv is a comma-delimited list of times to color totp outputs (e.g. 0:5,30:35 which is the default). ColorBetweenEnv = fieldTOTPEnv + "_BETWEEN" - // PlainKeyMode is plaintext based key resolution. - PlainKeyMode = "plaintext" - // CommandKeyMode will run an external command to get the key (from stdout). - CommandKeyMode = "command" ) // EnvOrDefault will get the environment value OR default if env is not set. @@ -51,11 +47,11 @@ func EnvOrDefault(envKey, defaultValue string) string { // GetKey will get the encryption key setup for lb func GetKey() ([]byte, error) { - useKeyMode := os.Getenv(KeyModeEnv) + useKeyMode := os.Getenv(keyModeEnv) if useKeyMode == "" { - useKeyMode = CommandKeyMode + useKeyMode = commandKeyMode } - useKey := os.Getenv(KeyEnv) + useKey := os.Getenv(keyEnv) if useKey == "" { return nil, errors.New("no key given") } @@ -72,7 +68,7 @@ func GetKey() ([]byte, error) { func getKey(keyMode, name string) ([]byte, error) { var data []byte switch keyMode { - case CommandKeyMode: + case commandKeyMode: parts, err := shlex.Split(name) if err != nil { return nil, err @@ -83,7 +79,7 @@ func getKey(keyMode, name string) ([]byte, error) { return nil, err } data = b - case PlainKeyMode: + case plainKeyMode: data = []byte(name) default: return nil, errors.New("unknown keymode") diff --git a/internal/inputs/env_test.go b/internal/inputs/env_test.go @@ -1,6 +1,7 @@ package inputs_test import ( + "fmt" "os" "testing" @@ -28,66 +29,84 @@ func TestFormatTOTP(t *testing.T) { } } -func TestColorSetting(t *testing.T) { - os.Setenv("LOCKBOX_NOCOLOR", "yes") - c, err := inputs.IsNoColorEnabled() +func checkYesNo(key string, t *testing.T, cb func() (bool, error), onEmpty bool) { + os.Setenv(key, "yes") + c, err := cb() if err != nil { t.Errorf("invalid error: %v", err) } if !c { t.Error("invalid setting") } - os.Setenv("LOCKBOX_NOCOLOR", "") - c, err = inputs.IsNoColorEnabled() + os.Setenv(key, "") + c, err = cb() if err != nil { t.Errorf("invalid error: %v", err) } - if c { + if c != onEmpty { t.Error("invalid setting") } - os.Setenv("LOCKBOX_NOCOLOR", "no") - c, err = inputs.IsNoColorEnabled() + os.Setenv(key, "no") + c, err = cb() if err != nil { t.Errorf("invalid error: %v", err) } if c { t.Error("invalid setting") } - os.Setenv("LOCKBOX_NOCOLOR", "lkaj;f") - _, err = inputs.IsNoColorEnabled() - if err == nil || err.Error() != "invalid yes/no env value for LOCKBOX_NOCOLOR" { + os.Setenv(key, "lkaj;f") + _, err = cb() + if err == nil || err.Error() != fmt.Sprintf("invalid yes/no env value for %s", key) { t.Errorf("unexpected error: %v", err) } } +func TestColorSetting(t *testing.T) { + checkYesNo("LOCKBOX_NOCOLOR", t, inputs.IsNoColorEnabled, false) +} + func TestInteractiveSetting(t *testing.T) { - os.Setenv("LOCKBOX_INTERACTIVE", "yes") - c, err := inputs.IsInteractive() - if err != nil { - t.Errorf("invalid error: %v", err) + checkYesNo("LOCKBOX_INTERACTIVE", t, inputs.IsInteractive, true) +} + +func TestIsReadOnly(t *testing.T) { + checkYesNo("LOCKBOX_READONLY", t, inputs.IsReadOnly, false) +} + +func TestIsNoClip(t *testing.T) { + checkYesNo("LOCKBOX_NOCLIP", t, inputs.IsNoClipEnabled, false) +} + +func TestTOTP(t *testing.T) { + os.Setenv("LOCKBOX_TOTP", "abc") + if inputs.TOTPToken() != "abc" { + t.Error("invalid totp token field") } - if !c { - t.Error("invalid setting") + os.Setenv("LOCKBOX_TOTP", "") + if inputs.TOTPToken() != "totp" { + t.Error("invalid totp token field") } - os.Setenv("LOCKBOX_INTERACTIVE", "no") - c, err = inputs.IsInteractive() - if err != nil { +} + +func TestGetKey(t *testing.T) { + os.Setenv("LOCKBOX_KEY", "aaa") + os.Setenv("LOCKBOX_KEYMODE", "lak;jfea") + if _, err := inputs.GetKey(); err.Error() != "unknown keymode" { t.Errorf("invalid error: %v", err) } - if c { - t.Error("invalid setting") - } - os.Setenv("LOCKBOX_INTERACTIVE", "") - c, err = inputs.IsInteractive() - if err != nil { + os.Setenv("LOCKBOX_KEYMODE", "plaintext") + os.Setenv("LOCKBOX_KEY", "") + if _, err := inputs.GetKey(); err.Error() != "no key given" { t.Errorf("invalid error: %v", err) } - if !c { - t.Error("invalid setting") + os.Setenv("LOCKBOX_KEY", "key") + k, err := inputs.GetKey() + if err != nil || string(k) != "key" { + t.Error("invalid key retrieval") } - os.Setenv("LOCKBOX_INTERACTIVE", "yaojia") - _, err = inputs.IsInteractive() - if err == nil || err.Error() != "invalid yes/no env value for LOCKBOX_INTERACTIVE" { - t.Errorf("unexpected error: %v", err) + os.Setenv("LOCKBOX_KEYMODE", "command") + os.Setenv("LOCKBOX_KEY", "invalid command text is long and invalid via shlex") + if _, err := inputs.GetKey(); err == nil { + t.Error("should have failed") } }