From aff201360099e5ea3370b02a9f307602b1fb6ddd Mon Sep 17 00:00:00 2001 From: Adrian Utrilla Date: Wed, 24 Aug 2016 17:12:46 -0700 Subject: [PATCH] Added tests for encryption and decryption --- sops.go | 2 + sops_test.go | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/sops.go b/sops.go index 16e3cc343..171ba1abb 100644 --- a/sops.go +++ b/sops.go @@ -49,6 +49,8 @@ func (tree TreeBranch) walkValue(in interface{}, path []string, onLeaves func(in return onLeaves(in, path) case bool: return onLeaves(in, path) + case float64: + return onLeaves(in, path) case TreeBranch: return tree.walkBranch(in, path, onLeaves) case []interface{}: diff --git a/sops_test.go b/sops_test.go index aa65a59c5..08f068afe 100644 --- a/sops_test.go +++ b/sops_test.go @@ -37,3 +37,121 @@ func TestUnencryptedSuffix(t *testing.T) { t.Errorf("Trees don't match: \ngot\t\t\t%+v,\nexpected\t\t%+v", tree.Branch, expected) } } + +type MockCipher struct{} + +func (m MockCipher) Encrypt(value interface{}, key []byte, additionalAuthData []byte) (string, error) { + return "a", nil +} + +func (m MockCipher) Decrypt(value string, key []byte, additionalAuthData []byte) (interface{}, error) { + return "a", nil +} + +func TestEncrypt(t *testing.T) { + branch := TreeBranch{ + TreeItem{ + Key: "foo", + Value: "bar", + }, + TreeItem{ + Key: "baz", + Value: TreeBranch{ + TreeItem{ + Key: "bar", + Value: 5, + }, + }, + }, + TreeItem{ + Key: "bar", + Value: false, + }, + TreeItem{ + Key: "foobar", + Value: 2.12, + }, + } + expected := TreeBranch{ + TreeItem{ + Key: "foo", + Value: "a", + }, + TreeItem{ + Key: "baz", + Value: TreeBranch{ + TreeItem{ + Key: "bar", + Value: "a", + }, + }, + }, + TreeItem{ + Key: "bar", + Value: "a", + }, + TreeItem{ + Key: "foobar", + Value: "a", + }, + } + tree := Tree{Branch: branch, Metadata: Metadata{UnencryptedSuffix: DefaultUnencryptedSuffix}} + tree.Encrypt(bytes.Repeat([]byte{'f'}, 32), MockCipher{}) + if !reflect.DeepEqual(tree.Branch, expected) { + t.Errorf("%s does not equal expected tree: %s", tree.Branch, expected) + } +} + +func TestDecrypt(t *testing.T) { + branch := TreeBranch{ + TreeItem{ + Key: "foo", + Value: "bar", + }, + TreeItem{ + Key: "baz", + Value: TreeBranch{ + TreeItem{ + Key: "bar", + Value: "5", + }, + }, + }, + TreeItem{ + Key: "bar", + Value: "false", + }, + TreeItem{ + Key: "foobar", + Value: "2.12", + }, + } + expected := TreeBranch{ + TreeItem{ + Key: "foo", + Value: "a", + }, + TreeItem{ + Key: "baz", + Value: TreeBranch{ + TreeItem{ + Key: "bar", + Value: "a", + }, + }, + }, + TreeItem{ + Key: "bar", + Value: "a", + }, + TreeItem{ + Key: "foobar", + Value: "a", + }, + } + tree := Tree{Branch: branch, Metadata: Metadata{UnencryptedSuffix: DefaultUnencryptedSuffix}} + tree.Decrypt(bytes.Repeat([]byte{'f'}, 32), MockCipher{}) + if !reflect.DeepEqual(tree.Branch, expected) { + t.Errorf("%s does not equal expected tree: %s", tree.Branch, expected) + } +}