Add new test to validate TOTP codes generation

This commit is contained in:
Pierre HUBERT 2022-04-20 18:06:04 +02:00
parent 834ba1987e
commit 10982190e7

View File

@ -95,3 +95,22 @@ impl TotpKey {
Ok(self.current_code()?.eq(code) || self.previous_code()?.eq(code))
}
}
#[cfg(test)]
mod test {
use crate::data::totp_key::TotpKey;
#[test]
fn test_timing() {
let key = TotpKey::new_random();
let code = key.current_code().unwrap();
let old_code = key.previous_code().unwrap();
assert_ne!(code, old_code);
}
#[test]
fn test_generation() {
let key = TotpKey::from_encoded_secret("JBSWY3DPEHPK3PXP");
assert_eq!("124851", key.get_code_at(|| 1650470683).unwrap());
}
}