Skip to content

Commit

Permalink
Merge pull request #220 from alan-turing-institute/update-mobile-test…
Browse files Browse the repository at this point in the history
…-servers

Update test servers in mobile with ephemeral ports
  • Loading branch information
sgreenbury authored Jan 20, 2025
2 parents 32b9716 + 63b6933 commit 6028c57
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ log = "0.4"
mockall = "0.11.4"
mongodb = "2.3.1"
petgraph = "0.6"
port_check = "0.2.1"
ps_sig = { git = "https://github.com/alan-turing-institute/RSS.git", rev = "ec9386e125d87c5f54898b34fbe0883b3b36ffd4" }
qrcode = "0.12.0"
rand = "0.8"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cargo build
```
Install the Trustchain CLI with:
```shell
cargo install --path trustchain-cli
cargo install --path crates/trustchain-cli
```

## Usage Guide
Expand Down
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: 1 addition & 0 deletions crates/trustchain-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ trustchain-ion = { path = "../trustchain-ion" }
axum-test-helper = { workspace = true }
itertools = { workspace = true }
mockall = { workspace = true }
port_check = { workspace = true }
tempfile = { workspace = true }
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.

33 changes: 30 additions & 3 deletions crates/trustchain-http/tests/attestation.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
/// Integration test for attestation challenge-response process.
//! Integration test for attestation challenge-response process.
use port_check::is_port_reachable;
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();
assert!(
!is_port_reachable("127.0.0.1:8081"),
"Port 8081 is required for Challenge-Response integration test but 8081 is already in use."
);
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 6028c57

Please sign in to comment.