Files
VoelinClient/third_party/dimpl/src/rng.rs
T
Claude d1c4424789 Vendor dimpl 0.7.4 unmodified and patch it in from third_party
str0m's DTLS stack (dimpl) hard-codes the SRTP profile a DTLS server picks
(AES-256-GCM first), with no way to configure it. Official TeamSpeak 6
clients use AES_CM_128_HMAC_SHA1_80, so we need to patch dimpl. This commit
only vendors the crate as published and routes crates.io's dimpl to it;
the change follows separately so it can be reviewed and dropped on its own.

third_party is excluded from the workspace so dimpl's dev-dependencies and
tests stay out of our builds, and own-crates.sh skips it like crates/proto.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-26 23:27:21 +00:00

101 lines
2.7 KiB
Rust

//! Seedable random number generator for deterministic testing.
//!
//! When a seed is provided via [`Config::rng_seed`], all non-cryptographic
//! randomness will be deterministic. This is useful for testing and debugging.
use rand::distr::{Distribution, StandardUniform};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
/// A random number generator that can be seeded for deterministic behavior.
///
/// When created with a seed, it produces deterministic random values.
/// When created without a seed, it uses the thread-local random generator.
pub struct SeededRng {
inner: Option<StdRng>,
}
impl SeededRng {
/// Create a new RNG with an optional seed.
///
/// If `seed` is `Some`, the RNG will produce deterministic values.
/// If `seed` is `None`, it will use the thread-local random generator.
pub fn new(seed: Option<u64>) -> Self {
let inner = seed.map(StdRng::seed_from_u64);
Self { inner }
}
/// Generate a random value of type T.
pub fn random<T>(&mut self) -> T
where
StandardUniform: Distribution<T>,
{
match self.inner.as_mut() {
Some(rng) => rng.random(),
None => rand::random(),
}
}
}
impl std::fmt::Debug for SeededRng {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let is_seeded = self.inner.is_some();
f.debug_struct("SeededRng")
.field("seeded", &is_seeded)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn seeded_rng_is_deterministic() {
let mut rng1 = SeededRng::new(Some(12345));
let mut rng2 = SeededRng::new(Some(12345));
let values1: [u8; 10] = [
rng1.random(),
rng1.random(),
rng1.random(),
rng1.random(),
rng1.random(),
rng1.random(),
rng1.random(),
rng1.random(),
rng1.random(),
rng1.random(),
];
let values2: [u8; 10] = [
rng2.random(),
rng2.random(),
rng2.random(),
rng2.random(),
rng2.random(),
rng2.random(),
rng2.random(),
rng2.random(),
rng2.random(),
rng2.random(),
];
assert_eq!(values1, values2, "Same seed should produce same values");
}
#[test]
fn different_seeds_produce_different_values() {
let mut rng1 = SeededRng::new(Some(12345));
let mut rng2 = SeededRng::new(Some(54321));
let value1: u64 = rng1.random();
let value2: u64 = rng2.random();
assert_ne!(
value1, value2,
"Different seeds should produce different values"
);
}
}