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
// TODO: add to the list maintained by maint/add_warning once it's available on all crates
53
#![warn(unnameable_types)]
54

            
55
mod config;
56
mod restriction;
57
mod selector;
58
mod target_port;
59
mod usage;
60

            
61
pub use config::RelaySelectionConfig;
62
pub use restriction::{RelayExclusion, RelayRestriction};
63
pub use selector::{RelaySelector, SelectionInfo};
64
pub use target_port::TargetPort;
65
pub use usage::RelayUsage;
66

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

            
77
/// Helper module for our tests.
78
#[cfg(test)]
79
pub(crate) mod testing {
80
    // @@ begin test lint list maintained by maint/add_warning @@
81
    #![allow(clippy::bool_assert_comparison)]
82
    #![allow(clippy::clone_on_copy)]
83
    #![allow(clippy::dbg_macro)]
84
    #![allow(clippy::mixed_attributes_style)]
85
    #![allow(clippy::print_stderr)]
86
    #![allow(clippy::print_stdout)]
87
    #![allow(clippy::single_char_pattern)]
88
    #![allow(clippy::unwrap_used)]
89
    #![allow(clippy::unchecked_time_subtraction)]
90
    #![allow(clippy::useless_vec)]
91
    #![allow(clippy::needless_pass_by_value)]
92
    #![allow(clippy::string_slice)] // See arti#2571
93
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
94

            
95
    use crate::{LowLevelRelayPredicate, RelaySelectionConfig};
96
    use std::collections::HashSet;
97
    use std::sync::LazyLock;
98
    use tor_netdir::{NetDir, Relay, SubnetConfig};
99
    use tor_netdoc::types::relay_flags::RelayFlag;
100

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

            
120
    /// Return a basic configuration.
121
    pub(crate) fn cfg() -> RelaySelectionConfig<'static> {
122
        static STABLE_PORTS: LazyLock<HashSet<u16>> = LazyLock::new(|| [22].into_iter().collect());
123
        RelaySelectionConfig {
124
            long_lived_ports: &STABLE_PORTS,
125
            subnet_config: SubnetConfig::default(),
126
        }
127
    }
128

            
129
    // Construct a test network to exercise the various cases in this crate.
130
    pub(crate) fn testnet() -> NetDir {
131
        tor_netdir::testnet::construct_custom_netdir(|idx, node, _| {
132
            if idx % 7 == 0 {
133
                node.rs.clear_flags(RelayFlag::Fast);
134
            }
135
            if idx % 5 == 0 {
136
                node.rs.clear_flags(RelayFlag::Stable);
137
            };
138
        })
139
        .unwrap()
140
        .unwrap_if_sufficient()
141
        .unwrap()
142
    }
143
}