Skip to content

Commit c6f9048

Browse files
committed
tests: Allow more configuration for test token
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent 7e0d445 commit c6f9048

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

cryptoki/tests/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ fn get_attribute_info_test() -> TestResult {
12001200
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;
12011201

12021202
let pub_attribs = vec![AttributeType::PublicExponent, AttributeType::Modulus];
1203-
let mut priv_attribs = [
1203+
let priv_attribs = [
12041204
AttributeType::PublicExponent,
12051205
AttributeType::Modulus,
12061206
AttributeType::PrivateExponent,

cryptoki/tests/common.rs

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

14+
fn get_token_label() -> Option<String> {
15+
env::var("TEST_TOKEN_LABEL").ok()
16+
}
17+
18+
fn skip_token_init() -> bool {
19+
match env::var("TEST_SKIP_TOKEN_INIT") {
20+
Ok(s) => s == "1",
21+
Err(_) => false,
22+
}
23+
}
24+
1425
fn get_pkcs11_path() -> String {
1526
env::var("TEST_PKCS11_MODULE")
1627
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string())
@@ -24,24 +35,41 @@ pub fn get_pkcs11() -> Pkcs11 {
2435
Pkcs11::new(get_pkcs11_path()).unwrap()
2536
}
2637

38+
fn get_slot(pkcs11: &Pkcs11) -> Slot {
39+
// find a slot, get the first one or one with name specified in the environment variable
40+
let mut slots = pkcs11.get_slots_with_token().unwrap();
41+
match get_token_label() {
42+
None => slots.remove(0),
43+
Some(label) => {
44+
for s in slots {
45+
let ti = pkcs11.get_token_info(s).unwrap();
46+
if ti.label() == label {
47+
return s;
48+
}
49+
}
50+
panic!("No token with Token Label `{label}` found");
51+
}
52+
}
53+
}
54+
2755
pub fn init_pins() -> (Pkcs11, Slot) {
2856
let pkcs11 = get_pkcs11();
2957

3058
// initialize the library
3159
pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();
3260

33-
// find a slot, get the first one
34-
let slot = pkcs11.get_slots_with_token().unwrap().remove(0);
35-
36-
let so_pin = AuthPin::new(SO_PIN.into());
37-
pkcs11.init_token(slot, &so_pin, "Test Token").unwrap();
61+
let slot = get_slot(&pkcs11);
3862

39-
{
40-
// open a session
41-
let session = pkcs11.open_rw_session(slot).unwrap();
42-
// log in the session
43-
session.login(UserType::So, Some(&so_pin)).unwrap();
44-
session.init_pin(&AuthPin::new(USER_PIN.into())).unwrap();
63+
if !skip_token_init() {
64+
let so_pin = AuthPin::new(SO_PIN.into());
65+
let _ = pkcs11.init_token(slot, &so_pin, "Test Token");
66+
{
67+
// open a session
68+
let session = pkcs11.open_rw_session(slot).unwrap();
69+
// log in the session
70+
session.login(UserType::So, Some(&so_pin)).unwrap();
71+
session.init_pin(&AuthPin::new(USER_PIN.into())).unwrap();
72+
}
4573
}
4674

4775
(pkcs11, slot)

0 commit comments

Comments
 (0)