Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Jan 4, 2024
1 parent 463c011 commit 3ba757e
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 4 deletions.
18 changes: 17 additions & 1 deletion pkcs11/src/api/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,22 @@ pub extern "C" fn C_DecryptVerifyUpdate(
mod tests {

use super::*;
use crate::data::SESSION_MANAGER;
use crate::{backend::slot::set_test_config_env, data::SESSION_MANAGER};

fn setup_session() -> cryptoki_sys::CK_SESSION_HANDLE {
SESSION_MANAGER.lock().unwrap().setup_dummy_session()
}

#[test]
fn test_decrypt_init_null_mech() {
set_test_config_env();
let rv = C_DecryptInit(0, std::ptr::null_mut(), 0);
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);
}

#[test]
fn test_decrypt_init_unknown_mech() {
set_test_config_env();
let mut mech = cryptoki_sys::CK_MECHANISM {
mechanism: 15000, // doesn't exist
pParameter: std::ptr::null_mut(),
Expand All @@ -239,6 +241,7 @@ mod tests {

#[test]
fn test_decrypt_init_invalid_session() {
set_test_config_env();
SESSION_MANAGER.lock().unwrap().delete_session(0);

let mut mech = cryptoki_sys::CK_MECHANISM {
Expand All @@ -253,6 +256,7 @@ mod tests {

#[test]
fn test_decrypt_invalid_session() {
set_test_config_env();
SESSION_MANAGER.lock().unwrap().delete_session(0);

let rv = C_Decrypt(
Expand All @@ -267,6 +271,7 @@ mod tests {

#[test]
fn test_decrypt_null_data_len() {
set_test_config_env();
let mut pEncryptedData = [0u8; 32];

let session_handle = setup_session();
Expand All @@ -283,6 +288,7 @@ mod tests {

#[test]
fn test_decrypt_null_encrypted_data() {
set_test_config_env();
let mut pulDataLen = 0;

let session_handle = setup_session();
Expand All @@ -299,6 +305,7 @@ mod tests {

#[test]
fn test_decrypt_null_data() {
set_test_config_env();
let mut pulDataLen = 0;

let session_handle = setup_session();
Expand All @@ -317,6 +324,7 @@ mod tests {

#[test]
fn test_decrypt_update_invalid_session() {
set_test_config_env();
SESSION_MANAGER.lock().unwrap().delete_session(0);

let rv = C_DecryptUpdate(
Expand All @@ -331,6 +339,7 @@ mod tests {

#[test]
fn test_decrypt_update_null_encrypted_part() {
set_test_config_env();
let session_handle = setup_session();

let mut pulPartLen = 0;
Expand All @@ -348,6 +357,7 @@ mod tests {

#[test]
fn test_decrypt_update_null_part_len() {
set_test_config_env();
let session_handle = setup_session();

let mut pEncryptedPart = [0u8; 32];
Expand All @@ -365,6 +375,7 @@ mod tests {

#[test]
fn test_decrypt_update_operation_not_initialized() {
set_test_config_env();
let session_handle = setup_session();

let mut pEncryptedPart = [0u8; 32];
Expand All @@ -383,6 +394,7 @@ mod tests {

#[test]
fn test_decrypt_final_invalid_session() {
set_test_config_env();
SESSION_MANAGER.lock().unwrap().delete_session(0);

let mut pulLastPartLen = 0;
Expand All @@ -393,6 +405,7 @@ mod tests {

#[test]
fn test_decrypt_final_null_last_part_len() {
set_test_config_env();
let session_handle = setup_session();

let mut lastPart = [0u8; 32];
Expand All @@ -403,6 +416,7 @@ mod tests {

#[test]
fn test_decrypt_final_operation_not_initialized() {
set_test_config_env();
let session_handle = setup_session();

let mut lastPart = [0u8; 32];
Expand All @@ -414,6 +428,7 @@ mod tests {

// #[test]
// fn test_decrypt_final_null_last_part() {
// set_test_config_env();
// let session_handle = setup_session();

// let mut pulLastPartLen = 0;
Expand All @@ -425,6 +440,7 @@ mod tests {
// unsupported function
#[test]
fn test_decrypt_verify_update() {
set_test_config_env();
let rv = C_DecryptVerifyUpdate(
0,
std::ptr::null_mut(),
Expand Down
9 changes: 9 additions & 0 deletions pkcs11/src/api/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,12 @@ pub extern "C" fn C_DecryptDigestUpdate(
mod tests {
use cryptoki_sys::CK_ULONG;

use crate::backend::slot::set_test_config_env;

use super::*;
#[test]
fn test_digest_init() {
set_test_config_env();
let rv = C_DigestInit(0, std::ptr::null_mut());
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);

Expand All @@ -128,6 +131,7 @@ mod tests {

#[test]
fn test_digest() {
set_test_config_env();
let rv = C_Digest(
0,
std::ptr::null_mut(),
Expand All @@ -153,6 +157,7 @@ mod tests {

#[test]
fn test_digest_update() {
set_test_config_env();
let rv = C_DigestUpdate(0, std::ptr::null_mut(), 0 as CK_ULONG);
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);

Expand All @@ -164,6 +169,7 @@ mod tests {

#[test]
fn test_digest_final() {
set_test_config_env();
let rv = C_DigestFinal(0, std::ptr::null_mut(), std::ptr::null_mut());
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);

Expand All @@ -176,12 +182,14 @@ mod tests {

#[test]
fn test_digest_key() {
set_test_config_env();
let rv = C_DigestKey(0, 0);
assert_eq!(rv, cryptoki_sys::CKR_FUNCTION_NOT_SUPPORTED);
}

#[test]
fn test_digest_encrypt_update() {
set_test_config_env();
let mut encrypted_part_len: CK_ULONG = 0;
let mut encrypted_part: Vec<u8> = Vec::new();
let mut part: Vec<u8> = Vec::new();
Expand All @@ -198,6 +206,7 @@ mod tests {

#[test]
fn test_decrypt_digest_update() {
set_test_config_env();
let mut encrypted_part_len: CK_ULONG = 0;
let mut encrypted_part: Vec<u8> = Vec::new();
let mut part: Vec<u8> = Vec::new();
Expand Down
21 changes: 20 additions & 1 deletion pkcs11/src/api/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,20 @@ pub extern "C" fn C_EncryptFinal(

#[cfg(test)]
mod tests {
use crate::data::SESSION_MANAGER;
use crate::{backend::slot::set_test_config_env, data::SESSION_MANAGER};

use super::*;

#[test]
fn test_encrypt_init_null_mechanism() {
set_test_config_env();
let rv = C_EncryptInit(0, std::ptr::null_mut(), 0);
assert_eq!(rv, cryptoki_sys::CKR_ARGUMENTS_BAD);
}

#[test]
fn test_encrypt_init_invalid_mechanism() {
set_test_config_env();
let mut mechanism = cryptoki_sys::CK_MECHANISM {
mechanism: 15000,
pParameter: std::ptr::null_mut(),
Expand All @@ -269,6 +271,7 @@ mod tests {

#[test]
fn test_encrypt_init_invalid_session() {
set_test_config_env();
SESSION_MANAGER.lock().unwrap().delete_session(1);

let mut mechanism = cryptoki_sys::CK_MECHANISM {
Expand All @@ -283,6 +286,7 @@ mod tests {

#[test]
fn test_encrypt_invalid_session() {
set_test_config_env();
SESSION_MANAGER.lock().unwrap().delete_session(1);

let mut data: Vec<u8> = Vec::new();
Expand All @@ -301,6 +305,7 @@ mod tests {

#[test]
fn test_encrypt_update_invalid_session() {
set_test_config_env();
SESSION_MANAGER.lock().unwrap().delete_session(1);

let mut data: Vec<u8> = Vec::new();
Expand All @@ -319,6 +324,7 @@ mod tests {

#[test]
fn test_encrypt_final_invalid_session() {
set_test_config_env();
SESSION_MANAGER.lock().unwrap().delete_session(1);

let mut encrypted_data: Vec<u8> = Vec::new();
Expand All @@ -330,6 +336,7 @@ mod tests {

#[test]
fn test_encrypt_null_data() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedDataLen: CK_ULONG = 0;
Expand All @@ -347,6 +354,7 @@ mod tests {

#[test]
fn test_encrypt_null_encrypted_data_len() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -364,6 +372,7 @@ mod tests {

#[test]
fn test_encrypt_null_encrypted_data() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -381,6 +390,7 @@ mod tests {

#[test]
fn test_encrypt_operation_not_initialized() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -399,6 +409,7 @@ mod tests {

#[test]
fn test_encrypt_update_null_part() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedPartLen: CK_ULONG = 0;
Expand All @@ -416,6 +427,7 @@ mod tests {

#[test]
fn test_encrypt_update_null_encrypted_part_len() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -433,6 +445,7 @@ mod tests {

#[test]
fn test_encrypt_update_null_encrypted_part() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = Vec::new();
Expand All @@ -450,6 +463,7 @@ mod tests {

#[test]
fn test_encrypt_update_buffer_too_small() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = vec![0; 100];
Expand All @@ -468,6 +482,7 @@ mod tests {

#[test]
fn test_encrypt_update_operation_not_initialized() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut data: Vec<u8> = vec![0; 100];
Expand All @@ -486,6 +501,7 @@ mod tests {

#[test]
fn test_encrypt_final_null_encrypted_part() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedPartLen: CK_ULONG = 0;
Expand All @@ -496,6 +512,7 @@ mod tests {

#[test]
fn test_encrypt_final_null_encrypted_part_len() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedPart: Vec<u8> = Vec::new();
Expand All @@ -510,6 +527,7 @@ mod tests {

// #[test]
// fn test_encrypt_final_buffer_too_small() {
// set_test_config_env();
// let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

// let mut pEncryptedPart: Vec<u8> = Vec::new();
Expand All @@ -525,6 +543,7 @@ mod tests {

#[test]
fn test_encrypt_final_operation_not_initialized() {
set_test_config_env();
let session_handle = SESSION_MANAGER.lock().unwrap().setup_dummy_session();

let mut pEncryptedPart: Vec<u8> = Vec::new();
Expand Down
Loading

0 comments on commit 3ba757e

Please sign in to comment.