From ea203104905d50e5eb79cb8394fecf8123eb6a93 Mon Sep 17 00:00:00 2001 From: Mark Percival Date: Mon, 1 Mar 2021 22:25:26 +0000 Subject: [PATCH 1/2] Update the fix example in docs and match them in tests --- README.md | 4 ++-- src/lib.rs | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bf7d2ce..a9a91b1 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,13 @@ git = "https://github.com/TimDumol/rust-otp" ```rust // first argument is the secret, second argument is the counter -assert_eq!(make_hotp("base32secret3232".to_ascii(), 0), Some(260182)); +assert_eq!(make_hotp("base32secret3232".to_ascii_uppercase().ok(), 0), Some(260182)); // first argument is the secret, followed by the time step in seconds (Google // Authenticator uses a time step of 30), and then the skew in seconds // (often used when calculating HOTPs for a sequence of consecutive // time intervals, to deal with potential latency and desynchronization). -assert_eq!(make_totp("base32secret3232".to_ascii(), 30, 0), Some(260182)); // true on Unix epoch +assert_eq!(make_totp("base32secret3232".to_ascii_uppercase(), 30, 0).ok(), Some(260182)); // true on Unix epoch ``` diff --git a/src/lib.rs b/src/lib.rs index 0d7445c..7a05bd2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ pub fn make_totp(secret: &str, time_step: u64, skew: i64) -> Result #[cfg(test)] mod tests { - use super::{make_hotp, make_totp_helper}; + use super::{make_hotp, make_totp, make_totp_helper}; #[test] fn hotp() { @@ -86,5 +86,8 @@ mod tests { assert_eq!(make_totp_helper("BASE32SECRET3232", 3600, 0, 7).unwrap(), 260182); assert_eq!(make_totp_helper("BASE32SECRET3232", 30, 0, 35).unwrap(), 55283); assert_eq!(make_totp_helper("BASE32SECRET3232", 1, -2, 1403).unwrap(), 316439); + assert!(make_totp(&"base32secret3232".to_ascii_uppercase(), 30, 0).is_ok()); + let bad_secret = make_totp("base32secret3232", 30, 0); + assert_eq!("invalid secret provided", bad_secret.unwrap_err().to_string()); } } From abc1c66f8f233fd6034a093d093b7259c372613c Mon Sep 17 00:00:00 2001 From: Mark Percival Date: Wed, 3 Mar 2021 20:23:18 +0000 Subject: [PATCH 2/2] Update tests, simplify --- src/lib.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7a05bd2..28d39be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ pub fn make_totp(secret: &str, time_step: u64, skew: i64) -> Result #[cfg(test)] mod tests { - use super::{make_hotp, make_totp, make_totp_helper}; + use super::{make_hotp, make_totp, make_totp_helper, Error}; #[test] fn hotp() { @@ -86,8 +86,11 @@ mod tests { assert_eq!(make_totp_helper("BASE32SECRET3232", 3600, 0, 7).unwrap(), 260182); assert_eq!(make_totp_helper("BASE32SECRET3232", 30, 0, 35).unwrap(), 55283); assert_eq!(make_totp_helper("BASE32SECRET3232", 1, -2, 1403).unwrap(), 316439); - assert!(make_totp(&"base32secret3232".to_ascii_uppercase(), 30, 0).is_ok()); - let bad_secret = make_totp("base32secret3232", 30, 0); - assert_eq!("invalid secret provided", bad_secret.unwrap_err().to_string()); + assert!( + matches!( + make_totp("base32secret3232", 30, 0).unwrap_err(), + Error::InvalidSecret{..} + ) + ); } }