git clone https://garage44.eu/jeroen/pinnacle.git.. /contacts/src/tests.rs
use super::*;
use alloc::vec;
fn s(v: &str) -> String {
v.into()
}
#[test]
fn reads_an_android_export() {
// vCard 2.1 as Android writes it: a name with an accent in
// quoted-printable, soft line breaks, types as bare parameters.
let text = "BEGIN:VCARD\r\n\
VERSION:2.1\r\n\
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:Jansen;Ren=C3=A9e;;;\r\n\
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:Ren=C3=A9e Jans=\r\n\
en\r\n\
TEL;CELL:+31 6 12345678\r\n\
TEL;HOME:050-1234567\r\n\
EMAIL;HOME:renee@example.nl\r\n\
END:VCARD\r\n";
let entries = parse(text);
assert_eq!(entries.len(), 1);
let e = &entries[0];
assert_eq!(e.name, "Renée Jansen");
assert_eq!(e.phones, vec![s("+31 6 12345678"), s("050-1234567")]);
assert_eq!(e.emails, vec![s("renee@example.nl")]);
}
#[test]
fn reads_a_google_export() {
// 3.0 with grouped properties, an escaped comma, a folded note and a
// photo that is skipped.
let text = "BEGIN:VCARD\n\
VERSION:3.0\n\
FN:Bo de Vries\n\
N:de Vries;Bo;;;\n\
item1.TEL;TYPE=CELL:06 98765432\n\
item1.X-ABLabel:mobile\n\
ADR;TYPE=HOME:;;Grote Markt 1;Groningen;;9711 LV;Nederland\n\
ORG:Garage44\\, Groningen;Dev\n\
BDAY:19850412\n\
PHOTO;ENCODING=b;TYPE=JPEG:/9j/4AAQSkZJRgABAQ\n\
NOTE:Met de fiets\\, altijd.\\nTweede regel die heel lang doo\n \
rloopt\n\
END:VCARD\n";
let e = &parse(text)[0];
assert_eq!(e.name, "Bo de Vries");
assert_eq!(e.phones, vec![s("06 98765432")]);
assert_eq!(e.address, "Grote Markt 1, Groningen, 9711 LV, Nederland");
assert_eq!(e.organisation, "Garage44, Groningen, Dev");
assert_eq!(e.birthday, "1985-04-12");
assert_eq!(
e.note,
"Met de fiets, altijd.\nTweede regel die heel lang doorloopt"
);
}
#[test]
fn reads_vcard_4_uris_and_names_from_n() {
let text = "BEGIN:VCARD\nVERSION:4.0\nN:Berg;Jan;;Dr.;\n\
TEL;VALUE=uri;TYPE=cell:tel:+31-6-1111\nEMAIL:mailto:jan@example.org\n\
BDAY:--0229\nEND:VCARD\n";
let e = &parse(text)[0];
assert_eq!(e.name, "Dr. Jan Berg");
assert_eq!(e.phones, vec![s("+31-6-1111")]);
assert_eq!(e.emails, vec![s("jan@example.org")]);
assert_eq!(e.birthday, "--02-29");
}
#[test]
fn reads_several_and_skips_what_is_empty_or_unfinished() {
let text = "BEGIN:VCARD\nFN:Ada\nEND:VCARD\n\
BEGIN:VCARD\nVERSION:3.0\nEND:VCARD\n\
garbage line\n\
BEGIN:VCARD\nFN:Bo\nEND:VCARD\n\
BEGIN:VCARD\nFN:Cut off";
let names: Vec<String> = parse(text).into_iter().map(|e| e.name).collect();
assert_eq!(names, vec![s("Ada"), s("Bo")]);
}
#[test]
fn a_contact_without_a_name_is_shown_by_what_it_has() {
let e = &parse("BEGIN:VCARD\nTEL:112\nEND:VCARD\n")[0];
assert_eq!(e.title(), "112");
}
fn everything() -> Entry {
let mut card = vec![7u8; CARD_SIZE];
card[..32].copy_from_slice(&[9u8; 32]);
Entry {
id: String::new(),
uid: s("pinnacle-1"),
name: s("Zoë van der Berg; jr."),
phones: vec![s("+31 6 1"), s("050 2")],
emails: vec![s("zoe@example.nl")],
address: s("Vismarkt 3, Groningen"),
birthday: s("1990-01-31"),
organisation: s("Cartopolis"),
note: s(
"Eerste regel\nTweede, met \\ en ; erin, en dan nog veel meer tekst zodat de regel gevouwen wordt",
),
key: Some([9u8; 32]),
card: Some(card),
}
}
#[test]
fn what_is_written_reads_back_the_same() {
let e = everything();
let text = e.to_vcard();
assert!(text.split("\r\n").all(|l| l.len() <= 75), "{text}");
assert_eq!(parse(&text), vec![e.clone()]);
let two = to_vcards(&[
e.clone(),
Entry {
name: s("Bo"),
..Entry::default()
},
]);
assert_eq!(parse(&two).len(), 2);
}
#[test]
fn the_id_is_kept_but_not_exported() {
let e = Entry {
id: "0123456789abcdef0123456789abcdef".into(),
..everything()
};
assert_eq!(parse(&e.to_vcard())[0].id, e.id);
assert_eq!(parse(&to_vcards(&[e]))[0].id, "");
}
#[test]
fn folds_between_characters() {
let e = Entry {
name: "é".repeat(60),
..Entry::default()
};
let text = e.to_vcard();
assert!(text.split("\r\n").all(|l| l.len() <= 75));
assert_eq!(parse(&text)[0].name, e.name);
}
#[test]
fn a_card_goes_only_with_its_own_key() {
let mut e = everything();
e.key = Some([1u8; 32]);
let back = &parse(&e.to_vcard())[0];
assert_eq!(back.key, Some([1u8; 32]));
assert_eq!(back.card, None);
// A card alone brings its key.
let mut e = everything();
e.key = None;
assert_eq!(parse(&e.to_vcard())[0].key, Some([9u8; 32]));
}
#[test]
fn hex_both_ways() {
assert_eq!(hex(&[0, 0xab, 0xff]), "00abff");
assert_eq!(unhex("00ABff"), Some(vec![0, 0xab, 0xff]));
assert_eq!(unhex("0g"), None);
assert_eq!(unhex("abc"), None);
}
/// Random ids that count up, so a test knows them.
fn counter() -> impl FnMut(&mut [u8]) {
let mut n = 0u8;
move |b: &mut [u8]| {
n += 1;
b.fill(n);
}
}
fn person(name: &str) -> Entry {
Entry {
name: s(name),
..Entry::default()
}
}
#[test]
fn a_new_entry_gets_an_id_and_a_uid() {
let (mut book, mut random) = (Book::default(), counter());
let id = book.put("", person("Ada"), &mut random).unwrap();
assert_eq!(id.len(), 32);
assert_eq!(book.entries[0].uid, id);
assert_eq!(
book.put("", Entry::default(), &mut random),
None,
"an empty one is refused"
);
}
#[test]
fn an_import_again_replaces_by_uid_or_key() {
let (mut book, mut random) = (Book::default(), counter());
let ada = Entry {
uid: s("google-ada"),
..person("Ada")
};
let id = book.put("", ada.clone(), &mut random).unwrap();
let again = Entry {
phones: vec![s("06 1")],
..ada
};
assert_eq!(book.put("", again, &mut random), Some(id.clone()));
assert_eq!(book.entries.len(), 1);
assert_eq!(book.entries[0].phones, vec![s("06 1")]);
// A contact with a device is known by its key, whatever its UID.
let key = [5u8; 32];
assert!(book.add_card(key, "Bo", &mut random));
assert!(!book.add_card(key, "Bo", &mut random), "one entry a card");
let bo = Entry {
uid: s("elsewhere"),
key: Some(key),
..person("Bo de Vries")
};
book.put("", bo, &mut random).unwrap();
assert_eq!(book.entries.len(), 2);
assert_eq!(book.name_of(&key), Some("Bo de Vries"));
}
#[test]
fn an_edit_keeps_the_key_it_leaves_out() {
let (mut book, mut random) = (Book::default(), counter());
let key = [7u8; 32];
book.add_card(key, "Cas", &mut random);
let id = book.entries[0].id.clone();
assert_eq!(id, hex(&key[..16]), "a contact's entry is named by its key");
let edit = Entry {
id: String::new(),
..person("Cas Smit")
};
assert_eq!(book.put(&id, edit, &mut random), Some(id.clone()));
assert_eq!(book.entries[0].key, Some(key));
assert_eq!(book.entries[0].uid, id);
}
#[test]
fn the_book_reads_back_what_it_kept() {
let (mut book, mut random) = (Book::default(), counter());
book.put("", person("Zoë"), &mut random);
book.put("", person("ada"), &mut random);
book.add_card([9u8; 32], "Bo", &mut random);
assert_eq!(
book.entries.iter().map(|e| e.name.as_str()).collect::<Vec<_>>(),
["ada", "Bo", "Zoë"],
"by name, whatever its case"
);
let again = Book::from_kept(&book.to_kept(), &mut random);
assert_eq!(again.entries, book.entries);
let bo = book.entries[1].id.clone();
let removed = book.remove(&bo).unwrap();
assert_eq!(removed.name, "Bo");
assert_eq!(book.name_of(&[9u8; 32]), None);
}
#[test]
fn ids_missing_or_twice_are_made_anew() {
let mut random = counter();
let text = "BEGIN:VCARD\nFN:A\nX-PINNACLE-ID:0000000000000000000000000000000a\nEND:VCARD\n\
BEGIN:VCARD\nFN:B\nX-PINNACLE-ID:0000000000000000000000000000000a\nEND:VCARD\n\
BEGIN:VCARD\nFN:C\nEND:VCARD\n";
let book = Book::from_kept(text, &mut random);
let mut ids: Vec<&str> = book.entries.iter().map(|e| e.id.as_str()).collect();
ids.sort();
ids.dedup();
assert_eq!(ids.len(), 3);
}