Skip to content

Commit 311c6a5

Browse files
committed
tests: Test different behavior in different tokens
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent 2107ae6 commit 311c6a5

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

cryptoki/tests/basic.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
mod common;
44

5-
use crate::common::{get_pkcs11, SO_PIN, USER_PIN};
5+
use crate::common::{get_pkcs11, is_softhsm, SO_PIN, USER_PIN};
66
use common::init_pins;
77
use cryptoki::context::Function;
88
use cryptoki::error::{Error, RvError};
@@ -411,7 +411,7 @@ fn import_export() -> TestResult {
411411
fn get_token_info() -> TestResult {
412412
let (pkcs11, slot) = init_pins();
413413
let info = pkcs11.get_token_info(slot)?;
414-
assert_eq!("SoftHSM project", info.manufacturer_id());
414+
assert_ne!("", info.manufacturer_id());
415415

416416
Ok(())
417417
}
@@ -698,9 +698,14 @@ fn get_info_test() -> TestResult {
698698
let (pkcs11, _) = init_pins();
699699
let info = pkcs11.get_library_info()?;
700700

701-
assert_eq!(info.cryptoki_version().major(), 2);
702-
assert_eq!(info.cryptoki_version().minor(), 40);
703-
assert_eq!(info.manufacturer_id(), String::from("SoftHSM"));
701+
assert_ne!("", info.manufacturer_id());
702+
if is_softhsm() {
703+
assert_eq!(info.cryptoki_version().major(), 2);
704+
assert_eq!(info.cryptoki_version().minor(), 40);
705+
} else {
706+
assert_eq!(info.cryptoki_version().major(), 3);
707+
assert_eq!(info.cryptoki_version().minor(), 0);
708+
}
704709
Ok(())
705710
}
706711

@@ -712,7 +717,7 @@ fn get_slot_info_test() -> TestResult {
712717
assert!(slot_info.token_present());
713718
assert!(!slot_info.hardware_slot());
714719
assert!(!slot_info.removable_device());
715-
assert_eq!(slot_info.manufacturer_id(), String::from("SoftHSM project"));
720+
assert_ne!("", slot_info.manufacturer_id());
716721
Ok(())
717722
}
718723

@@ -1286,9 +1291,13 @@ fn gcm_param_graceful_failure() -> TestResult {
12861291

12871292
#[test]
12881293
#[serial]
1289-
// Currently empty AAD crashes SoftHSM, see: https://github.com/opendnssec/SoftHSMv2/issues/605
1290-
#[ignore]
12911294
fn aes_gcm_no_aad() -> TestResult {
1295+
// Currently empty AAD crashes SoftHSM, see: https://github.com/opendnssec/SoftHSMv2/issues/605
1296+
if is_softhsm() {
1297+
/* return Ignore(); */
1298+
return Ok(());
1299+
}
1300+
12921301
// Encrypt two blocks of zeros with AES-128-GCM
12931302
let key = vec![0; 16];
12941303
let mut iv = [0; 12];
@@ -1384,8 +1393,13 @@ fn rsa_pkcs_oaep_empty() -> TestResult {
13841393

13851394
#[test]
13861395
#[serial]
1387-
#[ignore] // it's not clear why the test with data specified fails
13881396
fn rsa_pkcs_oaep_with_data() -> TestResult {
1397+
/* SoftHSM does not support additional OAEP Source */
1398+
if is_softhsm() {
1399+
/* return Ignore(); */
1400+
return Ok(());
1401+
}
1402+
13891403
let (pkcs11, slot) = init_pins();
13901404
let session = pkcs11.open_rw_session(slot)?;
13911405
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
@@ -1418,11 +1432,16 @@ fn rsa_pkcs_oaep_with_data() -> TestResult {
14181432
#[test]
14191433
#[serial]
14201434
fn get_slot_event() -> TestResult {
1421-
// Not implemented in SoftHSMv2
1422-
// https://github.com/opendnssec/SoftHSMv2/issues/370
14231435
let (pkcs11, _slot) = init_pins();
1424-
let event = pkcs11.get_slot_event()?;
1425-
assert_eq!(None, event);
1436+
if is_softhsm() {
1437+
// Not implemented in SoftHSMv2
1438+
// https://github.com/opendnssec/SoftHSMv2/issues/370
1439+
let event = pkcs11.get_slot_event()?;
1440+
assert_eq!(None, event);
1441+
} else {
1442+
// Not implemented in Kryoptic
1443+
pkcs11.get_slot_event().unwrap_err();
1444+
}
14261445
Ok(())
14271446
}
14281447

cryptoki/tests/common.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ pub static USER_PIN: &str = "fedcba";
1111
// The default SO pin
1212
pub static SO_PIN: &str = "abcdef";
1313

14+
fn get_pkcs11_path() -> String {
15+
env::var("TEST_PKCS11_MODULE")
16+
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string())
17+
}
18+
19+
pub fn is_softhsm() -> bool {
20+
get_pkcs11_path().contains("softhsm")
21+
}
22+
1423
pub fn get_pkcs11() -> Pkcs11 {
15-
Pkcs11::new(
16-
env::var("TEST_PKCS11_MODULE")
17-
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string()),
18-
)
19-
.unwrap()
24+
Pkcs11::new(get_pkcs11_path()).unwrap()
2025
}
2126

2227
pub fn init_pins() -> (Pkcs11, Slot) {

0 commit comments

Comments
 (0)