Skip to content

Commit

Permalink
Add tests for fetching keys with failure
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Feb 1, 2024
1 parent 37888ad commit 70d3c5c
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions pkcs11/src/backend/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,21 +678,26 @@ impl Session {
#[cfg(test)]
mod test {
use crate::{
backend::slot::{get_slot, init_for_tests},
backend::{
slot::{get_slot, init_for_tests},
ApiError,
},
config::config_file::RetryConfig,
};
use std::thread;

use super::*;

#[test]
#[ignore]
fn parrallel_fetch_all_keys() {
THREADS_ALLOWED.store(false, Ordering::Relaxed);
init_for_tests();
let slot = get_slot(0).unwrap();

let db = Arc::new((Mutex::new(Db::new()), Condvar::new()));
let mut sessions = Vec::new();
for _ in 0..20 {
for _ in 0..3 {
let session = Session {
db: db.clone(),
decrypt_ctx: None,
Expand Down Expand Up @@ -724,4 +729,51 @@ mod test {
}
})
}

#[test]
#[ignore]
fn parrallel_fetch_all_keys_fail() {
THREADS_ALLOWED.store(false, Ordering::Relaxed);
init_for_tests();
let slot = get_slot(0).unwrap();

let db = Arc::new((Mutex::new(Db::new()), Condvar::new()));
let mut sessions = Vec::new();
for _ in 0..3 {
let session = Session {
db: db.clone(),
decrypt_ctx: None,
encrypt_ctx: None,
sign_ctx: None,
device_error: 0,
enum_ctx: None,
flags: 0,
login_ctx: LoginCtx::new(
Some(crate::config::config_file::UserConfig {
username: "operator".into(),
password: Some("BadPassphrase".into()),
}),
None,
vec![slot.instances[0].clone()],
Some(RetryConfig {
count: 2,
delay_seconds: 0,
}),
),
slot_id: 0,
};
sessions.push(session);
}

thread::scope(|s| {
for session in &mut sessions {
s.spawn(|| match session.fetch_all_keys() {
Err(Error::Api(ApiError::Ureq(s))) => {
assert_eq!(s, "https://localhost:8443/api/v1/keys: status code 401")
}
_ => panic!(),
});
}
})
}
}

0 comments on commit 70d3c5c

Please sign in to comment.