Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aead: split new_test! into new_pass_test! and new_fail_test! #1803

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions aead/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,64 @@ pub fn run_fail_test<C: AeadInOut>(
}
}

/// 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() {
use $crate::KeyInit;
use $crate::dev::blobby::Blob6Iterator;
use $crate::dev::blobby::Blob5Iterator;

let data = include_bytes!(concat!("data/", $test_name, ".blb"));
for (i, row) in Blob6Iterator::new(data).unwrap().enumerate() {
let [key, nonce, aad, pt, ct, status] = row.unwrap();
for (i, row) in Blob5Iterator::new(data).unwrap().enumerate() {
let [key, nonce, aad, pt, ct] = row.unwrap();
let key = key.try_into().expect("wrong key size");
let nonce = nonce.try_into().expect("wrong nonce size");
let cipher = <$cipher as KeyInit>::new(key);

let res = match status {
[0] => $crate::dev::run_fail_test(&cipher, nonce, aad, ct),
[1] => $crate::dev::run_pass_test(&cipher, nonce, aad, pt, ct),
_ => panic!("invalid value for pass flag"),
};
let mut pass = status[0] == 1;
let res = $crate::dev::run_pass_test(&cipher, nonce, aad, pt, ct);
if let Err(reason) = res {
panic!(
"\n\
Failed test #{i}\n\
Failed (pass) test #{i}\n\
reason:\t{reason:?}\n\
key:\t{key:?}\n\
nonce:\t{nonce:?}\n\
aad:\t{aad:?}\n\
plaintext:\t{pt:?}\n\
ciphertext:\t{ct:?}\n\
pass:\t{pass}\n"
ciphertext:\t{ct:?}\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::KeyInit;
use $crate::dev::blobby::Blob4Iterator;

let data = include_bytes!(concat!("data/", $test_name, ".blb"));
for (i, row) in Blob4Iterator::new(data).unwrap().enumerate() {
let [key, nonce, aad, ct] = row.unwrap();
let key = key.try_into().expect("wrong key size");
let nonce = nonce.try_into().expect("wrong nonce size");
let cipher = <$cipher as KeyInit>::new(key);
let res = $crate::dev::run_fail_test(&cipher, nonce, aad, ct);
if let Err(reason) = res {
panic!(
"\n\
Failed (fail) test #{i}\n\
reason:\t{reason:?}\n\
key:\t{key:?}\n\
nonce:\t{nonce:?}\n\
aad:\t{aad:?}\n\
ciphertext:\t{ct:?}\n"
);
}
}
Expand Down
Binary file removed aead/tests/data/postfix.blb
Binary file not shown.
Binary file added aead/tests/data/postfix_fail.blb
Binary file not shown.
Binary file added aead/tests/data/postfix_pass.blb
Binary file not shown.
Binary file removed aead/tests/data/prefix.blb
Binary file not shown.
Binary file added aead/tests/data/prefix_fail.blb
Binary file not shown.
Binary file added aead/tests/data/prefix_pass.blb
Binary file not shown.
11 changes: 8 additions & 3 deletions aead/tests/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ impl AeadInOut for PostfixDummyAead {
}

#[cfg(feature = "dev")]
aead::new_test!(dummy_prefix, "prefix", PrefixDummyAead);
#[cfg(feature = "dev")]
aead::new_test!(dummy_postfix, "postfix", PostfixDummyAead);
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);
}