Skip to content

Commit 684e39b

Browse files
signature: Add unit test for OSSL_FUNC_SIGNATURE_DUPCTX
Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
1 parent 5c2ce9a commit 684e39b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

parsec-openssl-provider/src/keymgmt/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ pub fn kmgmt_keyobj_new(provctx: Arc<ParsecProviderContext>) -> Arc<ParsecProvid
3838
})
3939
}
4040

41+
#[cfg(test)]
42+
impl ParsecProviderKeyObject {
43+
pub fn get_key_name(&self) -> std::sync::MutexGuard<'_, Option<String>> {
44+
self.key_name.lock().unwrap()
45+
}
46+
}
47+
4148
/*
4249
should create a provider side key object. The provider context provctx is passed and may be incorporated
4350
in the key object, but that is not mandatory.

parsec-openssl-provider/src/signature/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,57 @@ pub const PARSEC_PROVIDER_SIGNATURE: [OSSL_ALGORITHM; 3] = [
119119
),
120120
ossl_algorithm!(),
121121
];
122+
123+
#[test]
124+
fn test_sign_dup() {
125+
use crate::parsec_provider_provider_init;
126+
127+
let out: *const OSSL_DISPATCH = std::ptr::null();
128+
let mut provctx: types::VOID_PTR = std::ptr::null_mut();
129+
130+
// Initialize the provider
131+
let result: Result<(), parsec_openssl2::Error> = unsafe {
132+
parsec_provider_provider_init(
133+
std::ptr::null(),
134+
std::ptr::null(),
135+
&out as *const _ as *mut _,
136+
&mut provctx as *mut VOID_PTR,
137+
)
138+
};
139+
assert!(result.is_ok());
140+
assert_ne!(provctx, std::ptr::null_mut());
141+
let s = String::from("");
142+
143+
let sig_ctx = unsafe { parsec_provider_signature_newctx(provctx, s.as_ptr() as _) };
144+
assert_ne!(sig_ctx, std::ptr::null_mut());
145+
146+
/*
147+
Change the Signature Context.
148+
The way we change it doesn't really matter.
149+
All we care about is whether this change is preserved after duplication.
150+
For this test, we are just changing the key_name of the Signature Context's Key Object.
151+
*/
152+
let arc_sig_ctx = unsafe { Arc::from_raw(sig_ctx as *const ParsecProviderSignatureContext) };
153+
*(arc_sig_ctx.keyobj.get_key_name()) = Some("PARSEC_TEST_KEYNAME".to_string());
154+
155+
// We change back to using pointers instead of Arcs so that we use the provider functions
156+
let sig_ctx = Arc::into_raw(arc_sig_ctx) as VOID_PTR;
157+
158+
// Duplicate the context and verify if the change is preserved on the duplicated context.
159+
let duplicated_ptr = unsafe { parsec_provider_signature_dupctx(sig_ctx) };
160+
assert_ne!(duplicated_ptr, std::ptr::null_mut());
161+
162+
let arc_duplicated =
163+
unsafe { Arc::from_raw(duplicated_ptr as *const ParsecProviderSignatureContext) };
164+
assert_eq!(
165+
*(arc_duplicated.clone().keyobj.clone().get_key_name()),
166+
Some("PARSEC_TEST_KEYNAME".to_string())
167+
);
168+
169+
// We change back to using pointers instead of Arcs to use the provider's free
170+
let duplicated_ptr = Arc::into_raw(arc_duplicated) as VOID_PTR;
171+
unsafe {
172+
parsec_provider_signature_freectx(duplicated_ptr);
173+
parsec_provider_signature_freectx(sig_ctx);
174+
}
175+
}

0 commit comments

Comments
 (0)