@@ -276,8 +276,8 @@ pub fn create_operation_mnemonic(mnemonic: String) -> Result<String> {
276
276
#[ cfg( test) ]
277
277
mod tests {
278
278
use ssi:: vc:: CredentialOrJWT ;
279
- use trustchain_core:: utils:: canonicalize_str;
280
- use trustchain_http:: utils :: init_http ;
279
+ use trustchain_core:: utils:: { canonicalize_str, init } ;
280
+ use trustchain_http:: config :: HTTPConfig ;
281
281
282
282
use crate :: config:: parse_toml;
283
283
@@ -416,45 +416,79 @@ mod tests {
416
416
"did": "did:ion:test:EiA1dZD7jVkS5ZP7JJO01t6HgTU3eeLpbKEV1voOFWJV0g"
417
417
}"# ;
418
418
419
+ fn init_http_ephemeral ( ) -> u16 {
420
+ init ( ) ;
421
+ // Create channel to receive port number from the thread with the server
422
+ let ( tx, rx) = std:: sync:: mpsc:: channel ( ) ;
423
+ std:: thread:: spawn ( move || {
424
+ let http_config = HTTPConfig {
425
+ host : "127.0.0.1" . parse ( ) . unwrap ( ) ,
426
+ port : 0 ,
427
+ server_did : Some (
428
+ "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A" . to_owned ( ) ,
429
+ ) ,
430
+ root_event_time : Some ( 1666265405 ) ,
431
+ ..Default :: default ( )
432
+ } ;
433
+ let rt = Runtime :: new ( ) . unwrap ( ) ;
434
+ rt. block_on ( async {
435
+ let server = trustchain_http:: server:: http_server ( http_config) ;
436
+ // Send assigned ephemeral port number to receiver
437
+ tx. send ( server. local_addr ( ) . port ( ) ) . unwrap ( ) ;
438
+ server. await . unwrap ( ) ;
439
+ } ) ;
440
+ } ) ;
441
+ // Receive port number to return for client
442
+ rx. recv ( ) . unwrap ( )
443
+ }
444
+
445
+ fn ffi_opts_with_port ( ffi_config : & str , port : u16 ) -> String {
446
+ let mut ffi_config = parse_toml ( ffi_config) ;
447
+ ffi_config
448
+ . endpoint_options
449
+ . as_mut ( )
450
+ . unwrap ( )
451
+ . trustchain_endpoint
452
+ . port = port;
453
+ serde_json:: to_string ( & ffi_config) . unwrap ( )
454
+ }
455
+
419
456
#[ test]
420
457
#[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
421
458
fn test_did_resolve ( ) {
422
- init_http ( ) ;
459
+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG , init_http_ephemeral ( ) ) ;
423
460
let did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q" . to_string ( ) ;
424
- let ffi_opts = serde_json:: to_string ( & parse_toml ( TEST_FFI_CONFIG ) ) . unwrap ( ) ;
425
461
did_resolve ( did, ffi_opts) . unwrap ( ) ;
426
462
}
427
463
428
464
#[ test]
429
465
#[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
430
466
fn test_did_verify ( ) {
431
- init_http ( ) ;
467
+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG , init_http_ephemeral ( ) ) ;
432
468
let did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q" . to_string ( ) ;
433
- let ffi_opts = serde_json:: to_string ( & parse_toml ( TEST_FFI_CONFIG ) ) . unwrap ( ) ;
434
469
did_verify ( did, ffi_opts) . unwrap ( ) ;
435
470
}
436
471
437
472
#[ test]
438
473
#[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
439
474
fn test_vc_verify_credential ( ) {
440
- init_http ( ) ;
441
- let ffi_opts = serde_json:: to_string ( & parse_toml ( TEST_FFI_CONFIG ) ) . unwrap ( ) ;
475
+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG , init_http_ephemeral ( ) ) ;
442
476
let credential: Credential = serde_json:: from_str ( TEST_CREDENTIAL ) . unwrap ( ) ;
443
477
vc_verify_credential ( serde_json:: to_string ( & credential) . unwrap ( ) , ffi_opts) . unwrap ( ) ;
444
478
}
445
479
446
480
#[ test]
447
481
#[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
448
482
fn test_vc_verify_rss_credential ( ) {
449
- let ffi_opts = serde_json :: to_string ( & parse_toml ( TEST_FFI_CONFIG_RSS ) ) . unwrap ( ) ;
483
+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG_RSS , init_http_ephemeral ( ) ) ;
450
484
let credential: Credential = serde_json:: from_str ( TEST_CREDENTIAL_RSS ) . unwrap ( ) ;
451
485
vc_verify_credential ( serde_json:: to_string ( & credential) . unwrap ( ) , ffi_opts) . unwrap ( ) ;
452
486
}
453
487
454
488
#[ test]
455
489
#[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
456
490
fn test_vc_redact_rss_credential ( ) {
457
- let ffi_opts = serde_json :: to_string ( & parse_toml ( TEST_FFI_CONFIG_RSS ) ) . unwrap ( ) ;
491
+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG_RSS , init_http_ephemeral ( ) ) ;
458
492
let credential: Credential = serde_json:: from_str ( TEST_CREDENTIAL_RSS ) . unwrap ( ) ;
459
493
let credential_subject_mask: CredentialSubject =
460
494
serde_json:: from_str ( TEST_CREDENTIAL_SUBJECT_MASK ) . unwrap ( ) ;
@@ -470,7 +504,7 @@ mod tests {
470
504
#[ test]
471
505
#[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
472
506
fn test_vp_issue_presentation ( ) {
473
- let ffi_opts = serde_json :: to_string ( & parse_toml ( TEST_FFI_CONFIG ) ) . unwrap ( ) ;
507
+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG_RSS , init_http_ephemeral ( ) ) ;
474
508
let credential: Credential = serde_json:: from_str ( TEST_CREDENTIAL ) . unwrap ( ) ;
475
509
let root_plus_1_did: & str = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A" ;
476
510
let presentation: Presentation = Presentation {
@@ -510,8 +544,7 @@ mod tests {
510
544
#[ test]
511
545
#[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
512
546
fn test_vp_verify_presentation ( ) {
513
- init_http ( ) ;
514
- let ffi_opts = serde_json:: to_string ( & parse_toml ( TEST_FFI_CONFIG ) ) . unwrap ( ) ;
547
+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG , init_http_ephemeral ( ) ) ;
515
548
vp_verify_presentation ( TEST_PRESENTATION . to_string ( ) , ffi_opts) . unwrap ( ) ;
516
549
}
517
550
0 commit comments