-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose filter api and add a basic test
Signed-off-by: Miroslav Kovar <miroslavkovar@protonmail.com>
- Loading branch information
Showing
6 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::ptr; | ||
|
||
use error::prelude::*; | ||
use utils::error; | ||
use indy_sys::CommandHandle; | ||
use libc::c_char; | ||
use filters; | ||
use utils::threadpool::spawn; | ||
use utils::cstring::CStringUtils; | ||
|
||
|
||
/// Filters proof requests based on name selected by verifier when creating the request. | ||
/// | ||
/// #Params | ||
/// command_handle: command handle to map callback to user context. | ||
/// requests: Serialized array of proof requests JSONs. | ||
/// | ||
/// # Example | ||
/// | ||
/// match_name: Name of the request to match. | ||
/// #Returns | ||
/// Error code as a u32 | ||
#[no_mangle] | ||
pub extern fn vcx_filter_proof_requests_by_name(command_handle: CommandHandle, | ||
requests: *const c_char, | ||
match_name: *const c_char, | ||
cb: Option<extern fn(xcommand_handle: CommandHandle, err: u32, requests: *const c_char)>) -> u32 { | ||
info!("vcx_filter_proof_requests_by_name >>>"); | ||
|
||
check_useful_c_str!(requests, VcxErrorKind::InvalidOption); | ||
check_useful_c_str!(match_name, VcxErrorKind::InvalidOption); | ||
check_useful_c_callback!(cb, VcxErrorKind::InvalidOption); | ||
|
||
trace!("vcx_filter_proof_requests_by_name(command_handle: {}, requests: {}, match_name: {})", | ||
command_handle, requests, match_name); | ||
|
||
spawn(move || { | ||
match filters::filter_proof_requests_by_name(&requests, &match_name) { | ||
Ok(x) => { | ||
trace!("vcx_filter_proof_requests_by_name_cb(command_handle: {}, requests: {}, rc: {}, requests: {})", | ||
command_handle, requests, error::SUCCESS.message, x); | ||
let x = CStringUtils::string_to_cstring(x); | ||
cb(command_handle,error::SUCCESS.code_num, x.as_ptr()); | ||
} | ||
Err(err) => { | ||
error!("vcx_filter_proof_requests_by_name_cb(command_handle: {}, rc: {}, msg: {})", | ||
command_handle, error::SUCCESS.message, err); | ||
cb(command_handle, err.into(), ptr::null_mut()); | ||
} | ||
}; | ||
|
||
Ok(()) | ||
}); | ||
|
||
error::SUCCESS.code_num | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
#[cfg(feature = "general_test")] | ||
fn test_vcx_filter_proof_requests_by_name() {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use serde_json; | ||
|
||
use error::prelude::*; | ||
use utils::error; | ||
|
||
use aries::messages::proof_presentation::presentation_request::PresentationRequest; | ||
|
||
// TODO: Log errors | ||
fn _filter_proof_requests_by_name(requests: &str, match_name: &str) -> VcxResult<Vec<PresentationRequest>> { | ||
let presentation_requests: Vec<PresentationRequest> = serde_json::from_str(requests) | ||
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Failed to deserialize Vec<PresentationRequest>: {}\nObtained error: {:?}", requests, err)))?; | ||
let filtered = presentation_requests | ||
.into_iter() | ||
.filter_map(|presentation_request| { | ||
match presentation_request.request_presentations_attach.content().ok() { | ||
Some(content) => { | ||
match serde_json::from_str(&content).map(|value: serde_json::Value| value.get("name").unwrap_or(&serde_json::Value::Null).as_str().unwrap_or("").to_string()) { | ||
Ok(name) if name == String::from(match_name) => Some(presentation_request), | ||
_ => None | ||
} | ||
} | ||
_ => None | ||
} | ||
}) | ||
.collect(); | ||
Ok(filtered) | ||
} | ||
|
||
pub fn filter_proof_requests_by_name(requests: &str, name: &str) -> VcxResult<String> { | ||
let presentation_requests: Vec<PresentationRequest> = _filter_proof_requests_by_name(requests, name)?; | ||
let filtered: String = serde_json::to_string(&presentation_requests) | ||
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Failed to serialize filtered proof requests: {}\nObtained error: {:?}", requests, err)))?; | ||
Ok(filtered) | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod tests { | ||
use super::*; | ||
use utils::constants::*; | ||
use utils::devsetup::*; | ||
use utils::httpclient::HttpClientMockResponse; | ||
use utils::mockdata::mockdata_proof; | ||
|
||
#[test] | ||
#[cfg(feature = "general_test")] | ||
fn test_filter_proof_requests_by_name() { | ||
let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array, "request1").unwrap(); | ||
assert_eq!(filtered.len(), 1); | ||
|
||
let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array, "request2").unwrap(); | ||
assert_eq!(filtered.len(), 1); | ||
|
||
let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array, "not there").unwrap(); | ||
assert_eq!(filtered.len(), 0); | ||
|
||
let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array, "").unwrap(); | ||
assert_eq!(filtered.len(), 0); | ||
|
||
let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array_empty_attach, "not there").unwrap(); | ||
assert_eq!(filtered.len(), 0); | ||
|
||
// TODO: fix the behavior so that this passes | ||
let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array_empty_attach, "").unwrap(); | ||
assert_eq!(filtered.len(), 0); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "general_test")] | ||
fn test_filter_proof_requests_by_name_serialize_deserialize() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters