From 5077af264e9bf3da5d7c7ec35bf906f05ea37cc9 Mon Sep 17 00:00:00 2001 From: Miroslav Kovar Date: Fri, 2 Oct 2020 18:48:59 +0200 Subject: [PATCH] Add local revocation integration test (#123) * Add local revocation integration test Signed-off-by: Miroslav Kovar * WIP: Test batch revocation on 3 connections Signed-off-by: Miroslav Kovar * Use differnt config per consumer Signed-off-by: Miroslav Kovar * Rename credential_offer to credential_handle Signed-off-by: Miroslav Kovar * Bump version Signed-off-by: Miroslav Kovar --- libvcx/Cargo.toml | 2 +- libvcx/src/connection.rs | 10 +- libvcx/src/lib.rs | 206 ++++++++++++++++++++------ libvcx/src/messages/agent_utils.rs | 2 +- libvcx/src/messages/get_message.rs | 4 +- libvcx/src/messages/update_message.rs | 4 +- libvcx/src/object_cache/mod.rs | 5 + libvcx/src/utils/devsetup.rs | 43 +++++- 8 files changed, 212 insertions(+), 64 deletions(-) diff --git a/libvcx/Cargo.toml b/libvcx/Cargo.toml index 881c58bd82..a0ddd5fc9b 100644 --- a/libvcx/Cargo.toml +++ b/libvcx/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "libvcx" -version = "0.10.0" +version = "0.11.0" authors = ["Absa Group Limited", "Hyperledger Indy Contributors "] publish = false description = "Absa's fork of HL LibVCX" diff --git a/libvcx/src/connection.rs b/libvcx/src/connection.rs index 5f05fc879a..c082bd86ce 100644 --- a/libvcx/src/connection.rs +++ b/libvcx/src/connection.rs @@ -295,7 +295,7 @@ pub mod tests { handle } - pub fn create_connected_connections() -> (u32, u32) { + pub fn create_connected_connections(consumer_handle: Option) -> (u32, u32) { debug!("Institution is going to create connection."); ::utils::devsetup::set_institution(); let faber_to_alice = create_connection("alice").unwrap(); @@ -303,7 +303,7 @@ pub mod tests { let details = connect(faber_to_alice).unwrap().unwrap(); update_state(faber_to_alice).unwrap(); - ::utils::devsetup::set_consumer(); + ::utils::devsetup::set_consumer(consumer_handle); debug!("Consumer is going to accept connection invitation."); let alice_to_faber = create_connection_with_invite("faber", &details).unwrap(); connect(alice_to_faber).unwrap(); @@ -316,7 +316,7 @@ pub mod tests { update_state(faber_to_alice).unwrap(); debug!("Consumer is going to complete the connection protocol."); - ::utils::devsetup::set_consumer(); + ::utils::devsetup::set_consumer(consumer_handle); update_state(alice_to_faber).unwrap(); assert_eq!(VcxStateType::VcxStateAccepted as u32, get_state(alice_to_faber)); @@ -576,13 +576,13 @@ pub mod tests { #[test] fn test_send_and_download_messages() { let _setup = SetupLibraryAgencyV2::init(); - let (alice_to_faber, faber_to_alice) = ::connection::tests::create_connected_connections(); + let (alice_to_faber, faber_to_alice) = ::connection::tests::create_connected_connections(None); send_generic_message(faber_to_alice, "Hello Alice", &json!({"msg_type": "toalice", "msg_title": "msg1"}).to_string()).unwrap(); send_generic_message(faber_to_alice, "How are you Alice?", &json!({"msg_type": "toalice", "msg_title": "msg2"}).to_string()).unwrap(); // AS CONSUMER GET MESSAGES - ::utils::devsetup::set_consumer(); + ::utils::devsetup::set_consumer(None); send_generic_message(alice_to_faber, "Hello Faber", &json!({"msg_type": "tofaber", "msg_title": "msg1"}).to_string()).unwrap(); // make sure messages has bee delivered diff --git a/libvcx/src/lib.rs b/libvcx/src/lib.rs index 8d44ffaf1d..6800d05769 100644 --- a/libvcx/src/lib.rs +++ b/libvcx/src/lib.rs @@ -71,7 +71,7 @@ mod tests { use proof; use settings; use utils::{ - constants::{DEFAULT_SCHEMA_ATTRS, TEST_TAILS_FILE}, + constants::TEST_TAILS_FILE, devsetup::{set_consumer, set_institution}, get_temp_dir_path, }; @@ -153,6 +153,7 @@ mod tests { } fn create_and_send_cred_offer(did: &str, cred_def_handle: u32, connection: u32, credential_data: &str) -> u32 { + set_institution(); info!("create_and_send_cred_offer >> creating issuer credential"); let handle_cred = issuer_credential::issuer_credential_create(cred_def_handle, "1".to_string(), @@ -167,9 +168,9 @@ mod tests { handle_cred } - fn send_cred_req(connection: u32) -> u32 { + fn send_cred_req(connection: u32, consumer_handle: Option) -> u32 { info!("send_cred_req >>> switching to consumer"); - set_consumer(); + set_consumer(consumer_handle); info!("send_cred_req :: getting offers"); let credential_offers = credential::get_credential_offer_messages(connection).unwrap(); let offers: Value = serde_json::from_str(&credential_offers).unwrap(); @@ -183,24 +184,25 @@ mod tests { credential } - fn send_credential(issuer_handle: u32, connection: u32, credential_handle: u32) { + fn send_credential(issuer_handle: u32, connection: u32, credential_handle: u32, consumer_handle: Option) { info!("send_credential >>> switching to institution"); set_institution(); info!("send_credential >>> getting offers"); issuer_credential::update_state(issuer_handle, None, None).unwrap(); assert_eq!(VcxStateType::VcxStateRequestReceived as u32, issuer_credential::get_state(issuer_handle).unwrap()); - println!("sending credential"); + info!("sending credential"); issuer_credential::send_credential(issuer_handle, connection).unwrap(); thread::sleep(Duration::from_millis(2000)); // AS CONSUMER STORE CREDENTIAL - ::utils::devsetup::set_consumer(); + ::utils::devsetup::set_consumer(consumer_handle); credential::update_state(credential_handle, None, None).unwrap(); thread::sleep(Duration::from_millis(2000)); - println!("storing credential"); + info!("storing credential"); assert_eq!(VcxStateType::VcxStateAccepted as u32, credential::get_state(credential_handle).unwrap()); } fn send_proof_request(connection_handle: u32, requested_attrs: &str, requested_preds: &str, revocation_interval: &str) -> u32 { + set_institution(); let proof_req_handle = proof::create_proof("1".to_string(), requested_attrs.to_string(), requested_preds.to_string(), @@ -211,8 +213,8 @@ mod tests { proof_req_handle } - fn create_proof(connection_handle: u32) -> u32 { - set_consumer(); + fn create_proof(connection_handle: u32, consumer_handle: Option) -> u32 { + set_consumer(consumer_handle); info!("create_proof >>> getting proof request messages"); let requests = disclosed_proof::get_proof_request_messages(connection_handle, None).unwrap(); info!("create_proof :: get proof request messages returned {}", requests); @@ -221,8 +223,8 @@ mod tests { disclosed_proof::create_proof(::utils::constants::DEFAULT_PROOF_NAME, &requests).unwrap() } - fn generate_and_send_proof(proof_handle: u32, connection_handle: u32, selected_credentials: &str) { - set_consumer(); + fn generate_and_send_proof(proof_handle: u32, connection_handle: u32, selected_credentials: &str, consumer_handle: Option) { + set_consumer(consumer_handle); info!("generate_and_send_proof >>> generating proof using selected credentials {}", selected_credentials); disclosed_proof::generate_proof(proof_handle, selected_credentials.into(), "{}".to_string()).unwrap(); @@ -238,31 +240,70 @@ mod tests { set_institution(); // GET REV REG DELTA BEFORE REVOCATION let (_, delta, timestamp) = ::utils::libindy::anoncreds::get_rev_reg_delta_json(&rev_reg_id.clone().unwrap(), None, None).unwrap(); - println!("revoking credential"); + info!("revoking credential"); ::issuer_credential::revoke_credential(issuer_handle).unwrap(); let (_, delta_after_revoke, _) = ::utils::libindy::anoncreds::get_rev_reg_delta_json(&rev_reg_id.unwrap(), Some(timestamp + 1), None).unwrap(); assert_ne!(delta, delta_after_revoke); } - fn _issue_address_credential(faber: u32, alice: u32, institution_did: &str) -> (String, String, Option, u32, u32) { + fn revoke_credential_local(issuer_handle: u32, rev_reg_id: Option) { + set_institution(); + let (_, delta, timestamp) = ::utils::libindy::anoncreds::get_rev_reg_delta_json(&rev_reg_id.clone().unwrap(), None, None).unwrap(); + info!("revoking credential locally"); + ::issuer_credential::revoke_credential_local(issuer_handle).unwrap(); + let (_, delta_after_revoke, _) = ::utils::libindy::anoncreds::get_rev_reg_delta_json(&rev_reg_id.unwrap(), Some(timestamp + 1), None).unwrap(); + assert_ne!(delta, delta_after_revoke); // They will not equal as we have saved the delta in cache + } + + fn publish_revocation(rev_reg_id: String) { + set_institution(); + ::utils::libindy::anoncreds::publish_local_revocations(rev_reg_id.as_str()).unwrap(); + } + + fn _create_address_schema() -> (String, String, String, String, u32, Option) { info!("test_real_proof_with_revocation >>> CREATE SCHEMA AND CRED DEF"); let attrs_list = json!(["address1", "address2", "city", "state", "zip"]).to_string(); - let (schema_id, _schema_json, cred_def_id, _cred_def_json, cred_def_handle, rev_reg_id) = - ::utils::libindy::anoncreds::tests::create_and_store_credential_def(&attrs_list, true); + ::utils::libindy::anoncreds::tests::create_and_store_credential_def(&attrs_list, true) + } + + fn _exchange_credential(credential_data: String, cred_def_handle: u32, faber: u32, alice: u32, consumer_handle: Option) -> u32 { + info!("Generated credential data: {}", credential_data); + let credential_offer = create_and_send_cred_offer(settings::CONFIG_INSTITUTION_DID, cred_def_handle, alice, &credential_data); + info!("AS CONSUMER SEND CREDENTIAL REQUEST"); + let credential = send_cred_req(faber, consumer_handle); + info!("AS INSTITUTION SEND CREDENTIAL"); + send_credential(credential_offer, alice, credential, consumer_handle); + credential_offer + } + + fn _issue_address_credential(faber: u32, alice: u32, consumer_handle: Option) -> (String, String, Option, u32, u32) { + let (schema_id, _schema_json, cred_def_id, _cred_def_json, cred_def_handle, rev_reg_id) = _create_address_schema(); info!("test_real_proof_with_revocation :: AS INSTITUTION SEND CREDENTIAL OFFER"); let (address1, address2, city, state, zip) = attr_names(); let credential_data = json!({address1: "123 Main St", address2: "Suite 3", city: "Draper", state: "UT", zip: "84000"}).to_string(); - info!("test_real_proof_with_revocation :: generated credential data: {}", credential_data); - let credential_offer = create_and_send_cred_offer(institution_did, cred_def_handle, alice, &credential_data); + let credential_handle = _exchange_credential(credential_data, cred_def_handle, faber, alice, consumer_handle); + (schema_id, cred_def_id, rev_reg_id, cred_def_handle, credential_handle) + } - info!("test_real_proof_with_revocation :: AS CONSUMER SEND CREDENTIAL REQUEST"); - let credential = send_cred_req(faber); + fn _verifier_create_proof_and_send_request(institution_did: &str, schema_id: &str, cred_def_id: &str, alice: u32) -> u32{ + let _requested_attrs = requested_attrs(&institution_did, &schema_id, &cred_def_id, None, None); + let requested_attrs_string = serde_json::to_string(&_requested_attrs).unwrap(); + send_proof_request(alice, &requested_attrs_string, "[]", "{}") + } - info!("test_real_proof_with_revocation :: AS INSTITUTION SEND CREDENTIAL"); - send_credential(credential_offer, alice, credential); - (schema_id, cred_def_id, rev_reg_id, cred_def_handle, credential_offer) + fn _prover_select_credentials_and_send_proof(faber: u32, consumer_handle: Option) { + set_consumer(consumer_handle); + info!("Prover :: Going to create proof"); + let proof_handle_prover = create_proof(faber, consumer_handle); + info!("Prover :: Retrieving matching credentials"); + let retrieved_credentials = disclosed_proof::retrieve_credentials(proof_handle_prover).unwrap(); + info!("Prover :: Based on proof, retrieved credentials: {}", &retrieved_credentials); + let selected_credentials_value = retrieved_to_selected_credentials_simple(&retrieved_credentials, true); + let selected_credentials_str = serde_json::to_string(&selected_credentials_value).unwrap(); + info!("Prover :: Retrieved credential converted to selected: {}", &selected_credentials_str); + generate_and_send_proof(proof_handle_prover, faber, &selected_credentials_str, consumer_handle); } #[cfg(feature = "agency_pool_tests")] @@ -272,12 +313,12 @@ mod tests { ::settings::set_config_value(::settings::CONFIG_PROTOCOL_TYPE, "4.0"); let institution_did = settings::get_config_value(settings::CONFIG_INSTITUTION_DID).unwrap(); - let (faber, alice) = ::connection::tests::create_connected_connections(); - let (schema_id, cred_def_id, rev_reg_id, _cred_def_handle, credential_offer) = _issue_address_credential(faber, alice, &institution_did); + let (faber, alice) = ::connection::tests::create_connected_connections(None); + let (schema_id, cred_def_id, rev_reg_id, _cred_def_handle, credential_handle) = _issue_address_credential(faber, alice, None); let time_before_revocation = time::get_time().sec as u64; info!("test_basic_revocation :: verifier :: Going to revoke credential"); - revoke_credential(credential_offer, rev_reg_id); + revoke_credential(credential_handle, rev_reg_id); thread::sleep(Duration::from_millis(2000)); let time_after_revocation = time::get_time().sec as u64; @@ -288,24 +329,99 @@ mod tests { info!("test_basic_revocation :: Going to seng proof request with attributes {}", &requested_attrs_string); let proof_handle_verifier = send_proof_request(alice, &requested_attrs_string, "[]", &interval); - info!("test_basic_revocation :: Going to create proof"); - let proof_handle_prover = create_proof(faber); - info!("test_basic_revocation :: retrieving matching credentials"); + _prover_select_credentials_and_send_proof(faber, None); - let retrieved_credentials = disclosed_proof::retrieve_credentials(proof_handle_prover).unwrap(); - info!("test_basic_revocation :: prover :: based on proof, retrieved credentials: {}", &retrieved_credentials); + info!("test_basic_revocation :: verifier :: going to verify proof"); + set_institution(); + proof::update_state(proof_handle_verifier, None, None).unwrap(); + assert_eq!(proof::get_proof_state(proof_handle_verifier).unwrap(), ProofStateType::ProofInvalid as u32); + } - let selected_credentials_value = retrieved_to_selected_credentials_simple(&retrieved_credentials, true); - let selected_credentials_str = serde_json::to_string(&selected_credentials_value).unwrap(); - info!("test_basic_revocation :: prover :: retrieved credential converted to selected: {}", &selected_credentials_str); - generate_and_send_proof(proof_handle_prover, faber, &selected_credentials_str); + #[cfg(feature = "agency_pool_tests")] + #[test] + fn test_local_revocation() { + let _setup = SetupLibraryAgencyV2ZeroFees::init(); + + let institution_did = settings::get_config_value(settings::CONFIG_INSTITUTION_DID).unwrap(); + let (faber, alice) = ::connection::tests::create_connected_connections(None); + let (schema_id, cred_def_id, rev_reg_id, _cred_def_handle, credential_handle) = _issue_address_credential(faber, alice, None); + + revoke_credential_local(credential_handle, rev_reg_id.clone()); + let proof_handle_verifier = _verifier_create_proof_and_send_request(&institution_did, &schema_id, &cred_def_id, alice); + _prover_select_credentials_and_send_proof(faber, None); + + set_institution(); + proof::update_state(proof_handle_verifier, None, None).unwrap(); + assert_eq!(proof::get_proof_state(proof_handle_verifier).unwrap(), ProofStateType::ProofValidated as u32); + + publish_revocation(rev_reg_id.clone().unwrap()); + let proof_handle_verifier = _verifier_create_proof_and_send_request(&institution_did, &schema_id, &cred_def_id, alice); + _prover_select_credentials_and_send_proof(faber, None); - info!("test_basic_revocation :: verifier :: going to verify proof"); set_institution(); proof::update_state(proof_handle_verifier, None, None).unwrap(); assert_eq!(proof::get_proof_state(proof_handle_verifier).unwrap(), ProofStateType::ProofInvalid as u32); } + #[cfg(feature = "agency_pool_tests")] + #[test] + fn test_batch_revocation() { + let _setup = SetupLibraryAgencyV2ZeroFees::init(); + let institution_did = settings::get_config_value(settings::CONFIG_INSTITUTION_DID).unwrap(); + + let consumer_1 = create_consumer_config(); + let consumer_2 = create_consumer_config(); + let consumer_3 = create_consumer_config(); + let (faber1, alice1) = ::connection::tests::create_connected_connections(Some(consumer_1)); + let (faber2, alice2) = ::connection::tests::create_connected_connections(Some(consumer_2)); + let (faber3, alice3) = ::connection::tests::create_connected_connections(Some(consumer_3)); + + // Issue and send three credentials of the same schema + let (schema_id, _schema_json, cred_def_id, _cred_def_json, cred_def_handle, rev_reg_id) = _create_address_schema(); + let (address1, address2, city, state, zip) = attr_names(); + let credential_data1 = json!({address1.clone(): "123 Main St", address2.clone(): "Suite 3", city.clone(): "Draper", state.clone(): "UT", zip.clone(): "84000"}).to_string(); + let credential_handle1 = _exchange_credential(credential_data1, cred_def_handle, faber1, alice1, Some(consumer_1)); + let credential_data2 = json!({address1.clone(): "101 Tela Lane", address2.clone(): "Suite 1", city.clone(): "SLC", state.clone(): "UT", zip.clone(): "8721"}).to_string(); + let credential_handle2 = _exchange_credential(credential_data2, cred_def_handle, faber2, alice2, Some(consumer_2)); + let credential_data3 = json!({address1.clone(): "5th Avenue", address2.clone(): "Suite 1234", city.clone(): "NYC", state.clone(): "NYS", zip.clone(): "84712"}).to_string(); + let credential_handle3 = _exchange_credential(credential_data3, cred_def_handle, faber3, alice3, Some(consumer_3)); + + revoke_credential_local(credential_handle1, rev_reg_id.clone()); + revoke_credential_local(credential_handle2, rev_reg_id.clone()); + + // Revoke two locally and verify their are all still valid + let proof_handle_verifier1 = _verifier_create_proof_and_send_request(&institution_did, &schema_id, &cred_def_id, alice1); + _prover_select_credentials_and_send_proof(faber1, Some(consumer_1)); + let proof_handle_verifier2 = _verifier_create_proof_and_send_request(&institution_did, &schema_id, &cred_def_id, alice2); + _prover_select_credentials_and_send_proof(faber2, Some(consumer_2)); + let proof_handle_verifier3 = _verifier_create_proof_and_send_request(&institution_did, &schema_id, &cred_def_id, alice3); + _prover_select_credentials_and_send_proof(faber3, Some(consumer_3)); + + set_institution(); + proof::update_state(proof_handle_verifier1, None, None).unwrap(); + proof::update_state(proof_handle_verifier2, None, None).unwrap(); + proof::update_state(proof_handle_verifier3, None, None).unwrap(); + assert_eq!(proof::get_proof_state(proof_handle_verifier1).unwrap(), ProofStateType::ProofValidated as u32); + assert_eq!(proof::get_proof_state(proof_handle_verifier2).unwrap(), ProofStateType::ProofValidated as u32); + assert_eq!(proof::get_proof_state(proof_handle_verifier3).unwrap(), ProofStateType::ProofValidated as u32); + + // Publish revocations and verify the two are invalid, third still valid + publish_revocation(rev_reg_id.clone().unwrap()); + let proof_handle_verifier1 = _verifier_create_proof_and_send_request(&institution_did, &schema_id, &cred_def_id, alice1); + _prover_select_credentials_and_send_proof(faber1, Some(consumer_1)); + let proof_handle_verifier2 = _verifier_create_proof_and_send_request(&institution_did, &schema_id, &cred_def_id, alice2); + _prover_select_credentials_and_send_proof(faber2, Some(consumer_2)); + let proof_handle_verifier3 = _verifier_create_proof_and_send_request(&institution_did, &schema_id, &cred_def_id, alice3); + _prover_select_credentials_and_send_proof(faber3, Some(consumer_3)); + set_institution(); + proof::update_state(proof_handle_verifier1, None, None).unwrap(); + proof::update_state(proof_handle_verifier2, None, None).unwrap(); + proof::update_state(proof_handle_verifier3, None, None).unwrap(); + assert_eq!(proof::get_proof_state(proof_handle_verifier1).unwrap(), ProofStateType::ProofInvalid as u32); + assert_eq!(proof::get_proof_state(proof_handle_verifier2).unwrap(), ProofStateType::ProofInvalid as u32); + assert_eq!(proof::get_proof_state(proof_handle_verifier3).unwrap(), ProofStateType::ProofValidated as u32); + } + #[cfg(feature = "agency_pool_tests")] #[test] fn test_revoked_credential_might_still_work() { @@ -313,14 +429,14 @@ mod tests { ::settings::set_config_value(::settings::CONFIG_PROTOCOL_TYPE, "4.0"); let institution_did = settings::get_config_value(settings::CONFIG_INSTITUTION_DID).unwrap(); - let (faber, alice) = ::connection::tests::create_connected_connections(); - let (schema_id, cred_def_id, rev_reg_id, _cred_def_handle, credential_offer) = _issue_address_credential(faber, alice, &institution_did); + let (faber, alice) = ::connection::tests::create_connected_connections(None); + let (schema_id, cred_def_id, rev_reg_id, _cred_def_handle, credential_handle) = _issue_address_credential(faber, alice, None); thread::sleep(Duration::from_millis(1000)); let time_before_revocation = time::get_time().sec as u64; thread::sleep(Duration::from_millis(2000)); info!("test_revoked_credential_might_still_work :: verifier :: Going to revoke credential"); - revoke_credential(credential_offer, rev_reg_id); + revoke_credential(credential_handle, rev_reg_id); thread::sleep(Duration::from_millis(2000)); let from = time_before_revocation - 100; @@ -333,7 +449,7 @@ mod tests { let proof_handle_verifier = send_proof_request(alice, &requested_attrs_string, "[]", &interval); info!("test_revoked_credential_might_still_work :: Going to create proof"); - let proof_handle_prover = create_proof(faber); + let proof_handle_prover = create_proof(faber, None); info!("test_revoked_credential_might_still_work :: retrieving matching credentials"); let retrieved_credentials = disclosed_proof::retrieve_credentials(proof_handle_prover).unwrap(); @@ -342,7 +458,7 @@ mod tests { let selected_credentials_value = retrieved_to_selected_credentials_simple(&retrieved_credentials, true); let selected_credentials_str = serde_json::to_string(&selected_credentials_value).unwrap(); info!("test_revoked_credential_might_still_work :: prover :: retrieved credential converted to selected: {}", &selected_credentials_str); - generate_and_send_proof(proof_handle_prover, faber, &selected_credentials_str); + generate_and_send_proof(proof_handle_prover, faber, &selected_credentials_str, None); info!("test_revoked_credential_might_still_work :: verifier :: going to verify proof"); set_institution(); @@ -378,7 +494,7 @@ mod tests { let number_of_attributes = 10; let institution_did = settings::get_config_value(settings::CONFIG_INSTITUTION_DID).unwrap(); - let (faber, alice) = ::connection::tests::create_connected_connections(); + let (faber, alice) = ::connection::tests::create_connected_connections(None); info!("test_real_proof :: AS INSTITUTION SEND CREDENTIAL OFFER"); let mut attrs_list: Value = serde_json::Value::Array(vec![]); @@ -397,10 +513,10 @@ mod tests { let credential_offer = create_and_send_cred_offer(&institution_did, cred_def_handle, alice, &credential_data); info!("test_real_proof :: AS CONSUMER SEND CREDENTIAL REQUEST"); - let credential = send_cred_req(faber); + let credential = send_cred_req(faber, None); info!("test_real_proof :: AS INSTITUTION SEND CREDENTIAL"); - send_credential(credential_offer, alice, credential); + send_credential(credential_offer, alice, credential, None); info!("test_real_proof :: AS INSTITUTION SEND PROOF REQUEST"); ::utils::devsetup::set_institution(); @@ -415,14 +531,14 @@ mod tests { let proof_req_handle = send_proof_request(alice, &requested_attrs, "[]", "{}"); info!("test_real_proof :: Going to create proof"); - let proof_handle = create_proof(faber); + let proof_handle = create_proof(faber, None); info!("test_real_proof :: retrieving matching credentials"); let retrieved_credentials = disclosed_proof::retrieve_credentials(proof_handle).unwrap(); let selected_credentials = retrieved_to_selected_credentials_simple(&retrieved_credentials, false); info!("test_real_proof :: generating and sending proof"); - generate_and_send_proof(proof_handle, faber, &serde_json::to_string(&selected_credentials).unwrap()); + generate_and_send_proof(proof_handle, faber, &serde_json::to_string(&selected_credentials).unwrap(), None); info!("test_real_proof :: AS INSTITUTION VALIDATE PROOF"); set_institution(); diff --git a/libvcx/src/messages/agent_utils.rs b/libvcx/src/messages/agent_utils.rs index af1f2d8e44..09a21b556e 100644 --- a/libvcx/src/messages/agent_utils.rs +++ b/libvcx/src/messages/agent_utils.rs @@ -619,7 +619,7 @@ mod tests { fn test_update_agent_webhook_real() { let _setup = SetupLibraryAgencyV2::init(); - ::utils::devsetup::set_consumer(); + ::utils::devsetup::set_consumer(None); update_agent_webhook("https://example.org").unwrap(); } } diff --git a/libvcx/src/messages/get_message.rs b/libvcx/src/messages/get_message.rs index 3979e4dbb2..365ba1852b 100644 --- a/libvcx/src/messages/get_message.rs +++ b/libvcx/src/messages/get_message.rs @@ -502,7 +502,7 @@ mod tests { let _setup = SetupLibraryAgencyV2::init(); let institution_did = settings::get_config_value(settings::CONFIG_INSTITUTION_DID).unwrap(); - let (_faber, alice) = ::connection::tests::create_connected_connections(); + let (_faber, alice) = ::connection::tests::create_connected_connections(None); let (_, cred_def_handle) = ::credential_def::tests::create_cred_def_real(false); @@ -521,7 +521,7 @@ mod tests { let hello_uid = ::connection::send_generic_message(alice, "hello", &json!({"msg_type":"hello", "msg_title": "hello", "ref_msg_id": null}).to_string()).unwrap(); // AS CONSUMER GET MESSAGES - ::utils::devsetup::set_consumer(); + ::utils::devsetup::set_consumer(None); let _all_messages = download_messages(None, None, None).unwrap(); diff --git a/libvcx/src/messages/update_message.rs b/libvcx/src/messages/update_message.rs index 2535b97f1a..e1892b0576 100644 --- a/libvcx/src/messages/update_message.rs +++ b/libvcx/src/messages/update_message.rs @@ -178,14 +178,14 @@ mod tests { #[test] fn test_update_agency_messages() { let _setup = SetupLibraryAgencyV2::init(); - let (_alice_to_faber, faber_to_alice) = ::connection::tests::create_connected_connections(); + let (_alice_to_faber, faber_to_alice) = ::connection::tests::create_connected_connections(None); send_generic_message(faber_to_alice, "Hello 1", &json!({"msg_type": "toalice", "msg_title": "msg1"}).to_string()).unwrap(); send_generic_message(faber_to_alice, "Hello 2", &json!({"msg_type": "toalice", "msg_title": "msg2"}).to_string()).unwrap(); send_generic_message(faber_to_alice, "Hello 3", &json!({"msg_type": "toalice", "msg_title": "msg3"}).to_string()).unwrap(); thread::sleep(Duration::from_millis(1000)); - ::utils::devsetup::set_consumer(); + ::utils::devsetup::set_consumer(None); let received = download_messages(None, Some(vec![MessageStatusCode::Received.to_string()]), None).unwrap(); assert_eq!(received.len(), 1); diff --git a/libvcx/src/object_cache/mod.rs b/libvcx/src/object_cache/mod.rs index c28f51b09f..eeeb670e5f 100644 --- a/libvcx/src/object_cache/mod.rs +++ b/libvcx/src/object_cache/mod.rs @@ -100,6 +100,11 @@ impl ObjectCache { let mut store = self._lock_store()?; Ok(store.clear()) } + + pub fn len(&self) -> VcxResult { + let store = self._lock_store()?; + Ok(store.len()) + } } #[cfg(test)] diff --git a/libvcx/src/utils/devsetup.rs b/libvcx/src/utils/devsetup.rs index 71c41ab6ba..458693f703 100644 --- a/libvcx/src/utils/devsetup.rs +++ b/libvcx/src/utils/devsetup.rs @@ -373,8 +373,10 @@ macro_rules! assert_match { ); } +// TODO: We could have an array of configs static mut INSTITUTION_CONFIG: u32 = 0; static mut CONSUMER_CONFIG: u32 = 0; +// static mut CONFIGS: Vec = Vec::new(); // Vector of handles lazy_static! { static ref CONFIG_STRING: ObjectCache = ObjectCache::::new("devsetup-config-cache"); @@ -467,7 +469,7 @@ pub fn cleanup_agency_env() { set_institution(); delete_wallet(&settings::get_wallet_name().unwrap(), None, None, None).unwrap(); - set_consumer(); + set_consumer(None); delete_wallet(&settings::get_wallet_name().unwrap(), None, None, None).unwrap(); delete_test_pool(); @@ -486,10 +488,11 @@ pub fn set_institution() { change_wallet_handle(); } -pub fn set_consumer() { +pub fn set_consumer(consumer_handle: Option) { settings::clear_config(); + info!("Using consumer handle: {:?}", consumer_handle); unsafe { - CONFIG_STRING.get(CONSUMER_CONFIG, |t| { + CONFIG_STRING.get(consumer_handle.unwrap_or(CONSUMER_CONFIG), |t| { warn!("Setting consumer config {}", t); settings::set_config_value(settings::CONFIG_PAYMENT_METHOD, settings::DEFAULT_PAYMENT_METHOD); let result = settings::process_config_string(&t, true); @@ -571,9 +574,8 @@ pub fn setup_agency_env(protocol_type: &str, use_zero_fees: bool) { settings::set_config_value(settings::CONFIG_GENESIS_PATH, utils::get_temp_dir_path(settings::DEFAULT_GENESIS_PATH).to_str().unwrap()); open_test_pool(); - // grab the generated did and vk from the consumer and enterprise - set_consumer(); + set_consumer(None); let did2 = settings::get_config_value(settings::CONFIG_INSTITUTION_DID).unwrap(); let vk2 = settings::get_config_value(settings::CONFIG_INSTITUTION_VERKEY).unwrap(); set_institution(); @@ -591,7 +593,7 @@ pub fn setup_agency_env(protocol_type: &str, use_zero_fees: bool) { wallet::delete_wallet(settings::DEFAULT_WALLET_NAME, None, None, None).unwrap(); // as trustees, mint tokens into each wallet - set_consumer(); + set_consumer(None); ::utils::libindy::payments::tests::token_setup(None, None, use_zero_fees); set_institution(); @@ -605,6 +607,31 @@ pub fn config_with_wallet_handle(wallet_n: &str, config: &str) -> String { config.to_string() } +pub fn create_consumer_config() -> u32 { + let consumer_id: u32 = CONFIG_STRING.len().unwrap() as u32; + let consumer_wallet_name = format!("{}_{}", constants::CONSUMER_PREFIX, consumer_id); + let seed = create_new_seed(); + let config = json!({ + "agency_url": C_AGENCY_ENDPOINT.to_string(), + "agency_did": C_AGENCY_DID.to_string(), + "agency_verkey": C_AGENCY_VERKEY.to_string(), + "wallet_name": consumer_wallet_name, + "wallet_key": settings::DEFAULT_WALLET_KEY.to_string(), + "wallet_key_derivation": settings::DEFAULT_WALLET_KEY_DERIVATION.to_string(), + "enterprise_seed": seed, + "agent_seed": seed, + "name": format!("consumer_{}", consumer_id).to_string(), + "logo": "http://www.logo.com".to_string(), + "path": constants::GENESIS_PATH.to_string(), + "protocol_type": "4.0" + }); + + debug!("setup_agency_env >> Going to provision consumer using config: {:?}", &config); + let consumer_config = ::messages::agent_utils::connect_register_provision(&config.to_string()).unwrap(); + + CONFIG_STRING.add(config_with_wallet_handle(&consumer_wallet_name, &consumer_config.to_string())).unwrap() +} + pub fn setup_wallet_env(test_name: &str) -> Result { settings::set_config_value(settings::CONFIG_ENABLE_TEST_MODE, "false"); init_wallet(test_name, None, None, None).map_err(|e| format!("Unable to init_wallet in tests: {}", e)) @@ -656,7 +683,7 @@ mod tests { pub fn test_two_enterprise_connections() { let _setup = SetupLibraryAgencyV2ZeroFees::init(); - let (_faber, _alice) = ::connection::tests::create_connected_connections(); - let (_faber, _alice) = ::connection::tests::create_connected_connections(); + let (_faber, _alice) = ::connection::tests::create_connected_connections(None); + let (_faber, _alice) = ::connection::tests::create_connected_connections(None); } }