Skip to main content

tor_dirmgr/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![allow(clippy::cognitive_complexity)] // See arti#2556
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![warn(clippy::or_fun_call)]
28#![deny(clippy::print_stderr)]
29#![deny(clippy::print_stdout)]
30#![warn(clippy::rc_buffer)]
31#![deny(clippy::ref_option_ref)]
32#![warn(clippy::semicolon_if_nothing_returned)]
33#![warn(clippy::trait_duplication_in_bounds)]
34#![deny(clippy::unchecked_time_subtraction)]
35#![deny(clippy::unnecessary_wraps)]
36#![warn(clippy::unseparated_literal_suffix)]
37#![deny(clippy::unwrap_used)]
38#![deny(clippy::mod_module_files)]
39#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
40#![allow(clippy::uninlined_format_args)]
41#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
42#![allow(clippy::result_large_err)] // temporary workaround for arti#587
43#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
44#![allow(clippy::needless_lifetimes)] // See arti#1765
45#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
46#![allow(clippy::collapsible_if)] // See arti#2342
47#![deny(clippy::unused_async)]
48#![deny(clippy::string_slice)] // See arti#2571
49#![allow(recursion_depth_exceeding_limit)] // arti#2715, rust/issues/159228
50//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
51
52// This clippy lint produces a false positive on `use strum`, below.
53// Attempting to apply the lint to just the use statement fails to suppress
54// this lint and instead produces another lint about a useless clippy attribute.
55#![allow(clippy::single_component_path_imports)]
56
57mod bootstrap;
58pub mod config;
59mod docid;
60mod docmeta;
61mod err;
62mod event;
63mod shared_ref;
64mod state;
65mod storage;
66
67#[cfg(feature = "dir-plugin")]
68mod as_plugin;
69#[cfg(feature = "bridge-client")]
70pub mod bridgedesc;
71#[cfg(feature = "dirfilter")]
72pub mod filter;
73
74use crate::docid::{CacheUsage, ClientRequest, DocQuery};
75use crate::err::BootstrapAction;
76#[cfg(not(feature = "experimental-api"))]
77use crate::shared_ref::SharedMutArc;
78#[cfg(feature = "experimental-api")]
79pub use crate::shared_ref::SharedMutArc;
80use crate::storage::{DynStore, Store};
81use bootstrap::AttemptId;
82use event::DirProgress;
83use postage::watch;
84use scopeguard::ScopeGuard;
85use tor_circmgr::CircMgr;
86use tor_dirclient::SourceInfo;
87use tor_dircommon::config::DirTolerance;
88use tor_error::{info_report, into_internal, warn_report};
89use tor_netdir::params::NetParameters;
90use tor_netdir::{DirEvent, MdReceiver, NetDir, NetDirProvider};
91
92use async_trait::async_trait;
93use futures::stream::BoxStream;
94use oneshot_fused_workaround as oneshot;
95use tor_netdoc::doc::netstatus::ProtoStatuses;
96use tor_rtcompat::scheduler::{TaskHandle, TaskSchedule};
97use tor_rtcompat::{Runtime, SpawnExt};
98use tracing::{debug, info, instrument, trace, warn};
99use web_time_compat::SystemTimeExt;
100
101use std::marker::PhantomData;
102use std::sync::atomic::{AtomicBool, Ordering};
103use std::sync::{Arc, Mutex};
104use std::time::Duration;
105use std::{collections::HashMap, sync::Weak};
106use std::{fmt::Debug, time::SystemTime};
107
108use crate::state::{DirState, NetDirChange};
109pub use config::DirMgrConfig;
110pub use docid::DocId;
111pub use err::Error;
112pub use event::{DirBlockage, DirBootstrapEvents, DirBootstrapStatus};
113pub use storage::DocumentText;
114pub use tor_dircommon::fallback::{FallbackDir, FallbackDirBuilder};
115pub use tor_netdir::Timeliness;
116
117#[cfg(feature = "dir-plugin")]
118pub use as_plugin::DirPlugin;
119
120/// Re-export of `strum` crate for use by an internal macro
121use strum;
122
123/// A Result as returned by this crate.
124pub type Result<T> = std::result::Result<T, Error>;
125
126/// Storage manager used by [`DirMgr`] and
127/// [`BridgeDescMgr`](bridgedesc::BridgeDescMgr)
128///
129/// Internally, this wraps up a sqlite database.
130///
131/// This is a handle, which is cheap to clone; clones share state.
132#[derive(Clone)]
133pub struct DirMgrStore<R: Runtime> {
134    /// The actual store
135    pub(crate) store: Arc<Mutex<crate::DynStore>>,
136
137    /// Be parameterized by Runtime even though we don't use it right now
138    pub(crate) runtime: PhantomData<R>,
139}
140
141impl<R: Runtime> DirMgrStore<R> {
142    /// Open the storage, according to the specified configuration
143    pub fn new(config: &DirMgrConfig, runtime: R, offline: bool) -> Result<Self> {
144        let store = Arc::new(Mutex::new(config.open_store(offline)?));
145        drop(runtime);
146        let runtime = PhantomData;
147        Ok(DirMgrStore { store, runtime })
148    }
149}
150
151/// Trait for DirMgr implementations
152#[async_trait]
153pub trait DirProvider: NetDirProvider {
154    /// Try to change our configuration to `new_config`.
155    ///
156    /// Actual behavior will depend on the value of `how`.
157    fn reconfigure(
158        &self,
159        new_config: &DirMgrConfig,
160        how: tor_config::Reconfigure,
161    ) -> std::result::Result<(), tor_config::ReconfigureError>;
162
163    /// Bootstrap a `DirProvider` that hasn't been bootstrapped yet.
164    async fn bootstrap(&self) -> Result<()>;
165
166    /// Return a stream of [`DirBootstrapStatus`] events to tell us about changes
167    /// in the latest directory's bootstrap status.
168    ///
169    /// Note that this stream can be lossy: the caller will not necessarily
170    /// observe every event on the stream
171    fn bootstrap_events(&self) -> BoxStream<'static, DirBootstrapStatus>;
172
173    /// Return a [`TaskHandle`] that can be used to manage the download process.
174    fn download_task_handle(&self) -> Option<TaskHandle> {
175        None
176    }
177}
178
179// NOTE(eta): We can't implement this for Arc<DirMgr<R>> due to trait coherence rules, so instead
180//            there's a blanket impl for Arc<T> in tor-netdir.
181impl<R: Runtime> NetDirProvider for DirMgr<R> {
182    fn netdir(&self, timeliness: Timeliness) -> tor_netdir::Result<Arc<NetDir>> {
183        use tor_netdir::Error as NetDirError;
184        let netdir = self.netdir.get().ok_or(NetDirError::NoInfo)?;
185        let lifetime = match timeliness {
186            Timeliness::Strict => netdir.lifetime().clone(),
187            Timeliness::Timely => self
188                .config
189                .get()
190                .tolerance
191                .extend_lifetime(netdir.lifetime()),
192            Timeliness::Unchecked => return Ok(netdir),
193        };
194        // TODO #2384 -- we have a runtime here; we should use it.
195        let now = SystemTime::get();
196        if lifetime.valid_after() > now {
197            Err(NetDirError::DirNotYetValid)
198        } else if lifetime.valid_until() < now {
199            Err(NetDirError::DirExpired)
200        } else {
201            Ok(netdir)
202        }
203    }
204
205    fn events(&self) -> BoxStream<'static, DirEvent> {
206        Box::pin(self.events.subscribe())
207    }
208
209    fn params(&self) -> Arc<dyn AsRef<tor_netdir::params::NetParameters>> {
210        if let Some(netdir) = self.netdir.get() {
211            // We have a directory, so we'd like to give it out for its
212            // parameters.
213            //
214            // We do this even if the directory is expired, since parameters
215            // don't really expire on any plausible timescale.
216            netdir
217        } else {
218            // We have no directory, so we'll give out the default parameters as
219            // modified by the provided override_net_params configuration.
220            //
221            self.default_parameters
222                .lock()
223                .expect("Poisoned lock")
224                .clone()
225        }
226        // TODO(nickm): If we felt extremely clever, we could add a third case
227        // where, if we have a pending directory with a validated consensus, we
228        // give out that consensus's network parameters even if we _don't_ yet
229        // have a full directory.  That's significant refactoring, though, for
230        // an unclear amount of benefit.
231    }
232
233    fn protocol_statuses(&self) -> Option<(SystemTime, Arc<ProtoStatuses>)> {
234        self.protocols.lock().expect("Poisoned lock").clone()
235    }
236}
237
238#[async_trait]
239impl<R: Runtime> DirProvider for Arc<DirMgr<R>> {
240    fn reconfigure(
241        &self,
242        new_config: &DirMgrConfig,
243        how: tor_config::Reconfigure,
244    ) -> std::result::Result<(), tor_config::ReconfigureError> {
245        DirMgr::reconfigure(self, new_config, how)
246    }
247
248    #[instrument(level = "trace", skip_all)]
249    async fn bootstrap(&self) -> Result<()> {
250        DirMgr::bootstrap(self).await
251    }
252
253    fn bootstrap_events(&self) -> BoxStream<'static, DirBootstrapStatus> {
254        Box::pin(DirMgr::bootstrap_events(self))
255    }
256
257    fn download_task_handle(&self) -> Option<TaskHandle> {
258        Some(self.task_handle.clone())
259    }
260}
261
262/// A directory manager to download, fetch, and cache a Tor directory.
263///
264/// A DirMgr can operate in three modes:
265///   * In **offline** mode, it only reads from the cache, and can
266///     only read once.
267///   * In **read-only** mode, it reads from the cache, but checks
268///     whether it can acquire an associated lock file.  If it can, then
269///     it enters read-write mode.  If not, it checks the cache
270///     periodically for new information.
271///   * In **read-write** mode, it knows that no other process will be
272///     writing to the cache, and it takes responsibility for fetching
273///     data from the network and updating the directory with new
274///     directory information.
275pub struct DirMgr<R: Runtime> {
276    /// Configuration information: where to find directories, how to
277    /// validate them, and so on.
278    config: tor_config::MutCfg<DirMgrConfig>,
279    /// Handle to our sqlite cache.
280    // TODO(nickm): I'd like to use an rwlock, but that's not feasible, since
281    // rusqlite::Connection isn't Sync.
282    // TODO is needed?
283    store: Arc<Mutex<DynStore>>,
284    /// Our latest sufficiently bootstrapped directory, if we have one.
285    ///
286    /// We use the RwLock so that we can give this out to a bunch of other
287    /// users, and replace it once a new directory is bootstrapped.
288    // TODO(eta): Eurgh! This is so many Arcs! (especially considering this
289    //            gets wrapped in an Arc)
290    netdir: Arc<SharedMutArc<NetDir>>,
291
292    /// Our latest set of recommended protocols.
293    protocols: Mutex<Option<(SystemTime, Arc<ProtoStatuses>)>>,
294
295    /// A set of network parameters to hand out when we have no directory.
296    default_parameters: Mutex<Arc<NetParameters>>,
297
298    /// A publisher handle that we notify whenever the consensus changes.
299    events: event::FlagPublisher<DirEvent>,
300
301    /// A publisher handle that we notify whenever our bootstrapping status
302    /// changes.
303    send_status: Mutex<watch::Sender<event::DirBootstrapStatus>>,
304
305    /// A receiver handle that gets notified whenever our bootstrapping status
306    /// changes.
307    ///
308    /// We don't need to keep this drained, since `postage::watch` already knows
309    /// to discard unread events.
310    receive_status: DirBootstrapEvents,
311
312    /// A circuit manager, if this DirMgr supports downloading.
313    circmgr: Option<Arc<CircMgr<R>>>,
314
315    /// Our asynchronous runtime.
316    runtime: R,
317
318    /// Whether or not we're operating in offline mode.
319    offline: bool,
320
321    /// If we're not in offline mode, stores whether or not the `DirMgr` has attempted
322    /// to bootstrap yet or not.
323    ///
324    /// This exists in order to prevent starting two concurrent bootstrap tasks.
325    ///
326    /// (In offline mode, this does nothing.)
327    bootstrap_started: AtomicBool,
328
329    /// A filter that gets applied to directory objects before we use them.
330    #[cfg(feature = "dirfilter")]
331    filter: crate::filter::FilterConfig,
332
333    /// A task schedule that can be used if we're bootstrapping.  If this is
334    /// None, then there's currently a scheduled task in progress.
335    task_schedule: Mutex<Option<TaskSchedule<R>>>,
336
337    /// A task handle that we return to anybody who needs to manage our download process.
338    task_handle: TaskHandle,
339}
340
341/// The possible origins of a document.
342///
343/// Used (for example) to report where we got a document from if it fails to
344/// parse.
345#[derive(Debug, Clone)]
346#[non_exhaustive]
347pub enum DocSource {
348    /// We loaded the document from our cache.
349    LocalCache,
350    /// We fetched the document from a server.
351    DirServer {
352        /// Information about the server we fetched the document from.
353        source: Option<SourceInfo>,
354    },
355}
356
357impl std::fmt::Display for DocSource {
358    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
359        match self {
360            DocSource::LocalCache => write!(f, "local cache"),
361            DocSource::DirServer { source: None } => write!(f, "directory server"),
362            DocSource::DirServer { source: Some(info) } => write!(f, "directory server {}", info),
363        }
364    }
365}
366
367impl<R: Runtime> DirMgr<R> {
368    /// Try to load the directory from disk, without launching any
369    /// kind of update process.
370    ///
371    /// This function runs in **offline** mode: it will give an error
372    /// if the result is not up-to-date, or not fully downloaded.
373    ///
374    /// In general, you shouldn't use this function in a long-running
375    /// program; it's only suitable for command-line or batch tools.
376    // TODO: I wish this function didn't have to be async or take a runtime.
377    pub fn load_once(runtime: R, config: DirMgrConfig) -> Result<Arc<NetDir>> {
378        let store = DirMgrStore::new(&config, runtime.clone(), true)?;
379        let dirmgr = Arc::new(Self::from_config(config, runtime, store, None, true)?);
380
381        // TODO: add some way to return a directory that isn't up-to-date
382        let attempt = AttemptId::next();
383        trace!(%attempt, "Trying to load a full directory from cache");
384        let outcome = dirmgr.load_directory(attempt);
385        trace!(%attempt, "Load result: {outcome:?}");
386        let _success = outcome?;
387
388        dirmgr
389            .netdir(Timeliness::Timely)
390            .map_err(|_| Error::DirectoryNotPresent)
391    }
392
393    /// Return a current netdir, either loading it or bootstrapping it
394    /// as needed.
395    ///
396    /// Like load_once, but will try to bootstrap (or wait for another
397    /// process to bootstrap) if we don't have an up-to-date
398    /// bootstrapped directory.
399    ///
400    /// In general, you shouldn't use this function in a long-running
401    /// program; it's only suitable for command-line or batch tools.
402    pub async fn load_or_bootstrap_once(
403        config: DirMgrConfig,
404        runtime: R,
405        store: DirMgrStore<R>,
406        circmgr: Arc<CircMgr<R>>,
407    ) -> Result<Arc<NetDir>> {
408        let dirmgr = DirMgr::bootstrap_from_config(config, runtime, store, circmgr).await?;
409        dirmgr
410            .timely_netdir()
411            .map_err(|_| Error::DirectoryNotPresent)
412    }
413
414    /// Create a new `DirMgr` in online mode, but don't bootstrap it yet.
415    ///
416    /// The `DirMgr` can be bootstrapped later with `bootstrap`.
417    pub fn create_unbootstrapped(
418        config: DirMgrConfig,
419        runtime: R,
420        store: DirMgrStore<R>,
421        circmgr: Arc<CircMgr<R>>,
422    ) -> Result<Arc<Self>> {
423        Ok(Arc::new(DirMgr::from_config(
424            config,
425            runtime,
426            store,
427            Some(circmgr),
428            false,
429        )?))
430    }
431
432    /// Bootstrap a `DirMgr` created in online mode that hasn't been bootstrapped yet.
433    ///
434    /// This function will not return until the directory is bootstrapped enough to build circuits.
435    /// It will also launch a background task that fetches any missing information, and that
436    /// replaces the directory when a new one is available.
437    ///
438    /// This function is intended to be used together with `create_unbootstrapped`. There is no
439    /// need to call this function otherwise.
440    ///
441    /// If bootstrapping has already successfully taken place, returns early with success.
442    ///
443    /// # Errors
444    ///
445    /// Returns an error if bootstrapping fails. If the error is [`Error::CantAdvanceState`],
446    /// it may be possible to successfully bootstrap later on by calling this function again.
447    ///
448    /// # Panics
449    ///
450    /// Panics if the `DirMgr` passed to this function was not created in online mode, such as
451    /// via `load_once`.
452    #[instrument(level = "trace", skip_all)]
453    pub async fn bootstrap(self: &Arc<Self>) -> Result<()> {
454        if self.offline {
455            return Err(Error::OfflineMode);
456        }
457
458        // The semantics of this are "attempt to replace a 'false' value with 'true'.
459        // If the value in bootstrap_started was not 'false' when the attempt was made, returns
460        // `Err`; this means another bootstrap attempt is in progress or has completed, so we
461        // return early.
462
463        // NOTE(eta): could potentially weaken the `Ordering` here in future
464        if self
465            .bootstrap_started
466            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
467            .is_err()
468        {
469            debug!("Attempted to bootstrap twice; ignoring.");
470            return Ok(());
471        }
472
473        // Use a RAII guard to reset `bootstrap_started` to `false` if we return early without
474        // completing bootstrap.
475        let reset_bootstrap_started = scopeguard::guard(&self.bootstrap_started, |v| {
476            v.store(false, Ordering::SeqCst);
477        });
478
479        let schedule = {
480            let sched = self.task_schedule.lock().expect("poisoned lock").take();
481            match sched {
482                Some(sched) => sched,
483                None => {
484                    debug!("Attempted to bootstrap twice; ignoring.");
485                    return Ok(());
486                }
487            }
488        };
489
490        // Try to load from the cache.
491        let attempt_id = AttemptId::next();
492        trace!(attempt=%attempt_id, "Starting to bootstrap directory");
493        let have_directory = self.load_directory(attempt_id)?;
494
495        let (mut sender, receiver) = if have_directory {
496            info!("Loaded a good directory from cache.");
497            (None, None)
498        } else {
499            info!("Didn't get usable directory from cache.");
500            let (sender, receiver) = oneshot::channel();
501            (Some(sender), Some(receiver))
502        };
503
504        // Whether we loaded or not, we now start downloading.
505        let dirmgr_weak = Arc::downgrade(self);
506        self.runtime
507            .spawn(async move {
508                // Use an RAII guard to make sure that when this task exits, the
509                // TaskSchedule object is put back.
510                //
511                // TODO(nick): Putting the schedule back isn't actually useful
512                // if the task exits _after_ we've bootstrapped for the first
513                // time, because of how bootstrap_started works.
514                let mut schedule = scopeguard::guard(schedule, |schedule| {
515                    if let Some(dm) = Weak::upgrade(&dirmgr_weak) {
516                        *dm.task_schedule.lock().expect("poisoned lock") = Some(schedule);
517                    }
518                });
519
520                // Don't warn when these are Error::ManagerDropped: that
521                // means that the DirMgr has been shut down.
522                if let Err(e) =
523                    Self::reload_until_owner(&dirmgr_weak, &mut schedule, attempt_id, &mut sender)
524                        .await
525                {
526                    match e {
527                        Error::ManagerDropped => {}
528                        _ => warn_report!(e, "Unrecovered error while waiting for bootstrap",),
529                    }
530                } else if let Err(e) =
531                    Self::download_forever(dirmgr_weak.clone(), &mut schedule, attempt_id, sender)
532                        .await
533                {
534                    match e {
535                        Error::ManagerDropped => {}
536                        _ => warn_report!(e, "Unrecovered error while downloading"),
537                    }
538                }
539            })
540            .map_err(|e| Error::from_spawn("directory updater task", e))?;
541
542        if let Some(receiver) = receiver {
543            match receiver.await {
544                Ok(()) => {
545                    info!("We have enough information to build circuits.");
546                    // Disarm the RAII guard, since we succeeded.  Now bootstrap_started will remain true.
547                    let _ = ScopeGuard::into_inner(reset_bootstrap_started);
548                }
549                Err(_) => {
550                    warn!("Bootstrapping task exited before finishing.");
551                    return Err(Error::CantAdvanceState);
552                }
553            }
554        }
555        Ok(())
556    }
557
558    /// Returns `true` if a bootstrap attempt is in progress, or successfully completed.
559    pub fn bootstrap_started(&self) -> bool {
560        self.bootstrap_started.load(Ordering::SeqCst)
561    }
562
563    /// Return a new directory manager from a given configuration,
564    /// bootstrapping from the network as necessary.
565    #[instrument(level = "trace", skip_all)]
566    pub async fn bootstrap_from_config(
567        config: DirMgrConfig,
568        runtime: R,
569        store: DirMgrStore<R>,
570        circmgr: Arc<CircMgr<R>>,
571    ) -> Result<Arc<Self>> {
572        let dirmgr = Self::create_unbootstrapped(config, runtime, store, circmgr)?;
573
574        dirmgr.bootstrap().await?;
575
576        Ok(dirmgr)
577    }
578
579    /// Try forever to either lock the storage (and thereby become the
580    /// owner), or to reload the database.
581    ///
582    /// If we have begin to have a bootstrapped directory, send a
583    /// message using `on_complete`.
584    ///
585    /// If we eventually become the owner, return Ok().
586    async fn reload_until_owner(
587        weak: &Weak<Self>,
588        schedule: &mut TaskSchedule<R>,
589        attempt_id: AttemptId,
590        on_complete: &mut Option<oneshot::Sender<()>>,
591    ) -> Result<()> {
592        let mut logged = false;
593        let mut bootstrapped;
594        {
595            let dirmgr = upgrade_weak_ref(weak)?;
596            bootstrapped = dirmgr.netdir.get().is_some();
597        }
598
599        loop {
600            {
601                let dirmgr = upgrade_weak_ref(weak)?;
602                trace!("Trying to take ownership of the directory cache lock");
603                if dirmgr.try_upgrade_to_readwrite()? {
604                    // We now own the lock!  (Maybe we owned it before; the
605                    // upgrade_to_readwrite() function is idempotent.)  We can
606                    // do our own bootstrapping.
607                    if logged {
608                        info!(
609                            "The previous owning process has given up the lock. We are now in charge of managing the directory."
610                        );
611                    }
612                    return Ok(());
613                }
614            }
615
616            if !logged {
617                logged = true;
618                if bootstrapped {
619                    info!("Another process is managing the directory. We'll use its cache.");
620                } else {
621                    info!(
622                        "Another process is bootstrapping the directory. Waiting till it finishes or exits."
623                    );
624                }
625            }
626
627            // We don't own the lock.  Somebody else owns the cache.  They
628            // should be updating it.  Wait a bit, then try again.
629            let pause = if bootstrapped {
630                std::time::Duration::new(120, 0)
631            } else {
632                std::time::Duration::new(5, 0)
633            };
634            schedule.sleep(pause).await?;
635            // TODO: instead of loading the whole thing we should have a
636            // database entry that says when the last update was, or use
637            // our state functions.
638            {
639                let dirmgr = upgrade_weak_ref(weak)?;
640                trace!("Trying to load from the directory cache");
641                if dirmgr.load_directory(attempt_id)? {
642                    // Successfully loaded a bootstrapped directory.
643                    if let Some(send_done) = on_complete.take() {
644                        let _ = send_done.send(());
645                    }
646                    if !bootstrapped {
647                        info!("The directory is now bootstrapped.");
648                    }
649                    bootstrapped = true;
650                }
651            }
652        }
653    }
654
655    /// Try to fetch our directory info and keep it updated, indefinitely.
656    ///
657    /// If we have begin to have a bootstrapped directory, send a
658    /// message using `on_complete`.
659    #[instrument(level = "trace", skip_all)]
660    async fn download_forever(
661        weak: Weak<Self>,
662        schedule: &mut TaskSchedule<R>,
663        mut attempt_id: AttemptId,
664        mut on_complete: Option<oneshot::Sender<()>>,
665    ) -> Result<()> {
666        let mut state: Box<dyn DirState> = {
667            let dirmgr = upgrade_weak_ref(&weak)?;
668            Box::new(state::GetConsensusState::new(
669                dirmgr.runtime.clone(),
670                dirmgr.config.get(),
671                CacheUsage::CacheOkay,
672                Some(dirmgr.netdir.clone()),
673                #[cfg(feature = "dirfilter")]
674                dirmgr
675                    .filter
676                    .clone()
677                    .unwrap_or_else(|| Arc::new(crate::filter::NilFilter)),
678            ))
679        };
680
681        trace!("Entering download loop.");
682
683        loop {
684            let mut usable = false;
685
686            let retry_config = {
687                let dirmgr = upgrade_weak_ref(&weak)?;
688                // TODO(nickm): instead of getting this every time we loop, it
689                // might be a good idea to refresh it with each attempt, at
690                // least at the point of checking the number of attempts.
691                dirmgr.config.get().schedule.retry_bootstrap()
692            };
693            let mut retry_delay = retry_config.schedule();
694
695            'retry_attempt: for try_num in retry_config.attempts() {
696                trace!(attempt=%attempt_id, ?try_num, "Trying to download a directory.");
697                let outcome = bootstrap::download(
698                    Weak::clone(&weak),
699                    &mut state,
700                    schedule,
701                    attempt_id,
702                    &mut on_complete,
703                )
704                .await;
705                trace!(attempt=%attempt_id, ?try_num, ?outcome, "Download is over.");
706
707                if let Err(err) = outcome {
708                    if state.is_ready(Readiness::Usable) {
709                        usable = true;
710                        info_report!(
711                            err,
712                            "Unable to completely download a directory. (Nevertheless, the directory is usable, so we'll pause for now)"
713                        );
714                        break 'retry_attempt;
715                    }
716
717                    match err.bootstrap_action() {
718                        BootstrapAction::Nonfatal => {
719                            return Err(into_internal!(
720                                "Nonfatal error should not have propagated here"
721                            )(err)
722                            .into());
723                        }
724                        BootstrapAction::Reset => {}
725                        BootstrapAction::Fatal => return Err(err),
726                    }
727
728                    let delay = retry_delay.next_delay(&mut rand::rng());
729                    warn_report!(
730                        err,
731                        "Unable to download a usable directory. (We will restart in {})",
732                        humantime::format_duration(delay),
733                    );
734                    {
735                        let dirmgr = upgrade_weak_ref(&weak)?;
736                        dirmgr.note_reset(attempt_id);
737                    }
738                    schedule.sleep(delay).await?;
739                    state = state.reset();
740                } else {
741                    info!(attempt=%attempt_id, "Directory is complete.");
742                    usable = true;
743                    break 'retry_attempt;
744                }
745            }
746
747            if !usable {
748                // we ran out of attempts.
749                warn!(
750                    "We failed {} times to bootstrap a directory. We're going to give up.",
751                    retry_config.n_attempts()
752                );
753                return Err(Error::CantAdvanceState);
754            } else {
755                // Report success, if appropriate.
756                if let Some(send_done) = on_complete.take() {
757                    let _ = send_done.send(());
758                }
759            }
760
761            let reset_at = state.reset_time();
762            match reset_at {
763                Some(t) => {
764                    trace!("Sleeping until {}", time::OffsetDateTime::from(t));
765                    schedule.sleep_until_wallclock(t).await?;
766                }
767                None => return Ok(()),
768            }
769            attempt_id = bootstrap::AttemptId::next();
770            trace!(attempt=%attempt_id, "Beginning new attempt to bootstrap directory");
771            state = state.reset();
772        }
773    }
774
775    /// Get a reference to the circuit manager, if we have one.
776    fn circmgr(&self) -> Result<Arc<CircMgr<R>>> {
777        self.circmgr.clone().ok_or(Error::NoDownloadSupport)
778    }
779
780    /// Try to change our configuration to `new_config`.
781    ///
782    /// Actual behavior will depend on the value of `how`.
783    pub fn reconfigure(
784        &self,
785        new_config: &DirMgrConfig,
786        how: tor_config::Reconfigure,
787    ) -> std::result::Result<(), tor_config::ReconfigureError> {
788        let config = self.config.get();
789        // We don't support changing these: doing so basically would require us
790        // to abort all our in-progress downloads, since they might be based on
791        // no-longer-viable information.
792        // NOTE: keep this in sync with the behaviour of `DirMgrConfig::update_from_config`
793        if new_config.cache_dir != config.cache_dir {
794            how.cannot_change("storage.cache_dir")?;
795        }
796        if new_config.cache_trust != config.cache_trust {
797            how.cannot_change("storage.permissions")?;
798        }
799        if new_config.authorities() != config.authorities() {
800            how.cannot_change("network.authorities")?;
801        }
802
803        if how == tor_config::Reconfigure::CheckAllOrNothing {
804            return Ok(());
805        }
806
807        let params_changed = new_config.override_net_params != config.override_net_params;
808
809        self.config
810            .map_and_replace(|cfg| cfg.update_from_config(new_config));
811
812        if params_changed {
813            let _ignore_err = self.netdir.mutate(|netdir| {
814                netdir.replace_overridden_parameters(&new_config.override_net_params);
815                Ok(())
816            });
817            {
818                let mut params = self.default_parameters.lock().expect("lock failed");
819                *params = Arc::new(NetParameters::from_map(&new_config.override_net_params));
820            }
821
822            // (It's okay to ignore the error, since it just means that there
823            // was no current netdir.)
824            self.events.publish(DirEvent::NewConsensus);
825        }
826
827        Ok(())
828    }
829
830    /// Return a stream of [`DirBootstrapStatus`] events to tell us about changes
831    /// in the latest directory's bootstrap status.
832    ///
833    /// Note that this stream can be lossy: the caller will not necessarily
834    /// observe every event on the stream
835    pub fn bootstrap_events(&self) -> event::DirBootstrapEvents {
836        self.receive_status.clone()
837    }
838
839    /// Replace the latest status with `progress` and broadcast to anybody
840    /// watching via a [`DirBootstrapEvents`] stream.
841    fn update_progress(&self, attempt_id: AttemptId, progress: DirProgress) {
842        // TODO(nickm): can I kill off this lock by having something else own the sender?
843        let mut sender = self.send_status.lock().expect("poisoned lock");
844        let mut status = sender.borrow_mut();
845
846        status.update_progress(attempt_id, progress);
847    }
848
849    /// Update our status tracker to note that some number of errors has
850    /// occurred.
851    fn note_errors(&self, attempt_id: AttemptId, n_errors: usize) {
852        if n_errors == 0 {
853            return;
854        }
855        let mut sender = self.send_status.lock().expect("poisoned lock");
856        let mut status = sender.borrow_mut();
857
858        status.note_errors(attempt_id, n_errors);
859    }
860
861    /// Update our status tracker to note that we've needed to reset our download attempt.
862    fn note_reset(&self, attempt_id: AttemptId) {
863        let mut sender = self.send_status.lock().expect("poisoned lock");
864        let mut status = sender.borrow_mut();
865
866        status.note_reset(attempt_id);
867    }
868
869    /// Try to make this a directory manager with read-write access to its
870    /// storage.
871    ///
872    /// Return true if we got the lock, or if we already had it.
873    ///
874    /// Return false if another process has the lock
875    fn try_upgrade_to_readwrite(&self) -> Result<bool> {
876        self.store
877            .lock()
878            .expect("Directory storage lock poisoned")
879            .upgrade_to_readwrite()
880    }
881
882    /// Return a reference to the store, if it is currently read-write.
883    #[cfg(test)]
884    fn store_if_rw(&self) -> Option<&Mutex<DynStore>> {
885        let rw = !self
886            .store
887            .lock()
888            .expect("Directory storage lock poisoned")
889            .is_readonly();
890        // A race-condition is possible here, but I believe it's harmless.
891        if rw { Some(&self.store) } else { None }
892    }
893
894    /// Construct a DirMgr from a DirMgrConfig.
895    ///
896    /// If `offline` is set, opens the SQLite store read-only and sets the offline flag in the
897    /// returned manager.
898    #[allow(clippy::unnecessary_wraps)] // API compat and future-proofing
899    fn from_config(
900        config: DirMgrConfig,
901        runtime: R,
902        store: DirMgrStore<R>,
903        circmgr: Option<Arc<CircMgr<R>>>,
904        offline: bool,
905    ) -> Result<Self> {
906        let netdir = Arc::new(SharedMutArc::new());
907        let events = event::FlagPublisher::new();
908        let default_parameters = NetParameters::from_map(&config.override_net_params);
909        let default_parameters = Mutex::new(Arc::new(default_parameters));
910
911        let (send_status, receive_status) = postage::watch::channel();
912        let send_status = Mutex::new(send_status);
913        let receive_status = DirBootstrapEvents {
914            inner: receive_status,
915        };
916        #[cfg(feature = "dirfilter")]
917        let filter = config.extensions.filter.clone();
918
919        // We create these early so the client code can access task_handle before bootstrap() returns.
920        let (task_schedule, task_handle) = TaskSchedule::new(runtime.clone());
921        let task_schedule = Mutex::new(Some(task_schedule));
922
923        // We load the cached protocol recommendations unconditionally: the caller needs them even
924        // if it does not try to load the reset of the cache.
925        let protocols = {
926            let store = store.store.lock().expect("lock poisoned");
927            store
928                .cached_protocol_recommendations()?
929                .map(|(t, p)| (t, Arc::new(p)))
930        };
931
932        Ok(DirMgr {
933            config: config.into(),
934            store: store.store,
935            netdir,
936            protocols: Mutex::new(protocols),
937            default_parameters,
938            events,
939            send_status,
940            receive_status,
941            circmgr,
942            runtime,
943            offline,
944            bootstrap_started: AtomicBool::new(false),
945            #[cfg(feature = "dirfilter")]
946            filter,
947            task_schedule,
948            task_handle,
949        })
950    }
951
952    /// Load the latest non-pending non-expired directory from the
953    /// cache, if it is newer than the one we have.
954    ///
955    /// Return false if there is no such consensus.
956    fn load_directory(self: &Arc<Self>, attempt_id: AttemptId) -> Result<bool> {
957        let state = state::GetConsensusState::new(
958            self.runtime.clone(),
959            self.config.get(),
960            CacheUsage::CacheOnly,
961            None,
962            #[cfg(feature = "dirfilter")]
963            self.filter
964                .clone()
965                .unwrap_or_else(|| Arc::new(crate::filter::NilFilter)),
966        );
967        let _ = bootstrap::load(self, Box::new(state), attempt_id)?;
968
969        Ok(self.netdir.get().is_some())
970    }
971
972    /// Return a new asynchronous stream that will receive notification
973    /// whenever the consensus has changed.
974    ///
975    /// Multiple events may be batched up into a single item: each time
976    /// this stream yields an event, all you can assume is that the event has
977    /// occurred at least once.
978    pub fn events(&self) -> impl futures::Stream<Item = DirEvent> + use<R> {
979        self.events.subscribe()
980    }
981
982    /// Try to load the text of a single document described by `doc` from
983    /// storage.
984    pub fn text(&self, doc: &DocId) -> Result<Option<DocumentText>> {
985        use itertools::Itertools;
986        let mut result = HashMap::new();
987        let query: DocQuery = (*doc).into();
988        let store = self.store.lock().expect("store lock poisoned");
989        query.load_from_store_into(&mut result, &**store)?;
990        let item = result.into_iter().at_most_one().map_err(|_| {
991            Error::CacheCorruption("Found more than one entry in storage for given docid")
992        })?;
993        if let Some((docid, doctext)) = item {
994            if &docid != doc {
995                return Err(Error::CacheCorruption(
996                    "Item from storage had incorrect docid.",
997                ));
998            }
999            Ok(Some(doctext))
1000        } else {
1001            Ok(None)
1002        }
1003    }
1004
1005    /// Load the text for a collection of documents.
1006    ///
1007    /// If many of the documents have the same type, this can be more
1008    /// efficient than calling [`text`](Self::text).
1009    pub fn texts<T>(&self, docs: T) -> Result<HashMap<DocId, DocumentText>>
1010    where
1011        T: IntoIterator<Item = DocId>,
1012    {
1013        let partitioned = docid::partition_by_type(docs);
1014        let mut result = HashMap::new();
1015        let store = self.store.lock().expect("store lock poisoned");
1016        for (_, query) in partitioned.into_iter() {
1017            query.load_from_store_into(&mut result, &**store)?;
1018        }
1019        Ok(result)
1020    }
1021
1022    /// Given a request we sent and the response we got from a
1023    /// directory server, see whether we should expand that response
1024    /// into "something larger".
1025    ///
1026    /// Currently, this handles expanding consensus diffs, and nothing
1027    /// else.  We do it at this stage of our downloading operation
1028    /// because it requires access to the store.
1029    fn expand_response_text(&self, req: &ClientRequest, text: String) -> Result<String> {
1030        if let ClientRequest::Consensus(req) = req {
1031            if tor_consdiff::looks_like_diff(&text) {
1032                if let Some(old_d) = req.old_consensus_digests().next() {
1033                    let db_val = {
1034                        let s = self.store.lock().expect("Directory storage lock poisoned");
1035                        s.consensus_by_sha3_digest_of_signed_part(old_d)?
1036                    };
1037                    if let Some((old_consensus, meta)) = db_val {
1038                        info!("Applying a consensus diff");
1039                        let new_consensus = tor_consdiff::apply_diff(
1040                            old_consensus.as_str()?,
1041                            &text,
1042                            Some(*meta.sha3_256_of_signed()),
1043                            tor_consdiff::DiffSizeStrictness::Apply,
1044                        )?;
1045                        new_consensus.check_digest()?;
1046                        return Ok(new_consensus.to_string());
1047                    }
1048                }
1049                return Err(Error::Unwanted(
1050                    "Received a consensus diff we did not ask for",
1051                ));
1052            }
1053        }
1054        Ok(text)
1055    }
1056
1057    /// If `state` has netdir changes to apply, apply them to our netdir.
1058    fn apply_netdir_changes(
1059        self: &Arc<Self>,
1060        state: &mut Box<dyn DirState>,
1061        store: &mut dyn Store,
1062    ) -> Result<()> {
1063        if let Some(change) = state.get_netdir_change() {
1064            match change {
1065                NetDirChange::AttemptReplace {
1066                    netdir,
1067                    consensus_meta,
1068                } => {
1069                    // Check the new netdir is sufficient, if we have a circmgr.
1070                    // (Unwraps are fine because the `Option` is `Some` until we take it.)
1071                    if let Some(ref cm) = self.circmgr {
1072                        if !cm
1073                            .netdir_is_sufficient(netdir.as_ref().expect("AttemptReplace had None"))
1074                        {
1075                            debug!("Got a new NetDir, but it doesn't have enough guards yet.");
1076                            return Ok(());
1077                        }
1078                    }
1079                    let is_stale = {
1080                        // Done inside a block to not hold a long-lived copy of the NetDir.
1081                        self.netdir
1082                            .get()
1083                            .map(|x| {
1084                                x.lifetime().valid_after()
1085                                    > netdir
1086                                        .as_ref()
1087                                        .expect("AttemptReplace had None")
1088                                        .lifetime()
1089                                        .valid_after()
1090                            })
1091                            .unwrap_or(false)
1092                    };
1093                    if is_stale {
1094                        warn!("Got a new NetDir, but it's older than the one we currently have!");
1095                        return Err(Error::NetDirOlder);
1096                    }
1097                    let cfg = self.config.get();
1098                    let mut netdir = netdir.take().expect("AttemptReplace had None");
1099                    netdir.replace_overridden_parameters(&cfg.override_net_params);
1100                    self.netdir.replace(netdir);
1101                    self.events.publish(DirEvent::NewConsensus);
1102                    self.events.publish(DirEvent::NewDescriptors);
1103
1104                    info!("Marked consensus usable.");
1105                    if !store.is_readonly() {
1106                        store.mark_consensus_usable(consensus_meta)?;
1107                        // Now that a consensus is usable, older consensuses may
1108                        // need to expire.
1109                        store.expire_all(&crate::storage::EXPIRATION_DEFAULTS)?;
1110                    }
1111                    Ok(())
1112                }
1113                NetDirChange::AddMicrodescs(mds) => {
1114                    self.netdir.mutate(|netdir| {
1115                        for md in mds.drain(..) {
1116                            netdir.add_microdesc(md);
1117                        }
1118                        Ok(())
1119                    })?;
1120                    self.events.publish(DirEvent::NewDescriptors);
1121                    Ok(())
1122                }
1123                NetDirChange::SetRequiredProtocol { timestamp, protos } => {
1124                    if !store.is_readonly() {
1125                        store.update_protocol_recommendations(timestamp, protos.as_ref())?;
1126                    }
1127                    let mut pr = self.protocols.lock().expect("Poisoned lock");
1128                    *pr = Some((timestamp, protos));
1129                    self.events.publish(DirEvent::NewProtocolRecommendation);
1130                    Ok(())
1131                }
1132            }
1133        } else {
1134            Ok(())
1135        }
1136    }
1137
1138    /// Experimental; temporary: Return a directory plugin to be used while tor-dirserver is a work
1139    /// in progress.
1140    #[cfg(feature = "dir-plugin")]
1141    pub fn get_plugin(&self) -> as_plugin::DirPlugin {
1142        as_plugin::DirPlugin {
1143            store: Arc::clone(&self.store),
1144        }
1145    }
1146}
1147
1148/// A degree of readiness for a given directory state object.
1149#[derive(Debug, Copy, Clone)]
1150enum Readiness {
1151    /// There is no more information to download.
1152    Complete,
1153    /// There is more information to download, but we don't need to
1154    Usable,
1155}
1156
1157/// Try to upgrade a weak reference to a DirMgr, and give an error on
1158/// failure.
1159fn upgrade_weak_ref<T>(weak: &Weak<T>) -> Result<Arc<T>> {
1160    Weak::upgrade(weak).ok_or(Error::ManagerDropped)
1161}
1162
1163/// Given a time `now`, and an amount of tolerated clock skew `tolerance`,
1164/// return the age of the oldest consensus that we should request at that time.
1165pub(crate) fn default_consensus_cutoff(
1166    now: SystemTime,
1167    tolerance: &DirTolerance,
1168) -> Result<SystemTime> {
1169    /// We _always_ allow at least this much age in our consensuses, to account
1170    /// for the fact that consensuses have some lifetime.
1171    const MIN_AGE_TO_ALLOW: Duration = Duration::from_secs(3 * 3600);
1172    let allow_skew = std::cmp::max(MIN_AGE_TO_ALLOW, tolerance.post_valid_tolerance());
1173    let cutoff = time::OffsetDateTime::from(now - allow_skew);
1174    // We now round cutoff to the next hour, so that we aren't leaking our exact
1175    // time to the directory cache.
1176    //
1177    // With the time crate, it's easier to calculate the "next hour" by rounding
1178    // _down_ then adding an hour; rounding up would sometimes require changing
1179    // the date too.
1180    let (h, _m, _s) = cutoff.to_hms();
1181    let cutoff = cutoff.replace_time(
1182        time::Time::from_hms(h, 0, 0)
1183            .map_err(tor_error::into_internal!("Failed clock calculation"))?,
1184    );
1185    let cutoff = cutoff + Duration::from_secs(3600);
1186
1187    Ok(cutoff.into())
1188}
1189
1190/// Return a list of the protocols [supported](tor_protover::doc_supported) by this crate
1191/// when running as a client.
1192pub fn supported_client_protocols() -> tor_protover::Protocols {
1193    use tor_protover::named::*;
1194    // WARNING: REMOVING ELEMENTS FROM THIS LIST CAN BE DANGEROUS!
1195    // SEE [`tor_protover::doc_changing`]
1196    [
1197        //
1198        DIRCACHE_CONSDIFF,
1199    ]
1200    .into_iter()
1201    .collect()
1202}
1203
1204#[cfg(test)]
1205mod test {
1206    // @@ begin test lint list maintained by maint/add_warning @@
1207    #![allow(clippy::bool_assert_comparison)]
1208    #![allow(clippy::clone_on_copy)]
1209    #![allow(clippy::dbg_macro)]
1210    #![allow(clippy::mixed_attributes_style)]
1211    #![allow(clippy::print_stderr)]
1212    #![allow(clippy::print_stdout)]
1213    #![allow(clippy::single_char_pattern)]
1214    #![allow(clippy::unwrap_used)]
1215    #![allow(clippy::unchecked_time_subtraction)]
1216    #![allow(clippy::useless_vec)]
1217    #![allow(clippy::needless_pass_by_value)]
1218    #![allow(clippy::string_slice)] // See arti#2571
1219    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
1220    use super::*;
1221    use crate::docmeta::{AuthCertMeta, ConsensusMeta};
1222    use std::time::Duration;
1223    use tempfile::TempDir;
1224    use tor_basic_utils::test_rng::testing_rng;
1225    use tor_netdoc::doc::netstatus::ConsensusFlavor;
1226    use tor_netdoc::doc::{authcert::AuthCertKeyIds, netstatus::Lifetime};
1227    use tor_rtcompat::SleepProvider;
1228
1229    #[test]
1230    fn protocols() {
1231        let pr = supported_client_protocols();
1232        let expected = "DirCache=2".parse().unwrap();
1233        assert_eq!(pr, expected);
1234    }
1235
1236    pub(crate) fn new_mgr<R: Runtime>(runtime: R) -> (TempDir, DirMgr<R>) {
1237        let dir = TempDir::new().unwrap();
1238        let config = DirMgrConfig {
1239            cache_dir: dir.path().into(),
1240            ..Default::default()
1241        };
1242        let store = DirMgrStore::new(&config, runtime.clone(), false).unwrap();
1243        let dirmgr = DirMgr::from_config(config, runtime, store, None, false).unwrap();
1244
1245        (dir, dirmgr)
1246    }
1247
1248    #[test]
1249    fn failing_accessors() {
1250        tor_rtcompat::test_with_one_runtime!(|rt| async {
1251            let (_tempdir, mgr) = new_mgr(rt);
1252
1253            assert!(mgr.circmgr().is_err());
1254            assert!(mgr.netdir(Timeliness::Unchecked).is_err());
1255        });
1256    }
1257
1258    #[test]
1259    fn load_and_store_internals() {
1260        tor_rtcompat::test_with_one_runtime!(|rt| async {
1261            let now = rt.wallclock();
1262            let tomorrow = now + Duration::from_secs(86400);
1263            let later = tomorrow + Duration::from_secs(86400);
1264
1265            let (_tempdir, mgr) = new_mgr(rt);
1266
1267            // Seed the storage with a bunch of junk.
1268            let d1 = [5_u8; 32];
1269            let d2 = [7; 32];
1270            let d3 = [42; 32];
1271            let d4 = [99; 20];
1272            let d5 = [12; 20];
1273            let certid1 = AuthCertKeyIds {
1274                id_fingerprint: d4.into(),
1275                sk_fingerprint: d5.into(),
1276            };
1277            let certid2 = AuthCertKeyIds {
1278                id_fingerprint: d5.into(),
1279                sk_fingerprint: d4.into(),
1280            };
1281
1282            {
1283                let mut store = mgr.store.lock().unwrap();
1284
1285                store
1286                    .store_microdescs(
1287                        &[
1288                            ("Fake micro 1", &d1),
1289                            ("Fake micro 2", &d2),
1290                            ("Fake micro 3", &d3),
1291                        ],
1292                        now,
1293                    )
1294                    .unwrap();
1295
1296                #[cfg(feature = "routerdesc")]
1297                store
1298                    .store_routerdescs(&[("Fake rd1", now, &d4), ("Fake rd2", now, &d5)])
1299                    .unwrap();
1300
1301                store
1302                    .store_authcerts(&[
1303                        (
1304                            AuthCertMeta::new(certid1, now, tomorrow),
1305                            "Fake certificate one",
1306                        ),
1307                        (
1308                            AuthCertMeta::new(certid2, now, tomorrow),
1309                            "Fake certificate two",
1310                        ),
1311                    ])
1312                    .unwrap();
1313
1314                let cmeta = ConsensusMeta::new(
1315                    Lifetime::new(now, tomorrow, later).unwrap(),
1316                    [102; 32],
1317                    [103; 32],
1318                );
1319                store
1320                    .store_consensus(&cmeta, ConsensusFlavor::Microdesc, false, "Fake consensus!")
1321                    .unwrap();
1322            }
1323
1324            // Try to get it with text().
1325            let t1 = mgr.text(&DocId::Microdesc(d1)).unwrap().unwrap();
1326            assert_eq!(t1.as_str(), Ok("Fake micro 1"));
1327
1328            let t2 = mgr
1329                .text(&DocId::LatestConsensus {
1330                    flavor: ConsensusFlavor::Microdesc,
1331                    cache_usage: CacheUsage::CacheOkay,
1332                })
1333                .unwrap()
1334                .unwrap();
1335            assert_eq!(t2.as_str(), Ok("Fake consensus!"));
1336
1337            let t3 = mgr.text(&DocId::Microdesc([255; 32])).unwrap();
1338            assert!(t3.is_none());
1339
1340            // Now try texts()
1341            let d_bogus = DocId::Microdesc([255; 32]);
1342            let res = mgr
1343                .texts(vec![
1344                    DocId::Microdesc(d2),
1345                    DocId::Microdesc(d3),
1346                    d_bogus,
1347                    DocId::AuthCert(certid2),
1348                    #[cfg(feature = "routerdesc")]
1349                    DocId::RouterDesc(d5),
1350                ])
1351                .unwrap();
1352            assert_eq!(
1353                res.get(&DocId::Microdesc(d2)).unwrap().as_str(),
1354                Ok("Fake micro 2")
1355            );
1356            assert_eq!(
1357                res.get(&DocId::Microdesc(d3)).unwrap().as_str(),
1358                Ok("Fake micro 3")
1359            );
1360            assert!(!res.contains_key(&d_bogus));
1361            assert_eq!(
1362                res.get(&DocId::AuthCert(certid2)).unwrap().as_str(),
1363                Ok("Fake certificate two")
1364            );
1365            #[cfg(feature = "routerdesc")]
1366            assert_eq!(
1367                res.get(&DocId::RouterDesc(d5)).unwrap().as_str(),
1368                Ok("Fake rd2")
1369            );
1370        });
1371    }
1372
1373    #[test]
1374    fn make_consensus_request() {
1375        tor_rtcompat::test_with_one_runtime!(|rt| async {
1376            let now = rt.wallclock();
1377            let tomorrow = now + Duration::from_secs(86400);
1378            let later = tomorrow + Duration::from_secs(86400);
1379
1380            let (_tempdir, mgr) = new_mgr(rt);
1381            let config = DirMgrConfig::default();
1382
1383            // Try with an empty store.
1384            let req = {
1385                let store = mgr.store.lock().unwrap();
1386                bootstrap::make_consensus_request(
1387                    now,
1388                    ConsensusFlavor::Microdesc,
1389                    &**store,
1390                    &config,
1391                )
1392                .unwrap()
1393            };
1394            let tolerance = DirTolerance::default().post_valid_tolerance();
1395            match req {
1396                ClientRequest::Consensus(r) => {
1397                    assert_eq!(r.old_consensus_digests().count(), 0);
1398                    let date = r.last_consensus_date().unwrap();
1399                    assert!(date >= now - tolerance);
1400                    assert!(date <= now - tolerance + Duration::from_secs(3600));
1401                }
1402                _ => panic!("Wrong request type"),
1403            }
1404
1405            // Add a fake consensus record.
1406            let d_prev = [42; 32];
1407            {
1408                let mut store = mgr.store.lock().unwrap();
1409
1410                let cmeta = ConsensusMeta::new(
1411                    Lifetime::new(now, tomorrow, later).unwrap(),
1412                    d_prev,
1413                    [103; 32],
1414                );
1415                store
1416                    .store_consensus(&cmeta, ConsensusFlavor::Microdesc, false, "Fake consensus!")
1417                    .unwrap();
1418            }
1419
1420            // Now try again.
1421            let req = {
1422                let store = mgr.store.lock().unwrap();
1423                bootstrap::make_consensus_request(
1424                    now,
1425                    ConsensusFlavor::Microdesc,
1426                    &**store,
1427                    &config,
1428                )
1429                .unwrap()
1430            };
1431            match req {
1432                ClientRequest::Consensus(r) => {
1433                    let ds: Vec<_> = r.old_consensus_digests().collect();
1434                    assert_eq!(ds.len(), 1);
1435                    assert_eq!(ds[0], &d_prev);
1436                    assert_eq!(r.last_consensus_date(), Some(now));
1437                }
1438                _ => panic!("Wrong request type"),
1439            }
1440        });
1441    }
1442
1443    #[test]
1444    fn make_other_requests() {
1445        tor_rtcompat::test_with_one_runtime!(|rt| async {
1446            use rand::RngExt;
1447            let (_tempdir, mgr) = new_mgr(rt);
1448
1449            let certid1 = AuthCertKeyIds {
1450                id_fingerprint: [99; 20].into(),
1451                sk_fingerprint: [100; 20].into(),
1452            };
1453            let mut rng = testing_rng();
1454            #[cfg(feature = "routerdesc")]
1455            let rd_ids: Vec<DocId> = (0..1000).map(|_| DocId::RouterDesc(rng.random())).collect();
1456            let md_ids: Vec<DocId> = (0..1000).map(|_| DocId::Microdesc(rng.random())).collect();
1457            let config = DirMgrConfig::default();
1458
1459            // Try an authcert.
1460            let query = DocId::AuthCert(certid1);
1461            let store = mgr.store.lock().unwrap();
1462            let reqs =
1463                bootstrap::make_requests_for_documents(&mgr.runtime, &[query], &**store, &config)
1464                    .unwrap();
1465            assert_eq!(reqs.len(), 1);
1466            let req = &reqs[0];
1467            if let ClientRequest::AuthCert(r) = req {
1468                assert_eq!(r.keys().next(), Some(&certid1));
1469            } else {
1470                panic!();
1471            }
1472
1473            // Try a bunch of mds.
1474            let reqs =
1475                bootstrap::make_requests_for_documents(&mgr.runtime, &md_ids, &**store, &config)
1476                    .unwrap();
1477            assert_eq!(reqs.len(), 2);
1478            assert!(matches!(reqs[0], ClientRequest::Microdescs(_)));
1479
1480            // Try a bunch of rds.
1481            #[cfg(feature = "routerdesc")]
1482            {
1483                let reqs = bootstrap::make_requests_for_documents(
1484                    &mgr.runtime,
1485                    &rd_ids,
1486                    &**store,
1487                    &config,
1488                )
1489                .unwrap();
1490                assert_eq!(reqs.len(), 2);
1491                assert!(matches!(reqs[0], ClientRequest::RouterDescs(_)));
1492            }
1493        });
1494    }
1495
1496    #[test]
1497    fn expand_response() {
1498        tor_rtcompat::test_with_one_runtime!(|rt| async {
1499            let now = rt.wallclock();
1500            let day = Duration::from_secs(86400);
1501            let config = DirMgrConfig::default();
1502
1503            let (_tempdir, mgr) = new_mgr(rt);
1504
1505            // Try a simple request: nothing should happen.
1506            let q = DocId::Microdesc([99; 32]);
1507            let r = {
1508                let store = mgr.store.lock().unwrap();
1509                bootstrap::make_requests_for_documents(&mgr.runtime, &[q], &**store, &config)
1510                    .unwrap()
1511            };
1512            let expanded = mgr.expand_response_text(&r[0], "ABC".to_string());
1513            assert_eq!(&expanded.unwrap(), "ABC");
1514
1515            // Try a consensus response that doesn't look like a diff in
1516            // response to a query that doesn't ask for one.
1517            let latest_id = DocId::LatestConsensus {
1518                flavor: ConsensusFlavor::Microdesc,
1519                cache_usage: CacheUsage::CacheOkay,
1520            };
1521            let r = {
1522                let store = mgr.store.lock().unwrap();
1523                bootstrap::make_requests_for_documents(
1524                    &mgr.runtime,
1525                    &[latest_id],
1526                    &**store,
1527                    &config,
1528                )
1529                .unwrap()
1530            };
1531            let expanded = mgr.expand_response_text(&r[0], "DEF".to_string());
1532            assert_eq!(&expanded.unwrap(), "DEF");
1533
1534            // Now stick some metadata and a string into the storage so that
1535            // we can ask for a diff.
1536            {
1537                let mut store = mgr.store.lock().unwrap();
1538                let d_in = [0x99; 32]; // This one, we can fake.
1539                let cmeta = ConsensusMeta::new(
1540                    Lifetime::new(now, now + day, now + 2 * day).unwrap(),
1541                    d_in,
1542                    d_in,
1543                );
1544                store
1545                    .store_consensus(
1546                        &cmeta,
1547                        ConsensusFlavor::Microdesc,
1548                        false,
1549                        "line 1\nline2\nline 3\n",
1550                    )
1551                    .unwrap();
1552            }
1553
1554            // Try expanding something that isn't a consensus, even if we'd like
1555            // one.
1556            let r = {
1557                let store = mgr.store.lock().unwrap();
1558                bootstrap::make_requests_for_documents(
1559                    &mgr.runtime,
1560                    &[latest_id],
1561                    &**store,
1562                    &config,
1563                )
1564                .unwrap()
1565            };
1566            let expanded = mgr.expand_response_text(&r[0], "hello".to_string());
1567            assert_eq!(&expanded.unwrap(), "hello");
1568
1569            // Finally, try "expanding" a diff (by applying it and checking the digest.
1570            let diff = "network-status-diff-version 1
1571hash 9999999999999999999999999999999999999999999999999999999999999999 8382374ca766873eb0d2530643191c6eaa2c5e04afa554cbac349b5d0592d300
15722c
1573replacement line
1574.
1575".to_string();
1576            let expanded = mgr.expand_response_text(&r[0], diff);
1577
1578            assert_eq!(expanded.unwrap(), "line 1\nreplacement line\nline 3\n");
1579
1580            // If the digest is wrong, that should get rejected.
1581            let diff = "network-status-diff-version 1
1582hash 9999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999
15832c
1584replacement line
1585.
1586".to_string();
1587            let expanded = mgr.expand_response_text(&r[0], diff);
1588            assert!(expanded.is_err());
1589        });
1590    }
1591}