Skip to content

Commit

Permalink
Allow tests to be run when alloc is enabled without std.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jul 10, 2019
1 parent 85f7503 commit 6b2ea35
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
14 changes: 7 additions & 7 deletions src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use crate::{c, error};
use untrusted;

#[cfg(any(test, feature = "alloc"))]
#[cfg(feature = "alloc")]
use crate::bits;

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -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()) }
Expand All @@ -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];
Expand Down Expand Up @@ -333,13 +333,13 @@ pub fn fold_5_bit_windows<R, I: FnOnce(Window) -> 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")]
Expand Down
53 changes: 35 additions & 18 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -156,13 +156,13 @@ pub fn compile_time_assert_std_error_error<T: 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 {
Expand Down Expand Up @@ -288,6 +288,7 @@ impl TestCase {
}

/// References a test input file.
#[cfg(feature = "alloc")]
#[macro_export]
macro_rules! test_file {
($file_name:expr) => {
Expand All @@ -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,
Expand All @@ -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<F>(test_file: File, mut f: F)
where
F: FnMut(&str, &mut TestCase) -> Result<(), error::Unspecified>,
Expand All @@ -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(&current_section, &mut test_case)
}));

#[cfg(not(feature = "std"))]
let result: Result<_, error::Unspecified> = Ok(f(&current_section, &mut test_case));

let result = match result {
Ok(Ok(())) => {
if !test_case
Expand All @@ -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 {
Expand Down Expand Up @@ -391,8 +404,7 @@ fn from_hex_digit(d: u8) -> Result<u8, String> {
}
}


#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn parse_test_case(
current_section: &mut String,
lines: &mut dyn Iterator<Item = &str>,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pbkdf2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
warnings
)]

use ring::{digest, error, pbkdf2, test, test_file};
use core::num::NonZeroU32;
use ring::{digest, error, pbkdf2, test, test_file};

#[test]
pub fn pbkdf2_tests() {
Expand Down

0 comments on commit 6b2ea35

Please sign in to comment.