From d2f9e204aacc13c6205a3d0f1022d4fd17073a7b Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Fri, 11 Jan 2019 13:30:39 +0000 Subject: Add the get_library_version function This patch adds the get_library_version function to the main library module that queries and returns the libnitrokey version. As the version fields are static values, we fetch them all at the same time and do not provide getters for the individual fields. --- CHANGELOG.md | 1 + TODO.md | 3 --- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ tests/lib.rs | 8 ++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 tests/lib.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f096b5..a6d561d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased - Make three additional error codes known: `CommandError::StringTooLong`, `CommandError::InvalidHexString` and `CommandError::TargetBufferTooSmall`. +- Add the `get_library_version` method to query the libnitrokey version. # v0.3.1 (2019-01-07) - Use `nitrokey-test` to select and execute the unit tests. diff --git a/TODO.md b/TODO.md index f839dc3..110b9a2 100644 --- a/TODO.md +++ b/TODO.md @@ -16,9 +16,6 @@ - `NK_list_devices_by_cpuID` - `NK_connect_with_ID` - `NK_get_device_model` - - `NK_get_library_version` - - `NK_get_major_library_version` - - `NK_get_minor_libray_version` - `NK_get_storage_production_info` - `NK_wink` - Fix timing issues with the `totp_no_pin` and `totp_pin` test cases. diff --git a/src/lib.rs b/src/lib.rs index bb34870..5b7ada1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,6 +104,25 @@ pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData}; pub use crate::pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT}; pub use crate::util::{CommandError, LogLevel}; +/// A version of the libnitrokey library. +/// +/// Use the [`get_library_version`](fn.get_library_version.html) function to query the library +/// version. +#[derive(Clone, Debug, PartialEq)] +pub struct Version { + /// The library version as a string. + /// + /// The library version is the output of `git describe --always` at compile time, for example + /// `v3.3` or `v3.4.1`. If the library has not been built from a release, the version string + /// contains the number of commits since the last release and the hash of the current commit, for + /// example `v3.3-19-gaee920b`. + pub git: String, + /// The major library version. + pub major: u32, + /// The minor library version. + pub minor: u32, +} + /// Enables or disables debug output. Calling this method with `true` is equivalent to setting the /// log level to `Debug`; calling it with `false` is equivalent to the log level `Error` (see /// [`set_log_level`][]). @@ -125,3 +144,28 @@ pub fn set_log_level(level: LogLevel) { nitrokey_sys::NK_set_debug_level(level.into()); } } + +/// Returns the libnitrokey library version. +/// +/// # Example +/// +/// ``` +/// let version = nitrokey::get_library_version(); +/// println!("Using libnitrokey {}", version.git); +/// ``` +pub fn get_library_version() -> Version { + // NK_get_library_version returns a static string, so we don’t have to free the pointer. + let git = unsafe { nitrokey_sys::NK_get_library_version() }; + let git = if git.is_null() { + String::new() + } else { + util::owned_str_from_ptr(git) + }; + let major = unsafe { nitrokey_sys::NK_get_major_library_version() }; + let minor = unsafe { nitrokey_sys::NK_get_minor_library_version() }; + Version { + git, + major, + minor, + } +} diff --git a/tests/lib.rs b/tests/lib.rs new file mode 100644 index 0000000..06de0ad --- /dev/null +++ b/tests/lib.rs @@ -0,0 +1,8 @@ +#[test] +fn get_library_version() { + let version = nitrokey::get_library_version(); + + assert!(!version.git.is_empty()); + assert!(version.git.starts_with("v")); + assert!(version.major > 0); +} -- cgit v1.2.3