Skip to content

Commit

Permalink
Deprecate ring::test.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Mar 9, 2025
1 parent 52b239c commit 111c1af
Show file tree
Hide file tree
Showing 32 changed files with 231 additions and 139 deletions.
8 changes: 6 additions & 2 deletions cavp/tests/shavs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use wasm_bindgen_test::wasm_bindgen_test_configure;
wasm_bindgen_test_configure!(run_in_browser);

mod digest_shavs {
use ring::{digest, test};
use ring::digest;
#[allow(deprecated)]
use ring::test;

fn run_known_answer_test(digest_alg: &'static digest::Algorithm, test_file: test::File) {
let section_name = &format!("L = {}", digest_alg.output_len());
Expand Down Expand Up @@ -51,7 +53,9 @@ mod digest_shavs {
#[allow(non_snake_case)]
mod $algorithm_name {
use super::run_known_answer_test;
use ring::{digest, test_file};
use ring::digest;
#[allow(deprecated)]
use ring::test_file;

#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
use wasm_bindgen_test::wasm_bindgen_test as test;
Expand Down
4 changes: 2 additions & 2 deletions src/aead/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ fn encrypt_iv_xor_block_using_ctr32(key: &impl EncryptCtr32, iv: Iv, mut block:
#[cfg(test)]
mod tests {
use super::*;
use crate::test;
use crate::testutil as test;

#[test]
pub fn test_aes() {
test::run(test_file!("aes_tests.txt"), |section, test_case| {
test::run(test_vector_file!("aes_tests.txt"), |section, test_case| {
assert_eq!(section, "");
let key = consume_key(test_case, "Key");
let input = test_case.consume_bytes("Input");
Expand Down
68 changes: 36 additions & 32 deletions src/aead/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ mod tests {
extern crate alloc;

use super::{super::overlapping::IndexError, *};
use crate::{error, test};
use crate::error;
use crate::testutil as test;
use alloc::vec;

const MAX_ALIGNMENT_AND_OFFSET: (usize, usize) = (15, 259);
Expand Down Expand Up @@ -252,38 +253,41 @@ mod tests {
// Reuse a buffer to avoid slowing down the tests with allocations.
let mut buf = vec![0u8; 1300];

test::run(test_file!("chacha_tests.txt"), move |section, test_case| {
assert_eq!(section, "");

let key = test_case.consume_bytes("Key");
let key: &[u8; KEY_LEN] = key.as_slice().try_into()?;
let key = Key::new(*key);

let ctr = test_case.consume_usize("Ctr");
let nonce = test_case.consume_bytes("Nonce");
let input = test_case.consume_bytes("Input");
let output = test_case.consume_bytes("Output");

// Run the test case over all prefixes of the input because the
// behavior of ChaCha20 implementation changes dependent on the
// length of the input.
for len in 0..=input.len() {
#[allow(clippy::cast_possible_truncation)]
chacha20_test_case_inner(
&key,
&nonce,
ctr as u32,
&input[..len],
&output[..len],
&mut buf,
max_alignment_and_offset,
cpu,
&f,
);
}
test::run(
test_vector_file!("chacha_tests.txt"),
move |section, test_case| {
assert_eq!(section, "");

let key = test_case.consume_bytes("Key");
let key: &[u8; KEY_LEN] = key.as_slice().try_into()?;
let key = Key::new(*key);

let ctr = test_case.consume_usize("Ctr");
let nonce = test_case.consume_bytes("Nonce");
let input = test_case.consume_bytes("Input");
let output = test_case.consume_bytes("Output");

// Run the test case over all prefixes of the input because the
// behavior of ChaCha20 implementation changes dependent on the
// length of the input.
for len in 0..=input.len() {
#[allow(clippy::cast_possible_truncation)]
chacha20_test_case_inner(
&key,
&nonce,
ctr as u32,
&input[..len],
&output[..len],
&mut buf,
max_alignment_and_offset,
cpu,
&f,
);
}

Ok(())
});
Ok(())
},
);
}

fn chacha20_test_case_inner(
Expand Down
29 changes: 16 additions & 13 deletions src/aead/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,26 @@ pub(super) fn sign(key: Key, input: &[u8], cpu_features: cpu::Features) -> Tag {
#[cfg(test)]
mod tests {
use super::*;
use crate::test;
use crate::testutil as test;

// Adapted from BoringSSL's crypto/poly1305/poly1305_test.cc.
#[test]
pub fn test_poly1305() {
let cpu_features = cpu::features();
test::run(test_file!("poly1305_test.txt"), |section, test_case| {
assert_eq!(section, "");
let key = test_case.consume_bytes("Key");
let key: &[u8; KEY_LEN] = key.as_slice().try_into().unwrap();
let input = test_case.consume_bytes("Input");
let expected_mac = test_case.consume_bytes("MAC");
let key = Key::new(*key);
let Tag(actual_mac) = sign(key, &input, cpu_features);
assert_eq!(expected_mac, actual_mac.as_ref());

Ok(())
})
test::run(
test_vector_file!("poly1305_test.txt"),
|section, test_case| {
assert_eq!(section, "");
let key = test_case.consume_bytes("Key");
let key: &[u8; KEY_LEN] = key.as_slice().try_into().unwrap();
let input = test_case.consume_bytes("Input");
let expected_mac = test_case.consume_bytes("MAC");
let key = Key::new(*key);
let Tag(actual_mac) = sign(key, &input, cpu_features);
assert_eq!(expected_mac, actual_mac.as_ref());

Ok(())
},
)
}
}
13 changes: 7 additions & 6 deletions src/arithmetic/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ fn unwrap_impossible_limb_slice_error(err: LimbSliceError) {
#[cfg(test)]
mod tests {
use super::*;
use crate::{cpu, test};
use crate::cpu;
use crate::testutil as test;

// Type-level representation of an arbitrary modulus.
struct M {}
Expand All @@ -844,7 +845,7 @@ mod tests {
fn test_elem_exp_consttime() {
let cpu_features = cpu::features();
test::run(
test_file!("../../crypto/fipsmodule/bn/test/mod_exp_tests.txt"),
test_vector_file!("../../crypto/fipsmodule/bn/test/mod_exp_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand Down Expand Up @@ -927,7 +928,7 @@ mod tests {
fn test_elem_mul() {
let cpu_features = cpu::features();
test::run(
test_file!("../../crypto/fipsmodule/bn/test/mod_mul_tests.txt"),
test_vector_file!("../../crypto/fipsmodule/bn/test/mod_mul_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand All @@ -952,7 +953,7 @@ mod tests {
fn test_elem_squared() {
let cpu_features = cpu::features();
test::run(
test_file!("bigint_elem_squared_tests.txt"),
test_vector_file!("bigint_elem_squared_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand All @@ -975,7 +976,7 @@ mod tests {
fn test_elem_reduced() {
let cpu_features = cpu::features();
test::run(
test_file!("bigint_elem_reduced_tests.txt"),
test_vector_file!("bigint_elem_reduced_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand All @@ -1002,7 +1003,7 @@ mod tests {
fn test_elem_reduced_once() {
let cpu_features = cpu::features();
test::run(
test_file!("bigint_elem_reduced_once_tests.txt"),
test_vector_file!("bigint_elem_reduced_once_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand Down
45 changes: 45 additions & 0 deletions src/deprecated_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2025 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#![doc(hidden)]

/// References a test input file.
#[macro_export]
macro_rules! test_file {
($file_name:expr) => {
$crate::test::File {
file_name: $file_name,
contents: include_str!($file_name),
}
};
}

pub use crate::testutil::{
compile_time_assert_clone, compile_time_assert_copy, compile_time_assert_eq,
compile_time_assert_send, compile_time_assert_sync, from_hex, run, File, TestCase,
};

#[cfg(feature = "std")]
pub use crate::testutil::compile_time_assert_std_error_error;

#[deprecated(note = "internal API that will be removed")]
#[doc(hidden)]
pub mod rand {
#[deprecated(note = "internal API that will be removed")]
pub type FixedByteRandom = crate::testutil::rand::FixedByteRandom;
#[deprecated(note = "internal API that will be removed")]
pub type FixedSliceRandom<'a> = crate::testutil::rand::FixedSliceRandom<'a>;
#[deprecated(note = "internal API that will be removed")]
pub type FixedSliceSequenceRandom<'a> = crate::testutil::rand::FixedSliceSequenceRandom<'a>;
}
14 changes: 0 additions & 14 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,6 @@ impl Context {
}

/// Returns the digest of `data` using the given digest algorithm.
///
/// # Examples:
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use ring::{digest, test};
/// let expected_hex = "09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b";
/// let expected: Vec<u8> = test::from_hex(expected_hex).unwrap();
/// let actual = digest::digest(&digest::SHA256, b"hello, world");
///
/// assert_eq!(&expected, &actual.as_ref());
/// # }
/// ```
pub fn digest(algorithm: &'static Algorithm, data: &[u8]) -> Digest {
let cpu = cpu::features();
Digest::compute_from(algorithm, data, cpu)
Expand Down
3 changes: 2 additions & 1 deletion src/ec/suite_b/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ fn ecdh(
#[cfg(test)]
mod tests {
use super::super::ops;
use crate::{agreement, ec, limb, test};
use crate::testutil as test;
use crate::{agreement, ec, limb};

static SUPPORTED_SUITE_B_ALGS: [(&str, &agreement::Algorithm, &ec::Curve, &ops::CommonOps); 2] = [
(
Expand Down
5 changes: 3 additions & 2 deletions src/ec/suite_b/ecdsa/digest_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ fn digest_scalar_(n: &Modulus<N>, digest: &[u8]) -> Scalar {
#[cfg(test)]
mod tests {
use super::digest_bytes_scalar;
use crate::{cpu, digest, ec::suite_b::ops::*, limb, test};
use crate::testutil as test;
use crate::{cpu, digest, ec::suite_b::ops::*, limb};

#[test]
fn test() {
let cpu = cpu::features();
test::run(
test_file!("ecdsa_digest_scalar_tests.txt"),
test_vector_file!("ecdsa_digest_scalar_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand Down
7 changes: 4 additions & 3 deletions src/ec/suite_b/ecdsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,15 @@ static EC_PUBLIC_KEY_P384_PKCS8_V1_TEMPLATE: pkcs8::Template = pkcs8::Template {

#[cfg(test)]
mod tests {
use crate::{rand, signature, test};
use crate::testutil as test;
use crate::{rand, signature};

#[test]
fn signature_ecdsa_sign_fixed_test() {
let rng = rand::SystemRandom::new();

test::run(
test_file!("ecdsa_sign_fixed_tests.txt"),
test_vector_file!("ecdsa_sign_fixed_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand Down Expand Up @@ -575,7 +576,7 @@ mod tests {
let rng = rand::SystemRandom::new();

test::run(
test_file!("ecdsa_sign_asn1_tests.txt"),
test_vector_file!("ecdsa_sign_asn1_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand Down
4 changes: 2 additions & 2 deletions src/ec/suite_b/ecdsa/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ pub static ECDSA_P384_SHA384_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificatio
mod tests {
extern crate alloc;
use super::*;
use crate::test;
use crate::testutil as test;
use alloc::{vec, vec::Vec};

#[test]
fn test_digest_based_test_vectors() {
let cpu = cpu::features();
test::run(
test_file!("../../../../crypto/fipsmodule/ecdsa/ecdsa_verify_tests.txt"),
test_vector_file!("../../../../crypto/fipsmodule/ecdsa/ecdsa_verify_tests.txt"),
|section, test_case| {
assert_eq!(section, "");

Expand Down
Loading

0 comments on commit 111c1af

Please sign in to comment.