diff --git a/aead/src/dev.rs b/aead/src/dev.rs index 2f942d557..a3e41f728 100644 --- a/aead/src/dev.rs +++ b/aead/src/dev.rs @@ -18,8 +18,6 @@ pub struct TestVector { pub plaintext: &'static [u8], /// Ciphertext pub ciphertext: &'static [u8], - /// Whether the test vector should pass (`[1]`) or fail (`[0]`) - pub pass: &'static [u8], } /// Run AEAD test for the provided passing test vector @@ -30,10 +28,8 @@ pub fn pass_test( aad, plaintext, ciphertext, - pass, }: &TestVector, ) -> Result<(), &'static str> { - assert_eq!(pass, &[1]); let nonce = nonce.try_into().expect("wrong nonce size"); let cipher = ::new_from_slice(key).expect("failed to initialize the cipher"); @@ -109,11 +105,9 @@ pub fn fail_test( nonce, aad, ciphertext, - pass, .. }: &TestVector, ) -> Result<(), &'static str> { - assert_eq!(pass, &[0]); let nonce = nonce.try_into().expect("wrong nonce size"); let cipher = ::new_from_slice(key).expect("failed to initialize the cipher"); @@ -131,9 +125,9 @@ pub fn fail_test( } } -/// Define AEAD test +/// Define AEAD test for passing test vectors #[macro_export] -macro_rules! new_test { +macro_rules! new_pass_test { ($name:ident, $test_name:expr, $cipher:ty $(,)?) => { #[test] fn $name() { @@ -142,17 +136,43 @@ macro_rules! new_test { $crate::dev::blobby::parse_into_structs!( include_bytes!(concat!("data/", $test_name, ".blb")); static TEST_VECTORS: &[ - TestVector { key, nonce, aad, plaintext, ciphertext, pass } + TestVector { key, nonce, aad, plaintext, ciphertext } ]; ); for (i, tv) in TEST_VECTORS.iter().enumerate() { - let pass = tv.pass[0] == 1; - let res = if pass { - $crate::dev::pass_test::<$cipher>(tv) - } else { - $crate::dev::fail_test::<$cipher>(tv) - }; + let res = $crate::dev::pass_test::<$cipher>(tv); + + if let Err(reason) = res { + panic!( + "\n\ + Failed test #{i}\n\ + reason:\t{reason:?}\n\ + test vector:\t{tv:?}\n" + ); + } + } + } + }; +} + +/// Define AEAD test for failing test vectors +#[macro_export] +macro_rules! new_fail_test { + ($name:ident, $test_name:expr, $cipher:ty $(,)?) => { + #[test] + fn $name() { + use $crate::dev::TestVector; + + $crate::dev::blobby::parse_into_structs!( + include_bytes!(concat!("data/", $test_name, ".blb")); + static TEST_VECTORS: &[ + TestVector { key, nonce, aad, plaintext, ciphertext } + ]; + ); + + for (i, tv) in TEST_VECTORS.iter().enumerate() { + let res = $crate::dev::fail_test::<$cipher>(tv); if let Err(reason) = res { panic!( diff --git a/aead/tests/data/postfix.blb b/aead/tests/data/postfix.blb deleted file mode 100644 index c3591020d..000000000 Binary files a/aead/tests/data/postfix.blb and /dev/null differ diff --git a/aead/tests/data/postfix_fail.blb b/aead/tests/data/postfix_fail.blb new file mode 100644 index 000000000..ba425f082 Binary files /dev/null and b/aead/tests/data/postfix_fail.blb differ diff --git a/aead/tests/data/postfix_pass.blb b/aead/tests/data/postfix_pass.blb new file mode 100644 index 000000000..cd4dd2b56 Binary files /dev/null and b/aead/tests/data/postfix_pass.blb differ diff --git a/aead/tests/data/prefix.blb b/aead/tests/data/prefix.blb deleted file mode 100644 index f6c09fc76..000000000 Binary files a/aead/tests/data/prefix.blb and /dev/null differ diff --git a/aead/tests/data/prefix_fail.blb b/aead/tests/data/prefix_fail.blb new file mode 100644 index 000000000..8e70a71fc Binary files /dev/null and b/aead/tests/data/prefix_fail.blb differ diff --git a/aead/tests/data/prefix_pass.blb b/aead/tests/data/prefix_pass.blb new file mode 100644 index 000000000..ec1d1aff2 Binary files /dev/null and b/aead/tests/data/prefix_pass.blb differ diff --git a/aead/tests/dummy.rs b/aead/tests/dummy.rs index ef7606f00..a4604d807 100644 --- a/aead/tests/dummy.rs +++ b/aead/tests/dummy.rs @@ -170,5 +170,12 @@ impl AeadInOut for PostfixDummyAead { } } -aead::new_test!(dummy_prefix, "prefix", PrefixDummyAead); -aead::new_test!(dummy_postfix, "postfix", PostfixDummyAead); +#[cfg(feature = "dev")] +mod tests { + use super::{PostfixDummyAead, PrefixDummyAead}; + + aead::new_pass_test!(dummy_prefix_pass, "prefix_pass", PrefixDummyAead); + aead::new_fail_test!(dummy_prefix_fail, "prefix_fail", PrefixDummyAead); + aead::new_pass_test!(dummy_postfix_pass, "postfix_pass", PostfixDummyAead); + aead::new_fail_test!(dummy_postfix_fail, "postfix_fail", PostfixDummyAead); +}