@@ -266,3 +266,61 @@ func TestSignAndVerifyListing(t *testing.T) {
266
266
t .Errorf ("verify signed listing: %v" , err )
267
267
}
268
268
}
269
+
270
+ func TestNormalize (t * testing.T ) {
271
+ testnode , err := test .NewNode ()
272
+ if err != nil {
273
+ t .Fatalf ("create test node: %v" , err )
274
+ }
275
+
276
+ priv , pub , err := crypto .GenerateEd25519Key (rand .Reader )
277
+ if err != nil {
278
+ t .Fatal (err )
279
+ }
280
+
281
+ pid , err := peer .IDFromPublicKey (pub )
282
+ if err != nil {
283
+ t .Fatal (err )
284
+ }
285
+
286
+ testnode .IpfsNode .Identity = pid
287
+ testnode .IpfsNode .PrivateKey = priv
288
+
289
+ // v4 listing is guaranteed to mutate when normalized
290
+ lb := factory .MustLoadListingFixture ("v4-cryptocurrency" )
291
+ l , err := repo .UnmarshalJSONSignedListing (lb )
292
+ if err != nil {
293
+ t .Fatalf ("unmarshal fixtured listing: %v" , err )
294
+ }
295
+
296
+ // sign replaces listing vendor ID with the one from the testnode
297
+ sl , err := l .GetListing ().Sign (testnode )
298
+ if err != nil {
299
+ t .Fatalf ("sign listing: %v" , err )
300
+ }
301
+
302
+ origSig := sl .GetSignature ()
303
+ origListingJSON , err := sl .MarshalJSON ()
304
+ if err != nil {
305
+ t .Fatalf ("marshal listing: %v" , err )
306
+ }
307
+
308
+ if err := sl .Normalize (); err != nil {
309
+ t .Fatalf ("normalize listing: %v" , err )
310
+ }
311
+
312
+ if sig := sl .GetSignature (); ! bytes .Equal (origSig , sig ) {
313
+ t .Errorf ("expected normalized signature to not change, but did" )
314
+ }
315
+
316
+ if listingJSON , err := sl .MarshalJSON (); err != nil {
317
+ t .Errorf ("marshal normalized listing: %v" , err )
318
+ } else {
319
+ // when normalizing a signed listing, the listing data mutates in place, but
320
+ // the signature should remain matched to the original (unchanged)
321
+ if bytes .Equal (origListingJSON , listingJSON ) {
322
+ t .Errorf ("expected listing JSON to change from normalization, but did not" )
323
+ t .Logf ("orig: %s\n actual: %s\n " , string (origListingJSON ), string (listingJSON ))
324
+ }
325
+ }
326
+ }
0 commit comments