Skip to content

Commit a225cf8

Browse files
committed
tests: Test different behavior in different tokens
Signed-off-by: Jakub Jelen <[email protected]>
1 parent 88676a5 commit a225cf8

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

@@ -1273,9 +1278,13 @@ fn sha256_digest() -> TestResult {
12731278

12741279
#[test]
12751280
#[serial]
1276-
// Currently empty AAD crashes SoftHSM, see: https://github.com/opendnssec/SoftHSMv2/issues/605
1277-
#[ignore]
12781281
fn aes_gcm_no_aad() -> TestResult {
1282+
// Currently empty AAD crashes SoftHSM, see: https://github.com/opendnssec/SoftHSMv2/issues/605
1283+
if is_softhsm() {
1284+
/* return Ignore(); */
1285+
return Ok(());
1286+
}
1287+
12791288
// Encrypt two blocks of zeros with AES-128-GCM
12801289
let key = vec![0; 16];
12811290
let mut iv = [0; 12];
@@ -1370,8 +1379,13 @@ fn rsa_pkcs_oaep_empty() -> TestResult {
13701379

13711380
#[test]
13721381
#[serial]
1373-
#[ignore] // it's not clear why the test with data specified fails
13741382
fn rsa_pkcs_oaep_with_data() -> TestResult {
1383+
/* SoftHSM does not support additional OAEP Source */
1384+
if is_softhsm() {
1385+
/* return Ignore(); */
1386+
return Ok(());
1387+
}
1388+
13751389
let (pkcs11, slot) = init_pins();
13761390
let session = pkcs11.open_rw_session(slot)?;
13771391
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
@@ -1404,11 +1418,16 @@ fn rsa_pkcs_oaep_with_data() -> TestResult {
14041418
#[test]
14051419
#[serial]
14061420
fn get_slot_event() -> TestResult {
1407-
// Not implemented in SoftHSMv2
1408-
// https://github.com/opendnssec/SoftHSMv2/issues/370
14091421
let (pkcs11, _slot) = init_pins();
1410-
let event = pkcs11.get_slot_event()?;
1411-
assert_eq!(None, event);
1422+
if is_softhsm() {
1423+
// Not implemented in SoftHSMv2
1424+
// https://github.com/opendnssec/SoftHSMv2/issues/370
1425+
let event = pkcs11.get_slot_event()?;
1426+
assert_eq!(None, event);
1427+
} else {
1428+
// Not implemented in Kryoptic
1429+
pkcs11.get_slot_event().unwrap_err();
1430+
}
14121431
Ok(())
14131432
}
14141433

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("PKCS11_SOFTHSM2_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("PKCS11_SOFTHSM2_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)