@@ -3,6 +3,7 @@ package signature
33import (
44 "encoding/json"
55 "io/ioutil"
6+ "path/filepath"
67 "testing"
78 "time"
89
@@ -11,6 +12,7 @@ import (
1112 "github.com/pkg/errors"
1213 "github.com/stretchr/testify/assert"
1314 "github.com/stretchr/testify/require"
15+ "github.com/xeipuuv/gojsonschema"
1416)
1517
1618func TestInvalidSignatureError (t * testing.T ) {
@@ -92,42 +94,57 @@ func modifiedUntrustedSignatureJSON(t *testing.T, validJSON []byte, modifyFn fun
9294}
9395
9496// Verify that input can be unmarshaled as an untrustedSignature, and that it passes JSON schema validation, and return the unmarshaled untrustedSignature.
95- func succesfullyUnmarshalUntrustedSignature (t * testing.T , input []byte ) untrustedSignature {
97+ func succesfullyUnmarshalUntrustedSignature (t * testing.T , schemaLoader gojsonschema. JSONLoader , input []byte ) untrustedSignature {
9698 inputString := string (input )
9799
98100 var s untrustedSignature
99101 err := json .Unmarshal (input , & s )
100102 require .NoError (t , err , inputString )
103+
104+ res , err := gojsonschema .Validate (schemaLoader , gojsonschema .NewStringLoader (inputString ))
105+ assert .True (t , err == nil , inputString )
106+ assert .True (t , res .Valid (), inputString )
107+
101108 return s
102109}
103110
104111// Verify that input can't be unmashaled as an untrusted signature, and that it fails JSON schema validation.
105- func assertUnmarshalUntrustedSignatureFails (t * testing.T , input []byte ) {
112+ func assertUnmarshalUntrustedSignatureFails (t * testing.T , schemaLoader gojsonschema. JSONLoader , input []byte ) {
106113 inputString := string (input )
107114
108115 var s untrustedSignature
109116 err := json .Unmarshal (input , & s )
110117 assert .Error (t , err , inputString )
118+
119+ res , err := gojsonschema .Validate (schemaLoader , gojsonschema .NewStringLoader (inputString ))
120+ assert .True (t , err != nil || ! res .Valid (), inputString )
111121}
112122
113123func TestUnmarshalJSON (t * testing.T ) {
124+ // NOTE: The schema at schemaPath is NOT authoritative; docs/atomic-signature.json and the code is, rather!
125+ // The schemaPath references are not testing that the code follows the behavior declared by the schema,
126+ // they are testing that the schema follows the behavior of the code!
127+ schemaPath , err := filepath .Abs ("../docs/atomic-signature-embedded-json.json" )
128+ require .NoError (t , err )
129+ schemaLoader := gojsonschema .NewReferenceLoader ("file://" + schemaPath )
130+
114131 // Invalid input. Note that json.Unmarshal is guaranteed to validate input before calling our
115132 // UnmarshalJSON implementation; so test that first, then test our error handling for completeness.
116- assertUnmarshalUntrustedSignatureFails (t , []byte ("&" ))
133+ assertUnmarshalUntrustedSignatureFails (t , schemaLoader , []byte ("&" ))
117134 var s untrustedSignature
118- err : = s .UnmarshalJSON ([]byte ("&" ))
135+ err = s .UnmarshalJSON ([]byte ("&" ))
119136 assert .Error (t , err )
120137
121138 // Not an object
122- assertUnmarshalUntrustedSignatureFails (t , []byte ("1" ))
139+ assertUnmarshalUntrustedSignatureFails (t , schemaLoader , []byte ("1" ))
123140
124141 // Start with a valid JSON.
125142 validSig := newUntrustedSignature ("digest!@#" , "reference#@!" )
126143 validJSON , err := validSig .MarshalJSON ()
127144 require .NoError (t , err )
128145
129146 // Success
130- s = succesfullyUnmarshalUntrustedSignature (t , validJSON )
147+ s = succesfullyUnmarshalUntrustedSignature (t , schemaLoader , validJSON )
131148 assert .Equal (t , validSig , s )
132149
133150 // Various ways to corrupt the JSON
@@ -170,7 +187,7 @@ func TestUnmarshalJSON(t *testing.T) {
170187 }
171188 for _ , fn := range breakFns {
172189 testJSON := modifiedUntrustedSignatureJSON (t , validJSON , fn )
173- assertUnmarshalUntrustedSignatureFails (t , testJSON )
190+ assertUnmarshalUntrustedSignatureFails (t , schemaLoader , testJSON )
174191 }
175192
176193 // Modifications to unrecognized fields in "optional" are allowed and ignored
@@ -180,7 +197,7 @@ func TestUnmarshalJSON(t *testing.T) {
180197 }
181198 for _ , fn := range allowedModificationFns {
182199 testJSON := modifiedUntrustedSignatureJSON (t , validJSON , fn )
183- s := succesfullyUnmarshalUntrustedSignature (t , testJSON )
200+ s := succesfullyUnmarshalUntrustedSignature (t , schemaLoader , testJSON )
184201 assert .Equal (t , validSig , s )
185202 }
186203
@@ -193,7 +210,7 @@ func TestUnmarshalJSON(t *testing.T) {
193210 }
194211 validJSON , err = validSig .MarshalJSON ()
195212 require .NoError (t , err )
196- s = succesfullyUnmarshalUntrustedSignature (t , validJSON )
213+ s = succesfullyUnmarshalUntrustedSignature (t , schemaLoader , validJSON )
197214 assert .Equal (t , validSig , s )
198215}
199216
0 commit comments