diff --git a/Cargo.toml b/Cargo.toml index f3151a1979..62c90b88e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -331,7 +331,7 @@ dev_urandom_fallback = ["std", "lazy_static"] internal_benches = [] slow_tests = [] std = ["alloc"] -test_logging = [] +test_logging = ["std"] # XXX: debug = false because of https://github.com/rust-lang/rust/issues/34122 diff --git a/src/lib.rs b/src/lib.rs index de15e21d4c..d7abd241d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,10 +65,10 @@ #![no_std] #![cfg_attr(feature = "internal_benches", allow(unstable_features), feature(test))] -#[cfg(any(test, feature = "alloc"))] +#[cfg(feature = "alloc")] extern crate alloc; -#[cfg(any(test, feature = "std"))] +#[cfg(feature = "std")] extern crate std; #[macro_use] diff --git a/src/limb.rs b/src/limb.rs index 1d65b46f9d..65fdae5ca7 100644 --- a/src/limb.rs +++ b/src/limb.rs @@ -21,7 +21,7 @@ use crate::{c, error}; use untrusted; -#[cfg(any(test, feature = "alloc"))] +#[cfg(feature = "alloc")] use crate::bits; #[cfg(feature = "alloc")] @@ -87,13 +87,13 @@ pub fn limbs_are_zero_constant_time(limbs: &[Limb]) -> LimbMask { unsafe { LIMBS_are_zero(limbs.as_ptr(), limbs.len()) } } -#[cfg(any(test, feature = "alloc"))] +#[cfg(feature = "alloc")] #[inline] pub fn limbs_are_even_constant_time(limbs: &[Limb]) -> LimbMask { unsafe { LIMBS_are_even(limbs.as_ptr(), limbs.len()) } } -#[cfg(any(test, feature = "alloc"))] +#[cfg(feature = "alloc")] #[inline] pub fn limbs_equal_limb_constant_time(a: &[Limb], b: Limb) -> LimbMask { unsafe { LIMBS_equal_limb(a.as_ptr(), b, a.len()) } @@ -106,7 +106,7 @@ pub fn limbs_equal_limb_constant_time(a: &[Limb], b: Limb) -> LimbMask { // with respect to `a.len()` or the value of the result or the value of the // most significant bit (It's 1, unless the input is zero, in which case it's // zero.) -#[cfg(any(test, feature = "alloc"))] +#[cfg(feature = "alloc")] pub fn limbs_minimal_bits(a: &[Limb]) -> bits::BitLength { for num_limbs in (1..=a.len()).rev() { let high_limb = a[num_limbs - 1]; @@ -333,13 +333,13 @@ pub fn fold_5_bit_windows R, F: Fn(R, Window) -> R>( } extern "C" { - #[cfg(any(test, feature = "alloc"))] + #[cfg(feature = "alloc")] fn LIMB_shr(a: Limb, shift: c::size_t) -> Limb; - #[cfg(any(test, feature = "alloc"))] + #[cfg(feature = "alloc")] fn LIMBS_are_even(a: *const Limb, num_limbs: c::size_t) -> LimbMask; fn LIMBS_are_zero(a: *const Limb, num_limbs: c::size_t) -> LimbMask; - #[cfg(any(test, feature = "alloc"))] + #[cfg(feature = "alloc")] fn LIMBS_equal_limb(a: *const Limb, b: Limb, num_limbs: c::size_t) -> LimbMask; fn LIMBS_less_than(a: *const Limb, b: *const Limb, num_limbs: c::size_t) -> LimbMask; #[cfg(feature = "alloc")] diff --git a/src/test.rs b/src/test.rs index 4c02ca9fac..330b698ef7 100644 --- a/src/test.rs +++ b/src/test.rs @@ -120,7 +120,7 @@ #[cfg(feature = "alloc")] use alloc::{format, string::String, vec::Vec}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] use crate::{bits, digest, error}; #[cfg(feature = "std")] @@ -156,13 +156,13 @@ pub fn compile_time_assert_std_error_error() {} /// typos and omissions. /// /// Requires the `std` default feature to be enabled. -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] #[derive(Debug)] pub struct TestCase { attributes: Vec<(String, String, bool)>, } -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] impl TestCase { /// Maps the string "true" to true and the string "false" to false. pub fn consume_bool(&mut self, key: &str) -> bool { @@ -288,6 +288,7 @@ impl TestCase { } /// References a test input file. +#[cfg(feature = "alloc")] #[macro_export] macro_rules! test_file { ($file_name:expr) => { @@ -299,6 +300,7 @@ macro_rules! test_file { } /// A test input file. +#[cfg(feature = "alloc")] pub struct File<'a> { /// The name (path) of the file. pub file_name: &'a str, @@ -312,7 +314,7 @@ pub struct File<'a> { /// failure either by returning `Err()` or by panicking. /// /// Requires the `std` default feature to be enabled -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub fn run(test_file: File, mut f: F) where F: FnMut(&str, &mut TestCase) -> Result<(), error::Unspecified>, @@ -323,9 +325,14 @@ where let mut failed = false; while let Some(mut test_case) = parse_test_case(&mut current_section, lines) { + #[cfg(feature = "std")] let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { f(¤t_section, &mut test_case) })); + + #[cfg(not(feature = "std"))] + let result: Result<_, error::Unspecified> = Ok(f(¤t_section, &mut test_case)); + let result = match result { Ok(Ok(())) => { if !test_case @@ -343,15 +350,21 @@ where Err(_) => Err("Test panicked."), }; - if let Err(msg) = result { + if result.is_err() { failed = true; + } - println!("{}: {}", test_file.file_name, msg); - for (name, value, consumed) in test_case.attributes { - let consumed_str = if consumed { "" } else { " (unconsumed)" }; - println!("{}{} = {}", name, consumed_str, value); - } - }; + #[cfg(feature = "std")] + { + if let Err(msg) = result { + println!("{}: {}", test_file.file_name, msg); + + for (name, value, consumed) in test_case.attributes { + let consumed_str = if consumed { "" } else { " (unconsumed)" }; + println!("{}{} = {}", name, consumed_str, value); + } + }; + } } if failed { @@ -391,8 +404,7 @@ fn from_hex_digit(d: u8) -> Result { } } - -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] fn parse_test_case( current_section: &mut String, lines: &mut dyn Iterator, @@ -403,7 +415,8 @@ fn parse_test_case( loop { let line = lines.next(); - if cfg!(feature = "test_logging") { + #[cfg(feature = "test_logging")] + { if let Some(text) = &line { println!("Line: {}", text); } @@ -561,7 +574,8 @@ mod tests { } #[test] - #[should_panic(expected = "Test failed.")] + #[cfg_attr(feature = "std", should_panic(expected = "Test failed."))] + #[cfg_attr(not(feature = "std"), should_panic)] fn one_panics() { test::run(test_file!("test_1_tests.txt"), |_, test_case| { let _ = test_case.consume_string("Key"); @@ -602,19 +616,22 @@ mod tests { } #[test] - #[should_panic(expected = "Test failed.")] + #[cfg_attr(feature = "std", should_panic(expected = "Test failed."))] + #[cfg_attr(not(feature = "std"), should_panic)] fn first_panic() { panic_one(0) } #[test] - #[should_panic(expected = "Test failed.")] + #[cfg_attr(feature = "std", should_panic(expected = "Test failed."))] + #[cfg_attr(not(feature = "std"), should_panic)] fn middle_panic() { panic_one(1) } #[test] - #[should_panic(expected = "Test failed.")] + #[cfg_attr(feature = "std", should_panic(expected = "Test failed."))] + #[cfg_attr(not(feature = "std"), should_panic)] fn last_panic() { panic_one(2) }