From fd2ae7f3e6821741cd1ae32409e69dcf95b746cf Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sun, 19 Mar 2023 20:43:39 +0100 Subject: [PATCH] Add first unit test --- src/main.rs | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3d1b193..67231c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,16 +6,18 @@ use totp_rfc6238::{HashAlgorithm, TotpGenerator}; /// Get the current time since epoch pub fn time() -> u64 { - SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() } - /// TOTP - One Time Password generator #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] struct Args { #[clap(subcommand)] - command: SubCommands + command: SubCommands, } #[derive(Subcommand, Debug)] @@ -48,24 +50,34 @@ fn main() { println!("To configure TOPT :"); println!("1. Please open https://mysignins.microsoft.com/security-info"); println!("2. Click on \"Add method\""); - println!("3. On the popup that appears, choose \"Authenticator app\" and click \"Add\"."); - println!("4. Click on \"I want to use a different authenticator app\" and click \"Next\""); + println!( + "3. On the popup that appears, choose \"Authenticator app\" and click \"Add\"." + ); + println!( + "4. Click on \"I want to use a different authenticator app\" and click \"Next\"" + ); println!("5. Click on \"Can't save image ?\" and copy the \"Secret key\""); - println!("6. Re-run this program running {} code --secret ", std::env::args().next().unwrap()); + println!( + "6. Re-run this program running {} code --secret ", + std::env::args().next().unwrap() + ); println!("7. Back on the browser, click Next and paste copied code"); } - SubCommands::Code { secret, topt_step, len } => { + SubCommands::Code { + secret, + topt_step, + len, + } => { let totp_generator = TotpGenerator::new() - .set_digit(len).unwrap() - .set_step(topt_step).unwrap() + .set_digit(len) + .unwrap() + .set_step(topt_step) + .unwrap() .set_hash_algorithm(HashAlgorithm::SHA1) .build(); - let key = base32::decode( - Alphabet::RFC4648 { padding: true }, - &secret, - ).unwrap(); + let key = base32::decode(Alphabet::RFC4648 { padding: true }, &secret).unwrap(); let code = totp_generator.get_code(&key); let next_update = totp_generator.get_next_update_time().unwrap(); @@ -75,3 +87,14 @@ fn main() { } } } + +#[cfg(test)] +mod test { + use crate::Args; + + #[test] + fn verify_cli() { + use clap::CommandFactory; + Args::command().debug_assert() + } +}