@@ -276,8 +276,8 @@ pub fn create_operation_mnemonic(mnemonic: String) -> Result<String> {
276276#[ cfg( test) ]
277277mod tests {
278278 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 ;
281281
282282 use crate :: config:: parse_toml;
283283
@@ -416,45 +416,79 @@ mod tests {
416416 "did": "did:ion:test:EiA1dZD7jVkS5ZP7JJO01t6HgTU3eeLpbKEV1voOFWJV0g"
417417 }"# ;
418418
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+
419456 #[ test]
420457 #[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
421458 fn test_did_resolve ( ) {
422- init_http ( ) ;
459+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG , init_http_ephemeral ( ) ) ;
423460 let did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q" . to_string ( ) ;
424- let ffi_opts = serde_json:: to_string ( & parse_toml ( TEST_FFI_CONFIG ) ) . unwrap ( ) ;
425461 did_resolve ( did, ffi_opts) . unwrap ( ) ;
426462 }
427463
428464 #[ test]
429465 #[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
430466 fn test_did_verify ( ) {
431- init_http ( ) ;
467+ let ffi_opts = ffi_opts_with_port ( TEST_FFI_CONFIG , init_http_ephemeral ( ) ) ;
432468 let did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q" . to_string ( ) ;
433- let ffi_opts = serde_json:: to_string ( & parse_toml ( TEST_FFI_CONFIG ) ) . unwrap ( ) ;
434469 did_verify ( did, ffi_opts) . unwrap ( ) ;
435470 }
436471
437472 #[ test]
438473 #[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
439474 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 ( ) ) ;
442476 let credential: Credential = serde_json:: from_str ( TEST_CREDENTIAL ) . unwrap ( ) ;
443477 vc_verify_credential ( serde_json:: to_string ( & credential) . unwrap ( ) , ffi_opts) . unwrap ( ) ;
444478 }
445479
446480 #[ test]
447481 #[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
448482 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 ( ) ) ;
450484 let credential: Credential = serde_json:: from_str ( TEST_CREDENTIAL_RSS ) . unwrap ( ) ;
451485 vc_verify_credential ( serde_json:: to_string ( & credential) . unwrap ( ) , ffi_opts) . unwrap ( ) ;
452486 }
453487
454488 #[ test]
455489 #[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
456490 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 ( ) ) ;
458492 let credential: Credential = serde_json:: from_str ( TEST_CREDENTIAL_RSS ) . unwrap ( ) ;
459493 let credential_subject_mask: CredentialSubject =
460494 serde_json:: from_str ( TEST_CREDENTIAL_SUBJECT_MASK ) . unwrap ( ) ;
@@ -470,7 +504,7 @@ mod tests {
470504 #[ test]
471505 #[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
472506 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 ( ) ) ;
474508 let credential: Credential = serde_json:: from_str ( TEST_CREDENTIAL ) . unwrap ( ) ;
475509 let root_plus_1_did: & str = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A" ;
476510 let presentation: Presentation = Presentation {
@@ -510,8 +544,7 @@ mod tests {
510544 #[ test]
511545 #[ ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC" ]
512546 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 ( ) ) ;
515548 vp_verify_presentation ( TEST_PRESENTATION . to_string ( ) , ffi_opts) . unwrap ( ) ;
516549 }
517550
0 commit comments