aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-17 12:47:52 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-20 21:08:50 +0000
commit5e258d26b55af6bed7c316b1c7ac12e20946702d (patch)
treefee2be71586bef2f1978eb1cf40ef84c8ac6492b
parent944e1fa0d51e547dde2a9368d2b8431b109f63c4 (diff)
downloadnitrokey-rs-5e258d26b55af6bed7c316b1c7ac12e20946702d.tar.gz
nitrokey-rs-5e258d26b55af6bed7c316b1c7ac12e20946702d.tar.bz2
Refactor library errors into LibraryError enum
Previously, library errors were part of the CommandError enum. As command errors and library errors are two different error types, they should be split into two enums.
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/auth.rs6
-rw-r--r--src/config.rs4
-rw-r--r--src/device.rs22
-rw-r--r--src/error.rs74
-rw-r--r--src/lib.rs2
-rw-r--r--src/otp.rs20
-rw-r--r--src/pws.rs14
-rw-r--r--src/util.rs4
-rw-r--r--tests/device.rs4
-rw-r--r--tests/otp.rs28
-rw-r--r--tests/pws.rs16
-rw-r--r--tests/util/mod.rs7
13 files changed, 123 insertions, 80 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index def5273..c34175e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,8 @@
- Refactor the error handling code:
- Implement `std::error::Error` for `CommandError`.
- Add the `Error` enum and the `Result` typedef.
+ - Add the `LibraryError` enum and move the library error variants from
+ `CommandError` to `LibraryError`.
- Return `Error` instead of `CommandError` in all public functions.
- Move the `CommandError::RngError` variant to `Error::RandError` and the
`CommandError::Unknown` variant to `Error::Unknown`.
diff --git a/src/auth.rs b/src/auth.rs
index e05f6b3..d1eb049 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -55,7 +55,7 @@ pub trait Authenticate {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`RngError`]: enum.CommandError.html#variant.RngError
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn authenticate_user(self, password: &str) -> Result<User<Self>, (Self, Error)>
@@ -101,7 +101,7 @@ pub trait Authenticate {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`RngError`]: enum.CommandError.html#variant.RngError
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn authenticate_admin(self, password: &str) -> Result<Admin<Self>, (Self, Error)>
@@ -287,7 +287,7 @@ impl<T: Device> Admin<T> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
pub fn write_config(&self, config: Config) -> Result<(), Error> {
let raw_config = RawConfig::try_from(config)?;
unsafe {
diff --git a/src/config.rs b/src/config.rs
index 741d67e..6aa6d10 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,4 +1,4 @@
-use crate::error::{CommandError, Error};
+use crate::error::{Error, LibraryError};
/// The configuration for a Nitrokey.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -41,7 +41,7 @@ fn option_to_config_otp_slot(value: Option<u8>) -> Result<u8, Error> {
if value < 3 {
Ok(value)
} else {
- Err(CommandError::InvalidSlot.into())
+ Err(LibraryError::InvalidSlot.into())
}
}
None => Ok(255),
diff --git a/src/device.rs b/src/device.rs
index ccd0597..5c4014b 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -461,7 +461,7 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn change_admin_pin(&self, current: &str, new: &str) -> Result<(), Error> {
let current_string = get_cstring(current)?;
@@ -497,7 +497,7 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn change_user_pin(&self, current: &str, new: &str) -> Result<(), Error> {
let current_string = get_cstring(current)?;
@@ -533,7 +533,7 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn unlock_user_pin(&self, admin_pin: &str, user_pin: &str) -> Result<(), Error> {
let admin_pin_string = get_cstring(admin_pin)?;
@@ -867,7 +867,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn change_update_pin(&self, current: &str, new: &str) -> Result<(), Error> {
let current_string = get_cstring(current)?;
@@ -907,7 +907,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn enable_firmware_update(&self, update_pin: &str) -> Result<(), Error> {
let update_pin_string = get_cstring(update_pin)?;
@@ -943,7 +943,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn enable_encrypted_volume(&self, user_pin: &str) -> Result<(), Error> {
let user_pin = get_cstring(user_pin)?;
@@ -1021,7 +1021,7 @@ impl Storage {
///
/// [`enable_encrypted_volume`]: #method.enable_encrypted_volume
/// [`AesDecryptionFailed`]: enum.CommandError.html#variant.AesDecryptionFailed
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
pub fn enable_hidden_volume(&self, volume_password: &str) -> Result<(), Error> {
let volume_password = get_cstring(volume_password)?;
unsafe {
@@ -1099,7 +1099,7 @@ impl Storage {
/// ```
///
/// [`AesDecryptionFailed`]: enum.CommandError.html#variant.AesDecryptionFailed
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
pub fn create_hidden_volume(
&self,
slot: u8,
@@ -1145,7 +1145,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn set_unencrypted_volume_mode(
&self,
@@ -1276,7 +1276,7 @@ impl Storage {
/// # }
/// ```
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn clear_new_sd_card_warning(&self, admin_pin: &str) -> Result<(), Error> {
let admin_pin = get_cstring(admin_pin)?;
@@ -1304,7 +1304,7 @@ impl Storage {
/// - [`InvalidString`][] if one of the provided passwords contains a null byte
/// - [`WrongPassword`][] if the admin password is wrong
///
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn export_firmware(&self, admin_pin: &str) -> Result<(), Error> {
let admin_pin_string = get_cstring(admin_pin)?;
diff --git a/src/error.rs b/src/error.rs
index c5a975e..f40d07f 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -11,6 +11,8 @@ pub enum Error {
CommandError(CommandError),
/// Placeholder for testing.
CommunicationError(CommunicationError),
+ /// A library usage error.
+ LibraryError(LibraryError),
/// An error that occured during random number generation.
RandError(rand_core::Error),
/// An unknown error returned by libnitrokey.
@@ -21,6 +23,8 @@ impl From<raw::c_int> for Error {
fn from(code: raw::c_int) -> Self {
if let Some(err) = CommandError::try_from(code) {
Error::CommandError(err)
+ } else if let Some(err) = LibraryError::try_from(code) {
+ Error::LibraryError(err)
} else {
Error::Unknown(code.into())
}
@@ -33,6 +37,12 @@ impl From<CommandError> for Error {
}
}
+impl From<LibraryError> for Error {
+ fn from(err: LibraryError) -> Self {
+ Error::LibraryError(err)
+ }
+}
+
impl From<rand_core::Error> for Error {
fn from(error: rand_core::Error) -> Self {
Error::RandError(error)
@@ -44,6 +54,7 @@ impl error::Error for Error {
match *self {
Error::CommandError(ref err) => Some(err),
Error::CommunicationError(_) => None,
+ Error::LibraryError(ref err) => Some(err),
Error::RandError(ref err) => Some(err),
Error::Unknown(_) => None,
}
@@ -55,6 +66,7 @@ impl fmt::Display for Error {
match *self {
Error::CommandError(ref err) => write!(f, "Command error: {}", err),
Error::CommunicationError(_) => write!(f, "Placeholder"),
+ Error::LibraryError(ref err) => write!(f, "Library error: {}", err),
Error::RandError(ref err) => write!(f, "RNG error: {}", err),
Error::Unknown(ref err) => write!(f, "Unknown error: {}", err),
}
@@ -90,16 +102,6 @@ pub enum CommandError {
AesDecryptionFailed,
/// An unspecified error occurred.
Undefined,
- /// You passed a string containing a null byte.
- InvalidString,
- /// A supplied string exceeded a length limit.
- StringTooLong,
- /// You passed an invalid slot.
- InvalidSlot,
- /// The supplied string was not in hexadecimal format.
- InvalidHexString,
- /// The target buffer was smaller than the source.
- TargetBufferTooSmall,
}
/// Placeholder for testing.
@@ -122,10 +124,6 @@ impl CommandError {
8 => Some(CommandError::NotSupported),
9 => Some(CommandError::UnknownCommand),
10 => Some(CommandError::AesDecryptionFailed),
- 200 => Some(CommandError::StringTooLong),
- 201 => Some(CommandError::InvalidSlot),
- 202 => Some(CommandError::InvalidHexString),
- 203 => Some(CommandError::TargetBufferTooSmall),
_ => None,
}
}
@@ -149,13 +147,6 @@ impl CommandError {
CommandError::UnknownCommand => "This command is unknown".into(),
CommandError::AesDecryptionFailed => "AES decryption failed".into(),
CommandError::Undefined => "An unspecified error occurred".into(),
- CommandError::InvalidString => "You passed a string containing a null byte".into(),
- CommandError::StringTooLong => "The supplied string is too long".into(),
- CommandError::InvalidSlot => "The given slot is invalid".into(),
- CommandError::InvalidHexString => {
- "The supplied string is not in hexadecimal format".into()
- }
- CommandError::TargetBufferTooSmall => "The target buffer is too small".into(),
}
}
}
@@ -167,3 +158,44 @@ impl fmt::Display for CommandError {
write!(f, "{}", self.as_str())
}
}
+
+/// A library usage error.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum LibraryError {
+ /// A supplied string exceeded a length limit.
+ StringTooLong,
+ /// You passed an invalid slot.
+ InvalidSlot,
+ /// The supplied string was not in hexadecimal format.
+ InvalidHexString,
+ /// The target buffer was smaller than the source.
+ TargetBufferTooSmall,
+ /// You passed a string containing a null byte.
+ InvalidString,
+}
+
+impl LibraryError {
+ fn try_from(value: raw::c_int) -> Option<Self> {
+ match value {
+ 200 => Some(LibraryError::StringTooLong),
+ 201 => Some(LibraryError::InvalidSlot),
+ 202 => Some(LibraryError::InvalidHexString),
+ 203 => Some(LibraryError::TargetBufferTooSmall),
+ _ => None,
+ }
+ }
+}
+
+impl error::Error for LibraryError {}
+
+impl fmt::Display for LibraryError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.write_str(match *self {
+ LibraryError::StringTooLong => "The supplied string is too long",
+ LibraryError::InvalidSlot => "The given slot is invalid",
+ LibraryError::InvalidHexString => "The supplied string is not in hexadecimal format",
+ LibraryError::TargetBufferTooSmall => "The target buffer is too small",
+ LibraryError::InvalidString => "You passed a string containing a null byte",
+ })
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 8522e83..993ec92 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -104,7 +104,7 @@ pub use crate::device::{
connect, connect_model, Device, DeviceWrapper, Model, Pro, SdCardData, Storage,
StorageProductionInfo, StorageStatus, VolumeMode, VolumeStatus,
};
-pub use crate::error::{CommandError, CommunicationError, Error, Result};
+pub use crate::error::{CommandError, CommunicationError, Error, LibraryError, Result};
pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData};
pub use crate::pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT};
pub use crate::util::LogLevel;
diff --git a/src/otp.rs b/src/otp.rs
index 5dfe8b1..7535a77 100644
--- a/src/otp.rs
+++ b/src/otp.rs
@@ -47,8 +47,8 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`NoName`]: enum.CommandError.html#variant.NoName
fn write_hotp_slot(&self, data: OtpSlotData, counter: u64) -> Result<(), Error>;
@@ -83,8 +83,8 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`NoName`]: enum.CommandError.html#variant.NoName
fn write_totp_slot(&self, data: OtpSlotData, time_window: u16) -> Result<(), Error>;
@@ -115,7 +115,7 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
fn erase_hotp_slot(&self, slot: u8) -> Result<(), Error>;
/// Erases a TOTP slot.
@@ -145,7 +145,7 @@ pub trait ConfigureOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
fn erase_totp_slot(&self, slot: u8) -> Result<(), Error>;
}
@@ -216,7 +216,7 @@ pub trait GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
fn get_hotp_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_hotp_slot_name(slot)) }
@@ -245,7 +245,7 @@ pub trait GenerateOtp {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
fn get_totp_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_totp_slot_name(slot)) }
@@ -275,7 +275,7 @@ pub trait GenerateOtp {
/// ```
///
/// [`get_config`]: trait.Device.html#method.get_config
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`NotAuthorized`]: enum.CommandError.html#variant.NotAuthorized
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
fn get_hotp_code(&self, slot: u8) -> Result<String, Error> {
@@ -320,7 +320,7 @@ pub trait GenerateOtp {
///
/// [`set_time`]: #method.set_time
/// [`get_config`]: trait.Device.html#method.get_config
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`NotAuthorized`]: enum.CommandError.html#variant.NotAuthorized
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
fn get_totp_code(&self, slot: u8) -> Result<String, Error> {
diff --git a/src/pws.rs b/src/pws.rs
index e974737..47965d7 100644
--- a/src/pws.rs
+++ b/src/pws.rs
@@ -109,7 +109,7 @@ pub trait GetPasswordSafe {
/// [`lock`]: trait.Device.html#method.lock
/// [`AesDecryptionFailed`]: enum.CommandError.html#variant.AesDecryptionFailed
/// [`Device::build_aes_key`]: trait.Device.html#method.build_aes_key
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
/// [`Unknown`]: enum.CommandError.html#variant.Unknown
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn get_password_safe(&self, user_pin: &str) -> Result<PasswordSafe<'_>, Error>;
@@ -207,7 +207,7 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
pub fn get_slot_name(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_name(slot)) }
@@ -240,7 +240,7 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
pub fn get_slot_login(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_login(slot)) }
@@ -273,7 +273,7 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
/// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed
pub fn get_slot_password(&self, slot: u8) -> Result<String, Error> {
unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_password(slot)) }
@@ -304,8 +304,8 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
- /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
+ /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString
pub fn write_slot(
&self,
slot: u8,
@@ -350,7 +350,7 @@ impl<'a> PasswordSafe<'a> {
/// # }
/// ```
///
- /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot
+ /// [`InvalidSlot`]: enum.LibraryError.html#variant.InvalidSlot
pub fn erase_slot(&self, slot: u8) -> Result<(), Error> {
unsafe { get_command_result(nitrokey_sys::NK_erase_password_safe_slot(slot)) }
}
diff --git a/src/util.rs b/src/util.rs
index 3b9904f..2738fce 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};
+use crate::error::{CommandError, Error, LibraryError};
/// Log level for libnitrokey.
///
@@ -78,7 +78,7 @@ pub fn generate_password(length: usize) -> Result<Vec<u8>, Error> {
}
pub fn get_cstring<T: Into<Vec<u8>>>(s: T) -> Result<CString, Error> {
- CString::new(s).or(Err(CommandError::InvalidString.into()))
+ CString::new(s).or(Err(LibraryError::InvalidString.into()))
}
impl Into<i32> for LogLevel {
diff --git a/tests/device.rs b/tests/device.rs
index 174624f..ee5dae1 100644
--- a/tests/device.rs
+++ b/tests/device.rs
@@ -6,7 +6,7 @@ use std::{thread, time};
use nitrokey::{
Authenticate, CommandError, Config, ConfigureOtp, Device, Error, GenerateOtp, GetPasswordSafe,
- OtpMode, OtpSlotData, Storage, VolumeMode,
+ LibraryError, OtpMode, OtpSlotData, Storage, VolumeMode,
};
use nitrokey_test::test as test_device;
@@ -130,7 +130,7 @@ fn config(device: DeviceWrapper) {
assert_eq!(config, get_config);
let config = Config::new(None, Some(9), None, true);
- assert_cmd_err!(CommandError::InvalidSlot, admin.write_config(config));
+ assert_lib_err!(LibraryError::InvalidSlot, admin.write_config(config));
let config = Config::new(Some(1), None, Some(0), false);
assert_ok!((), admin.write_config(config));
diff --git a/tests/otp.rs b/tests/otp.rs
index d328351..51a6539 100644
--- a/tests/otp.rs
+++ b/tests/otp.rs
@@ -4,8 +4,8 @@ use std::fmt::Debug;
use std::ops::Deref;
use nitrokey::{
- Admin, Authenticate, CommandError, Config, ConfigureOtp, Device, GenerateOtp, OtpMode,
- OtpSlotData,
+ Admin, Authenticate, CommandError, Config, ConfigureOtp, Device, GenerateOtp, LibraryError,
+ OtpMode, OtpSlotData,
};
use nitrokey_test::test as test_device;
@@ -106,7 +106,7 @@ fn hotp_slot_name(device: DeviceWrapper) {
let result = device.get_hotp_slot_name(1);
assert_eq!("test-hotp", result.unwrap());
let result = device.get_hotp_slot_name(4);
- assert_cmd_err!(CommandError::InvalidSlot, result);
+ assert_lib_err!(LibraryError::InvalidSlot, result);
}
#[test_device]
@@ -115,17 +115,17 @@ fn hotp_error(device: DeviceWrapper) {
let slot_data = OtpSlotData::new(1, "", HOTP_SECRET, OtpMode::SixDigits);
assert_cmd_err!(CommandError::NoName, admin.write_hotp_slot(slot_data, 0));
let slot_data = OtpSlotData::new(4, "test", HOTP_SECRET, OtpMode::SixDigits);
- assert_cmd_err!(
- CommandError::InvalidSlot,
+ assert_lib_err!(
+ LibraryError::InvalidSlot,
admin.write_hotp_slot(slot_data, 0)
);
let slot_data = OtpSlotData::new(1, "test", "foobar", OtpMode::SixDigits);
- assert_cmd_err!(
- CommandError::InvalidHexString,
+ assert_lib_err!(
+ LibraryError::InvalidHexString,
admin.write_hotp_slot(slot_data, 0)
);
let code = admin.get_hotp_code(4);
- assert_cmd_err!(CommandError::InvalidSlot, code);
+ assert_lib_err!(LibraryError::InvalidSlot, code);
}
#[test_device]
@@ -249,7 +249,7 @@ fn totp_slot_name(device: DeviceWrapper) {
assert!(result.is_ok());
assert_eq!("test-totp", result.unwrap());
let result = device.get_totp_slot_name(16);
- assert_cmd_err!(CommandError::InvalidSlot, result);
+ assert_lib_err!(LibraryError::InvalidSlot, result);
}
#[test_device]
@@ -258,17 +258,17 @@ fn totp_error(device: DeviceWrapper) {
let slot_data = OtpSlotData::new(1, "", TOTP_SECRET, OtpMode::SixDigits);
assert_cmd_err!(CommandError::NoName, admin.write_totp_slot(slot_data, 0));
let slot_data = OtpSlotData::new(20, "test", TOTP_SECRET, OtpMode::SixDigits);
- assert_cmd_err!(
- CommandError::InvalidSlot,
+ assert_lib_err!(
+ LibraryError::InvalidSlot,
admin.write_totp_slot(slot_data, 0)
);
let slot_data = OtpSlotData::new(4, "test", "foobar", OtpMode::SixDigits);
- assert_cmd_err!(
- CommandError::InvalidHexString,
+ assert_lib_err!(
+ LibraryError::InvalidHexString,
admin.write_totp_slot(slot_data, 0)
);
let code = admin.get_totp_code(20);
- assert_cmd_err!(CommandError::InvalidSlot, code);
+ assert_lib_err!(LibraryError::InvalidSlot, code);
}
#[test_device]
diff --git a/tests/pws.rs b/tests/pws.rs
index a4647bd..51e6189 100644
--- a/tests/pws.rs
+++ b/tests/pws.rs
@@ -3,7 +3,9 @@ mod util;
use std::ffi::CStr;
use libc::{c_int, c_void, free};
-use nitrokey::{CommandError, Device, Error, GetPasswordSafe, PasswordSafe, SLOT_COUNT};
+use nitrokey::{
+ CommandError, Device, Error, GetPasswordSafe, LibraryError, PasswordSafe, SLOT_COUNT,
+};
use nitrokey_sys;
use nitrokey_test::test as test_device;
@@ -104,17 +106,17 @@ fn get_data(device: DeviceWrapper) {
assert_eq!(login, pws.get_slot_login(1).unwrap());
assert_eq!(password, pws.get_slot_password(1).unwrap());
- assert_cmd_err!(CommandError::InvalidSlot, pws.get_slot_name(SLOT_COUNT));
- assert_cmd_err!(CommandError::InvalidSlot, pws.get_slot_login(SLOT_COUNT));
- assert_cmd_err!(CommandError::InvalidSlot, pws.get_slot_password(SLOT_COUNT));
+ assert_lib_err!(LibraryError::InvalidSlot, pws.get_slot_name(SLOT_COUNT));
+ assert_lib_err!(LibraryError::InvalidSlot, pws.get_slot_login(SLOT_COUNT));
+ assert_lib_err!(LibraryError::InvalidSlot, pws.get_slot_password(SLOT_COUNT));
}
#[test_device]
fn write(device: DeviceWrapper) {
let pws = get_pws(&device);
- assert_cmd_err!(
- CommandError::InvalidSlot,
+ assert_lib_err!(
+ LibraryError::InvalidSlot,
pws.write_slot(SLOT_COUNT, "name", "login", "password")
);
@@ -137,7 +139,7 @@ fn write(device: DeviceWrapper) {
#[test_device]
fn erase(device: DeviceWrapper) {
let pws = get_pws(&device);
- assert_cmd_err!(CommandError::InvalidSlot, pws.erase_slot(SLOT_COUNT));
+ assert_lib_err!(LibraryError::InvalidSlot, pws.erase_slot(SLOT_COUNT));
assert_ok!((), pws.write_slot(0, "name", "login", "password"));
assert_ok!((), pws.erase_slot(0));
diff --git a/tests/util/mod.rs b/tests/util/mod.rs
index f0d0bb5..b1d3ea3 100644
--- a/tests/util/mod.rs
+++ b/tests/util/mod.rs
@@ -67,3 +67,10 @@ macro_rules! assert_cmd_err {
assert_err!(::nitrokey::Error::CommandError, $left, $right);
};
}
+
+#[macro_export]
+macro_rules! assert_lib_err {
+ ($left:expr, $right:expr) => {
+ assert_err!(::nitrokey::Error::LibraryError, $left, $right);
+ };
+}