Skip to content

Commit af52b5c

Browse files
committed
Added unit tests for a few more error cases to improve code coverage.
1 parent 32b0391 commit af52b5c

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/callback.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ mod tests {
656656
use std::{
657657
borrow::Cow,
658658
ffi::CString,
659+
hint::black_box,
659660
sync::{Arc, Mutex},
660661
};
661662

@@ -666,6 +667,7 @@ mod tests {
666667
}
667668

668669
fn do_invoke_callbacks(cb_manager: &mut CallbackManager) {
670+
let _ = black_box(format!("{:?}", cb_manager));
669671
let _ = cb_manager.auth_cb(cstr!("/HS/SRK/my/auth/path"), Some(cstr!("some object")));
670672
let _ = cb_manager.sign_cb(
671673
cstr!("/HS/SRK/my/sign/path"),
@@ -738,14 +740,16 @@ mod tests {
738740
let mut cb_manager = CallbackManager::new(CallbackTest::new("my_password"));
739741
do_invoke_callbacks(&mut cb_manager);
740742

741-
let my_callbacks = cb_manager.into_inner();
743+
let mut my_callbacks = cb_manager.into_inner();
742744
let downcasted: &CallbackTest = my_callbacks.downcast().expect("Downcast failed!");
743745
let paths = downcasted.get_paths();
744746

745747
assert_paths(&paths.0, "/HS/SRK/my/auth/path");
746748
assert_paths(&paths.1, "/HS/SRK/my/sign/path");
747749
assert_paths(&paths.2, "/HS/SRK/my/bran/path");
748750
assert_paths(&paths.3, "/HS/SRK/my/actn/path");
751+
752+
let _ = black_box(my_callbacks.downcast_mut::<CallbackTest>().expect("Downcast failed!"));
749753
}
750754

751755
#[test]

tests/05_key_test.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use function_name::named;
1717
use log::debug;
1818
use serial_test::serial;
1919
use std::collections::HashSet;
20-
use tss2_fapi_rs::{BaseErrorCode, ErrorCode, FapiContext, ImportData, KeyFlags};
20+
use tss2_fapi_rs::{BaseErrorCode, ErrorCode, FapiContext, ImportData, InternalError, KeyFlags};
2121

2222
const KEY_FLAGS: &[KeyFlags] = &[KeyFlags::NoDA];
2323

@@ -243,6 +243,13 @@ fn test_get_tpm_blobs() {
243243
Err(error) => panic!("Key creation has failed: {:?}", error),
244244
}
245245

246+
// Try with all three flags set to false
247+
match context.get_tpm_blobs(key_path, false, false, false) {
248+
Ok(_) => panic!("Retrieved TPM blob when it was not expected!"),
249+
Err(ErrorCode::InternalError(InternalError::InvalidArguments)) => (),
250+
Err(error) => panic!("Failed to obtain TPM blobs: {:?}", error),
251+
};
252+
246253
// Fetch public/private key data
247254
let blobs = match context.get_tpm_blobs(key_path, true, false, false) {
248255
Ok(value) => value,

tests/08_nv_test.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use log::{debug, trace};
2020
use rand::{Rng, SeedableRng, rng};
2121
use rand_chacha::ChaChaRng;
2222
use serial_test::serial;
23-
use tss2_fapi_rs::{FapiContext, NvFlags};
23+
use tss2_fapi_rs::{ErrorCode, FapiContext, InternalError, NvFlags};
2424

2525
const NV_ORDINARY_FLAGS: &[NvFlags] = &[NvFlags::NoDA];
2626
const NV_COUNTER_FLAGS: &[NvFlags] = &[NvFlags::Counter, NvFlags::NoDA];
@@ -143,6 +143,13 @@ fn test_nv_counter() {
143143
// Initialize TPM, if not already initialized
144144
tpm_initialize!(context, PASSWORD, MyCallbacks::new(PASSWORD, None));
145145

146+
// Try to create with an explicit size
147+
match context.create_nv(nv_path, Some(NV_COUNTER_FLAGS), NonZeroUsize::new(1usize), None, None) {
148+
Ok(_) => panic!("Counter was created when it was not expected!"),
149+
Err(ErrorCode::InternalError(InternalError::InvalidArguments)) => (),
150+
Err(error) => panic!("NV index creation has failed: {:?}", error),
151+
}
152+
146153
// Create NV index, if not already created
147154
match context.create_nv(nv_path, Some(NV_COUNTER_FLAGS), None, None, None) {
148155
Ok(_) => debug!("NV index created."),

0 commit comments

Comments
 (0)