diff --git a/openpgp-card/src/card_do.rs b/openpgp-card/src/card_do.rs index 8e861a7..9803851 100644 --- a/openpgp-card/src/card_do.rs +++ b/openpgp-card/src/card_do.rs @@ -264,7 +264,7 @@ pub struct ExtendedLengthInfo { } /// Cardholder Related Data (see spec pg. 22) -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct Cardholder { name: Option, lang: Option>, @@ -303,7 +303,7 @@ impl From 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, diff --git a/openpgp-card/src/card_do/algo_attrs.rs b/openpgp-card/src/card_do/algo_attrs.rs index b08fb35..7172e8f 100644 --- a/openpgp-card/src/card_do/algo_attrs.rs +++ b/openpgp-card/src/card_do/algo_attrs.rs @@ -150,3 +150,5 @@ impl TryFrom<&[u8]> for Algo { complete(parse(data)) } } + +// Tests in algo_info cover this module diff --git a/openpgp-card/src/card_do/application_id.rs b/openpgp-card/src/card_do/application_id.rs index 0d8a863..1c6f409 100644 --- a/openpgp-card/src/card_do/application_id.rs +++ b/openpgp-card/src/card_do/application_id.rs @@ -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, + } + ); + } +} diff --git a/openpgp-card/src/card_do/cardholder.rs b/openpgp-card/src/card_do/cardholder.rs index 2336b9a..4eb443d 100644 --- a/openpgp-card/src/card_do/cardholder.rs +++ b/openpgp-card/src/card_do/cardholder.rs @@ -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< 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, + }, + ); + } +} diff --git a/openpgp-card/src/card_do/fingerprint.rs b/openpgp-card/src/card_do/fingerprint.rs index 52a44db..c14b80e 100644 --- a/openpgp-card/src/card_do/fingerprint.rs +++ b/openpgp-card/src/card_do/fingerprint.rs @@ -101,3 +101,50 @@ impl TryFrom<&[u8]> for KeySet { .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 = (&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" + ); + } +} diff --git a/openpgp-card/src/card_do/key_generation_times.rs b/openpgp-card/src/card_do/key_generation_times.rs index ec63784..003d9a9 100644 --- a/openpgp-card/src/card_do/key_generation_times.rs +++ b/openpgp-card/src/card_do/key_generation_times.rs @@ -77,3 +77,25 @@ impl TryFrom<&[u8]> for KeySet { .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 = (&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); + } +} diff --git a/openpgp-card/src/card_do/pw_status.rs b/openpgp-card/src/card_do/pw_status.rs index a6b84e6..898d218 100644 --- a/openpgp-card/src/card_do/pw_status.rs +++ b/openpgp-card/src/card_do/pw_status.rs @@ -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 + } + ); + } +}