//! Fake implementations of some traits for use in testing. usecrate::crypto::Key; usecrate::error::{ServiceError, ServiceResult}; usecrate::identifiers::{AuthenticatorId, SecureUserId}; usecrate::service::{InitParams, SdcpParams, TrustyEnvironment, VerifiedBootState}; usecrate::time::{Clock, Instant}; use std::collections::BTreeMap; use std::sync::Mutex; use std::time::Duration; use zerocopy::{FromBytes, Immutable, IntoBytes};
// The simulated clock and trusty structures in here protect all of their internal simulated state // with mutexes. This is for two reasons: // * these traits are expected to be thread-safe // * we need to be able to manipulate internal state to control the simulation // The mutex provides both the safety and interior mutability.
/// A simulated clock implementation for testing. It provides additional functions to allow the /// test code to control the current time that it will report. pubstruct SimulatedClock {
current_time: Mutex<Instant>,
} impl SimulatedClock { /// Creates a new simulated clock. /// /// Note that this will allocate a new clock globally which cannot be deallocated, so individual /// tests should not create more than one instance of this. Since the clock is conceptually /// intended to be a singleton this should not be a serious limitation in practice. pubfn new() -> &'static Self { Box::leak(Box::new(Self { current_time: Mutex::new(Instant::MIN) }))
}
/// Advances the clock by a specified duration. pubfn advance_time(&self, duration: Duration) { letmut now = self.current_time.lock().unwrap();
*now = *now + duration;
}
}
/// A fake trusty environment. /// /// Instead of a real clock and real files this environment uses a simulated clock and stores the /// key and file contents in memory. pubstruct SimulatedTrusty {
clock: &'static SimulatedClock,
auth_token_key: Mutex<Vec<u8>>,
device_boot_state: Mutex<VerifiedBootState>,
files: Mutex<BTreeMap<String, Vec<u8>>>,
} impl SimulatedTrusty { /// Create a new simulated trusty environment. /// /// The default environement includes a clock and no stored internal contents. Tests that want /// to set up some additional initial state can do so using helpers provided. pubfn new() -> Self { Self {
clock: SimulatedClock::new(),
auth_token_key: Default::default(),
device_boot_state: Default::default(),
files: Default::default(),
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.