Repository: google/OpenSK
File: src/env/test/mod.rs (Rust)

Code before:
        let storage = new_storage();
        let store = Store::new(storage).ok().unwrap();
        let upgrade_storage = Some(BufferUpgradeStorage::new().unwrap());
        let customization = DEFAULT_CUSTOMIZATION.into();
        TestEnv {
            rng,
            user_presence,
            store,
            upgrade_storage,
            customization,
        }
    }

    pub fn disable_upgrade_storage(&mut self) {
        self.upgrade_storage = None;
    }

    pub fn customization_mut(&mut self) -> &mut TestCustomization {
        &mut self.customization
    }

    pub fn rng(&mut self) -> &mut TestRng256 {
        &mut self.rng
    }

    pub fn set_enterprise_attestation(&mut self) {
        storage::init(self).ok().unwrap();
        let mut key_bytes = [0; 32];
        let private_key = crypto::ecdsa::SecKey::gensk(self.rng());
        private_key.to_bytes(array_mut_ref!(key_bytes, 0, 32));
        storage::set_attestation_certificate(self, &[0xCC]).unwrap();
        storage::set_attestation_private_key(self, &key_bytes).unwrap();
        storage::enable_enterprise_attestation(self).unwrap();
    }
}

impl TestUserPresence {
    pub fn set(&mut self, check: impl Fn(Channel) -> Result<(), Ctap2StatusCode> + 'static) {
        self.check = Box::new(check);
    }
}

impl UserPresence for TestUserPresence {
    fn check(&mut self, channel: Channel) -> Result<(), Ctap2StatusCode> {
        (self.check)(channel)
    }
}

impl FirmwareProtection for TestEnv {
    fn lock(&mut self) -> bool {
        true

Review comment (refactor):
I wonder if this is the right place to add this function. It only affects the storage, not customization, so it's not really something you do to env, but that uses env to affect storage.

We could end up with a lot of these helper functions that take an `&mut Env` for test purposes and do some setup with it. Maybe a helper mod might be appropriate, in anticipation?

Code after:
        let storage = new_storage();
        let store = Store::new(storage).ok().unwrap();
        let upgrade_storage = Some(BufferUpgradeStorage::new().unwrap());
        let customization = DEFAULT_CUSTOMIZATION.into();
        TestEnv {
            rng,
            user_presence,
            store,
            upgrade_storage,
            customization,
        }
    }

    pub fn disable_upgrade_storage(&mut self) {
        self.upgrade_storage = None;
    }

    pub fn customization_mut(&mut self) -> &mut TestCustomization {
        &mut self.customization
    }

    pub fn rng(&mut self) -> &mut TestRng256 {
        &mut self.rng
    }
}

impl TestUserPresence {
    pub fn set(&mut self, check: impl Fn(Channel) -> Result<(), Ctap2StatusCode> + 'static) {
        self.check = Box::new(check);
    }
}

impl UserPresence for TestUserPresence {
    fn check(&mut self, channel: Channel) -> Result<(), Ctap2StatusCode> {
        (self.check)(channel)
    }
}

impl FirmwareProtection for TestEnv {
    fn lock(&mut self) -> bool {
        true
    }
}

impl Env for TestEnv {
    type Rng = TestRng256;
    type UserPresence = TestUserPresence;
    type Storage = BufferStorage;
    type UpgradeStorage = BufferUpgradeStorage;
    type FirmwareProtection = Self;
    type Write = TestWrite;