@@ -15,8 +15,14 @@ import (
15
15
16
16
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"}}}}`
17
17
18
+ const supportedRdDigest = `{"digest":{"sha256":"a1234567b1234567c1234567d1234567e1234567f1234567a1234567b1234567","custom":"myCustomEnvoding","sha1":"a1234567b1234567c1234567d1234567e1234567"}}`
19
+
18
20
const badRd = `{"downloadLocation":"https://example.com/test.zip","mediaType":"theMediaType"}`
19
21
22
+ const badRdDigestEncoding = `{"digest":{"sha256":"badDigest"},"downloadLocation":"https://example.com/test.zip","mediaType":"theMediaType"}`
23
+
24
+ const badRdDigestLength = `{"digest":{"sha256":"abc123"},"downloadLocation":"https://example.com/test.zip","mediaType":"theMediaType"}`
25
+
20
26
func createTestResourceDescriptor () (* ResourceDescriptor , error ) {
21
27
// Create a ResourceDescriptor
22
28
a , err := structpb .NewStruct (map [string ]interface {}{
@@ -56,6 +62,16 @@ func TestJsonUnmarshalResourceDescriptor(t *testing.T) {
56
62
assert .True (t , proto .Equal (got , want ), "Protos do not match" )
57
63
}
58
64
65
+ func TestSupportedResourceDescriptorDigest (t * testing.T ) {
66
+ got := & ResourceDescriptor {}
67
+ err := protojson .Unmarshal ([]byte (supportedRdDigest ), got )
68
+
69
+ assert .NoError (t , err , "Error during JSON unmarshalling" )
70
+
71
+ err = got .Validate ()
72
+ assert .NoError (t , err , "Error during validation of valid supported RD digests" )
73
+ }
74
+
59
75
func TestBadResourceDescriptor (t * testing.T ) {
60
76
got := & ResourceDescriptor {}
61
77
err := protojson .Unmarshal ([]byte (badRd ), got )
@@ -65,3 +81,23 @@ func TestBadResourceDescriptor(t *testing.T) {
65
81
err = got .Validate ()
66
82
assert .ErrorIs (t , err , ErrRDRequiredField , "created malformed ResourceDescriptor" )
67
83
}
84
+
85
+ func TestBadResourceDescriptorDigestEncoding (t * testing.T ) {
86
+ got := & ResourceDescriptor {}
87
+ err := protojson .Unmarshal ([]byte (badRdDigestEncoding ), got )
88
+
89
+ assert .NoError (t , err , "Error during JSON unmarshalling" )
90
+
91
+ err = got .Validate ()
92
+ assert .ErrorIs (t , err , ErrInvalidDigestEncoding , "did not get expected error when validating ResourceDescriptor with invalid digest encoding" )
93
+ }
94
+
95
+ func TestBadResourceDescriptorDigestLength (t * testing.T ) {
96
+ got := & ResourceDescriptor {}
97
+ err := protojson .Unmarshal ([]byte (badRdDigestLength ), got )
98
+
99
+ assert .NoError (t , err , "Error during JSON unmarshalling" )
100
+
101
+ err = got .Validate ()
102
+ assert .ErrorIs (t , err , ErrIncorrectDigestLength , "did not get expected error when validating ResourceDescriptor with incorrect digest length" )
103
+ }
0 commit comments