Skip to content

Commit aae99a0

Browse files
authored
aead: split new_test! into new_pass_test! and new_fail_test! (#2019)
1 parent 219861d commit aae99a0

File tree

8 files changed

+44
-17
lines changed

8 files changed

+44
-17
lines changed

aead/src/dev.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ pub struct TestVector {
1818
pub plaintext: &'static [u8],
1919
/// Ciphertext
2020
pub ciphertext: &'static [u8],
21-
/// Whether the test vector should pass (`[1]`) or fail (`[0]`)
22-
pub pass: &'static [u8],
2321
}
2422

2523
/// Run AEAD test for the provided passing test vector
@@ -30,10 +28,8 @@ pub fn pass_test<C: AeadInOut + KeyInit>(
3028
aad,
3129
plaintext,
3230
ciphertext,
33-
pass,
3431
}: &TestVector,
3532
) -> Result<(), &'static str> {
36-
assert_eq!(pass, &[1]);
3733
let nonce = nonce.try_into().expect("wrong nonce size");
3834
let cipher = <C as KeyInit>::new_from_slice(key).expect("failed to initialize the cipher");
3935

@@ -109,11 +105,9 @@ pub fn fail_test<C: AeadInOut + KeyInit>(
109105
nonce,
110106
aad,
111107
ciphertext,
112-
pass,
113108
..
114109
}: &TestVector,
115110
) -> Result<(), &'static str> {
116-
assert_eq!(pass, &[0]);
117111
let nonce = nonce.try_into().expect("wrong nonce size");
118112
let cipher = <C as KeyInit>::new_from_slice(key).expect("failed to initialize the cipher");
119113

@@ -131,9 +125,9 @@ pub fn fail_test<C: AeadInOut + KeyInit>(
131125
}
132126
}
133127

134-
/// Define AEAD test
128+
/// Define AEAD test for passing test vectors
135129
#[macro_export]
136-
macro_rules! new_test {
130+
macro_rules! new_pass_test {
137131
($name:ident, $test_name:expr, $cipher:ty $(,)?) => {
138132
#[test]
139133
fn $name() {
@@ -142,17 +136,43 @@ macro_rules! new_test {
142136
$crate::dev::blobby::parse_into_structs!(
143137
include_bytes!(concat!("data/", $test_name, ".blb"));
144138
static TEST_VECTORS: &[
145-
TestVector { key, nonce, aad, plaintext, ciphertext, pass }
139+
TestVector { key, nonce, aad, plaintext, ciphertext }
146140
];
147141
);
148142

149143
for (i, tv) in TEST_VECTORS.iter().enumerate() {
150-
let pass = tv.pass[0] == 1;
151-
let res = if pass {
152-
$crate::dev::pass_test::<$cipher>(tv)
153-
} else {
154-
$crate::dev::fail_test::<$cipher>(tv)
155-
};
144+
let res = $crate::dev::pass_test::<$cipher>(tv);
145+
146+
if let Err(reason) = res {
147+
panic!(
148+
"\n\
149+
Failed test #{i}\n\
150+
reason:\t{reason:?}\n\
151+
test vector:\t{tv:?}\n"
152+
);
153+
}
154+
}
155+
}
156+
};
157+
}
158+
159+
/// Define AEAD test for failing test vectors
160+
#[macro_export]
161+
macro_rules! new_fail_test {
162+
($name:ident, $test_name:expr, $cipher:ty $(,)?) => {
163+
#[test]
164+
fn $name() {
165+
use $crate::dev::TestVector;
166+
167+
$crate::dev::blobby::parse_into_structs!(
168+
include_bytes!(concat!("data/", $test_name, ".blb"));
169+
static TEST_VECTORS: &[
170+
TestVector { key, nonce, aad, plaintext, ciphertext }
171+
];
172+
);
173+
174+
for (i, tv) in TEST_VECTORS.iter().enumerate() {
175+
let res = $crate::dev::fail_test::<$cipher>(tv);
156176

157177
if let Err(reason) = res {
158178
panic!(

aead/tests/data/postfix.blb

-209 Bytes
Binary file not shown.

aead/tests/data/postfix_fail.blb

120 Bytes
Binary file not shown.

aead/tests/data/postfix_pass.blb

120 Bytes
Binary file not shown.

aead/tests/data/prefix.blb

-209 Bytes
Binary file not shown.

aead/tests/data/prefix_fail.blb

120 Bytes
Binary file not shown.

aead/tests/data/prefix_pass.blb

120 Bytes
Binary file not shown.

aead/tests/dummy.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,12 @@ impl AeadInOut for PostfixDummyAead {
170170
}
171171
}
172172

173-
aead::new_test!(dummy_prefix, "prefix", PrefixDummyAead);
174-
aead::new_test!(dummy_postfix, "postfix", PostfixDummyAead);
173+
#[cfg(feature = "dev")]
174+
mod tests {
175+
use super::{PostfixDummyAead, PrefixDummyAead};
176+
177+
aead::new_pass_test!(dummy_prefix_pass, "prefix_pass", PrefixDummyAead);
178+
aead::new_fail_test!(dummy_prefix_fail, "prefix_fail", PrefixDummyAead);
179+
aead::new_pass_test!(dummy_postfix_pass, "postfix_pass", PostfixDummyAead);
180+
aead::new_fail_test!(dummy_postfix_fail, "postfix_fail", PostfixDummyAead);
181+
}

0 commit comments

Comments
 (0)