feature: handle some errors in decode_carte_ps

This commit is contained in:
Florian Briand 2024-08-02 23:00:44 +02:00 committed by theo
parent 8b4fe4fd10
commit f083c696f8
Signed by: theo
SSH Key Fingerprint: SHA256:IbehMhSwpXrGUj7vj9iVvfdwe3g09IL9KLUz0zFzcXU
2 changed files with 29 additions and 6 deletions

View File

@ -52,7 +52,7 @@ struct SituationPS {
habilitation_à_signer_un_lot: String,
}
pub fn lire_carte(code_pin: &str, lecteur: &str) -> CartePS {
pub fn lire_carte(code_pin: &str, lecteur: &str) -> Result<CartePS, String> {
let resource_ps = CString::new(lecteur).expect("CString::new failed");
let resource_reader = CString::new("").expect("CString::new failed");
let card_number = CString::new(code_pin).expect("CString::new failed");
@ -79,7 +79,7 @@ pub fn lire_carte(code_pin: &str, lecteur: &str) -> CartePS {
decode_carte_ps(groups)
}
fn decode_carte_ps(groups: Vec<Block>) -> CartePS {
fn decode_carte_ps(groups: Vec<Block>) -> Result<CartePS, String> {
let mut carte_ps = CartePS::default();
for group in groups {
for (field_index, field) in group.content.iter().enumerate() {
@ -244,11 +244,16 @@ fn decode_carte_ps(groups: Vec<Block>) -> CartePS {
.habilitation_à_signer_un_lot =
String::from_utf8_lossy(field.content).to_string();
}
_ => (),
_ => {
return Err(format!(
"Unknown (group, field) pair: ({}, {})",
group.id, field.id
))
}
}
}
}
carte_ps
Ok(carte_ps)
}
#[cfg(test)]
@ -275,7 +280,7 @@ mod test_decode_carte_ps {
48, 2, 48, 48, 1, 48, 1, 48, 1, 48, 1, 49, 1, 49,
];
let blocks = decode_ssv_memory(bytes, bytes.len());
let carte_ps = decode_carte_ps(blocks);
let carte_ps = decode_carte_ps(blocks).unwrap();
assert_eq!(carte_ps.titulaire.type_de_carte_ps, "0");
assert_eq!(carte_ps.titulaire.type_d_identification_nationale, "8");
@ -342,4 +347,22 @@ mod test_decode_carte_ps {
);
assert_eq!(carte_ps.situations[0].habilitation_à_signer_un_lot, "1");
}
#[test]
#[should_panic]
fn test_missing_field() {
todo!();
}
#[test]
#[should_panic]
fn test_unknown_group_field_pair() {
todo!();
}
#[test]
#[should_panic]
fn test_invalid_field_format() {
todo!();
}
}

View File

@ -51,7 +51,7 @@ pub fn demo() {
let code_pin = "1234";
let lecteur = "HID Global OMNIKEY 3x21 Smart Card Reader 0";
let carte_ps = lire_carte(code_pin, lecteur);
let carte_ps = lire_carte(code_pin, lecteur).unwrap();
println!("CartePS: {:#?}", carte_ps);
ssv_lire_config();