mirror of
https://github.com/getsops/sops.git
synced 2026-02-05 12:45:21 +01:00
Added tests for encryption and decryption
This commit is contained in:
2
sops.go
2
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{}:
|
||||
|
||||
118
sops_test.go
118
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user