@@ -376,25 +376,25 @@ func TestUnrecognizedFieldRolesSigned(t *testing.T) {
376376 root .Signed .UnrecognizedFields = testUnrecognizedField
377377 rootJSON , err := root .ToBytes (false )
378378 assert .NoError (t , err )
379- assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" root\" ,\" consistent_snapshot\" :true,\" expires\" :\" 2030-08-15T14:30:45.0000001Z \" ,\" keys\" :{},\" roles\" :{\" root\" :{\" keyids\" :[],\" threshold\" :1},\" snapshot\" :{\" keyids\" :[],\" threshold\" :1},\" targets\" :{\" keyids\" :[],\" threshold\" :1},\" timestamp\" :{\" keyids\" :[],\" threshold\" :1}},\" spec_version\" :\" 1.0.31\" ,\" test\" :\" true\" ,\" version\" :1}}" ), rootJSON )
379+ assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" root\" ,\" consistent_snapshot\" :true,\" expires\" :\" 2030-08-15T14:30:45Z \" ,\" keys\" :{},\" roles\" :{\" root\" :{\" keyids\" :[],\" threshold\" :1},\" snapshot\" :{\" keyids\" :[],\" threshold\" :1},\" targets\" :{\" keyids\" :[],\" threshold\" :1},\" timestamp\" :{\" keyids\" :[],\" threshold\" :1}},\" spec_version\" :\" 1.0.31\" ,\" test\" :\" true\" ,\" version\" :1}}" ), rootJSON )
380380
381381 targets := Targets (fixedExpire )
382382 targets .Signed .UnrecognizedFields = testUnrecognizedField
383383 targetsJSON , err := targets .ToBytes (false )
384384 assert .NoError (t , err )
385- assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" targets\" ,\" expires\" :\" 2030-08-15T14:30:45.0000001Z \" ,\" spec_version\" :\" 1.0.31\" ,\" targets\" :{},\" test\" :\" true\" ,\" version\" :1}}" ), targetsJSON )
385+ assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" targets\" ,\" expires\" :\" 2030-08-15T14:30:45Z \" ,\" spec_version\" :\" 1.0.31\" ,\" targets\" :{},\" test\" :\" true\" ,\" version\" :1}}" ), targetsJSON )
386386
387387 snapshot := Snapshot (fixedExpire )
388388 snapshot .Signed .UnrecognizedFields = testUnrecognizedField
389389 snapshotJSON , err := snapshot .ToBytes (false )
390390 assert .NoError (t , err )
391- assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" snapshot\" ,\" expires\" :\" 2030-08-15T14:30:45.0000001Z \" ,\" meta\" :{\" targets.json\" :{\" version\" :1}},\" spec_version\" :\" 1.0.31\" ,\" test\" :\" true\" ,\" version\" :1}}" ), snapshotJSON )
391+ assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" snapshot\" ,\" expires\" :\" 2030-08-15T14:30:45Z \" ,\" meta\" :{\" targets.json\" :{\" version\" :1}},\" spec_version\" :\" 1.0.31\" ,\" test\" :\" true\" ,\" version\" :1}}" ), snapshotJSON )
392392
393393 timestamp := Timestamp (fixedExpire )
394394 timestamp .Signed .UnrecognizedFields = testUnrecognizedField
395395 timestampJSON , err := timestamp .ToBytes (false )
396396 assert .NoError (t , err )
397- assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" timestamp\" ,\" expires\" :\" 2030-08-15T14:30:45.0000001Z \" ,\" meta\" :{\" snapshot.json\" :{\" version\" :1}},\" spec_version\" :\" 1.0.31\" ,\" test\" :\" true\" ,\" version\" :1}}" ), timestampJSON )
397+ assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" timestamp\" ,\" expires\" :\" 2030-08-15T14:30:45Z \" ,\" meta\" :{\" snapshot.json\" :{\" version\" :1}},\" spec_version\" :\" 1.0.31\" ,\" test\" :\" true\" ,\" version\" :1}}" ), timestampJSON )
398398}
399399func TestUnrecognizedFieldGenericMetadata (t * testing.T ) {
400400 // fixed expire
@@ -408,7 +408,7 @@ func TestUnrecognizedFieldGenericMetadata(t *testing.T) {
408408 root .UnrecognizedFields = testUnrecognizedField
409409 rootJSON , err := root .ToBytes (false )
410410 assert .NoError (t , err )
411- assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" root\" ,\" consistent_snapshot\" :true,\" expires\" :\" 2030-08-15T14:30:45.0000001Z \" ,\" keys\" :{},\" roles\" :{\" root\" :{\" keyids\" :[],\" threshold\" :1},\" snapshot\" :{\" keyids\" :[],\" threshold\" :1},\" targets\" :{\" keyids\" :[],\" threshold\" :1},\" timestamp\" :{\" keyids\" :[],\" threshold\" :1}},\" spec_version\" :\" 1.0.31\" ,\" version\" :1},\" test\" :\" true\" }" ), rootJSON )
411+ assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" root\" ,\" consistent_snapshot\" :true,\" expires\" :\" 2030-08-15T14:30:45Z \" ,\" keys\" :{},\" roles\" :{\" root\" :{\" keyids\" :[],\" threshold\" :1},\" snapshot\" :{\" keyids\" :[],\" threshold\" :1},\" targets\" :{\" keyids\" :[],\" threshold\" :1},\" timestamp\" :{\" keyids\" :[],\" threshold\" :1}},\" spec_version\" :\" 1.0.31\" ,\" version\" :1},\" test\" :\" true\" }" ), rootJSON )
412412}
413413func TestTargetFilesCustomField (t * testing.T ) {
414414 // custom JSON to test
@@ -426,7 +426,7 @@ func TestTargetFilesCustomField(t *testing.T) {
426426 targets .Signed .Targets ["testTarget" ] = targetFile
427427 targetsJSON , err := targets .ToBytes (false )
428428 assert .NoError (t , err )
429- assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" targets\" ,\" expires\" :\" 2030-08-15T14:30:45.0000001Z \" ,\" spec_version\" :\" 1.0.31\" ,\" targets\" :{\" testTarget\" :{\" custom\" :{\" test\" :true},\" hashes\" :{},\" length\" :0}},\" version\" :1}}" ), targetsJSON )
429+ assert .Equal (t , []byte ("{\" signatures\" :[],\" signed\" :{\" _type\" :\" targets\" ,\" expires\" :\" 2030-08-15T14:30:45Z \" ,\" spec_version\" :\" 1.0.31\" ,\" targets\" :{\" testTarget\" :{\" custom\" :{\" test\" :true},\" hashes\" :{},\" length\" :0}},\" version\" :1}}" ), targetsJSON )
430430}
431431
432432func TestFromBytes (t * testing.T ) {
@@ -509,7 +509,10 @@ func TestToByte(t *testing.T) {
509509 root .Signatures = append (root .Signatures , Signature {KeyID : "roothash" , Signature : hash ["ed25519" ]})
510510 rootBytes , err := root .ToBytes (false )
511511 assert .NoError (t , err )
512- assert .Equal (t , string (testRootBytes ), string (rootBytes ))
512+ // Even though the input expires string carries sub-second precision, the
513+ // serialized output must use the spec-required whole-second UTC format.
514+ expectedRootBytes := []byte ("{\" signatures\" :[{\" keyid\" :\" roothash\" ,\" sig\" :\" 1307990e6ba5ca145eb35e99182a9bec46531bc54ddf656a602c780fa0240dee\" }],\" signed\" :{\" _type\" :\" root\" ,\" consistent_snapshot\" :true,\" expires\" :\" 2030-08-15T14:30:45Z\" ,\" keys\" :{\" roothash\" :{\" keytype\" :\" ed25519\" ,\" keyval\" :{\" public\" :\" pubrootval\" },\" scheme\" :\" ed25519\" },\" snapshothash\" :{\" keytype\" :\" ed25519\" ,\" keyval\" :{\" public\" :\" pubsval\" },\" scheme\" :\" ed25519\" },\" targetshash\" :{\" keytype\" :\" ed25519\" ,\" keyval\" :{\" public\" :\" pubtrval\" },\" scheme\" :\" ed25519\" },\" timestamphash\" :{\" keytype\" :\" ed25519\" ,\" keyval\" :{\" public\" :\" pubtmval\" },\" scheme\" :\" ed25519\" }},\" roles\" :{\" root\" :{\" keyids\" :[\" roothash\" ],\" threshold\" :1},\" snapshot\" :{\" keyids\" :[\" snapshothash\" ],\" threshold\" :1},\" targets\" :{\" keyids\" :[\" targetshash\" ],\" threshold\" :1},\" timestamp\" :{\" keyids\" :[\" timestamphash\" ],\" threshold\" :1}},\" spec_version\" :\" 1.0.31\" ,\" version\" :1}}" )
515+ assert .Equal (t , string (expectedRootBytes ), string (rootBytes ))
513516}
514517
515518func TestFromFile (t * testing.T ) {
@@ -573,7 +576,10 @@ func TestToFile(t *testing.T) {
573576 assert .FileExists (t , fileName )
574577 data , err := os .ReadFile (fileName )
575578 assert .NoError (t , err )
576- assert .Equal (t , string (testRootBytes ), string (data ))
579+ // The input bytes carry a sub-second expires; the written output must use
580+ // the spec-required whole-second UTC format.
581+ expectedBytes := []byte ("{\" signatures\" :[{\" keyid\" :\" roothash\" ,\" sig\" :\" 1307990e6ba5ca145eb35e99182a9bec46531bc54ddf656a602c780fa0240dee\" }],\" signed\" :{\" _type\" :\" root\" ,\" consistent_snapshot\" :true,\" expires\" :\" 2030-08-15T14:30:45Z\" ,\" keys\" :{\" roothash\" :{\" keytype\" :\" ed25519\" ,\" keyval\" :{\" public\" :\" pubrootval\" },\" scheme\" :\" ed25519\" },\" snapshothash\" :{\" keytype\" :\" ed25519\" ,\" keyval\" :{\" public\" :\" pubsval\" },\" scheme\" :\" ed25519\" },\" targetshash\" :{\" keytype\" :\" ed25519\" ,\" keyval\" :{\" public\" :\" pubtrval\" },\" scheme\" :\" ed25519\" },\" timestamphash\" :{\" keytype\" :\" ed25519\" ,\" keyval\" :{\" public\" :\" pubtmval\" },\" scheme\" :\" ed25519\" }},\" roles\" :{\" root\" :{\" keyids\" :[\" roothash\" ],\" threshold\" :1},\" snapshot\" :{\" keyids\" :[\" snapshothash\" ],\" threshold\" :1},\" targets\" :{\" keyids\" :[\" targetshash\" ],\" threshold\" :1},\" timestamp\" :{\" keyids\" :[\" timestamphash\" ],\" threshold\" :1}},\" spec_version\" :\" 1.0.31\" ,\" version\" :1}}" )
582+ assert .Equal (t , string (expectedBytes ), string (data ))
577583
578584 err = os .RemoveAll (tmpDir )
579585 assert .NoError (t , err )
0 commit comments