summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-12-16 15:42:33 +0000
committerRobin Krahl <robin.krahl@ireas.org>2018-12-16 17:04:07 +0100
commit7d64de856294a21fa3da560aefbff12271fb0146 (patch)
tree85544119d7ea8f11cac5091819e778c7afc29e49
parent938096dc85715915515e3749e03fdcad4cb98e1b (diff)
downloadnitrokey-rs-7d64de856294a21fa3da560aefbff12271fb0146.tar.gz
nitrokey-rs-7d64de856294a21fa3da560aefbff12271fb0146.tar.bz2
Refactor tests into the top-level tests directory
Newer Rust versions support integration tests in a top-level tests directory. This patch refactors the existing unit tests into integration tests.
-rw-r--r--src/lib.rs2
-rw-r--r--src/tests/mod.rs4
-rw-r--r--tests/device.rs (renamed from src/tests/device.rs)40
-rw-r--r--tests/otp.rs (renamed from src/tests/otp.rs)8
-rw-r--r--tests/pws.rs (renamed from src/tests/pws.rs)37
-rw-r--r--tests/util/mod.rs (renamed from src/tests/util.rs)6
6 files changed, 61 insertions, 36 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 2df704e..d0e4357 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -96,8 +96,6 @@ mod config;
mod device;
mod otp;
mod pws;
-#[cfg(test)]
-mod tests;
mod util;
use nitrokey_sys;
diff --git a/src/tests/mod.rs b/src/tests/mod.rs
deleted file mode 100644
index 34ca0aa..0000000
--- a/src/tests/mod.rs
+++ /dev/null
@@ -1,4 +0,0 @@
-mod device;
-mod otp;
-mod pws;
-mod util;
diff --git a/src/tests/device.rs b/tests/device.rs
index 1f583e0..7ceac1e 100644
--- a/src/tests/device.rs
+++ b/tests/device.rs
@@ -1,8 +1,12 @@
+mod util;
+
use std::ffi::CStr;
use std::process::Command;
use std::{thread, time};
-use tests::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};
-use {Authenticate, CommandError, Config, Device, Storage};
+
+use nitrokey::{Authenticate, CommandError, Config, Device, Storage};
+
+use crate::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};
static ADMIN_NEW_PASSWORD: &str = "1234567890";
static USER_NEW_PASSWORD: &str = "abcdefghij";
@@ -22,38 +26,38 @@ fn count_nitrokey_block_devices() -> usize {
#[test]
#[cfg_attr(not(feature = "test-no-device"), ignore)]
fn connect_no_device() {
- assert!(::connect().is_err());
- assert!(::Pro::connect().is_err());
- assert!(::Storage::connect().is_err());
+ assert!(nitrokey::connect().is_err());
+ assert!(nitrokey::Pro::connect().is_err());
+ assert!(nitrokey::Storage::connect().is_err());
}
#[test]
#[cfg_attr(not(feature = "test-pro"), ignore)]
fn connect_pro() {
- assert!(::connect().is_ok());
- assert!(::Pro::connect().is_ok());
- assert!(::Storage::connect().is_err());
- match ::connect().unwrap() {
- ::DeviceWrapper::Pro(_) => assert!(true),
- ::DeviceWrapper::Storage(_) => assert!(false),
+ assert!(nitrokey::connect().is_ok());
+ assert!(nitrokey::Pro::connect().is_ok());
+ assert!(nitrokey::Storage::connect().is_err());
+ match nitrokey::connect().unwrap() {
+ nitrokey::DeviceWrapper::Pro(_) => assert!(true),
+ nitrokey::DeviceWrapper::Storage(_) => assert!(false),
};
}
#[test]
#[cfg_attr(not(feature = "test-storage"), ignore)]
fn connect_storage() {
- assert!(::connect().is_ok());
- assert!(::Pro::connect().is_err());
- assert!(::Storage::connect().is_ok());
- match ::connect().unwrap() {
- ::DeviceWrapper::Pro(_) => assert!(false),
- ::DeviceWrapper::Storage(_) => assert!(true),
+ assert!(nitrokey::connect().is_ok());
+ assert!(nitrokey::Pro::connect().is_err());
+ assert!(nitrokey::Storage::connect().is_ok());
+ match nitrokey::connect().unwrap() {
+ nitrokey::DeviceWrapper::Pro(_) => assert!(false),
+ nitrokey::DeviceWrapper::Storage(_) => assert!(true),
};
}
fn assert_empty_serial_number() {
unsafe {
- let ptr = ::nitrokey_sys::NK_device_serial_number();
+ let ptr = nitrokey_sys::NK_device_serial_number();
assert!(!ptr.is_null());
let cstr = CStr::from_ptr(ptr);
assert_eq!(cstr.to_string_lossy(), "");
diff --git a/src/tests/otp.rs b/tests/otp.rs
index cf71d9d..03feb48 100644
--- a/src/tests/otp.rs
+++ b/tests/otp.rs
@@ -1,6 +1,10 @@
+mod util;
+
use std::ops::Deref;
-use tests::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};
-use {Admin, Authenticate, CommandError, Config, ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData};
+
+use nitrokey::{Admin, Authenticate, CommandError, Config, ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData};
+
+use crate::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};
// test suite according to RFC 4226, Appendix D
static HOTP_SECRET: &str = "3132333435363738393031323334353637383930";
diff --git a/src/tests/pws.rs b/tests/pws.rs
index adb3f93..69ad664 100644
--- a/src/tests/pws.rs
+++ b/tests/pws.rs
@@ -1,8 +1,31 @@
-use device::Device;
+mod util;
+
+use std::ffi::CStr;
+
+use libc::{c_int, c_void, free};
+use nitrokey::{CommandError, Device, GetPasswordSafe, PasswordSafe, SLOT_COUNT};
use nitrokey_sys;
-use pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT};
-use tests::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};
-use util::{result_from_string, CommandError};
+
+use crate::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};
+
+fn get_slot_name_direct(slot: u8) -> Result<String, CommandError> {
+ let ptr = unsafe { nitrokey_sys::NK_get_password_safe_slot_name(slot) };
+ if ptr.is_null() {
+ return Err(CommandError::Unknown);
+ }
+ let s = unsafe { CStr::from_ptr(ptr).to_string_lossy().into_owned() };
+ unsafe { free(ptr as *mut c_void) };
+ match s.is_empty() {
+ true => {
+ let error = unsafe { nitrokey_sys::NK_get_last_command_status() } as c_int;
+ match error {
+ 0 => Err(CommandError::Unknown),
+ other => Err(CommandError::from(other)),
+ }
+ },
+ false => Ok(s),
+ }
+}
fn get_pws(device: &Target) -> PasswordSafe {
device.get_password_safe(USER_PASSWORD).unwrap()
@@ -28,13 +51,13 @@ fn drop() {
let pws = get_pws(&device);
assert!(pws.write_slot(1, "name", "login", "password").is_ok());
assert_eq!("name", pws.get_slot_name(1).unwrap());
- let result = result_from_string(unsafe { nitrokey_sys::NK_get_password_safe_slot_name(1) });
+ let result = get_slot_name_direct(1);
assert_eq!(Ok(String::from("name")), result);
}
- let result = result_from_string(unsafe { nitrokey_sys::NK_get_password_safe_slot_name(1) });
+ let result = get_slot_name_direct(1);
assert_eq!(Ok(String::from("name")), result);
assert!(device.lock().is_ok());
- let result = result_from_string(unsafe { nitrokey_sys::NK_get_password_safe_slot_name(1) });
+ let result = get_slot_name_direct(1);
assert_eq!(Err(CommandError::NotAuthorized), result);
}
diff --git a/src/tests/util.rs b/tests/util/mod.rs
index c6fbb8f..257586e 100644
--- a/src/tests/util.rs
+++ b/tests/util/mod.rs
@@ -2,10 +2,10 @@ pub static ADMIN_PASSWORD: &str = "12345678";
pub static USER_PASSWORD: &str = "123456";
#[cfg(feature = "test-no-device")]
-pub type Target = ::Pro;
+pub type Target = nitrokey::Pro;
#[cfg(feature = "test-pro")]
-pub type Target = ::Pro;
+pub type Target = nitrokey::Pro;
#[cfg(feature = "test-storage")]
-pub type Target = ::Storage;
+pub type Target = nitrokey::Storage;