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
#![warn(clippy::cognitive_complexity)]
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
#![deny(clippy::print_stderr)]
28
#![deny(clippy::print_stdout)]
29
#![warn(clippy::rc_buffer)]
30
#![deny(clippy::ref_option_ref)]
31
#![warn(clippy::semicolon_if_nothing_returned)]
32
#![warn(clippy::trait_duplication_in_bounds)]
33
#![deny(clippy::unchecked_time_subtraction)]
34
#![deny(clippy::unnecessary_wraps)]
35
#![warn(clippy::unseparated_literal_suffix)]
36
#![deny(clippy::unwrap_used)]
37
#![deny(clippy::mod_module_files)]
38
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39
#![allow(clippy::uninlined_format_args)]
40
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43
#![allow(clippy::needless_lifetimes)] // See arti#1765
44
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45
#![allow(clippy::collapsible_if)] // See arti#2342
46
#![deny(clippy::unused_async)]
47
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
48

            
49
mod config;
50
mod restriction;
51
mod selector;
52
mod target_port;
53
mod usage;
54

            
55
pub use config::RelaySelectionConfig;
56
pub use restriction::{RelayExclusion, RelayRestriction};
57
pub use selector::{RelaySelector, SelectionInfo};
58
pub use target_port::TargetPort;
59
pub use usage::RelayUsage;
60

            
61
/// A property that can be provided by relays.
62
///
63
/// The predicates that implement this trait are typically lower level ones that
64
/// represent only some of the properties that need to be checked before a relay
65
/// can be used.  Code should generally use RelaySelector instead.
66
pub trait LowLevelRelayPredicate {
67
    /// Return true if `relay` provides this predicate.
68
    fn low_level_predicate_permits_relay(&self, relay: &tor_netdir::Relay<'_>) -> bool;
69
}
70

            
71
/// Helper module for our tests.
72
#[cfg(test)]
73
pub(crate) mod testing {
74
    // @@ begin test lint list maintained by maint/add_warning @@
75
    #![allow(clippy::bool_assert_comparison)]
76
    #![allow(clippy::clone_on_copy)]
77
    #![allow(clippy::dbg_macro)]
78
    #![allow(clippy::mixed_attributes_style)]
79
    #![allow(clippy::print_stderr)]
80
    #![allow(clippy::print_stdout)]
81
    #![allow(clippy::single_char_pattern)]
82
    #![allow(clippy::unwrap_used)]
83
    #![allow(clippy::unchecked_time_subtraction)]
84
    #![allow(clippy::useless_vec)]
85
    #![allow(clippy::needless_pass_by_value)]
86
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
87

            
88
    use crate::{LowLevelRelayPredicate, RelaySelectionConfig};
89
    use std::collections::HashSet;
90
    use std::sync::LazyLock;
91
    use tor_netdir::{NetDir, Relay, SubnetConfig};
92
    use tor_netdoc::types::relay_flags::RelayFlag;
93

            
94
    /// Use a predicate to divide a NetDir into the relays that do and do not
95
    /// conform (respectively).
96
    ///
97
    /// # Panics
98
    ///
99
    /// Panics if either the "yes" list or the "no" list is empty, to ensure
100
    /// that all of our tests are really testing something.
101
    pub(crate) fn split_netdir<'a, P: LowLevelRelayPredicate>(
102
        netdir: &'a NetDir,
103
        pred: &P,
104
    ) -> (Vec<Relay<'a>>, Vec<Relay<'a>>) {
105
        let (yes, no): (Vec<_>, Vec<_>) = netdir
106
            .relays()
107
            .partition(|r| pred.low_level_predicate_permits_relay(r));
108
        assert!(!yes.is_empty());
109
        assert!(!no.is_empty());
110
        (yes, no)
111
    }
112

            
113
    /// Return a basic configuration.
114
    pub(crate) fn cfg() -> RelaySelectionConfig<'static> {
115
        static STABLE_PORTS: LazyLock<HashSet<u16>> = LazyLock::new(|| [22].into_iter().collect());
116
        RelaySelectionConfig {
117
            long_lived_ports: &STABLE_PORTS,
118
            subnet_config: SubnetConfig::default(),
119
        }
120
    }
121

            
122
    // Construct a test network to exercise the various cases in this crate.
123
    pub(crate) fn testnet() -> NetDir {
124
        tor_netdir::testnet::construct_custom_netdir(|idx, node, _| {
125
            if idx % 7 == 0 {
126
                node.rs.clear_flags(RelayFlag::Fast);
127
            }
128
            if idx % 5 == 0 {
129
                node.rs.clear_flags(RelayFlag::Stable);
130
            };
131
        })
132
        .unwrap()
133
        .unwrap_if_sufficient()
134
        .unwrap()
135
    }
136
}