Skip to content

Commit d3d6c85

Browse files
committed
test: Use existing openssl2 functions for tests
The openssl2 layer provides functions that map the error from openssl C calls to a rust error. This also provides proper logs for debugging in case of an error. Signed-off-by: Gowtham Suresh Kumar <gowtham.sureshkumar@arm.com>
1 parent 25354c1 commit d3d6c85

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

parsec-openssl-provider-shared/e2e_tests/src/lib.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
pub use foreign_types_shared::ForeignType;
77
pub use parsec_openssl_provider::parsec_openssl2::openssl::{lib_ctx::LibCtx, provider::Provider};
88
pub use parsec_openssl_provider::parsec_openssl2::openssl_bindings::*;
9+
use parsec_openssl_provider::parsec_openssl2::openssl_returns_1;
910

1011
// These needs to be replaced with consts from the key management module
1112
pub const PARSEC_PROVIDER_RSA: &[u8; 4] = b"RSA\0";
@@ -26,16 +27,14 @@ pub unsafe fn load_key(lib_ctx: &LibCtx, param: *mut OSSL_PARAM, parsec_pkey: *m
2627
PARSEC_PROVIDER_PROPERTY.as_ptr() as *const ::std::os::raw::c_char,
2728
);
2829
assert_ne!(evp_ctx, std::ptr::null_mut());
29-
assert_eq!(EVP_PKEY_fromdata_init(evp_ctx), 1);
30-
assert_eq!(
31-
EVP_PKEY_fromdata(
32-
evp_ctx,
33-
parsec_pkey as _,
34-
EVP_PKEY_KEY_PARAMETERS.try_into().unwrap(),
35-
param,
36-
),
37-
1
38-
);
30+
openssl_returns_1(EVP_PKEY_fromdata_init(evp_ctx)).unwrap();
31+
openssl_returns_1(EVP_PKEY_fromdata(
32+
evp_ctx,
33+
parsec_pkey as _,
34+
EVP_PKEY_KEY_PARAMETERS.try_into().unwrap(),
35+
param,
36+
))
37+
.unwrap();
3938
assert_ne!(*parsec_pkey, std::ptr::null_mut());
4039

4140
EVP_PKEY_CTX_free(evp_ctx);

parsec-openssl-provider-shared/e2e_tests/tests/provider.rs

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use e2e_tests::*;
5-
use parsec_openssl_provider::parsec_openssl2::{openssl_bindings, ossl_param};
5+
use parsec_openssl_provider::parsec_openssl2::{
6+
openssl_bindings, openssl_returns_1, openssl_returns_nonnull, openssl_returns_nonnull_const,
7+
ossl_param,
8+
};
69
use parsec_openssl_provider::PARSEC_PROVIDER_KEY_NAME;
710
use std::ffi::CStr;
811

@@ -63,31 +66,25 @@ fn test_parsec_provider_gettable_param() {
6366
OSSL_PROVIDER_gettable_params(provider.as_ptr() as *const ossl_provider_st);
6467

6568
// Checks if the returned structure contains OSSL_PROV_PARAM_NAME
66-
assert_ne!(
67-
openssl_bindings::OSSL_PARAM_locate(
68-
gettable_params as _,
69-
openssl_bindings::OSSL_PROV_PARAM_NAME.as_ptr() as *const std::os::raw::c_char,
70-
),
71-
std::ptr::null_mut()
72-
);
69+
openssl_returns_nonnull(openssl_bindings::OSSL_PARAM_locate(
70+
gettable_params as _,
71+
openssl_bindings::OSSL_PROV_PARAM_NAME.as_ptr() as *const std::os::raw::c_char,
72+
))
73+
.unwrap();
7374

7475
// Checks if the returned structure contains OSSL_PROV_PARAM_VERSION
75-
assert_ne!(
76-
openssl_bindings::OSSL_PARAM_locate(
77-
gettable_params as _,
78-
openssl_bindings::OSSL_PROV_PARAM_VERSION.as_ptr() as *const std::os::raw::c_char,
79-
),
80-
std::ptr::null_mut()
81-
);
76+
openssl_returns_nonnull(openssl_bindings::OSSL_PARAM_locate(
77+
gettable_params as _,
78+
openssl_bindings::OSSL_PROV_PARAM_VERSION.as_ptr() as *const std::os::raw::c_char,
79+
))
80+
.unwrap();
8281

8382
// Checks if the returned structure contains OSSL_PROV_PARAM_STATUS
84-
assert_ne!(
85-
openssl_bindings::OSSL_PARAM_locate(
86-
gettable_params as _,
87-
openssl_bindings::OSSL_PROV_PARAM_STATUS.as_ptr() as *const std::os::raw::c_char,
88-
),
89-
std::ptr::null_mut()
90-
);
83+
openssl_returns_nonnull(openssl_bindings::OSSL_PARAM_locate(
84+
gettable_params as _,
85+
openssl_bindings::OSSL_PROV_PARAM_STATUS.as_ptr() as *const std::os::raw::c_char,
86+
))
87+
.unwrap();
9188
}
9289
}
9390

@@ -128,18 +125,16 @@ fn test_parsec_provider_get_param() {
128125
assert_eq!(OSSL_PARAM_modified(&params[2] as _), 0);
129126

130127
// Fetch the providers
131-
assert_eq!(
132-
OSSL_PROVIDER_get_params(
133-
provider.as_ptr() as *const ossl_provider_st,
134-
params.as_ptr() as *mut OSSL_PARAM,
135-
),
136-
1
137-
);
128+
openssl_returns_1(OSSL_PROVIDER_get_params(
129+
provider.as_ptr() as *const ossl_provider_st,
130+
params.as_ptr() as *mut OSSL_PARAM,
131+
))
132+
.unwrap();
138133

139134
// Ensure the structure is populated by the provider
140-
assert_eq!(OSSL_PARAM_modified(&params as _), 1);
141-
assert_eq!(OSSL_PARAM_modified(&params[1] as _), 1);
142-
assert_eq!(OSSL_PARAM_modified(&params[2] as _), 1);
135+
openssl_returns_1(OSSL_PARAM_modified(&params as _)).unwrap();
136+
openssl_returns_1(OSSL_PARAM_modified(&params[1] as _)).unwrap();
137+
openssl_returns_1(OSSL_PARAM_modified(&params[2] as _)).unwrap();
143138

144139
// Verify the returned provider parameters
145140
let prov_name = CStr::from_ptr(prov_name);
@@ -163,14 +158,12 @@ fn test_provider_query_supported() {
163158

164159
let mut no_cache: i32 = 0;
165160
unsafe {
166-
assert_ne!(
167-
OSSL_PROVIDER_query_operation(
168-
provider.as_ptr() as _,
169-
OSSL_OP_KEYMGMT.try_into().unwrap(),
170-
&mut no_cache as _,
171-
),
172-
std::ptr::null_mut()
173-
);
161+
openssl_returns_nonnull_const(OSSL_PROVIDER_query_operation(
162+
provider.as_ptr() as _,
163+
OSSL_OP_KEYMGMT.try_into().unwrap(),
164+
&mut no_cache as _,
165+
))
166+
.unwrap();
174167
}
175168
}
176169

0 commit comments

Comments
 (0)