#![deny(unsafe_code)]
#![deny(missing_docs)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::panic)]
use rustls::client::ClientSessionStore;
use std::sync::Arc;
#[derive(Debug)]
pub struct ConfigurableSessionStore {
max_sessions: usize,
inner: Arc<dyn ClientSessionStore>,
}
impl ConfigurableSessionStore {
#[must_use]
pub fn new(max_sessions: usize) -> Self {
Self {
max_sessions,
inner: Arc::new(rustls::client::ClientSessionMemoryCache::new(max_sessions)),
}
}
#[must_use]
pub fn capacity(&self) -> usize {
self.max_sessions
}
#[must_use]
pub fn as_store(&self) -> Arc<dyn ClientSessionStore> {
self.inner.clone()
}
}
#[derive(Debug)]
pub struct PersistentSessionStore {
path: std::path::PathBuf,
max_sessions: usize,
inner: Arc<dyn ClientSessionStore>,
}
impl PersistentSessionStore {
#[must_use]
pub fn new(path: impl Into<std::path::PathBuf>, max_sessions: usize) -> Self {
Self {
path: path.into(),
max_sessions,
inner: Arc::new(rustls::client::ClientSessionMemoryCache::new(max_sessions)),
}
}
#[must_use]
pub fn capacity(&self) -> usize {
self.max_sessions
}
#[must_use]
pub fn path(&self) -> &std::path::Path {
&self.path
}
#[must_use]
pub fn as_store(&self) -> Arc<dyn ClientSessionStore> {
self.inner.clone()
}
#[must_use]
pub const fn is_persistence_enabled(&self) -> bool {
false
}
}
#[must_use]
pub fn create_session_store(
persistence: Option<&crate::tls::SessionPersistenceConfig>,
) -> Arc<dyn ClientSessionStore> {
if let Some(config) = persistence {
Arc::new(rustls::client::ClientSessionMemoryCache::new(config.max_sessions))
} else {
Arc::new(rustls::client::ClientSessionMemoryCache::new(32))
}
}
#[must_use]
pub fn create_resumption_config(
persistence: Option<&crate::tls::SessionPersistenceConfig>,
) -> rustls::client::Resumption {
let store = create_session_store(persistence);
rustls::client::Resumption::store(store)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_configurable_store_creation_succeeds() {
let store = ConfigurableSessionStore::new(100);
assert_eq!(store.capacity(), 100);
}
#[test]
fn test_persistent_store_creation_succeeds() {
let store = PersistentSessionStore::new(std::env::temp_dir().join("test.bin"), 50);
assert_eq!(store.capacity(), 50);
assert!(!store.is_persistence_enabled());
}
#[test]
fn test_create_session_store_none_succeeds() {
let store = create_session_store(None);
assert_eq!(Arc::strong_count(&store), 1);
}
#[test]
fn test_create_session_store_with_config_succeeds() {
let config =
crate::tls::SessionPersistenceConfig::new(std::env::temp_dir().join("test.bin"), 200);
let store = create_session_store(Some(&config));
assert_eq!(Arc::strong_count(&store), 1);
}
#[test]
fn test_create_resumption_config_succeeds() {
let resumption = create_resumption_config(None);
let _ = resumption;
}
}