68 lines
2.3 KiB
Rust
68 lines
2.3 KiB
Rust
mod request_id_tests {
|
|
use super::*;
|
|
use sha2::{Digest, Sha256};
|
|
|
|
#[test]
|
|
fn hexadecimal_encoding_matches_previous_sha256_display() {
|
|
for byte in 0u8..=255 {
|
|
let digest = Sha256::digest([byte; 32]);
|
|
assert_eq!(request_id_hex(&digest), format!("{digest:x}"));
|
|
}
|
|
assert_eq!(
|
|
request_id_hex(&[0, 1, 15, 16, 127, 128, 254, 255]),
|
|
"00010f107f80feff"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn request_ids_keep_their_64_hexadecimal_format_and_randomness() {
|
|
let mut seen = std::collections::HashSet::new();
|
|
for _ in 0..512 {
|
|
let id = request_id_hash(1);
|
|
assert_eq!(id.len(), 64);
|
|
assert!(id
|
|
.bytes()
|
|
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)));
|
|
assert!(
|
|
seen.insert(id),
|
|
"fixed sequence must still use independent CSPRNG output"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn independent_sequential_ids_keep_full_length_and_unique_values() {
|
|
let ids: std::collections::HashSet<_> = (0..4096u64)
|
|
.map(request_id_hash)
|
|
.collect();
|
|
assert_eq!(ids.len(), 4096);
|
|
assert!(ids.iter().all(|id| id.len() == 64));
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "explicit release-mode microbenchmark; not an acceptance threshold"]
|
|
fn benchmark_request_id_formatting() {
|
|
use std::hint::black_box;
|
|
const ITERATIONS: u32 = 200_000;
|
|
let digest = Sha256::digest(b"same SHA256 digest for both formatters");
|
|
for round in 0..7 {
|
|
for candidate in if round % 2 == 0 {
|
|
[false, true]
|
|
} else {
|
|
[true, false]
|
|
} {
|
|
let start = Instant::now();
|
|
for _ in 0..ITERATIONS {
|
|
let value = if candidate {
|
|
request_id_hex(black_box(&digest))
|
|
} else {
|
|
format!("{:x}", black_box(&digest))
|
|
};
|
|
black_box(value);
|
|
}
|
|
println!("{{\"round\":{round},\"candidate\":{candidate},\"iterations\":{ITERATIONS},\"ns_per_op\":{}}}", start.elapsed().as_nanos() as f64 / ITERATIONS as f64);
|
|
}
|
|
}
|
|
}
|
|
}
|