Skip to content

Commit 0b1abde

Browse files
committed
Deprecate ring::test.
1 parent 52b239c commit 0b1abde

32 files changed

+232
-139
lines changed

cavp/tests/shavs.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use wasm_bindgen_test::wasm_bindgen_test_configure;
2121
wasm_bindgen_test_configure!(run_in_browser);
2222

2323
mod digest_shavs {
24-
use ring::{digest, test};
24+
use ring::digest;
25+
#[allow(deprecated)]
26+
use ring::test;
2527

2628
fn run_known_answer_test(digest_alg: &'static digest::Algorithm, test_file: test::File) {
2729
let section_name = &format!("L = {}", digest_alg.output_len());
@@ -51,7 +53,9 @@ mod digest_shavs {
5153
#[allow(non_snake_case)]
5254
mod $algorithm_name {
5355
use super::run_known_answer_test;
54-
use ring::{digest, test_file};
56+
use ring::digest;
57+
#[allow(deprecated)]
58+
use ring::test_file;
5559

5660
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
5761
use wasm_bindgen_test::wasm_bindgen_test as test;

src/aead/aes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ fn encrypt_iv_xor_block_using_ctr32(key: &impl EncryptCtr32, iv: Iv, mut block:
207207
#[cfg(test)]
208208
mod tests {
209209
use super::*;
210-
use crate::test;
210+
use crate::testutil as test;
211211

212212
#[test]
213213
pub fn test_aes() {
214-
test::run(test_file!("aes_tests.txt"), |section, test_case| {
214+
test::run(test_vector_file!("aes_tests.txt"), |section, test_case| {
215215
assert_eq!(section, "");
216216
let key = consume_key(test_case, "Key");
217217
let input = test_case.consume_bytes("Input");

src/aead/chacha.rs

+36-32
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ mod tests {
201201
extern crate alloc;
202202

203203
use super::{super::overlapping::IndexError, *};
204-
use crate::{error, test};
204+
use crate::error;
205+
use crate::testutil as test;
205206
use alloc::vec;
206207

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

255-
test::run(test_file!("chacha_tests.txt"), move |section, test_case| {
256-
assert_eq!(section, "");
257-
258-
let key = test_case.consume_bytes("Key");
259-
let key: &[u8; KEY_LEN] = key.as_slice().try_into()?;
260-
let key = Key::new(*key);
261-
262-
let ctr = test_case.consume_usize("Ctr");
263-
let nonce = test_case.consume_bytes("Nonce");
264-
let input = test_case.consume_bytes("Input");
265-
let output = test_case.consume_bytes("Output");
266-
267-
// Run the test case over all prefixes of the input because the
268-
// behavior of ChaCha20 implementation changes dependent on the
269-
// length of the input.
270-
for len in 0..=input.len() {
271-
#[allow(clippy::cast_possible_truncation)]
272-
chacha20_test_case_inner(
273-
&key,
274-
&nonce,
275-
ctr as u32,
276-
&input[..len],
277-
&output[..len],
278-
&mut buf,
279-
max_alignment_and_offset,
280-
cpu,
281-
&f,
282-
);
283-
}
256+
test::run(
257+
test_vector_file!("chacha_tests.txt"),
258+
move |section, test_case| {
259+
assert_eq!(section, "");
260+
261+
let key = test_case.consume_bytes("Key");
262+
let key: &[u8; KEY_LEN] = key.as_slice().try_into()?;
263+
let key = Key::new(*key);
264+
265+
let ctr = test_case.consume_usize("Ctr");
266+
let nonce = test_case.consume_bytes("Nonce");
267+
let input = test_case.consume_bytes("Input");
268+
let output = test_case.consume_bytes("Output");
269+
270+
// Run the test case over all prefixes of the input because the
271+
// behavior of ChaCha20 implementation changes dependent on the
272+
// length of the input.
273+
for len in 0..=input.len() {
274+
#[allow(clippy::cast_possible_truncation)]
275+
chacha20_test_case_inner(
276+
&key,
277+
&nonce,
278+
ctr as u32,
279+
&input[..len],
280+
&output[..len],
281+
&mut buf,
282+
max_alignment_and_offset,
283+
cpu,
284+
&f,
285+
);
286+
}
284287

285-
Ok(())
286-
});
288+
Ok(())
289+
},
290+
);
287291
}
288292

289293
fn chacha20_test_case_inner(

src/aead/poly1305.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,26 @@ pub(super) fn sign(key: Key, input: &[u8], cpu_features: cpu::Features) -> Tag {
9292
#[cfg(test)]
9393
mod tests {
9494
use super::*;
95-
use crate::test;
95+
use crate::testutil as test;
9696

9797
// Adapted from BoringSSL's crypto/poly1305/poly1305_test.cc.
9898
#[test]
9999
pub fn test_poly1305() {
100100
let cpu_features = cpu::features();
101-
test::run(test_file!("poly1305_test.txt"), |section, test_case| {
102-
assert_eq!(section, "");
103-
let key = test_case.consume_bytes("Key");
104-
let key: &[u8; KEY_LEN] = key.as_slice().try_into().unwrap();
105-
let input = test_case.consume_bytes("Input");
106-
let expected_mac = test_case.consume_bytes("MAC");
107-
let key = Key::new(*key);
108-
let Tag(actual_mac) = sign(key, &input, cpu_features);
109-
assert_eq!(expected_mac, actual_mac.as_ref());
110-
111-
Ok(())
112-
})
101+
test::run(
102+
test_vector_file!("poly1305_test.txt"),
103+
|section, test_case| {
104+
assert_eq!(section, "");
105+
let key = test_case.consume_bytes("Key");
106+
let key: &[u8; KEY_LEN] = key.as_slice().try_into().unwrap();
107+
let input = test_case.consume_bytes("Input");
108+
let expected_mac = test_case.consume_bytes("MAC");
109+
let key = Key::new(*key);
110+
let Tag(actual_mac) = sign(key, &input, cpu_features);
111+
assert_eq!(expected_mac, actual_mac.as_ref());
112+
113+
Ok(())
114+
},
115+
)
113116
}
114117
}

src/arithmetic/bigint.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@ fn unwrap_impossible_limb_slice_error(err: LimbSliceError) {
833833
#[cfg(test)]
834834
mod tests {
835835
use super::*;
836-
use crate::{cpu, test};
836+
use crate::cpu;
837+
use crate::testutil as test;
837838

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

@@ -927,7 +928,7 @@ mod tests {
927928
fn test_elem_mul() {
928929
let cpu_features = cpu::features();
929930
test::run(
930-
test_file!("../../crypto/fipsmodule/bn/test/mod_mul_tests.txt"),
931+
test_vector_file!("../../crypto/fipsmodule/bn/test/mod_mul_tests.txt"),
931932
|section, test_case| {
932933
assert_eq!(section, "");
933934

@@ -952,7 +953,7 @@ mod tests {
952953
fn test_elem_squared() {
953954
let cpu_features = cpu::features();
954955
test::run(
955-
test_file!("bigint_elem_squared_tests.txt"),
956+
test_vector_file!("bigint_elem_squared_tests.txt"),
956957
|section, test_case| {
957958
assert_eq!(section, "");
958959

@@ -975,7 +976,7 @@ mod tests {
975976
fn test_elem_reduced() {
976977
let cpu_features = cpu::features();
977978
test::run(
978-
test_file!("bigint_elem_reduced_tests.txt"),
979+
test_vector_file!("bigint_elem_reduced_tests.txt"),
979980
|section, test_case| {
980981
assert_eq!(section, "");
981982

@@ -1002,7 +1003,7 @@ mod tests {
10021003
fn test_elem_reduced_once() {
10031004
let cpu_features = cpu::features();
10041005
test::run(
1005-
test_file!("bigint_elem_reduced_once_tests.txt"),
1006+
test_vector_file!("bigint_elem_reduced_once_tests.txt"),
10061007
|section, test_case| {
10071008
assert_eq!(section, "");
10081009

src/deprecated_test.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2025 Brian Smith.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10+
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12+
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13+
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
#![doc(hidden)]
16+
17+
/// References a test input file.
18+
#[macro_export]
19+
macro_rules! test_file {
20+
($file_name:expr) => {
21+
$crate::test::File {
22+
file_name: $file_name,
23+
contents: include_str!($file_name),
24+
}
25+
};
26+
}
27+
28+
pub use crate::testutil::{
29+
compile_time_assert_clone, compile_time_assert_copy, compile_time_assert_eq,
30+
compile_time_assert_send, compile_time_assert_sync, from_hex, run, File, TestCase,
31+
};
32+
33+
#[cfg(feature = "std")]
34+
pub use crate::testutil::compile_time_assert_std_error_error;
35+
36+
#[deprecated(note = "internal API that will be removed")]
37+
#[doc(hidden)]
38+
pub mod rand {
39+
#[deprecated(note = "internal API that will be removed")]
40+
pub type FixedByteRandom = crate::testutil::rand::FixedByteRandom;
41+
#[deprecated(note = "internal API that will be removed")]
42+
pub type FixedSliceRandom<'a> = crate::testutil::rand::FixedSliceRandom<'a>;
43+
#[deprecated(note = "internal API that will be removed")]
44+
pub type FixedSliceSequenceRandom<'a> = crate::testutil::rand::FixedSliceSequenceRandom<'a>;
45+
}

src/digest.rs

-14
Original file line numberDiff line numberDiff line change
@@ -287,20 +287,6 @@ impl Context {
287287
}
288288

289289
/// Returns the digest of `data` using the given digest algorithm.
290-
///
291-
/// # Examples:
292-
///
293-
/// ```
294-
/// # #[cfg(feature = "alloc")]
295-
/// # {
296-
/// use ring::{digest, test};
297-
/// let expected_hex = "09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b";
298-
/// let expected: Vec<u8> = test::from_hex(expected_hex).unwrap();
299-
/// let actual = digest::digest(&digest::SHA256, b"hello, world");
300-
///
301-
/// assert_eq!(&expected, &actual.as_ref());
302-
/// # }
303-
/// ```
304290
pub fn digest(algorithm: &'static Algorithm, data: &[u8]) -> Digest {
305291
let cpu = cpu::features();
306292
Digest::compute_from(algorithm, data, cpu)

src/ec/suite_b/ecdh.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ fn ecdh(
147147
#[cfg(test)]
148148
mod tests {
149149
use super::super::ops;
150-
use crate::{agreement, ec, limb, test};
150+
use crate::testutil as test;
151+
use crate::{agreement, ec, limb};
151152

152153
static SUPPORTED_SUITE_B_ALGS: [(&str, &agreement::Algorithm, &ec::Curve, &ops::CommonOps); 2] = [
153154
(

src/ec/suite_b/ecdsa/digest_scalar.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ fn digest_scalar_(n: &Modulus<N>, digest: &[u8]) -> Scalar {
6868
#[cfg(test)]
6969
mod tests {
7070
use super::digest_bytes_scalar;
71-
use crate::{cpu, digest, ec::suite_b::ops::*, limb, test};
71+
use crate::testutil as test;
72+
use crate::{cpu, digest, ec::suite_b::ops::*, limb};
7273

7374
#[test]
7475
fn test() {
7576
let cpu = cpu::features();
7677
test::run(
77-
test_file!("ecdsa_digest_scalar_tests.txt"),
78+
test_vector_file!("ecdsa_digest_scalar_tests.txt"),
7879
|section, test_case| {
7980
assert_eq!(section, "");
8081

src/ec/suite_b/ecdsa/signing.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,15 @@ static EC_PUBLIC_KEY_P384_PKCS8_V1_TEMPLATE: pkcs8::Template = pkcs8::Template {
526526

527527
#[cfg(test)]
528528
mod tests {
529-
use crate::{rand, signature, test};
529+
use crate::testutil as test;
530+
use crate::{rand, signature};
530531

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

535536
test::run(
536-
test_file!("ecdsa_sign_fixed_tests.txt"),
537+
test_vector_file!("ecdsa_sign_fixed_tests.txt"),
537538
|section, test_case| {
538539
assert_eq!(section, "");
539540

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

577578
test::run(
578-
test_file!("ecdsa_sign_asn1_tests.txt"),
579+
test_vector_file!("ecdsa_sign_asn1_tests.txt"),
579580
|section, test_case| {
580581
assert_eq!(section, "");
581582

src/ec/suite_b/ecdsa/verification.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ pub static ECDSA_P384_SHA384_ASN1: EcdsaVerificationAlgorithm = EcdsaVerificatio
277277
mod tests {
278278
extern crate alloc;
279279
use super::*;
280-
use crate::test;
280+
use crate::testutil as test;
281281
use alloc::{vec, vec::Vec};
282282

283283
#[test]
284284
fn test_digest_based_test_vectors() {
285285
let cpu = cpu::features();
286286
test::run(
287-
test_file!("../../../../crypto/fipsmodule/ecdsa/ecdsa_verify_tests.txt"),
287+
test_vector_file!("../../../../crypto/fipsmodule/ecdsa/ecdsa_verify_tests.txt"),
288288
|section, test_case| {
289289
assert_eq!(section, "");
290290

0 commit comments

Comments
 (0)