Skip to content

Commit

Permalink
add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
dictcp committed Oct 20, 2018
1 parent b8c84db commit b39f0d6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ creation_rules:
unencrypted_suffix: _unencrypted
- path_regex: bar?foo$
encrypted_suffix: _enc
encrypted_selector: data.foo
key_groups:
- kms:
- arn: baz
Expand Down Expand Up @@ -234,6 +235,12 @@ func TestLoadConfigFileWithEncryptedSuffix(t *testing.T) {
assert.Equal(t, "_enc", conf.EncryptedSuffix)
}

func TestLoadConfigFileWithEncryptedSelector(t *testing.T) {
conf, err := loadForFileFromBytes(sampleConfigWithSuffixParameters, "barfoo", nil)
assert.Equal(t, nil, err)
assert.Equal(t, "data.foo", conf.EncryptedSelector)
}

func TestLoadConfigFileWithInvalidParameters(t *testing.T) {
_, err := loadForFileFromBytes(sampleConfigWithInvalidParameters, "foobar", nil)
assert.NotNil(t, err)
Expand Down
52 changes: 52 additions & 0 deletions sops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,58 @@ func TestEncryptedSuffix(t *testing.T) {
}
}


func TestEncryptedSelector(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo_encrypted",
Value: "bar",
},
TreeItem{
Key: "bar",
Value: TreeBranch{
TreeItem{
Key: "foo",
Value: "bar",
},
},
},
}
tree := Tree{Branch: branch, Metadata: Metadata{EncryptedSelector: "bar.foo"}}
expected := TreeBranch{
TreeItem{
Key: "foo_encrypted",
Value: "rab",
},
TreeItem{
Key: "bar",
Value: TreeBranch{
TreeItem{
Key: "foo",
Value: "bar",
},
},
},
}
cipher := reverseCipher{}
_, err := tree.Encrypt(bytes.Repeat([]byte("f"), 32), cipher)
if err != nil {
t.Errorf("Encrypting the tree failed: %s", err)
}
if !reflect.DeepEqual(tree.Branch, expected) {
t.Errorf("Trees don't match: \ngot \t\t%+v,\n expected \t\t%+v", tree.Branch, expected)
}
_, err = tree.Decrypt(bytes.Repeat([]byte("f"), 32), cipher)
if err != nil {
t.Errorf("Decrypting the tree failed: %s", err)
}
expected[0].Value = "bar"
if !reflect.DeepEqual(tree.Branch, expected) {
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, path string) (string, error) {
Expand Down

0 comments on commit b39f0d6

Please sign in to comment.