use crate::Result;
use crate::storage::DynStore;
use tor_dircommon::{
authority::AuthorityContacts,
config::{DirTolerance, DownloadScheduleConfig, NetworkConfig},
};
use tor_netdoc::doc::netstatus::{self};
use std::path::PathBuf;
#[derive(Debug, Clone)]
#[cfg_attr(test, derive(Default))]
#[allow(clippy::exhaustive_structs)]
pub struct DirMgrConfig {
pub cache_dir: PathBuf,
pub cache_trust: fs_mistrust::Mistrust,
pub network: NetworkConfig,
pub schedule: DownloadScheduleConfig,
pub tolerance: DirTolerance,
pub override_net_params: netstatus::NetParams<i32>,
pub extensions: DirMgrExtensions,
}
impl DirMgrConfig {
pub(crate) fn open_store(&self, readonly: bool) -> Result<DynStore> {
Ok(Box::new(
crate::storage::SqliteStore::from_path_and_mistrust(
&self.cache_dir,
&self.cache_trust,
readonly,
)?,
))
}
pub fn authorities(&self) -> &AuthorityContacts {
self.network.authorities()
}
pub fn fallbacks(&self) -> &tor_dircommon::fallback::FallbackList {
self.network.fallback_caches()
}
pub(crate) fn update_from_config(&self, new_config: &DirMgrConfig) -> DirMgrConfig {
DirMgrConfig {
cache_dir: self.cache_dir.clone(),
cache_trust: self.cache_trust.clone(),
network: new_config.network.clone(),
schedule: new_config.schedule.clone(),
tolerance: new_config.tolerance.clone(),
override_net_params: new_config.override_net_params.clone(),
extensions: new_config.extensions.clone(),
}
}
#[cfg(feature = "experimental-api")]
pub fn update_config(&self, new_config: &DirMgrConfig) -> DirMgrConfig {
self.update_from_config(new_config)
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct DirMgrExtensions {
#[cfg(feature = "dirfilter")]
pub filter: crate::filter::FilterConfig,
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_time_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
#![allow(clippy::unnecessary_wraps)]
use super::*;
use tempfile::tempdir;
#[test]
fn simplest_config() -> Result<()> {
let tmp = tempdir().unwrap();
let dir = DirMgrConfig {
cache_dir: tmp.path().into(),
..Default::default()
};
assert!(dir.authorities().v3idents().len() >= 3);
assert!(dir.fallbacks().len() >= 3);
Ok(())
}
#[test]
fn build_dirmgrcfg() -> Result<()> {
let mut bld = DirMgrConfig::default();
let tmp = tempdir().unwrap();
bld.override_net_params.set("circwindow".into(), 999);
bld.cache_dir = tmp.path().into();
assert_eq!(bld.override_net_params.get("circwindow").unwrap(), &999);
Ok(())
}
}