Skip to content

Commit

Permalink
Tweak u32 handle releasing handling (#716)
Browse files Browse the repository at this point in the history
* Print warning in case of repeated release call instead of failing

Signed-off-by: Patrik Stas <[email protected]>

* Move tests from api_c to api_vcx

Signed-off-by: Patrik Stas <[email protected]>

* Reformat code

Signed-off-by: Patrik Stas <[email protected]>

Signed-off-by: Patrik Stas <[email protected]>
  • Loading branch information
Patrik-Stas authored Jan 4, 2023
1 parent 8c2e95d commit 9e0b170
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 283 deletions.
26 changes: 0 additions & 26 deletions libvcx/src/api_c/ledger/credential_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,32 +555,6 @@ mod tests {
assert!(handle > 0);
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_credentialdef_release() {
let _setup = SetupMocks::init();

let cb = return_types_u32::Return_U32_U32::new().unwrap();
assert_eq!(
vcx_credentialdef_create_v2(
cb.command_handle,
CString::new("Test Source ID Release Test").unwrap().into_raw(),
CString::new(SCHEMA_ID).unwrap().into_raw(),
CString::new("tag").unwrap().into_raw(),
true,
Some(cb.get_callback()),
),
error::SUCCESS_ERR_CODE
);

let handle = cb.receive(TimeoutUtils::some_medium()).unwrap();
let unknown_handle = handle + 1;
assert_eq!(
vcx_credentialdef_release(unknown_handle),
u32::from(LibvcxErrorKind::InvalidCredDefHandle)
);
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_creddef_get_id() {
Expand Down
15 changes: 0 additions & 15 deletions libvcx/src/api_c/ledger/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,21 +679,6 @@ mod tests {
assert_eq!(schema_as_json["data"].to_string(), data);
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_schema_release() {
let _setup = SetupMocks::init();

let (_, schema_name, schema_version, data) = prepare_schema_data();
let handle = vcx_schema_create_c_closure(&schema_name, &schema_version, &data).unwrap();

let unknown_handle = handle + 1;
assert_eq!(
vcx_schema_release(unknown_handle),
u32::from(LibvcxErrorKind::InvalidSchemaHandle)
);
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_prepare_schema_success() {
Expand Down
20 changes: 0 additions & 20 deletions libvcx/src/api_c/protocols/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,24 +1397,4 @@ mod tests {
error::SUCCESS_ERR_CODE
);
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_credential_release() {
let _setup = SetupMocks::init();

let handle = _vcx_credential_create_with_offer_c_closure(ARIES_CREDENTIAL_OFFER).unwrap();

assert_eq!(
vcx_credential_release(handle + 1),
u32::from(LibvcxErrorKind::InvalidCredentialHandle)
);

assert_eq!(vcx_credential_release(handle), error::SUCCESS_ERR_CODE);

assert_eq!(
vcx_credential_release(handle),
u32::from(LibvcxErrorKind::InvalidCredentialHandle)
);
}
}
17 changes: 0 additions & 17 deletions libvcx/src/api_c/protocols/disclosed_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,23 +1211,6 @@ mod tests {
assert!(handle > 0 && disclosed_proof.is_some());
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_disclosed_proof_release() {
let _setup = SetupMocks::init();

let handle = _vcx_disclosed_proof_create_with_request_c_closure(ARIES_PROOF_REQUEST_PRESENTATION).unwrap();
assert_eq!(
vcx_disclosed_proof_release(handle + 1),
u32::from(LibvcxErrorKind::InvalidDisclosedProofHandle)
);
assert_eq!(vcx_disclosed_proof_release(handle), error::SUCCESS_ERR_CODE);
assert_eq!(
vcx_disclosed_proof_release(handle),
u32::from(LibvcxErrorKind::InvalidDisclosedProofHandle)
);
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_disclosed_proof_serialize_and_deserialize() {
Expand Down
19 changes: 0 additions & 19 deletions libvcx/src/api_c/protocols/issuer_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,23 +1060,4 @@ pub mod tests {
);
cb.receive(TimeoutUtils::some_medium()).unwrap();
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_credential_release() {
let _setup = SetupMocks::init();

let handle = _vcx_issuer_create_credential_c_closure().unwrap();
assert_eq!(
vcx_issuer_credential_release(handle + 1),
u32::from(LibvcxErrorKind::InvalidIssuerCredentialHandle)
);

assert_eq!(vcx_issuer_credential_release(handle), error::SUCCESS_ERR_CODE);

assert_eq!(
vcx_issuer_credential_release(handle),
u32::from(LibvcxErrorKind::InvalidIssuerCredentialHandle)
);
}
}
27 changes: 0 additions & 27 deletions libvcx/src/api_c/protocols/mediated_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1768,33 +1768,6 @@ mod tests {
cb.receive(TimeoutUtils::some_medium()).unwrap().unwrap();
}

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_vcx_connection_release() {
let _setup = SetupMocks::init();

let handle = build_test_connection_inviter_requested().await;

let rc = vcx_connection_release(handle);
assert_eq!(rc, SUCCESS_ERR_CODE);

let unknown_handle = handle + 1;
assert_eq!(
vcx_connection_release(unknown_handle),
u32::from(LibvcxErrorKind::InvalidConnectionHandle)
);

let cb = return_types_u32::Return_U32_STR::new().unwrap();
let rc = vcx_connection_connect(
0,
handle,
CString::new("{}").unwrap().into_raw(),
Some(cb.get_callback()),
);
assert!(cb.receive(TimeoutUtils::some_custom(1)).is_err());
assert_eq!(rc, SUCCESS_ERR_CODE);
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_connection_deserialize_succeeds() {
Expand Down
22 changes: 0 additions & 22 deletions libvcx/src/api_c/protocols/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,28 +991,6 @@ mod tests {
let _ = cb.receive(TimeoutUtils::some_medium()).is_err();
}

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_get_proof_returns_proof_with_proof_state_invalid() {
let _setup = SetupMocks::init();

let proof_handle = proof::from_string(mockdata_proof::SERIALIZIED_PROOF_REVOKED).unwrap();

let cb = return_types_u32::Return_U32_U32_STR::new().unwrap();
assert_eq!(
vcx_get_proof_msg(cb.command_handle, proof_handle, Some(cb.get_callback())),
error::SUCCESS_ERR_CODE
);
let (state, _) = cb.receive(TimeoutUtils::some_medium()).unwrap();
assert_eq!(state, ProofStateType::ProofInvalid as u32);

vcx_proof_release(proof_handle);
assert_eq!(
vcx_proof_release(proof_handle),
u32::from(LibvcxErrorKind::InvalidProofHandle)
);
}

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_vcx_connection_get_state() {
Expand Down
35 changes: 7 additions & 28 deletions libvcx/src/api_c/vcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,34 +971,13 @@ mod tests {
let credential = credential::credential_create_with_offer("name", ARIES_CREDENTIAL_OFFER).unwrap();

vcx_shutdown(true);
assert_eq!(
mediated_connection::release(connection).unwrap_err().kind(),
LibvcxErrorKind::InvalidConnectionHandle
);
assert_eq!(
issuer_credential::release(issuer_credential).unwrap_err().kind(),
LibvcxErrorKind::InvalidIssuerCredentialHandle
);
assert_eq!(
schema::release(schema).unwrap_err().kind(),
LibvcxErrorKind::InvalidSchemaHandle
);
assert_eq!(
proof::release(proof).unwrap_err().kind(),
LibvcxErrorKind::InvalidProofHandle
);
assert_eq!(
credential_def::release(credentialdef).unwrap_err().kind(),
LibvcxErrorKind::InvalidCredDefHandle
);
assert_eq!(
credential::release(credential).unwrap_err().kind(),
LibvcxErrorKind::InvalidCredentialHandle
);
assert_eq!(
disclosed_proof::release(disclosed_proof).unwrap_err().kind(),
LibvcxErrorKind::InvalidDisclosedProofHandle
);
assert_eq!(mediated_connection::is_valid_handle(connection), false);
assert_eq!(issuer_credential::is_valid_handle(issuer_credential), false);
assert_eq!(schema::is_valid_handle(schema), false);
assert_eq!(proof::is_valid_handle(proof), false);
assert_eq!(credential_def::is_valid_handle(credentialdef), false);
assert_eq!(credential::is_valid_handle(credential), false);
assert_eq!(disclosed_proof::is_valid_handle(disclosed_proof), false);
assert_eq!(get_main_wallet_handle(), INVALID_WALLET_HANDLE);
}

Expand Down
9 changes: 9 additions & 0 deletions libvcx/src/api_vcx/api_handle/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,15 @@ pub mod tests {
offer
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_credential_release() {
let _setup = SetupDefaults::init();
let handle = credential_create_with_offer("test_credential_create_with_offer", ARIES_CREDENTIAL_OFFER).unwrap();
release(handle).unwrap();
assert_eq!(to_string(handle).unwrap_err().kind, LibvcxErrorKind::InvalidHandle);
}

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_credential_create_with_offer() {
Expand Down
25 changes: 23 additions & 2 deletions libvcx/src/api_vcx/api_handle/credential_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ pub mod tests {

use super::*;

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_vcx_credentialdef_release() {
let _setup = SetupMocks::init();
let schema_handle = schema::tests::create_schema_real().await;
sleep(Duration::from_secs(1));

let schema_id = schema::get_schema_id(schema_handle).unwrap();
let issuer_did = get_config_value(CONFIG_INSTITUTION_DID).unwrap();
let cred_def_handle = create("1".to_string(), schema_id, "tag_1".to_string(), false)
.await
.unwrap();
release(cred_def_handle).unwrap();
assert_eq!(
to_string(cred_def_handle).unwrap_err().kind,
LibvcxErrorKind::InvalidHandle
)
}

pub async fn create_and_publish_nonrevocable_creddef() -> (u32, u32) {
let schema_handle = schema::tests::create_schema_real().await;
sleep(Duration::from_secs(1));
Expand Down Expand Up @@ -238,8 +257,10 @@ pub mod tests {
let h2 = create("SourceId".to_string(), SCHEMA_ID.to_string(), "tag".to_string(), false)
.await
.unwrap();

release_all();
assert_eq!(release(h1).unwrap_err().kind(), LibvcxErrorKind::InvalidCredDefHandle);
assert_eq!(release(h2).unwrap_err().kind(), LibvcxErrorKind::InvalidCredDefHandle);

assert_eq!(is_valid_handle(h1), false);
assert_eq!(is_valid_handle(h2), false);
}
}
9 changes: 9 additions & 0 deletions libvcx/src/api_vcx/api_handle/disclosed_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ mod tests {
requests
}

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_vcx_disclosed_proof_release() {
let _setup = SetupMocks::init();
let handle = create_with_proof_request("TEST_CREDENTIAL", ARIES_PROOF_REQUEST_PRESENTATION).unwrap();
release(handle).unwrap();
assert_eq!(to_string(handle).unwrap_err().kind, LibvcxErrorKind::InvalidHandle)
}

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_create_proof() {
Expand Down
46 changes: 12 additions & 34 deletions libvcx/src/api_vcx/api_handle/issuer_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ pub mod tests {
"{\"attr\":\"value\"}"
}

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_credential_release() {
let _setup = SetupMocks::init();
let handle = _issuer_credential_create();
release(handle).unwrap();
assert_eq!(to_string(handle).unwrap_err().kind, LibvcxErrorKind::InvalidHandle)
}

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_issuer_credential_create_succeeds() {
Expand Down Expand Up @@ -368,40 +377,9 @@ pub mod tests {
let h1 = _issuer_credential_create();
let h2 = _issuer_credential_create();
let h3 = _issuer_credential_create();
let h4 = _issuer_credential_create();
let h5 = _issuer_credential_create();
release_all();
assert_eq!(
release(h1).unwrap_err().kind(),
LibvcxErrorKind::InvalidIssuerCredentialHandle
);
assert_eq!(
release(h2).unwrap_err().kind(),
LibvcxErrorKind::InvalidIssuerCredentialHandle
);
assert_eq!(
release(h3).unwrap_err().kind(),
LibvcxErrorKind::InvalidIssuerCredentialHandle
);
assert_eq!(
release(h4).unwrap_err().kind(),
LibvcxErrorKind::InvalidIssuerCredentialHandle
);
assert_eq!(
release(h5).unwrap_err().kind(),
LibvcxErrorKind::InvalidIssuerCredentialHandle
);
}

#[tokio::test]
#[cfg(feature = "general_test")]
async fn test_errors() {
let _setup = SetupEmpty::init();

assert_eq!(to_string(0).unwrap_err().kind(), LibvcxErrorKind::InvalidHandle);
assert_eq!(
release(0).unwrap_err().kind(),
LibvcxErrorKind::InvalidIssuerCredentialHandle
);
assert_eq!(is_valid_handle(h1), false);
assert_eq!(is_valid_handle(h2), false);
assert_eq!(is_valid_handle(h3), false);
}
}
Loading

0 comments on commit 9e0b170

Please sign in to comment.