Skip to content

Commit 6028c57

Browse files
authored
Merge pull request #220 from alan-turing-institute/update-mobile-test-servers
Update test servers in mobile with ephemeral ports
2 parents 32b9716 + 63b6933 commit 6028c57

File tree

7 files changed

+79
-46
lines changed

7 files changed

+79
-46
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ log = "0.4"
4545
mockall = "0.11.4"
4646
mongodb = "2.3.1"
4747
petgraph = "0.6"
48+
port_check = "0.2.1"
4849
ps_sig = { git = "https://github.com/alan-turing-institute/RSS.git", rev = "ec9386e125d87c5f54898b34fbe0883b3b36ffd4" }
4950
qrcode = "0.12.0"
5051
rand = "0.8"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ cargo build
3838
```
3939
Install the Trustchain CLI with:
4040
```shell
41-
cargo install --path trustchain-cli
41+
cargo install --path crates/trustchain-cli
4242
```
4343

4444
## Usage Guide

crates/trustchain-ffi/src/mobile.rs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ pub fn create_operation_mnemonic(mnemonic: String) -> Result<String> {
276276
#[cfg(test)]
277277
mod 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

crates/trustchain-http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ trustchain-ion = { path = "../trustchain-ion" }
4444
axum-test-helper = { workspace = true }
4545
itertools = { workspace = true }
4646
mockall = { workspace = true }
47+
port_check = { workspace = true }
4748
tempfile = { workspace = true }

crates/trustchain-http/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub mod server;
1717
pub mod state;
1818
pub mod static_handlers;
1919
pub mod store;
20-
pub mod utils;
2120
pub mod verifier;
2221

2322
/// Fragment for service ID of Trustchain attestion

crates/trustchain-http/src/utils.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

crates/trustchain-http/tests/attestation.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,56 @@
1-
/// Integration test for attestation challenge-response process.
1+
//! Integration test for attestation challenge-response process.
2+
use port_check::is_port_reachable;
3+
use tokio::runtime::Runtime;
24
use trustchain_core::verifier::Verifier;
35
use trustchain_http::attestation_encryption_utils::{josekit_to_ssi_jwk, ssi_to_josekit_jwk};
46
use trustchain_http::attestation_utils::{
57
attestation_request_path, CRState, ElementwiseSerializeDeserialize, IdentityCRChallenge,
68
IdentityCRInitiation,
79
};
810
use trustchain_http::attestor::present_identity_challenge;
11+
use trustchain_http::config::HTTPConfig;
912
use trustchain_http::requester::{
1013
identity_response, initiate_content_challenge, initiate_identity_challenge,
1114
};
1215

13-
use trustchain_http::utils::init_http;
1416
use trustchain_ion::{trustchain_resolver, verifier::TrustchainVerifier};
1517

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

1921
use mockall::automock;
20-
use trustchain_core::utils::extract_keys;
22+
use trustchain_core::utils::{extract_keys, init};
2123

2224
#[automock]
2325
pub trait AttestationUtils {
2426
fn attestation_request_path(&self) -> String;
2527
}
2628

29+
fn init_http() {
30+
init();
31+
assert!(
32+
!is_port_reachable("127.0.0.1:8081"),
33+
"Port 8081 is required for Challenge-Response integration test but 8081 is already in use."
34+
);
35+
let http_config = HTTPConfig {
36+
host: "127.0.0.1".parse().unwrap(),
37+
port: 8081,
38+
server_did: Some("did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A".to_owned()),
39+
root_event_time: Some(1666265405),
40+
..Default::default()
41+
};
42+
43+
// Run test server in own thread
44+
std::thread::spawn(|| {
45+
let rt = Runtime::new().unwrap();
46+
rt.block_on(async {
47+
trustchain_http::server::http_server(http_config)
48+
.await
49+
.unwrap();
50+
});
51+
});
52+
}
53+
2754
#[tokio::test]
2855
#[ignore]
2956
async fn attestation_challenge_response() {

0 commit comments

Comments
 (0)