lockbox

password manager
Log | Files | Refs | README | LICENSE

commit b0d711c34c369c25a498a00ce2498f2127a61af7
parent 327dc4cbe24803801b066cfe57352eb020978fb0
Author: Sean Enck <sean@ttypty.com>
Date:   Tue, 13 Sep 2022 19:41:23 -0400

tests should be in test packages

Diffstat:
Minternal/cli/args_test.go | 18++++++++++--------
Minternal/colors/terminal_test.go | 18++++++++++--------
Minternal/encrypt/core_test.go | 17+++++++++--------
Minternal/inputs/env_test.go | 20+++++++++++---------
Minternal/platform/clipboard_test.go | 32+++++++++++++++++---------------
Minternal/platform/core_test.go | 10++++++----
Minternal/store/filesystem_test.go | 36++++++++++++++++++++++--------------
7 files changed, 85 insertions(+), 66 deletions(-)

diff --git a/internal/cli/args_test.go b/internal/cli/args_test.go @@ -1,50 +1,52 @@ -package cli +package cli_test import ( "testing" + + "github.com/enckse/lockbox/internal/cli" ) func TestClipArg(t *testing.T) { - options := ParseArgs("-clip") + options := cli.ParseArgs("-clip") if !options.Clip { t.Error("clip should be set") } } func TestMultiArg(t *testing.T) { - options := ParseArgs("-multi") + options := cli.ParseArgs("-multi") if !options.Multi { t.Error("multi should be set") } } func TestListArg(t *testing.T) { - options := ParseArgs("-list") + options := cli.ParseArgs("-list") if !options.List { t.Error("list should be set") } } func TestOnce(t *testing.T) { - if options := ParseArgs("-once"); !options.Once { + if options := cli.ParseArgs("-once"); !options.Once { t.Error("once should be set") } } func TestShort(t *testing.T) { - if options := ParseArgs("-short"); !options.Short { + if options := cli.ParseArgs("-short"); !options.Short { t.Error("short should be set") } } func TestYes(t *testing.T) { - if options := ParseArgs("-yes"); !options.Yes { + if options := cli.ParseArgs("-yes"); !options.Yes { t.Error("yes should be set") } } func TestDefaults(t *testing.T) { - args := ParseArgs("this/is/a/test") + args := cli.ParseArgs("this/is/a/test") if args.Clip || args.Once || args.Short || args.List || args.Multi || args.Yes { t.Error("defaults should all be false") } diff --git a/internal/colors/terminal_test.go b/internal/colors/terminal_test.go @@ -1,24 +1,26 @@ -package colors +package colors_test import ( "os" "testing" + + "github.com/enckse/lockbox/internal/colors" ) func TestHasColoring(t *testing.T) { os.Setenv("LOCKBOX_INTERACTIVE", "yes") os.Setenv("LOCKBOX_NOCOLOR", "no") - term, err := NewTerminal(Red) + term, err := colors.NewTerminal(colors.Red) if err != nil { t.Errorf("color was valid: %v", err) } - if term.Start != termBeginRed || term.End != termEndRed { + if term.Start != "\033[1;31m" || term.End != "\033[0m" { t.Error("bad resulting color") } } func TestBadColor(t *testing.T) { - _, err := NewTerminal(Color(5)) + _, err := colors.NewTerminal(colors.Color(5)) if err == nil || err.Error() != "bad color" { t.Errorf("invalid color error: %v", err) } @@ -27,7 +29,7 @@ func TestBadColor(t *testing.T) { func TestNoColoring(t *testing.T) { os.Setenv("LOCKBOX_INTERACTIVE", "no") os.Setenv("LOCKBOX_NOCOLOR", "yes") - term, err := NewTerminal(Red) + term, err := colors.NewTerminal(colors.Red) if err != nil { t.Errorf("color was valid: %v", err) } @@ -36,7 +38,7 @@ func TestNoColoring(t *testing.T) { } os.Setenv("LOCKBOX_INTERACTIVE", "yes") os.Setenv("LOCKBOX_NOCOLOR", "yes") - term, err = NewTerminal(Red) + term, err = colors.NewTerminal(colors.Red) if err != nil { t.Errorf("color was valid: %v", err) } @@ -45,7 +47,7 @@ func TestNoColoring(t *testing.T) { } os.Setenv("LOCKBOX_INTERACTIVE", "no") os.Setenv("LOCKBOX_NOCOLOR", "no") - term, err = NewTerminal(Red) + term, err = colors.NewTerminal(colors.Red) if err != nil { t.Errorf("color was valid: %v", err) } @@ -54,7 +56,7 @@ func TestNoColoring(t *testing.T) { } os.Setenv("LOCKBOX_INTERACTIVE", "yes") os.Setenv("LOCKBOX_NOCOLOR", "no") - term, err = NewTerminal(Red) + term, err = colors.NewTerminal(colors.Red) if err != nil { t.Errorf("color was valid: %v", err) } diff --git a/internal/encrypt/core_test.go b/internal/encrypt/core_test.go @@ -1,4 +1,4 @@ -package encrypt +package encrypt_test import ( "fmt" @@ -6,6 +6,7 @@ import ( "path/filepath" "testing" + "github.com/enckse/lockbox/internal/encrypt" "github.com/enckse/lockbox/internal/store" ) @@ -25,7 +26,7 @@ func setupData(t *testing.T) string { } func TestEncryptDecryptCommand(t *testing.T) { - e, err := NewLockbox(LockboxOptions{Key: "echo test", KeyMode: CommandKeyMode, File: setupData(t)}) + e, err := encrypt.NewLockbox(encrypt.LockboxOptions{Key: "echo test", KeyMode: encrypt.CommandKeyMode, File: setupData(t)}) if err != nil { t.Errorf("failed to create lockbox: %v", err) } @@ -44,11 +45,11 @@ func TestEncryptDecryptCommand(t *testing.T) { func TestEmptyKey(t *testing.T) { setupData(t) - _, err := NewLockbox(LockboxOptions{}) + _, err := encrypt.NewLockbox(encrypt.LockboxOptions{}) if err == nil || err.Error() != "no key given" { t.Errorf("invalid error: %v", err) } - _, err = NewLockbox(LockboxOptions{KeyMode: CommandKeyMode, Key: "echo"}) + _, err = encrypt.NewLockbox(encrypt.LockboxOptions{KeyMode: encrypt.CommandKeyMode, Key: "echo"}) if err == nil || err.Error() != "key is empty" { t.Errorf("invalid error: %v", err) } @@ -56,9 +57,9 @@ func TestEmptyKey(t *testing.T) { func TestKeyLength(t *testing.T) { val := "" - for i := 0; i < keyLength+10; i++ { + for i := 0; i < 42; i++ { val = fmt.Sprintf("a%s", val) - _, err := NewLockbox(LockboxOptions{KeyMode: PlainKeyMode, Key: val}) + _, err := encrypt.NewLockbox(encrypt.LockboxOptions{KeyMode: encrypt.PlainKeyMode, Key: val}) if err != nil { t.Error("no error expected") } @@ -66,14 +67,14 @@ func TestKeyLength(t *testing.T) { } func TestUnknownMode(t *testing.T) { - _, err := NewLockbox(LockboxOptions{KeyMode: "aaa", Key: "echo"}) + _, err := encrypt.NewLockbox(encrypt.LockboxOptions{KeyMode: "aaa", Key: "echo"}) if err == nil || err.Error() != "unknown keymode" { t.Errorf("invalid error: %v", err) } } func TestEncryptDecryptPlainText(t *testing.T) { - e, err := NewLockbox(LockboxOptions{Key: "plain", KeyMode: PlainKeyMode, File: setupData(t)}) + e, err := encrypt.NewLockbox(encrypt.LockboxOptions{Key: "plain", KeyMode: encrypt.PlainKeyMode, File: setupData(t)}) if err != nil { t.Errorf("failed to create lockbox: %v", err) } diff --git a/internal/inputs/env_test.go b/internal/inputs/env_test.go @@ -1,13 +1,15 @@ -package inputs +package inputs_test import ( "os" "testing" + + "github.com/enckse/lockbox/internal/inputs" ) func TestColorSetting(t *testing.T) { os.Setenv("LOCKBOX_NOCOLOR", "yes") - c, err := IsNoColorEnabled() + c, err := inputs.IsNoColorEnabled() if err != nil { t.Errorf("invalid error: %v", err) } @@ -15,7 +17,7 @@ func TestColorSetting(t *testing.T) { t.Error("invalid setting") } os.Setenv("LOCKBOX_NOCOLOR", "") - c, err = IsNoColorEnabled() + c, err = inputs.IsNoColorEnabled() if err != nil { t.Errorf("invalid error: %v", err) } @@ -23,7 +25,7 @@ func TestColorSetting(t *testing.T) { t.Error("invalid setting") } os.Setenv("LOCKBOX_NOCOLOR", "no") - c, err = IsNoColorEnabled() + c, err = inputs.IsNoColorEnabled() if err != nil { t.Errorf("invalid error: %v", err) } @@ -31,7 +33,7 @@ func TestColorSetting(t *testing.T) { t.Error("invalid setting") } os.Setenv("LOCKBOX_NOCOLOR", "lkaj;f") - _, err = IsNoColorEnabled() + _, err = inputs.IsNoColorEnabled() if err == nil || err.Error() != "invalid yes/no env value for LOCKBOX_NOCOLOR" { t.Errorf("unexpected error: %v", err) } @@ -39,7 +41,7 @@ func TestColorSetting(t *testing.T) { func TestInteractiveSetting(t *testing.T) { os.Setenv("LOCKBOX_INTERACTIVE", "yes") - c, err := IsInteractive() + c, err := inputs.IsInteractive() if err != nil { t.Errorf("invalid error: %v", err) } @@ -47,7 +49,7 @@ func TestInteractiveSetting(t *testing.T) { t.Error("invalid setting") } os.Setenv("LOCKBOX_INTERACTIVE", "no") - c, err = IsInteractive() + c, err = inputs.IsInteractive() if err != nil { t.Errorf("invalid error: %v", err) } @@ -55,7 +57,7 @@ func TestInteractiveSetting(t *testing.T) { t.Error("invalid setting") } os.Setenv("LOCKBOX_INTERACTIVE", "") - c, err = IsInteractive() + c, err = inputs.IsInteractive() if err != nil { t.Errorf("invalid error: %v", err) } @@ -63,7 +65,7 @@ func TestInteractiveSetting(t *testing.T) { t.Error("invalid setting") } os.Setenv("LOCKBOX_INTERACTIVE", "yaojia") - _, err = IsInteractive() + _, err = inputs.IsInteractive() if err == nil || err.Error() != "invalid yes/no env value for LOCKBOX_INTERACTIVE" { t.Errorf("unexpected error: %v", err) } diff --git a/internal/platform/clipboard_test.go b/internal/platform/clipboard_test.go @@ -1,14 +1,17 @@ -package platform +package platform_test import ( + "fmt" "os" "testing" + + "github.com/enckse/lockbox/internal/platform" ) func TestNoClipboard(t *testing.T) { os.Setenv("LOCKBOX_CLIPMAX", "") os.Setenv("LOCKBOX_NOCLIP", "yes") - _, err := NewClipboard() + _, err := platform.NewClipboard() if err == nil || err.Error() != "clipboard is off" { t.Errorf("invalid error: %v", err) } @@ -16,9 +19,9 @@ func TestNoClipboard(t *testing.T) { func TestMaxTime(t *testing.T) { os.Setenv("LOCKBOX_NOCLIP", "no") - os.Setenv("LOCKBOX_PLATFORM", string(LinuxWayland)) + os.Setenv("LOCKBOX_PLATFORM", string(platform.LinuxWayland)) os.Setenv("LOCKBOX_CLIPMAX", "") - c, err := NewClipboard() + c, err := platform.NewClipboard() if err != nil { t.Errorf("invalid clipboard: %v", err) } @@ -26,7 +29,7 @@ func TestMaxTime(t *testing.T) { t.Error("invalid default") } os.Setenv("LOCKBOX_CLIPMAX", "1") - c, err = NewClipboard() + c, err = platform.NewClipboard() if err != nil { t.Errorf("invalid clipboard: %v", err) } @@ -34,12 +37,12 @@ func TestMaxTime(t *testing.T) { t.Error("invalid default") } os.Setenv("LOCKBOX_CLIPMAX", "-1") - _, err = NewClipboard() + _, err = platform.NewClipboard() if err == nil || err.Error() != "clipboard max time must be greater than 0" { t.Errorf("invalid max time error: %v", err) } os.Setenv("LOCKBOX_CLIPMAX", "$&(+") - _, err = NewClipboard() + _, err = platform.NewClipboard() if err == nil || err.Error() != "strconv.Atoi: parsing \"$&(+\": invalid syntax" { t.Errorf("invalid max time error: %v", err) } @@ -48,26 +51,25 @@ func TestMaxTime(t *testing.T) { func TestClipboardInstances(t *testing.T) { os.Setenv("LOCKBOX_NOCLIP", "no") os.Setenv("LOCKBOX_CLIPMAX", "") - for _, item := range []System{MacOS, LinuxWayland, LinuxX, WindowsLinux} { + for _, item := range []platform.System{platform.MacOS, platform.LinuxWayland, platform.LinuxX, platform.WindowsLinux} { os.Setenv("LOCKBOX_PLATFORM", string(item)) - c, err := NewClipboard() + _, err := platform.NewClipboard() if err != nil { t.Errorf("invalid clipboard: %v", err) } - if len(c.copying) == 0 || len(c.pasting) == 0 { - t.Error("invalid command retrieved") - } } } func TestArgs(t *testing.T) { - c := Clipboard{copying: []string{"cp"}, pasting: []string{"paste", "with", "args"}} + os.Setenv("LOCKBOX_PLATFORM", string(platform.WindowsLinux)) + c, _ := platform.NewClipboard() cmd, args := c.Args(true) - if cmd != "cp" || len(args) != 0 { + if cmd != "clip.exe" || len(args) != 0 { t.Error("invalid parse") } cmd, args = c.Args(false) - if cmd != "paste" || len(args) != 2 || args[0] != "with" || args[1] != "args" { + if cmd != "powershell.exe" || len(args) != 2 || args[0] != "-command" || args[1] != "Get-Clipboard" { + fmt.Println(args) t.Error("invalid parse") } } diff --git a/internal/platform/core_test.go b/internal/platform/core_test.go @@ -1,14 +1,16 @@ -package platform +package platform_test import ( "os" "testing" + + "github.com/enckse/lockbox/internal/platform" ) func TestNewPlatform(t *testing.T) { - for _, item := range []System{MacOS, LinuxWayland, LinuxX, WindowsLinux} { + for _, item := range []platform.System{platform.MacOS, platform.LinuxWayland, platform.LinuxX, platform.WindowsLinux} { os.Setenv("LOCKBOX_PLATFORM", string(item)) - s, err := NewPlatform() + s, err := platform.NewPlatform() if err != nil { t.Errorf("invalid clipboard: %v", err) } @@ -20,7 +22,7 @@ func TestNewPlatform(t *testing.T) { func TestNewPlatformUnknown(t *testing.T) { os.Setenv("LOCKBOX_PLATFORM", "afleaj") - _, err := NewPlatform() + _, err := platform.NewPlatform() if err == nil || err.Error() != "unknown platform mode" { t.Errorf("error expected for platform: %v", err) } diff --git a/internal/store/filesystem_test.go b/internal/store/filesystem_test.go @@ -1,4 +1,4 @@ -package store +package store_test import ( "fmt" @@ -6,10 +6,14 @@ import ( "path/filepath" "strings" "testing" + + "github.com/enckse/lockbox/internal/inputs" + "github.com/enckse/lockbox/internal/store" ) func TestListErrors(t *testing.T) { - _, err := FileSystem{path: "aaa"}.List(ViewOptions{}) + os.Setenv(inputs.StoreEnv, "aaa") + _, err := store.NewFileSystemStore().List(store.ViewOptions{}) if err == nil || err.Error() != "store does not exist" { t.Errorf("invalid store error: %v", err) } @@ -17,7 +21,7 @@ func TestListErrors(t *testing.T) { func TestList(t *testing.T) { testStore := "bin" - if PathExists(testStore) { + if store.PathExists(testStore) { if err := os.RemoveAll(testStore); err != nil { t.Errorf("invalid error on remove: %v", err) } @@ -26,19 +30,20 @@ func TestList(t *testing.T) { t.Errorf("unable to makedir: %v", err) } for _, path := range []string{"test", "test2", "aaa", "sub/aaaaajk", "sub/12lkjafav"} { - if err := os.WriteFile(filepath.Join(testStore, path+extension), []byte(""), 0644); err != nil { + if err := os.WriteFile(filepath.Join(testStore, path+".lb"), []byte(""), 0644); err != nil { t.Errorf("failed to write %s: %v", path, err) } } - s := FileSystem{path: testStore} - res, err := s.List(ViewOptions{}) + os.Setenv(inputs.StoreEnv, testStore) + s := store.NewFileSystemStore() + res, err := s.List(store.ViewOptions{}) if err != nil { t.Errorf("unable to list: %v", err) } if len(res) != 5 { t.Error("mismatched results") } - res, err = s.List(ViewOptions{Display: true}) + res, err = s.List(store.ViewOptions{Display: true}) if err != nil { t.Errorf("unable to list: %v", err) } @@ -49,7 +54,7 @@ func TestList(t *testing.T) { t.Errorf("not sorted: %v", res) } idx := 0 - res, err = s.List(ViewOptions{Filter: func(path string) string { + res, err = s.List(store.ViewOptions{Filter: func(path string) string { if strings.Contains(path, "test") { idx++ return fmt.Sprintf("%d", idx) @@ -62,7 +67,7 @@ func TestList(t *testing.T) { if len(res) != 2 || res[0] != "1" || res[1] != "2" { t.Error("mismatch filter results") } - res, err = s.List(ViewOptions{ErrorOnEmpty: false, Filter: func(path string) string { + res, err = s.List(store.ViewOptions{ErrorOnEmpty: false, Filter: func(path string) string { return "" }}) if err != nil { @@ -71,7 +76,7 @@ func TestList(t *testing.T) { if len(res) != 0 { t.Error("should be empty list") } - _, err = s.List(ViewOptions{ErrorOnEmpty: true, Filter: func(path string) string { + _, err = s.List(store.ViewOptions{ErrorOnEmpty: true, Filter: func(path string) string { return "" }}) if err == nil || err.Error() != "no results found" { @@ -80,7 +85,8 @@ func TestList(t *testing.T) { } func TestFileSystemFile(t *testing.T) { - f := FileSystem{path: "abc"} + os.Setenv(inputs.StoreEnv, "abc") + f := store.NewFileSystemStore() p := f.NewPath("test") if p != "abc/test.lb" { t.Error("invalid join result") @@ -88,7 +94,8 @@ func TestFileSystemFile(t *testing.T) { } func TestCleanPath(t *testing.T) { - f := FileSystem{path: "abc"} + os.Setenv(inputs.StoreEnv, "abc") + f := store.NewFileSystemStore() c := f.CleanPath("xyz") if c != "xyz" { t.Error("invalid clean") @@ -104,11 +111,12 @@ func TestCleanPath(t *testing.T) { } func TestNewFile(t *testing.T) { - f := FileSystem{path: "abc"}.NewFile("xyz") + os.Setenv(inputs.StoreEnv, "abc") + f := store.NewFileSystemStore().NewFile("xyz") if f != "xyz.lb" { t.Error("invalid file") } - f = FileSystem{path: "abc"}.NewFile("xyz.lb") + f = store.NewFileSystemStore().NewFile("xyz.lb") if f != "xyz.lb" { t.Error("invalid file, had suffix") }