101 lines · 4.8 KB · RustRaw
use super::*;const TIMES: [u64; 6] = [59, 1_111_111_109, 1_111_111_111, 1_234_567_890, 2_000_000_000, 20_000_000_000];fn rfc(algorithm: Algorithm, seed: &[u8]) -> Totp { Totp::from_bytes(seed, algorithm, 8, 30).unwrap()}#[test]fn rfc_6238_appendix_b_sha1() { let totp = rfc(Algorithm::Sha1, b"12345678901234567890"); let codes: Vec<String> = TIMES.iter().map(|&t| totp.code_at(t)).collect(); assert_eq!(codes, ["94287082", "07081804", "14050471", "89005924", "69279037", "65353130"]);}#[test]fn rfc_6238_appendix_b_sha256() { let totp = rfc(Algorithm::Sha256, b"12345678901234567890123456789012"); let codes: Vec<String> = TIMES.iter().map(|&t| totp.code_at(t)).collect(); assert_eq!(codes, ["46119246", "68084774", "67062674", "91819424", "90698825", "77737706"]);}#[test]fn rfc_6238_appendix_b_sha512() { let totp = rfc(Algorithm::Sha512, b"1234567890123456789012345678901234567890123456789012345678901234"); let codes: Vec<String> = TIMES.iter().map(|&t| totp.code_at(t)).collect(); assert_eq!(codes, ["90693936", "25091201", "99943326", "93441116", "38618901", "47863826"]);}#[test]fn rfc_4226_appendix_d_hotp() { let expected = [755224, 287082, 359152, 969429, 338314, 254676, 287922, 162583, 399871, 520489]; for (counter, want) in expected.iter().enumerate() { assert_eq!(hotp(b"12345678901234567890", counter as u64, 6, Algorithm::Sha1), *want); }}#[test]fn six_digits_keep_their_leading_zero_and_the_seconds_left_count_down() { let totp = Totp::from_bytes(b"12345678901234567890", Algorithm::Sha1, 6, 30).unwrap(); assert_eq!(totp.code_at(1_111_111_109), "081804"); assert_eq!(totp.now(59), Code { code: "287082".into(), seconds_left: 1, period: 30 }); assert_eq!(totp.now(60).seconds_left, 30); assert_eq!(totp.now(61).seconds_left, 29);}#[test]fn an_otpauth_address_reads_every_parameter() { // base32 of "12345678901234567890" let totp = Totp::parse("otpauth://totp/Example%20Co:alice%40example.com?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=Example%20Co&algorithm=SHA1&digits=8&period=30").unwrap(); assert_eq!(totp.issuer.as_deref(), Some("Example Co")); assert_eq!(totp.account.as_deref(), Some("alice@example.com")); assert_eq!((totp.algorithm, totp.digits, totp.period), (Algorithm::Sha1, 8, 30)); assert_eq!(totp.code_at(59), "94287082");}#[test]fn defaults_are_sha1_six_digits_thirty_seconds_and_the_label_names_the_issuer() { let totp = Totp::parse("otpauth://totp/Site:bob?secret=gezdgnbvgy3tqojqgezdgnbvgy3tqojq").unwrap(); assert_eq!((totp.algorithm, totp.digits, totp.period), (Algorithm::Sha1, 6, 30)); assert_eq!(totp.issuer.as_deref(), Some("Site")); assert_eq!(totp.account.as_deref(), Some("bob"));}#[test]fn sha256_and_sha512_and_other_periods_read() { let totp = Totp::parse("otpauth://totp/x?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA&algorithm=SHA256&digits=8").unwrap(); assert_eq!(totp.code_at(59), "46119246"); let totp = Totp::parse("otpauth://totp/x?secret=GEZDGNBVGY3TQOJQ&algorithm=sha512&period=60&digits=7").unwrap(); assert_eq!((totp.algorithm, totp.digits, totp.period), (Algorithm::Sha512, 7, 60)); assert_eq!(totp.now(61).seconds_left, 59);}#[test]fn a_bare_secret_with_spaces_reads_as_the_defaults() { let totp = Totp::parse("gezd gnbv gy3t qojq gezd gnbv gy3t qojq").unwrap(); assert_eq!(totp.digits, 6); assert_eq!(totp.code_at(59), "287082");}#[test]fn what_is_not_a_time_code_is_refused_in_words() { assert_eq!(Totp::parse("otpauth://hotp/x?secret=GEZDGNBV&counter=1").unwrap_err(), TotpError::CounterCode); assert_eq!(Totp::parse("otpauth://totp/x?issuer=a").unwrap_err(), TotpError::NoSecret); assert_eq!(Totp::parse("otpauth://totp/x?secret=GE1!").unwrap_err(), TotpError::NotBase32); assert_eq!(Totp::parse("otpauth://totp/x?secret=GEZDGNBV&algorithm=MD5").unwrap_err(), TotpError::BadAlgorithm("MD5".into())); assert_eq!(Totp::parse("otpauth://totp/x?secret=GEZDGNBV&digits=9").unwrap_err(), TotpError::BadDigits("9".into())); assert_eq!(Totp::parse("otpauth://totp/x?secret=GEZDGNBV&period=0").unwrap_err(), TotpError::BadPeriod("0".into())); assert_eq!(Totp::parse("https://example.com/").unwrap_err(), TotpError::NotAnAddress); assert!(!format!("{:?}", Totp::parse("GEZDGNBV").unwrap()).contains("GEZDGNBV"), "Debug never prints the secret");}#[test]fn the_address_round_trips() { let first = Totp::parse("otpauth://totp/Acme:me%40acme.test?secret=GEZDGNBVGY3TQOJQ&algorithm=SHA256&digits=8&period=45&issuer=Acme").unwrap(); let again = Totp::parse(&first.to_uri()).unwrap(); assert_eq!(first, again); assert_eq!(base32_encode(b"12345678901234567890"), "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"); assert_eq!(base32_encode(b"f"), "MY"); assert_eq!(base32_decode("MY======").unwrap().as_slice(), b"f");}