tor_netdir/
testnet.rs

1//! Support for unit tests, in this crate and elsewhere.
2//!
3//! This module is only enabled when the `testing` feature is enabled.
4//!
5//! It is not covered by semver for the `tor-netdir` crate: see notes
6//! on [`construct_network`].
7//!
8//! # Panics
9//!
10//! These functions can panic on numerous possible internal failures:
11//! only use these functions for testing.
12
13#![allow(clippy::unwrap_used)]
14
15use crate::{MdReceiver, PartialNetDir};
16use std::iter;
17use std::net::SocketAddr;
18use std::time::{Duration, SystemTime};
19#[cfg(feature = "geoip")]
20use tor_geoip::GeoipDb;
21use tor_netdoc::doc::microdesc::{Microdesc, MicrodescBuilder};
22use tor_netdoc::doc::netstatus::{Lifetime, MdRouterStatusBuilder, RelayFlags, RelayWeight};
23use tor_netdoc::doc::netstatus::{MdConsensus, MdConsensusBuilder};
24
25pub use tor_netdoc::{BuildError, BuildResult};
26
27/// A set of builder objects for a single node.
28#[derive(Debug, Clone)]
29#[non_exhaustive]
30pub struct NodeBuilders {
31    /// Builds a routerstatus for a single node.
32    ///
33    /// Adjust fields in this builder to change the node's properties.
34    pub rs: MdRouterStatusBuilder,
35
36    /// Builds a microdescriptor for a single node.
37    ///
38    /// Adjust fields in this builder in order to change the node's
39    /// properties.
40    pub md: MicrodescBuilder,
41
42    /// Set this value to `true` to omit the microdesc from the network.
43    pub omit_md: bool,
44
45    /// Set this value to `true` to omit the routerdesc from the network.
46    pub omit_rs: bool,
47}
48
49/// Helper: a customization function that does nothing.
50pub fn simple_net_func(_idx: usize, _nb: &mut NodeBuilders, _bld: &mut MdConsensusBuilder) {}
51
52/// As [`construct_network()`], but return a [`PartialNetDir`].
53pub fn construct_netdir() -> PartialNetDir {
54    construct_custom_netdir(simple_net_func).expect("failed to build default testing netdir")
55}
56
57/// As [`construct_custom_network()`], but return a [`PartialNetDir`],
58/// and allow network parameter customisation.
59pub fn construct_custom_netdir_with_params<F, P, PK>(
60    func: F,
61    params: P,
62    lifetime: Option<Lifetime>,
63) -> BuildResult<PartialNetDir>
64where
65    F: FnMut(usize, &mut NodeBuilders, &mut MdConsensusBuilder),
66    P: IntoIterator<Item = (PK, i32)>,
67    PK: Into<String>,
68{
69    construct_custom_netdir_with_params_inner(
70        func,
71        params,
72        lifetime,
73        #[cfg(feature = "geoip")]
74        None,
75    )
76}
77
78/// Implementation of `construct_custom_netdir_with_params`, written this way to avoid
79/// the GeoIP argument crossing a crate API boundary.
80fn construct_custom_netdir_with_params_inner<F, P, PK>(
81    func: F,
82    params: P,
83    lifetime: Option<Lifetime>,
84    #[cfg(feature = "geoip")] geoip_db: Option<&GeoipDb>,
85) -> BuildResult<PartialNetDir>
86where
87    F: FnMut(usize, &mut NodeBuilders, &mut MdConsensusBuilder),
88    P: IntoIterator<Item = (PK, i32)>,
89    PK: Into<String>,
90{
91    let (consensus, microdescs) = construct_custom_network(func, lifetime)?;
92    #[cfg(feature = "geoip")]
93    let mut dir = if let Some(db) = geoip_db {
94        PartialNetDir::new_with_geoip(consensus, Some(&params.into_iter().collect()), db)
95    } else {
96        PartialNetDir::new(consensus, Some(&params.into_iter().collect()))
97    };
98    #[cfg(not(feature = "geoip"))]
99    let mut dir = PartialNetDir::new(consensus, Some(&params.into_iter().collect()));
100    for md in microdescs {
101        dir.add_microdesc(md);
102    }
103
104    Ok(dir)
105}
106
107/// As [`construct_custom_network()`], but return a [`PartialNetDir`].
108pub fn construct_custom_netdir<F>(func: F) -> BuildResult<PartialNetDir>
109where
110    F: FnMut(usize, &mut NodeBuilders, &mut MdConsensusBuilder),
111{
112    construct_custom_netdir_with_params(func, iter::empty::<(&str, _)>(), None)
113}
114
115#[cfg(feature = "geoip")]
116/// As [`construct_custom_netdir()`], but with a `GeoipDb`.
117pub fn construct_custom_netdir_with_geoip<F>(func: F, db: &GeoipDb) -> BuildResult<PartialNetDir>
118where
119    F: FnMut(usize, &mut NodeBuilders, &mut MdConsensusBuilder),
120{
121    construct_custom_netdir_with_params_inner(func, iter::empty::<(&str, _)>(), None, Some(db))
122}
123
124/// As [`construct_custom_network`], but do not require a
125/// customization function.
126pub fn construct_network() -> BuildResult<(MdConsensus, Vec<Microdesc>)> {
127    construct_custom_network(simple_net_func, None)
128}
129
130/// Build a fake network with enough information to enable some basic
131/// tests.
132///
133/// By default, the constructed network will contain 40 relays,
134/// numbered 0 through 39. They will have with RSA and Ed25519
135/// identity fingerprints set to 0x0000...00 through 0x2727...27.
136/// Each pair of relays is in a family with one another: 0x00..00 with
137/// 0x01..01, and so on.
138///
139/// All relays are marked as usable.  The first ten are marked with no
140/// additional flags.  The next ten are marked with the exit flag.
141/// The next ten are marked with the guard flag.  The last ten are
142/// marked with the exit _and_ guard flags.
143///
144/// TAP and Ntor onion keys are present, but unusable.
145///
146/// Odd-numbered exit relays are set to allow ports 80 and 443 on
147/// IPv4.  Even-numbered exit relays are set to allow ports 1-65535
148/// on IPv4.  No exit relays are marked to support IPv6.
149///
150/// Even-numbered relays support the `DirCache=2` protocol.
151///
152/// Every relay is given a measured weight based on its position
153/// within its group of ten.  The weights for the ten relays in each
154/// group are: 1000, 2000, 3000, ... 10000.  There is no additional
155/// flag-based bandwidth weighting.
156///
157/// The consensus is declared as using method 34, and as being valid for
158/// one day (in realtime) after the current `SystemTime`.
159///
160/// # Customization
161///
162/// Before each relay is added to the consensus or the network, it is
163/// passed through the provided filtering function.  This function
164/// receives as its arguments the current index (in range 0..40), a
165/// [`MdRouterStatusBuilder`], and a [`MicrodescBuilder`].  If it
166/// returns a `RouterStatusBuilder`, the corresponding router status
167/// is added to the consensus.  If it returns a `MicrodescBuilder`,
168/// the corresponding microdescriptor is added to the vector of
169/// microdescriptor.
170///
171/// # Notes for future expansion
172///
173/// _Resist the temptation to make unconditional changes to this
174/// function._ If the network generated by this function gets more and
175/// more complex, then it will become harder and harder over time to
176/// make it support new test cases and new behavior, and eventually
177/// we'll have to throw the whole thing away.  (We ran into this
178/// problem with Tor's unit tests.)
179///
180/// Instead, refactor this function so that it takes a
181/// description of what kind of network to build, and then builds it from
182/// that description.
183pub fn construct_custom_network<F>(
184    mut func: F,
185    lifetime: Option<Lifetime>,
186) -> BuildResult<(MdConsensus, Vec<Microdesc>)>
187where
188    F: FnMut(usize, &mut NodeBuilders, &mut MdConsensusBuilder),
189{
190    let f = RelayFlags::RUNNING
191        | RelayFlags::VALID
192        | RelayFlags::V2DIR
193        | RelayFlags::FAST
194        | RelayFlags::STABLE;
195    // define 4 groups of flags
196    let flags = [
197        f | RelayFlags::HSDIR,
198        f | RelayFlags::EXIT,
199        f | RelayFlags::GUARD,
200        f | RelayFlags::EXIT | RelayFlags::GUARD,
201    ];
202
203    let lifetime = lifetime.map(Ok).unwrap_or_else(|| {
204        let now = SystemTime::now();
205        let one_day = Duration::new(86400, 0);
206
207        Lifetime::new(now, now + one_day / 2, now + one_day)
208    })?;
209
210    let mut bld = MdConsensus::builder();
211    bld.consensus_method(34)
212        .lifetime(lifetime)
213        .param("bwweightscale", 1)
214        .weights("".parse()?);
215
216    let mut microdescs = Vec::new();
217    for idx in 0..40_u8 {
218        // Each relay gets a couple of no-good onion keys.
219        // Its identity fingerprints are set to `idx`, repeating.
220        // They all get the same address.
221        let flags = flags[(idx / 10) as usize];
222        let policy = if flags.contains(RelayFlags::EXIT) {
223            if idx % 2 == 1 {
224                "accept 80,443"
225            } else {
226                "accept 1-65535"
227            }
228        } else {
229            "reject 1-65535"
230        };
231        // everybody is family with the adjacent relay.
232        let fam_id = [idx ^ 1; 20];
233        let family = hex::encode(fam_id);
234
235        let mut md_builder = Microdesc::builder();
236        md_builder
237            .ntor_key((*b"----nothing in dirmgr uses this-").into())
238            .ed25519_id([idx; 32].into())
239            .family(family.parse().unwrap())
240            .parse_ipv4_policy(policy)
241            .unwrap();
242        let protocols = if idx % 2 == 0 {
243            // even-numbered relays are dircaches.
244            "DirCache=2".parse().unwrap()
245        } else {
246            "".parse().unwrap()
247        };
248        let weight = RelayWeight::Measured(1000 * u32::from(idx % 10 + 1));
249        let mut rs_builder = bld.rs();
250        rs_builder
251            .identity([idx; 20].into())
252            .add_or_port(SocketAddr::from(([idx % 5, 0, 0, 3], 9001)))
253            .protos(protocols)
254            .set_flags(flags)
255            .weight(weight);
256
257        let mut node_builders = NodeBuilders {
258            rs: rs_builder,
259            md: md_builder,
260            omit_rs: false,
261            omit_md: false,
262        };
263
264        func(idx as usize, &mut node_builders, &mut bld);
265
266        let md = node_builders.md.testing_md()?;
267        let md_digest = *md.digest();
268        if !node_builders.omit_md {
269            microdescs.push(md);
270        }
271
272        if !node_builders.omit_rs {
273            node_builders
274                .rs
275                .doc_digest(md_digest)
276                .build_into(&mut bld)?;
277        }
278    }
279
280    let consensus = bld.testing_consensus()?;
281
282    Ok((consensus, microdescs))
283}
284
285#[cfg(test)]
286mod test {
287    // @@ begin test lint list maintained by maint/add_warning @@
288    #![allow(clippy::bool_assert_comparison)]
289    #![allow(clippy::clone_on_copy)]
290    #![allow(clippy::dbg_macro)]
291    #![allow(clippy::mixed_attributes_style)]
292    #![allow(clippy::print_stderr)]
293    #![allow(clippy::print_stdout)]
294    #![allow(clippy::single_char_pattern)]
295    #![allow(clippy::unwrap_used)]
296    #![allow(clippy::unchecked_duration_subtraction)]
297    #![allow(clippy::useless_vec)]
298    #![allow(clippy::needless_pass_by_value)]
299    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
300    use super::*;
301    #[test]
302    fn try_with_function() {
303        let mut val = 0_u32;
304        let _net = construct_custom_netdir(|_idx, _nb, _bld| {
305            val += 1;
306        });
307        assert_eq!(val, 40);
308    }
309}