-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic Golang tests for Statement layer
Signed-off-by: Marcela Melara <[email protected]>
- Loading branch information
1 parent
12dd21a
commit 4cda93e
Showing
3 changed files
with
180 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Tests for in-toto attestation ResourceDescriptor protos. | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const wantFullRd = `{"name":"theName","uri":"https://example.com","digest":{"alg1":"abc123"},"content":"Ynl0ZXNjb250ZW50","downloadLocation":"https://example.com/test.zip","mediaType":"theMediaType","annotations":{"a1":{"keyNum": 13,"keyStr":"value1"},"a2":{"keyObj":{"subKey":"subVal"}}}}` | ||
|
||
const badRd = `{"downloadLocation":"https://example.com/test.zip","mediaType":"theMediaType"}` | ||
|
||
func createTestResourceDescriptor() (*ResourceDescriptor, error) { | ||
// Create a ResourceDescriptor | ||
a1, err := structpb.NewStruct(map[string]interface{}{ | ||
"keyStr": "value1", | ||
"keyNum": 13}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
a2, err := structpb.NewStruct(map[string]interface{}{ | ||
"keyObj": map[string]interface{}{ | ||
"subKey": "subVal"}}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewResourceDescriptorPb("theName", "https://example.com", | ||
map[string]string{"alg1": "abc123"}, []byte("bytescontent"), | ||
"https://example.com/test.zip", "theMediaType", | ||
map[string]*structpb.Struct{"a1": a1, "a2": a2}) | ||
} | ||
|
||
func TestJsonUnmarshalResourceDescriptor(t *testing.T) { | ||
got := &ResourceDescriptor{} | ||
err := protojson.Unmarshal([]byte(wantFullRd), got) | ||
|
||
assert.Nil(t, err, "Error during JSON unmarshalling") | ||
|
||
want, err := createTestResourceDescriptor() | ||
|
||
assert.Nil(t, err, "Error during test RD creation") | ||
assert.True(t, proto.Equal(got, want), "Protos do not match") | ||
} | ||
|
||
func TestBadResourceDescriptor(t *testing.T) { | ||
got := &ResourceDescriptor{} | ||
err := protojson.Unmarshal([]byte(badRd), got) | ||
|
||
assert.Nil(t, err, "Error during JSON unmarshalling") | ||
|
||
result := IsValidResourceDescriptor(got) | ||
|
||
assert.False(t, result, "Error: created malformed ResourceDescriptor") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Tests for in-toto attestation ResourceDescriptor protos. | ||
*/ | ||
|
||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const wantSt = `{"_type":"https://in-toto.io/Statement/v1","subject":[{"name":"theSub","digest":{"alg1":"abc123"}}],"predicateType":"thePredicate","predicate":{"keyObj":{"subKey":"subVal"}}}` | ||
|
||
func createTestStatement() (*Statement, error) { | ||
// Create a Statement | ||
sub, err := NewResourceDescriptorPb("theSub", "", | ||
map[string]string{"alg1": "abc123"}, nil, "", "", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pred, err := structpb.NewStruct(map[string]interface{}{ | ||
"keyObj": map[string]interface{}{ | ||
"subKey": "subVal"}}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewStatementPb([]*ResourceDescriptor{sub}, "thePredicate", | ||
pred) | ||
} | ||
|
||
func TestJsonUnmarshalStatement(t *testing.T) { | ||
got := &Statement{} | ||
err := protojson.Unmarshal([]byte(wantSt), got) | ||
|
||
assert.Nil(t, err, "Error during JSON unmarshalling") | ||
|
||
want, err := createTestStatement() | ||
|
||
assert.Nil(t, err, "Error during test Statement creation") | ||
assert.True(t, proto.Equal(got, want), "Protos do not match") | ||
} | ||
|
||
func TestBadStatementType(t *testing.T) { | ||
var badStType = `{"_type":"https://in-toto.io/Statement/v0","subject":[{"name":"theSub","digest":{"alg1":"abc123"}}],"predicateType":"thePredicate","predicate":{"keyObj":{"subKey":"subVal"}}}` | ||
|
||
got := &Statement{} | ||
err := protojson.Unmarshal([]byte(badStType), got) | ||
|
||
assert.Nil(t, err, "Error during JSON unmarshalling") | ||
|
||
result := IsValidStatement(got) | ||
|
||
assert.False(t, result, | ||
"Error: created malformed Statement (bad type)") | ||
} | ||
|
||
func TestBadStatementSubject(t *testing.T) { | ||
var badStNoSub = `{"_type":"https://in-toto.io/Statement/v1","subject":[],"predicateType":"thePredicate","predicate":{"keyObj":{"subKey":"subVal"}}}` | ||
|
||
got := &Statement{} | ||
err := protojson.Unmarshal([]byte(badStNoSub), got) | ||
|
||
assert.Nil(t, err, "Error during JSON unmarshalling") | ||
|
||
result := IsValidStatement(got) | ||
|
||
assert.False(t, result, | ||
"Error: created malformed Statement (empty subject)") | ||
|
||
var badStBadSub = `{"_type":"https://in-toto.io/Statement/v1","subject":[{"downloadLocation":"https://example.com/test.zip"}],"predicateType":"thePredicate","predicate":{"keyObj":{"subKey":"subVal"}}}` | ||
|
||
got = &Statement{} | ||
err = protojson.Unmarshal([]byte(badStBadSub), got) | ||
|
||
assert.Nil(t, err, "Error during JSON unmarshalling") | ||
|
||
result = IsValidStatement(got) | ||
|
||
assert.False(t, result, | ||
"Error: created malformed Statement (bad subject)") | ||
} | ||
|
||
func TestBadStatementPredicate(t *testing.T) { | ||
var badStPredType = `{"_type":"https://in-toto.io/Statement/v1","subject":[{"name":"theSub","digest":{"alg1":"abc123"}}],"predicateType":"","predicate":{"keyObj":{"subKey":"subVal"}}}` | ||
|
||
got := &Statement{} | ||
err := protojson.Unmarshal([]byte(badStPredType), got) | ||
|
||
assert.Nil(t, err, "Error during JSON unmarshalling") | ||
|
||
result := IsValidStatement(got) | ||
|
||
assert.False(t, result, | ||
"Error: created malformed Statement (bad predicate type)") | ||
|
||
var badStPred = `{"_type":"https://in-toto.io/Statement/v1","subject":[{"name":"theSub","digest":{"alg1":"abc123"}}],"predicateType":"thePredicate"}` | ||
|
||
got = &Statement{} | ||
err = protojson.Unmarshal([]byte(badStPred), got) | ||
|
||
assert.Nil(t, err, "Error during JSON unmarshalling") | ||
|
||
result = IsValidStatement(got) | ||
|
||
assert.False(t, result, | ||
"Error: created malformed Statement (no prdicate)") | ||
} |