use asn1_der::{ Asn1DerError, Asn1DerErrorVariant::{InOutError, InvalidData, Other, Unsupported}, }; pub trait OptionExt { /// Returns the `Some` variant or pretty prints the error and panics fn assert(self, name: &str) -> T; /// Returns the `Some` variant or pretty prints the error and panics fn assert_index(self, name: &str, i: usize) -> T; } impl OptionExt for Option { fn assert(self, name: &str) -> T { self.unwrap_or_else(|| { eprintln!("Unexpectet `None` result @\"{}\"", name); panic!("Panicked due to fatal error"); }) } fn assert_index(self, name: &str, i: usize) -> T { self.unwrap_or_else(|| { eprintln!("Unexpected `None` result @\"{}\":{}", name, i); panic!("Panicked due to fatal error"); }) } } pub trait ResultExt { /// Returns the `Ok` variant or pretty prints the error and panics fn assert(self, name: &str) -> T; /// Returns the `Ok` variant or pretty prints the error and panics fn assert_index(self, name: &str, i: usize) -> T; /// Ensures that the result is an `Err` of type `variant` fn assert_err(self, variant: &str, name: &str); } impl ResultExt for Result { fn assert(self, name: &str) -> T { self.unwrap_or_else(|e| { eprintln!("Fatal error @\"{}\": {}", name, e); panic!("Panicked due to fatal error"); }) } fn assert_index(self, name: &str, i: usize) -> T { self.unwrap_or_else(|e| { eprintln!("Fatal error @\"{}\":{}: {}", name, i, e); panic!("Panicked due to fatal error"); }) } fn assert_err(self, variant: &str, name: &str) { match self { Err(Asn1DerError { error: InOutError(_), .. }) if variant == "InOutError" => (), Err(Asn1DerError { error: InvalidData(_), .. }) if variant == "InvalidData" => (), Err(Asn1DerError { error: Unsupported(_), .. }) if variant == "Unsupported" => (), Err(Asn1DerError { error: Other(_), .. }) if variant == "Other" => (), Ok(_) => { eprintln!("Unexpected success @\"{}\"; expected {}", name, variant); panic!("Panicked due to unexpected success") } Err(e) => { eprintln!("Unexpected error kind @\"{}\"; got {}\ninstead of {}", name, e, variant); panic!("Panicked due to invalid error kind"); } } } } pub mod test_ok { #[derive(serde::Serialize, serde::Deserialize)] pub struct Length { pub name: String, pub bytes: Vec, pub value: Option, } #[derive(serde::Serialize, serde::Deserialize)] pub struct Object { pub name: String, pub bytes: Vec, pub tag: u8, pub value: Vec, } #[derive(serde::Serialize, serde::Deserialize)] pub struct TypedBool { pub name: String, pub bytes: Vec, pub value: Vec, pub bool: bool, } #[derive(serde::Serialize, serde::Deserialize)] pub struct TypedInteger { pub name: String, pub bytes: Vec, pub value: Vec, pub uint: Option, pub int: Option, } #[derive(serde::Serialize, serde::Deserialize)] pub struct TypedNull { pub name: String, pub bytes: Vec, } #[derive(serde::Serialize, serde::Deserialize)] pub struct TypedOctetString { pub name: String, pub bytes: Vec, pub value: Vec, } #[derive(serde::Serialize, serde::Deserialize)] pub struct TypedSequence { pub name: String, pub bytes: Vec, pub value: Vec, pub sequence: Vec, } #[derive(serde::Serialize, serde::Deserialize)] pub struct TypedUtf8String { pub name: String, pub bytes: Vec, pub value: Vec, pub utf8str: String, } #[derive(serde::Serialize, serde::Deserialize)] pub struct Typed { pub bool: Vec, pub integer: Vec, pub null: Vec, pub octet_string: Vec, pub sequence: Vec, pub utf8_string: Vec, } /// A test vector for valid constructions #[derive(serde::Serialize, serde::Deserialize)] pub struct Test { pub length: Vec, pub object: Vec, pub typed: Typed, } /// Loads the test vectors for valid constructions pub fn load() -> Test { serde_json::from_str(include_str!("../ok.json")).expect("Failed to load test vectors") } } pub mod test_err { #[derive(serde::Serialize, serde::Deserialize)] pub struct Length { pub name: String, pub bytes: Vec, pub err: String, } #[derive(serde::Serialize, serde::Deserialize)] pub struct Object { pub name: String, pub bytes: Vec, err: String, err_32bit: Option, } impl Object { /// Gets the platform dependent error pub fn err(&self) -> &str { match self.err_32bit.as_ref() { #[cfg(target_pointer_width = "32")] Some(err_32bit) => err_32bit, _ => &self.err, } } } #[derive(serde::Serialize, serde::Deserialize)] pub struct TypedAny { pub name: String, pub bytes: Vec, pub err: String, } #[derive(serde::Serialize, serde::Deserialize)] pub struct Typed { pub bool: Vec, pub integer: Vec, pub null: Vec, pub octet_string: Vec, pub sequence: Vec, pub utf8_string: Vec, } /// A test vector for invalid constructions #[derive(serde::Serialize, serde::Deserialize)] pub struct Test { pub length: Vec, pub object: Vec, pub typed: Typed, } /// Loads the test vectors for invalid constructions pub fn load() -> Test { serde_json::from_str(include_str!("../err.json")).expect("Failed to load test vectors") } }