aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-17 13:38:28 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-20 21:08:52 +0000
commitc191e875492ff8aeab1b4493b87486cd265f0edc (patch)
tree5892f3787c3a67dac859a3dba5d056034bf1bcc4
parentc3e551dd40142bcd2552972d549f31ad7483621d (diff)
downloadnitrokey-rs-c191e875492ff8aeab1b4493b87486cd265f0edc.tar.gz
nitrokey-rs-c191e875492ff8aeab1b4493b87486cd265f0edc.tar.bz2
Introduce the Error::UnexpectedError variant
The UnexpectedError variant is used when a libnitrokey function returns a value that violates the function’s contract, for example if a function returns a null pointer although it guarantees to never return null. Previously, we returned a CommandError::Unspecified in these cases.
-rw-r--r--src/error.rs4
-rw-r--r--src/util.rs6
-rw-r--r--tests/pws.rs2
3 files changed, 8 insertions, 4 deletions
diff --git a/src/error.rs b/src/error.rs
index ef9b149..b27124c 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -14,6 +14,8 @@ pub enum Error {
LibraryError(LibraryError),
/// An error that occured during random number generation.
RandError(rand_core::Error),
+ /// An error that is caused by an unexpected value returned by libnitrokey.
+ UnexpectedError,
/// An unknown error returned by libnitrokey.
Unknown(i64),
}
@@ -63,6 +65,7 @@ impl error::Error for Error {
Error::CommunicationError(ref err) => Some(err),
Error::LibraryError(ref err) => Some(err),
Error::RandError(ref err) => Some(err),
+ Error::UnexpectedError => None,
Error::Unknown(_) => None,
}
}
@@ -75,6 +78,7 @@ impl fmt::Display for Error {
Error::CommunicationError(ref err) => write!(f, "Communication error: {}", err),
Error::LibraryError(ref err) => write!(f, "Library error: {}", err),
Error::RandError(ref err) => write!(f, "RNG error: {}", err),
+ Error::UnexpectedError => write!(f, "An unexpected error occurred"),
Error::Unknown(ref err) => write!(f, "Unknown error: {}", err),
}
}
diff --git a/src/util.rs b/src/util.rs
index 2738fce..79b8c34 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -5,7 +5,7 @@ use libc::{c_void, free};
use rand_core::RngCore;
use rand_os::OsRng;
-use crate::error::{CommandError, Error, LibraryError};
+use crate::error::{Error, LibraryError};
/// Log level for libnitrokey.
///
@@ -36,7 +36,7 @@ pub fn owned_str_from_ptr(ptr: *const c_char) -> String {
pub fn result_from_string(ptr: *const c_char) -> Result<String, Error> {
if ptr.is_null() {
- return Err(CommandError::Undefined.into());
+ return Err(Error::UnexpectedError);
}
unsafe {
let s = owned_str_from_ptr(ptr);
@@ -65,7 +65,7 @@ pub fn get_last_result() -> Result<(), Error> {
pub fn get_last_error() -> Error {
return match get_last_result() {
- Ok(()) => CommandError::Undefined.into(),
+ Ok(()) => Error::UnexpectedError,
Err(err) => err,
};
}
diff --git a/tests/pws.rs b/tests/pws.rs
index 51e6189..b89d7f6 100644
--- a/tests/pws.rs
+++ b/tests/pws.rs
@@ -14,7 +14,7 @@ use crate::util::{ADMIN_PASSWORD, USER_PASSWORD};
fn get_slot_name_direct(slot: u8) -> Result<String, Error> {
let ptr = unsafe { nitrokey_sys::NK_get_password_safe_slot_name(slot) };
if ptr.is_null() {
- return Err(CommandError::Undefined.into());
+ return Err(Error::UnexpectedError);
}
let s = unsafe { CStr::from_ptr(ptr).to_string_lossy().into_owned() };
unsafe { free(ptr as *mut c_void) };