Add simple unit tests for all card_do

This commit is contained in:
Heiko Schaefer 2021-09-02 20:04:30 +02:00
parent c7751ff4ce
commit 393e58d489
8 changed files with 174 additions and 2 deletions

View file

@ -264,7 +264,7 @@ pub struct ExtendedLengthInfo {
}
/// Cardholder Related Data (see spec pg. 22)
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct Cardholder {
name: Option<String>,
lang: Option<Vec<[char; 2]>>,
@ -303,7 +303,7 @@ impl From<u8> for Sex {
}
/// PW status Bytes (see spec page 23)
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct PWStatus {
pub(crate) pw1_cds_multi: bool,
pub(crate) pw1_pin_block: bool,

View file

@ -150,3 +150,5 @@ impl TryFrom<&[u8]> for Algo {
complete(parse(data))
}
}
// Tests in algo_info cover this module

View file

@ -67,3 +67,29 @@ impl ApplicationId {
format!("{:04X}:{:08X}", self.manufacturer, self.serial)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_gnuk() {
let data = [
0xd2, 0x76, 0x0, 0x1, 0x24, 0x1, 0x2, 0x0, 0xff, 0xfe, 0x43, 0x19,
0x42, 0x40, 0x0, 0x0,
];
let aid = ApplicationId::try_from(&data[..])
.expect("failed to parse application id");
assert_eq!(
aid,
ApplicationId {
application: 0x1,
version: 0x200,
manufacturer: 0xfffe,
serial: 0x43194240,
}
);
}
}

View file

@ -52,3 +52,28 @@ impl TryFrom<&[u8]> for Cardholder {
Ok(Cardholder { name, lang, sex })
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test() {
let data = [
0x5b, 0x8, 0x42, 0x61, 0x72, 0x3c, 0x3c, 0x46, 0x6f, 0x6f, 0x5f,
0x2d, 0x4, 0x64, 0x65, 0x65, 0x6e, 0x5f, 0x35, 0x1, 0x32,
];
let ch = Cardholder::try_from(&data[..])
.expect("failed to parse cardholder");
assert_eq!(
ch,
Cardholder {
name: Some("Bar<<Foo".to_string()),
lang: Some(vec![['d', 'e'], ['e', 'n']]),
sex: Some(Sex::Female)
}
);
}
}

View file

@ -43,3 +43,24 @@ impl TryFrom<&[u8]> for ExtendedLengthInfo {
})
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_floss34() {
let data = [0x2, 0x2, 0x8, 0x0, 0x2, 0x2, 0x8, 0x0];
let eli = ExtendedLengthInfo::try_from(&data[..])
.expect("failed to parse extended length info");
assert_eq!(
eli,
ExtendedLengthInfo {
max_command_bytes: 2048,
max_response_bytes: 2048,
},
);
}
}

View file

@ -101,3 +101,50 @@ impl TryFrom<&[u8]> for KeySet<Fingerprint> {
.map_err(Error::InternalError)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test() {
let data3 = [
0xb7, 0xcd, 0x9f, 0x76, 0x37, 0x1e, 0x7, 0x7f, 0x76, 0x1c, 0x82,
0x65, 0x55, 0x54, 0x3e, 0x6d, 0x65, 0x6d, 0x1d, 0x80, 0x62, 0xd7,
0x34, 0x22, 0x65, 0xd2, 0xef, 0x33, 0x64, 0xe3, 0x79, 0x52, 0xd9,
0x5e, 0x94, 0x20, 0x5f, 0x4c, 0xce, 0x8b, 0x3f, 0x9, 0x7a, 0xf2,
0xfd, 0x76, 0xa5, 0xa7, 0x57, 0x9b, 0x51, 0x1f, 0xf, 0x44, 0x9a,
0x25, 0x80, 0x2d, 0xb2, 0xb8,
];
let fp_set: KeySet<Fingerprint> = (&data3[..])
.try_into()
.expect("failed to parse fingerprint set");
assert_eq!(
format!("{}", fp_set.signature().unwrap()),
"B7CD9F76371E077F761C826555543E6D656D1D80"
);
assert_eq!(
format!("{}", fp_set.decryption().unwrap()),
"62D7342265D2EF3364E37952D95E94205F4CCE8B"
);
assert_eq!(
format!("{}", fp_set.authentication().unwrap()),
"3F097AF2FD76A5A7579B511F0F449A25802DB2B8"
);
let data1 = [
0xb7, 0xcd, 0x9f, 0x76, 0x37, 0x1e, 0x7, 0x7f, 0x76, 0x1c, 0x82,
0x65, 0x55, 0x54, 0x3e, 0x6d, 0x65, 0x6d, 0x1d, 0x80,
];
let fp = Fingerprint::try_from(&data1[..])
.expect("failed to parse fingerprint");
assert_eq!(
format!("{}", fp),
"B7CD9F76371E077F761C826555543E6D656D1D80"
);
}
}

View file

@ -77,3 +77,25 @@ impl TryFrom<&[u8]> for KeySet<KeyGenerationTime> {
.map_err(Error::InternalError)
}
}
#[cfg(test)]
mod test {
use super::*;
use std::convert::TryInto;
#[test]
fn test() {
let data3 = [
0x60, 0xf3, 0xff, 0x71, 0x60, 0xf3, 0xff, 0x72, 0x60, 0xf3, 0xff,
0x83,
];
let fp_set: KeySet<KeyGenerationTime> = (&data3[..])
.try_into()
.expect("failed to parse KeyGenerationTime set");
assert_eq!(fp_set.signature().unwrap().get(), 0x60f3ff71);
assert_eq!(fp_set.decryption().unwrap().get(), 0x60f3ff72);
assert_eq!(fp_set.authentication().unwrap().get(), 0x60f3ff83);
}
}

View file

@ -74,3 +74,32 @@ impl TryFrom<&[u8]> for PWStatus {
}
}
}
#[cfg(test)]
mod test {
use super::*;
use std::convert::TryInto;
#[test]
fn test() {
let data = [0x0, 0x40, 0x40, 0x40, 0x3, 0x0, 0x3];
let pws: PWStatus =
(&data[..]).try_into().expect("failed to parse PWStatus");
assert_eq!(
pws,
PWStatus {
pw1_cds_multi: false,
pw1_pin_block: false,
pw1_len: 0x40,
rc_len: 0x40,
pw3_pin_block: false,
pw3_len: 0x40,
err_count_pw1: 3,
err_count_rst: 0,
err_count_pw3: 3
}
);
}
}