commit 23014eda042fab8ffbbb0e0dd09618a514e17b7b
parent b62042b2358a726930bf02c319de027459e17b64
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 16 Jul 2022 09:09:14 -0400
tests for encryption wrapper
Diffstat:
4 files changed, 85 insertions(+), 10 deletions(-)
diff --git a/Makefile b/Makefile
@@ -11,6 +11,7 @@ $(TARGETS): cmd/$@/* internal/* go.*
go build -ldflags '-X main.version=$(VERSION) -X main.libExec=$(LIBEXEC) -X main.mainExe=$(MAIN)' -trimpath -buildmode=pie -mod=readonly -modcacherw -o $@ cmd/$(shell basename $@)/main.go
check: $(TARGETS)
+ cd internal && go test .
cd tests && make BUILD=../$(BUILD)
clean:
diff --git a/go.sum b/go.sum
@@ -14,12 +14,5 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
-golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
-golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a h1:ppl5mZgokTT8uPkmYOyEUmPTr3ypaKkg5eFOGrAmxxE=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
-golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
-golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
diff --git a/internal/encrypt.go b/internal/encrypt.go
@@ -32,9 +32,10 @@ type (
// LockboxOptions represent options to create a lockbox from.
LockboxOptions struct {
- Key string
- KeyMode string
- File string
+ Key string
+ KeyMode string
+ File string
+ callback func(string) string
}
)
@@ -55,6 +56,9 @@ func newLockbox(key, keyMode, file string) (Lockbox, error) {
if useKey == "" {
useKey = os.Getenv("LOCKBOX_KEY")
}
+ if useKey == "" {
+ return Lockbox{}, NewLockboxError("no key given")
+ }
b, err := getKey(useKeyMode, useKey)
if err != nil {
return Lockbox{}, err
diff --git a/internal/encrypt_test.go b/internal/encrypt_test.go
@@ -0,0 +1,77 @@
+package internal
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func setupData(t *testing.T) string {
+ os.Setenv("LOCKBOX_KEYMODE", "")
+ os.Setenv("LOCKBOX_KEY", "")
+ if !PathExists("bin") {
+ if err := os.MkdirAll("bin", 0755); err != nil {
+ t.Errorf("failed to setup bin directory: %v", err)
+ }
+ }
+ return filepath.Join("bin", "test.lb")
+}
+
+func TestEncryptDecryptCommand(t *testing.T) {
+ e, err := NewLockbox(LockboxOptions{Key: "echo test", KeyMode: CommandKeyMode, File: setupData(t)})
+ if err != nil {
+ t.Errorf("failed to create lockbox: %v", err)
+ }
+ data := []byte("datum")
+ if err := e.Encrypt(data); err != nil {
+ t.Errorf("failed to encrypt: %v", err)
+ }
+ d, err := e.Decrypt()
+ if err != nil {
+ t.Errorf("failed to encrypt: %v", err)
+ }
+ if string(d) != string(data) {
+ t.Error("data mismatch")
+ }
+}
+
+func TestEmptyKey(t *testing.T) {
+ setupData(t)
+ _, err := NewLockbox(LockboxOptions{})
+ if err == nil || err.Error() != "no key given" {
+ t.Errorf("invalid error: %v", err)
+ }
+ _, err = NewLockbox(LockboxOptions{KeyMode: CommandKeyMode, Key: "echo"})
+ if err == nil || err.Error() != "key is empty" {
+ t.Errorf("invalid error: %v", err)
+ }
+ _, err = NewLockbox(LockboxOptions{KeyMode: CommandKeyMode, Key: "echo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})
+ if err == nil || err.Error() != "key is too large for use" {
+ t.Errorf("invalid error: %v", err)
+ }
+}
+
+func TestUnknownMode(t *testing.T) {
+ _, err := NewLockbox(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)})
+ if err != nil {
+ t.Errorf("failed to create lockbox: %v", err)
+ }
+ data := []byte("datum")
+ if err := e.Encrypt(data); err != nil {
+ t.Errorf("failed to encrypt: %v", err)
+ }
+ d, err := e.Decrypt()
+ if err != nil {
+ t.Errorf("failed to encrypt: %v", err)
+ }
+ if string(d) != string(data) {
+ t.Error("data mismatch")
+ }
+}