[core] Tests: RFC 6238/4226 vectors, otpauth parsing, Wi-Fi escaping, device key wrap
This commit is contained in:
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user