This commit is contained in:
Mateusz Gruszczyński committed 2026-10-17 11:57:16 +02:00
1 parent 6b937aaf85
commit 87c8393d86
79 files changed
+6727 -297

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
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 OS entropy"
);
}
}
#[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);
}
}
}
}