Make rng in TestEnv deterministic and seedable (#461)
* Move three dependent customizations into new file * default_min_pin_length(_rp_ids) and max_rp_ids_length * Did some backing store tricks to make the list configurable in TestCustomization. * Add testing for TestCustomization * Change assert comparison to assert_eq * Separate tests * Move 3 pure constants to new file * Return Vec<String> for rp_ids() * Make rng in TestEnv deterministic and seedable * Move seed method to TestRng256 * Change some constant name in comments to snake case * Move seed rng of env to the start * Fix unused warning * Make rng in TestEnv deterministic and seedable * Move seed method to TestRng256 * Move seed rng of env to the start * Fix unused warning * Seed rng in all fuzz targets * Fix error introduced when merging Co-authored-by: Julien Cretin <cretin@google.com>
This commit is contained in:
34
src/env/test/mod.rs
vendored
34
src/env/test/mod.rs
vendored
@@ -4,21 +4,41 @@ use crate::api::firmware_protection::FirmwareProtection;
|
||||
use crate::ctap::status_code::Ctap2StatusCode;
|
||||
use crate::ctap::Channel;
|
||||
use crate::env::{Env, UserPresence};
|
||||
use crypto::rng256::ThreadRng256;
|
||||
use crypto::rng256::Rng256;
|
||||
use customization::TestCustomization;
|
||||
use persistent_store::{BufferOptions, BufferStorage, Store};
|
||||
use rand::rngs::StdRng;
|
||||
use rand::{Rng, SeedableRng};
|
||||
|
||||
mod customization;
|
||||
mod upgrade_storage;
|
||||
|
||||
pub struct TestEnv {
|
||||
rng: ThreadRng256,
|
||||
rng: TestRng256,
|
||||
user_presence: TestUserPresence,
|
||||
store: Store<BufferStorage>,
|
||||
upgrade_storage: Option<BufferUpgradeStorage>,
|
||||
customization: TestCustomization,
|
||||
}
|
||||
|
||||
pub struct TestRng256 {
|
||||
rng: StdRng,
|
||||
}
|
||||
|
||||
impl TestRng256 {
|
||||
pub fn seed_rng_from_u64(&mut self, state: u64) {
|
||||
self.rng = StdRng::seed_from_u64(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl Rng256 for TestRng256 {
|
||||
fn gen_uniform_u8x32(&mut self) -> [u8; 32] {
|
||||
let mut result = [Default::default(); 32];
|
||||
self.rng.fill(&mut result);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TestUserPresence {
|
||||
check: Box<dyn Fn(Channel) -> Result<(), Ctap2StatusCode>>,
|
||||
}
|
||||
@@ -48,7 +68,9 @@ fn new_storage() -> BufferStorage {
|
||||
|
||||
impl TestEnv {
|
||||
pub fn new() -> Self {
|
||||
let rng = ThreadRng256 {};
|
||||
let rng = TestRng256 {
|
||||
rng: StdRng::seed_from_u64(0),
|
||||
};
|
||||
let user_presence = TestUserPresence {
|
||||
check: Box::new(|_| Ok(())),
|
||||
};
|
||||
@@ -72,6 +94,10 @@ impl TestEnv {
|
||||
pub fn customization_mut(&mut self) -> &mut TestCustomization {
|
||||
&mut self.customization
|
||||
}
|
||||
|
||||
pub fn rng(&mut self) -> &mut TestRng256 {
|
||||
&mut self.rng
|
||||
}
|
||||
}
|
||||
|
||||
impl TestUserPresence {
|
||||
@@ -93,7 +119,7 @@ impl FirmwareProtection for TestEnv {
|
||||
}
|
||||
|
||||
impl Env for TestEnv {
|
||||
type Rng = ThreadRng256;
|
||||
type Rng = TestRng256;
|
||||
type UserPresence = TestUserPresence;
|
||||
type Storage = BufferStorage;
|
||||
type UpgradeStorage = BufferUpgradeStorage;
|
||||
|
||||
Reference in New Issue
Block a user