From 8846dc11401e8581ecfab57f83bcc2cff001feff Mon Sep 17 00:00:00 2001 From: Sebastien Duthil Date: Wed, 18 Sep 2024 23:46:04 -0400 Subject: [PATCH] sops_test: add more tests on errors Signed-off-by: Sebastien Duthil --- sops_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/sops_test.go b/sops_test.go index 25e0cd7ad..fe1f44e2c 100644 --- a/sops_test.go +++ b/sops_test.go @@ -844,6 +844,23 @@ func TestDecrypt(t *testing.T) { } } +type WrongType struct{} + +func TestEncryptWrongType(t *testing.T) { + branches := TreeBranches{ + TreeBranch{ + TreeItem{ + Key: "foo", + Value: WrongType{}, + }, + }, + } + tree := Tree{Branches: branches, Metadata: Metadata{UnencryptedSuffix: DefaultUnencryptedSuffix}} + result, err := tree.Encrypt(bytes.Repeat([]byte{'f'}, 32), MockCipher{}) + assert.Equal(t, "", result) + assert.ErrorContains(t, err, "unknown type: sops.WrongType") +} + func TestTruncateTree(t *testing.T) { tree := TreeBranch{ TreeItem{ @@ -875,6 +892,45 @@ func TestTruncateTree(t *testing.T) { assert.Equal(t, expected, result) } +func TestTruncateTreeNotFound(t *testing.T) { + tree := TreeBranch{ + TreeItem{ + Key: "foo", + Value: 2, + }, + } + result, err := tree.Truncate([]interface{}{"baz"}) + assert.ErrorContains(t, err, "baz") + assert.Nil(t, result, "Truncate result was not nil upon %s", err) +} + +func TestTruncateTreeNotArray(t *testing.T) { + tree := TreeBranch{ + TreeItem{ + Key: "foo", + Value: 2, + }, + } + result, err := tree.Truncate([]interface{}{"foo", 99}) + assert.ErrorContains(t, err, "99") + assert.Nil(t, result, "Truncate result was not nil upon %s", err) +} + +func TestTruncateTreeArrayOutOfBounds(t *testing.T) { + tree := TreeBranch{ + TreeItem{ + Key: "foo", + Value: []interface{}{ + "one", + "two", + }, + }, + } + result, err := tree.Truncate([]interface{}{"foo", 99}) + assert.ErrorContains(t, err, "99") + assert.Nil(t, result, "Truncate result was not nil upon %s", err) +} + func TestEncryptComments(t *testing.T) { tree := Tree{ Branches: TreeBranches{ @@ -1360,8 +1416,9 @@ func TestUnsetKeyNotFound(t *testing.T) { }, }, } - unset, err := branch.Unset([]interface{}{"foo", "unknown"}) - assert.Equal(t, err.(*SopsKeyNotFound).Key, "unknown") + unset, err := branch.Unset([]interface{}{"foo", "unknown-value"}) + assert.Equal(t, err.(*SopsKeyNotFound).Key, "unknown-value") + assert.ErrorContains(t, err, "unknown-value") assert.Nil(t, unset, "Unset result was not nil upon %s", err) } @@ -1435,6 +1492,10 @@ func TestEmitAsMap(t *testing.T) { Key: "number", Value: 42, }, + TreeItem{ + Key: Comment{"comment"}, + Value: nil, + }, }, TreeBranch{ TreeItem{