@@ -275,114 +275,3 @@ impl TryFrom<CatalystSignedDocument> for Vec<u8> {
275
275
Ok ( minicbor:: to_vec ( value) ?)
276
276
}
277
277
}
278
-
279
- #[ cfg( test) ]
280
- mod tests {
281
- use std:: str:: FromStr ;
282
-
283
- use ed25519_dalek:: { SigningKey , VerifyingKey } ;
284
- use metadata:: { ContentEncoding , ContentType } ;
285
- use rand:: rngs:: OsRng ;
286
-
287
- use super :: * ;
288
- use crate :: { providers:: VerifyingKeyProvider , validator:: validate_signatures} ;
289
-
290
- fn test_metadata ( ) -> ( UuidV7 , UuidV4 , serde_json:: Value ) {
291
- let alg = Algorithm :: EdDSA ;
292
- let uuid_v7 = UuidV7 :: new ( ) ;
293
- let uuid_v4 = UuidV4 :: new ( ) ;
294
- let section = "$" . to_string ( ) ;
295
- let collabs = vec ! [ "Alex1" . to_string( ) , "Alex2" . to_string( ) ] ;
296
- let content_type = ContentType :: Json ;
297
- let content_encoding = ContentEncoding :: Brotli ;
298
-
299
- let metadata_fields = serde_json:: json!( {
300
- "alg" : alg. to_string( ) ,
301
- "content-type" : content_type. to_string( ) ,
302
- "content-encoding" : content_encoding. to_string( ) ,
303
- "type" : uuid_v4. to_string( ) ,
304
- "id" : uuid_v7. to_string( ) ,
305
- "ver" : uuid_v7. to_string( ) ,
306
- "ref" : { "id" : uuid_v7. to_string( ) } ,
307
- "reply" : { "id" : uuid_v7. to_string( ) , "ver" : uuid_v7. to_string( ) } ,
308
- "template" : { "id" : uuid_v7. to_string( ) } ,
309
- "section" : section,
310
- "collabs" : collabs,
311
- "campaign_id" : { "id" : uuid_v7. to_string( ) } ,
312
- "election_id" : uuid_v4. to_string( ) ,
313
- "brand_id" : { "id" : uuid_v7. to_string( ) } ,
314
- "category_id" : { "id" : uuid_v7. to_string( ) } ,
315
- } ) ;
316
- ( uuid_v7, uuid_v4, metadata_fields)
317
- }
318
-
319
- #[ test]
320
- fn catalyst_signed_doc_cbor_roundtrip_test ( ) {
321
- let ( uuid_v7, uuid_v4, metadata_fields) = test_metadata ( ) ;
322
- let content = serde_json:: to_vec ( & serde_json:: Value :: Null ) . unwrap ( ) ;
323
-
324
- let doc = Builder :: new ( )
325
- . with_json_metadata ( metadata_fields. clone ( ) )
326
- . unwrap ( )
327
- . with_decoded_content ( content. clone ( ) )
328
- . build ( ) ;
329
-
330
- assert ! ( !doc. problem_report( ) . is_problematic( ) ) ;
331
-
332
- let bytes: Vec < u8 > = doc. try_into ( ) . unwrap ( ) ;
333
- let decoded: CatalystSignedDocument = bytes. as_slice ( ) . try_into ( ) . unwrap ( ) ;
334
-
335
- assert_eq ! ( decoded. doc_type( ) . unwrap( ) , uuid_v4) ;
336
- assert_eq ! ( decoded. doc_id( ) . unwrap( ) , uuid_v7) ;
337
- assert_eq ! ( decoded. doc_ver( ) . unwrap( ) , uuid_v7) ;
338
- assert_eq ! ( decoded. doc_content( ) . decoded_bytes( ) . unwrap( ) , & content) ;
339
- // TODO: after this test will be moved as a crate integration test, enable this
340
- // assertion assert_eq!(decoded.doc_meta(), metadata_fields.extra());
341
- }
342
-
343
- struct Provider ( anyhow:: Result < Option < VerifyingKey > > ) ;
344
- impl VerifyingKeyProvider for Provider {
345
- async fn try_get_key (
346
- & self , _kid : & IdUri ,
347
- ) -> anyhow:: Result < Option < ed25519_dalek:: VerifyingKey > > {
348
- let res = self . 0 . as_ref ( ) . map_err ( |e| anyhow:: anyhow!( "{e}" ) ) ?;
349
- Ok ( * res)
350
- }
351
- }
352
-
353
- #[ tokio:: test]
354
- async fn signature_verification_test ( ) {
355
- let mut csprng = OsRng ;
356
- let sk: SigningKey = SigningKey :: generate ( & mut csprng) ;
357
- let content = serde_json:: to_vec ( & serde_json:: Value :: Null ) . unwrap ( ) ;
358
- let pk = sk. verifying_key ( ) ;
359
-
360
- let kid_str = format ! (
361
- "id.catalyst://cardano/{}/0/0" ,
362
- base64_url:: encode( pk. as_bytes( ) )
363
- ) ;
364
-
365
- let kid = IdUri :: from_str ( & kid_str) . unwrap ( ) ;
366
- let ( _, _, metadata) = test_metadata ( ) ;
367
- let signed_doc = Builder :: new ( )
368
- . with_decoded_content ( content)
369
- . with_json_metadata ( metadata)
370
- . unwrap ( )
371
- . add_signature ( sk. to_bytes ( ) , kid. clone ( ) )
372
- . unwrap ( )
373
- . build ( ) ;
374
- assert ! ( !signed_doc. problem_report( ) . is_problematic( ) ) ;
375
-
376
- assert ! (
377
- validate_signatures( & signed_doc, & Provider ( Err ( anyhow:: anyhow!( "some error" ) ) ) )
378
- . await
379
- . is_err( )
380
- ) ;
381
- assert ! ( validate_signatures( & signed_doc, & Provider ( Ok ( Some ( pk) ) ) )
382
- . await
383
- . unwrap( ) ) ;
384
- assert ! ( !validate_signatures( & signed_doc, & Provider ( Ok ( None ) ) )
385
- . await
386
- . unwrap( ) ) ;
387
- }
388
- }
0 commit comments