@@ -23,8 +23,11 @@ import (
2323 "testing"
2424 "time"
2525
26+ "encoding/json"
27+
2628 "github.com/Sirupsen/logrus"
2729 ctxu "github.com/docker/distribution/context"
30+ canonicaljson "github.com/docker/go/canonical/json"
2831 "github.com/docker/notary"
2932 "github.com/docker/notary/client"
3033 "github.com/docker/notary/cryptoservice"
@@ -198,8 +201,9 @@ func TestClientTUFInteraction(t *testing.T) {
198201 defer os .Remove (tempFile .Name ())
199202
200203 var (
201- output string
202- target = "sdgkadga"
204+ output string
205+ target = "sdgkadga"
206+ target2 = "foobar"
203207 )
204208 // -- tests --
205209
@@ -251,6 +255,62 @@ func TestClientTUFInteraction(t *testing.T) {
251255 output , err = runCommand (t , tempDir , "-s" , server .URL , "list" , "gun" )
252256 require .NoError (t , err )
253257 require .False (t , strings .Contains (string (output ), target ))
258+
259+ // Test a target with custom data.
260+ tempFileForTargetCustom , err := ioutil .TempFile ("" , "targetCustom" )
261+ require .NoError (t , err )
262+ var customData canonicaljson.RawMessage
263+ err = canonicaljson .Unmarshal ([]byte ("\" Lorem ipsum dolor sit amet, consectetur adipiscing elit\" " ), & customData )
264+ require .NoError (t , err )
265+ _ , err = tempFileForTargetCustom .Write (customData )
266+ require .NoError (t , err )
267+ tempFileForTargetCustom .Close ()
268+ defer os .Remove (tempFileForTargetCustom .Name ())
269+
270+ // add a target
271+ _ , err = runCommand (t , tempDir , "add" , "gun" , target2 , tempFile .Name (), "--custom" , tempFileForTargetCustom .Name ())
272+ require .NoError (t , err )
273+
274+ // check status - see target
275+ output , err = runCommand (t , tempDir , "status" , "gun" )
276+ require .NoError (t , err )
277+ require .Contains (t , output , target2 )
278+
279+ // publish repo
280+ _ , err = runCommand (t , tempDir , "-s" , server .URL , "publish" , "gun" )
281+ require .NoError (t , err )
282+
283+ // check status - no targets
284+ output , err = runCommand (t , tempDir , "status" , "gun" )
285+ require .NoError (t , err )
286+ require .False (t , strings .Contains (string (output ), target2 ))
287+
288+ // list repo - see target
289+ output , err = runCommand (t , tempDir , "-s" , server .URL , "list" , "gun" )
290+ require .NoError (t , err )
291+ require .Contains (t , output , target2 )
292+
293+ // Check the file this was written to to inspect metadata
294+ cache , err := nstorage .NewFileStore (
295+ filepath .Join (tempDir , "tuf" , filepath .FromSlash ("gun" ), "metadata" ),
296+ "json" ,
297+ )
298+ require .NoError (t , err )
299+ rawTargets , err := cache .Get ("targets" )
300+ require .NoError (t , err )
301+ parsedTargets := data.SignedTargets {}
302+ err = json .Unmarshal (rawTargets , & parsedTargets )
303+ require .NoError (t , err )
304+ require .Equal (t , * parsedTargets .Signed .Targets [target2 ].Custom , customData )
305+
306+ // trigger a lookup error with < 2 args
307+ _ , err = runCommand (t , tempDir , "-s" , server .URL , "lookup" , "gun" )
308+ require .Error (t , err )
309+
310+ // lookup target and repo - see target
311+ output , err = runCommand (t , tempDir , "-s" , server .URL , "lookup" , "gun" , target2 )
312+ require .NoError (t , err )
313+ require .Contains (t , output , target2 )
254314}
255315
256316func TestClientDeleteTUFInteraction (t * testing.T ) {
@@ -422,6 +482,7 @@ func TestClientTUFAddByHashInteraction(t *testing.T) {
422482 target1 = "sdgkadga"
423483 target2 = "asdfasdf"
424484 target3 = "qwerty"
485+ target4 = "foobar"
425486 )
426487 // -- tests --
427488
@@ -541,6 +602,57 @@ func TestClientTUFAddByHashInteraction(t *testing.T) {
541602 // publish repo
542603 _ , err = runCommand (t , tempDir , "-s" , server .URL , "publish" , "gun" )
543604 require .NoError (t , err )
605+
606+ tempFile , err := ioutil .TempFile ("" , "targetCustom" )
607+ require .NoError (t , err )
608+ var customData canonicaljson.RawMessage
609+ err = canonicaljson .Unmarshal ([]byte ("\" Lorem ipsum dolor sit amet, consectetur adipiscing elit\" " ), & customData )
610+ require .NoError (t , err )
611+ _ , err = tempFile .Write (customData )
612+ require .NoError (t , err )
613+ tempFile .Close ()
614+ defer os .Remove (tempFile .Name ())
615+
616+ // add a target by sha512 and custom data
617+ _ , err = runCommand (t , tempDir , "addhash" , "gun" , target4 , "3" , "--sha512" , targetSha512Hex , "--custom" , tempFile .Name ())
618+ require .NoError (t , err )
619+
620+ // check status - see target
621+ output , err = runCommand (t , tempDir , "status" , "gun" )
622+ require .NoError (t , err )
623+ require .Contains (t , output , target4 )
624+
625+ // publish repo
626+ _ , err = runCommand (t , tempDir , "-s" , server .URL , "publish" , "gun" )
627+ require .NoError (t , err )
628+
629+ // check status - no targets
630+ output , err = runCommand (t , tempDir , "status" , "gun" )
631+ require .NoError (t , err )
632+ require .False (t , strings .Contains (string (output ), target4 ))
633+
634+ // list repo - see target
635+ output , err = runCommand (t , tempDir , "-s" , server .URL , "list" , "gun" )
636+ require .NoError (t , err )
637+ require .Contains (t , output , target4 )
638+
639+ // Check the file this was written to to inspect metadata
640+ cache , err := nstorage .NewFileStore (
641+ filepath .Join (tempDir , "tuf" , filepath .FromSlash ("gun" ), "metadata" ),
642+ "json" ,
643+ )
644+ require .NoError (t , err )
645+ rawTargets , err := cache .Get ("targets" )
646+ require .NoError (t , err )
647+ parsedTargets := data.SignedTargets {}
648+ err = json .Unmarshal (rawTargets , & parsedTargets )
649+ require .NoError (t , err )
650+ require .Equal (t , * parsedTargets .Signed .Targets [target4 ].Custom , customData )
651+
652+ // lookup target and repo - see target
653+ output , err = runCommand (t , tempDir , "-s" , server .URL , "lookup" , "gun" , target4 )
654+ require .NoError (t , err )
655+ require .Contains (t , output , target4 )
544656}
545657
546658// Initialize repo and test delegations commands by adding, listing, and removing delegations
0 commit comments