Skip to content

Commit

Permalink
Update test servers in mobile with ephemeral ports
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreenbury committed Jan 13, 2025
1 parent 74d6d15 commit 642d970
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
59 changes: 46 additions & 13 deletions crates/trustchain-ffi/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ pub fn create_operation_mnemonic(mnemonic: String) -> Result<String> {
#[cfg(test)]
mod tests {
use ssi::vc::CredentialOrJWT;
use trustchain_core::utils::canonicalize_str;
use trustchain_http::utils::init_http;
use trustchain_core::utils::{canonicalize_str, init};
use trustchain_http::config::HTTPConfig;

use crate::config::parse_toml;

Expand Down Expand Up @@ -416,45 +416,79 @@ mod tests {
"did": "did:ion:test:EiA1dZD7jVkS5ZP7JJO01t6HgTU3eeLpbKEV1voOFWJV0g"
}"#;

fn init_http_ephemeral() -> u16 {
init();
// Create channel to receive port number from the thread with the server
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let http_config = HTTPConfig {
host: "127.0.0.1".parse().unwrap(),
port: 0,
server_did: Some(
"did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A".to_owned(),
),
root_event_time: Some(1666265405),
..Default::default()
};
let rt = Runtime::new().unwrap();
rt.block_on(async {
let server = trustchain_http::server::http_server(http_config);
// Send assigned ephemeral port number to receiver
tx.send(server.local_addr().port()).unwrap();
server.await.unwrap();
});
});
// Receive port number to return for client
rx.recv().unwrap()
}

fn ffi_opts_with_port(ffi_config: &str, port: u16) -> String {
let mut ffi_config = parse_toml(ffi_config);
ffi_config
.endpoint_options
.as_mut()
.unwrap()
.trustchain_endpoint
.port = port;
serde_json::to_string(&ffi_config).unwrap()
}

#[test]
#[ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC"]
fn test_did_resolve() {
init_http();
let ffi_opts = ffi_opts_with_port(TEST_FFI_CONFIG, init_http_ephemeral());
let did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q".to_string();
let ffi_opts = serde_json::to_string(&parse_toml(TEST_FFI_CONFIG)).unwrap();
did_resolve(did, ffi_opts).unwrap();
}

#[test]
#[ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC"]
fn test_did_verify() {
init_http();
let ffi_opts = ffi_opts_with_port(TEST_FFI_CONFIG, init_http_ephemeral());
let did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q".to_string();
let ffi_opts = serde_json::to_string(&parse_toml(TEST_FFI_CONFIG)).unwrap();
did_verify(did, ffi_opts).unwrap();
}

#[test]
#[ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC"]
fn test_vc_verify_credential() {
init_http();
let ffi_opts = serde_json::to_string(&parse_toml(TEST_FFI_CONFIG)).unwrap();
let ffi_opts = ffi_opts_with_port(TEST_FFI_CONFIG, init_http_ephemeral());
let credential: Credential = serde_json::from_str(TEST_CREDENTIAL).unwrap();
vc_verify_credential(serde_json::to_string(&credential).unwrap(), ffi_opts).unwrap();
}

#[test]
#[ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC"]
fn test_vc_verify_rss_credential() {
let ffi_opts = serde_json::to_string(&parse_toml(TEST_FFI_CONFIG_RSS)).unwrap();
let ffi_opts = ffi_opts_with_port(TEST_FFI_CONFIG_RSS, init_http_ephemeral());
let credential: Credential = serde_json::from_str(TEST_CREDENTIAL_RSS).unwrap();
vc_verify_credential(serde_json::to_string(&credential).unwrap(), ffi_opts).unwrap();
}

#[test]
#[ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC"]
fn test_vc_redact_rss_credential() {
let ffi_opts = serde_json::to_string(&parse_toml(TEST_FFI_CONFIG_RSS)).unwrap();
let ffi_opts = ffi_opts_with_port(TEST_FFI_CONFIG_RSS, init_http_ephemeral());
let credential: Credential = serde_json::from_str(TEST_CREDENTIAL_RSS).unwrap();
let credential_subject_mask: CredentialSubject =
serde_json::from_str(TEST_CREDENTIAL_SUBJECT_MASK).unwrap();
Expand All @@ -470,7 +504,7 @@ mod tests {
#[test]
#[ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC"]
fn test_vp_issue_presentation() {
let ffi_opts = serde_json::to_string(&parse_toml(TEST_FFI_CONFIG)).unwrap();
let ffi_opts = ffi_opts_with_port(TEST_FFI_CONFIG_RSS, init_http_ephemeral());
let credential: Credential = serde_json::from_str(TEST_CREDENTIAL).unwrap();
let root_plus_1_did: &str = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A";
let presentation: Presentation = Presentation {
Expand Down Expand Up @@ -510,8 +544,7 @@ mod tests {
#[test]
#[ignore = "integration test requires ION, MongoDB, IPFS and Bitcoin RPC"]
fn test_vp_verify_presentation() {
init_http();
let ffi_opts = serde_json::to_string(&parse_toml(TEST_FFI_CONFIG)).unwrap();
let ffi_opts = ffi_opts_with_port(TEST_FFI_CONFIG, init_http_ephemeral());
vp_verify_presentation(TEST_PRESENTATION.to_string(), ffi_opts).unwrap();
}

Expand Down
1 change: 0 additions & 1 deletion crates/trustchain-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod server;
pub mod state;
pub mod static_handlers;
pub mod store;
pub mod utils;
pub mod verifier;

/// Fragment for service ID of Trustchain attestion
Expand Down
28 changes: 0 additions & 28 deletions crates/trustchain-http/src/utils.rs

This file was deleted.

26 changes: 24 additions & 2 deletions crates/trustchain-http/tests/attestation.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
/// Integration test for attestation challenge-response process.
use tokio::runtime::Runtime;
use trustchain_core::verifier::Verifier;
use trustchain_http::attestation_encryption_utils::{josekit_to_ssi_jwk, ssi_to_josekit_jwk};
use trustchain_http::attestation_utils::{
attestation_request_path, CRState, ElementwiseSerializeDeserialize, IdentityCRChallenge,
IdentityCRInitiation,
};
use trustchain_http::attestor::present_identity_challenge;
use trustchain_http::config::HTTPConfig;
use trustchain_http::requester::{
identity_response, initiate_content_challenge, initiate_identity_challenge,
};

use trustchain_http::utils::init_http;
use trustchain_ion::{trustchain_resolver, verifier::TrustchainVerifier};

// The root event time of DID documents used in integration test below.
const ROOT_EVENT_TIME_1: u64 = 1666265405;

use mockall::automock;
use trustchain_core::utils::extract_keys;
use trustchain_core::utils::{extract_keys, init};

#[automock]
pub trait AttestationUtils {
fn attestation_request_path(&self) -> String;
}

fn init_http() {
init();
let http_config = HTTPConfig {
host: "127.0.0.1".parse().unwrap(),
port: 8081,
server_did: Some("did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A".to_owned()),
root_event_time: Some(1666265405),
..Default::default()
};

// Run test server in own thread
std::thread::spawn(|| {
let rt = Runtime::new().unwrap();
rt.block_on(async {
trustchain_http::server::http_server(http_config)
.await
.unwrap();
});
});
}

#[tokio::test]
#[ignore]
async fn attestation_challenge_response() {
Expand Down

0 comments on commit 642d970

Please sign in to comment.