[core] Tests: RFC 6238/4226 vectors, otpauth parsing, Wi-Fi escaping, device key wrap

This commit is contained in:
2026-10-31 10:44:24 +01:00
parent 686c662835
commit fe90e7691b
3 changed files with 189 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
//! Wi-Fi QR payloads: every special character escaped, so a phone reads back exactly what was stored.
use custos_qr::{WifiSecurity, escape, secret_payload, wifi_payload};
/// What a reader does: split on unescaped `;`, undo `\x` escapes.
fn parse(payload: &str) -> Vec<(String, String)> {
let body = payload.strip_prefix("WIFI:").expect("prefix").strip_suffix(";;").expect("suffix");
let mut fields = Vec::new();
let mut cur = String::new();
let mut chars = body.chars();
while let Some(c) = chars.next() {
match c {
'\\' => cur.push(chars.next().expect("escape at end")),
';' => fields.push(std::mem::take(&mut cur)),
c => cur.push(c),
}
}
fields.push(cur);
fields
.into_iter()
.map(|f| {
let (k, v) = f.split_once(':').expect("key:value");
(k.to_owned(), v.to_owned())
})
.collect()
}
#[test]
fn escapes_every_special_character() {
assert_eq!(escape(r#"\;,:""#), r#"\\\;\,\:\""#);
assert_eq!(escape("plain-text_123 ok"), "plain-text_123 ok");
}
#[test]
fn ssid_and_password_with_every_special_character_survive() {
let nasty = r#"My\Net;5G,"home":x"#;
let pass = r#"p\a;s,s:w"o"r;d\"#;
let p = wifi_payload(nasty, pass, WifiSecurity::Wpa, true);
assert_eq!(p, r#"WIFI:T:WPA;S:My\\Net\;5G\,\"home\"\:x;P:p\\a\;s\,s\:w\"o\"r\;d\\;H:true;;"#);
let f = parse(&p);
assert_eq!(
f,
vec![
("T".into(), "WPA".into()),
("S".into(), nasty.into()),
("P".into(), pass.into()),
("H".into(), "true".into()),
]
);
}
#[test]
fn security_kinds_and_hidden_flag() {
assert_eq!(wifi_payload("Cafe", "secret", WifiSecurity::Wep, false), "WIFI:T:WEP;S:Cafe;P:secret;H:false;;");
// An open network has no password field.
assert_eq!(wifi_payload("Open", "ignored", WifiSecurity::None, false), "WIFI:T:nopass;S:Open;H:false;;");
assert_eq!(WifiSecurity::parse("WPA"), WifiSecurity::Wpa);
assert_eq!(WifiSecurity::parse("nopass"), WifiSecurity::None);
assert_eq!(WifiSecurity::parse("WEP"), WifiSecurity::Wep);
assert_eq!(WifiSecurity::parse("anything else"), WifiSecurity::Wpa);
}
#[test]
fn unicode_ssid_is_kept() {
let p = wifi_payload("Žluťoučký kůň 📶", "heslo", WifiSecurity::Wpa, false);
assert_eq!(parse(&p)[1].1, "Žluťoučký kůň 📶");
}
#[test]
fn secret_payload_is_the_value_itself() {
assert_eq!(secret_payload("sk-test;with:specials"), "sk-test;with:specials");
}
+93
View File
@@ -0,0 +1,93 @@
//! RFC 6238 Appendix B and RFC 4226 Appendix D test vectors, exactly as published, plus otpauth parsing.
use custos_totp::{Algorithm, Totp, hotp};
const SEED_SHA1: &[u8] = b"12345678901234567890";
const SEED_SHA256: &[u8] = b"12345678901234567890123456789012";
const SEED_SHA512: &[u8] = b"1234567890123456789012345678901234567890123456789012345678901234";
#[test]
fn rfc6238_appendix_b() {
// Time (s), SHA1, SHA256, SHA512 — 8 digits, period 30, T0 = 0.
let table: [(u64, &str, &str, &str); 6] = [
(59, "94287082", "46119246", "90693936"),
(1111111109, "07081804", "68084774", "25091201"),
(1111111111, "14050471", "67062674", "99943326"),
(1234567890, "89005924", "91819424", "93441116"),
(2000000000, "69279037", "90698825", "38618901"),
(20000000000, "65353130", "77737706", "47863826"),
];
for (t, sha1, sha256, sha512) in table {
for (seed, alg, want) in [
(SEED_SHA1, Algorithm::Sha1, sha1),
(SEED_SHA256, Algorithm::Sha256, sha256),
(SEED_SHA512, Algorithm::Sha512, sha512),
] {
let totp = Totp::new(seed.to_vec(), alg, 8, 30).unwrap();
assert_eq!(totp.code_at(t), want, "T={t} {alg:?}");
}
}
}
#[test]
fn rfc4226_appendix_d() {
let want = ["755224", "287082", "359152", "969429", "338314", "254676", "287922", "162583", "399871", "520489"];
for (counter, code) in want.iter().enumerate() {
assert_eq!(hotp(SEED_SHA1, counter as u64, 6, Algorithm::Sha1), *code, "counter {counter}");
}
}
#[test]
fn otpauth_uri_and_base32() {
// "12345678901234567890" in base32.
let b32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ";
let t = Totp::parse(&format!(
"otpauth://totp/Example%20Co:alice%40example.com?secret={b32}&issuer=Example%20Co&algorithm=SHA1&digits=8&period=30"
))
.unwrap();
assert_eq!(t.code_at(59), "94287082");
assert_eq!(t.issuer(), Some("Example Co"));
assert_eq!(t.account(), Some("alice@example.com"));
assert_eq!((t.digits(), t.period(), t.algorithm()), (8, 30, Algorithm::Sha1));
// Defaults: SHA1, 6 digits, 30 s. A bare secret (lower case, spaces, padding) works too.
let bare = Totp::parse("gezd gnbv gy3t qojq gezd gnbv gy3t qojq").unwrap();
assert_eq!(bare.code_at(59), "287082");
assert_eq!((bare.digits(), bare.period()), (6, 30));
assert_eq!(Totp::parse(&format!("{b32}====")).unwrap().code_at(59), "287082");
let sha256 = Totp::parse(
"otpauth://totp/x?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA&algorithm=SHA256&digits=8",
)
.unwrap();
assert_eq!(sha256.code_at(59), "46119246");
for bad in [
"otpauth://hotp/x?secret=GEZDGNBV&counter=1",
"otpauth://totp/x?digits=6",
"otpauth://totp/x?secret=not*base32",
"otpauth://totp/x?secret=GEZDGNBV&digits=9",
"otpauth://totp/x?secret=GEZDGNBV&period=0",
"otpauth://totp/x?secret=GEZDGNBV&algorithm=MD5",
"",
] {
assert!(Totp::parse(bad).is_err(), "{bad} was accepted");
}
}
#[test]
fn round_trip_through_uri_and_countdown() {
let t = Totp::parse("otpauth://totp/GitHub:mark?secret=JBSWY3DPEHPK3PXP&issuer=GitHub").unwrap();
let back = Totp::parse(&t.to_uri()).unwrap();
assert_eq!(back.code_at(1_700_000_000), t.code_at(1_700_000_000));
assert_eq!(back.issuer(), Some("GitHub"));
assert_eq!(t.seconds_left(1_700_000_000), 30 - 1_700_000_000 % 30);
assert_eq!(t.seconds_left(60), 30);
}
#[test]
fn secret_is_redacted_in_debug() {
let t = Totp::parse("otpauth://totp/x?secret=JBSWY3DPEHPK3PXP").unwrap();
let out = format!("{t:?}");
assert!(!out.contains("JBSWY3DPEHPK3PXP") && !out.contains("72, 101") && out.contains("[redacted]"), "{out}");
}
+24
View File
@@ -202,3 +202,27 @@ fn no_secret_in_debug_or_display() {
// Non-secret fields stay readable in Debug, which is what makes it useful.
assert!(format!("{item:?}").contains("Example"));
}
#[test]
fn device_wrap_opens_the_same_vault_and_nothing_else() {
// Windows Hello gives the desktop app a signature; the core turns it into a key wrap.
let v = sample_vault("pw");
let bytes = v.to_bytes().unwrap();
let material = b"signature bytes from the device key";
let blob = v.wrap_for_device(material).unwrap();
assert_ne!(v.wrap_for_device(material).unwrap(), blob, "fresh salt and nonce each time");
let back = Vault::open_with_device(&bytes, &blob, material).unwrap();
assert_eq!(back.items(), v.items());
assert!(matches!(Vault::open_with_device(&bytes, &blob, b"other signature"), Err(Error::DeviceKeyRejected)));
for i in 0..blob.len() {
let mut t = blob.clone();
t[i] ^= 1;
assert!(Vault::open_with_device(&bytes, &t, material).is_err(), "blob byte {i}");
}
// A blob made for another vault does not open this one.
let other = sample_vault("pw").wrap_for_device(material).unwrap();
assert!(Vault::open_with_device(&bytes, &other, material).is_err());
assert!(!format!("{:?}", Error::DeviceKeyRejected).contains("signature"));
}