1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use super::TorPath;
use crate::{DirInfo, Error, Result};
use tor_error::bad_api_usage;
use tor_guardmgr::{GuardMgr, GuardMonitor, GuardUsable};
use tor_netdir::{Relay, WeightRole};
use tor_rtcompat::Runtime;
use rand::Rng;
#[non_exhaustive]
pub struct DirPathBuilder {}
impl Default for DirPathBuilder {
fn default() -> Self {
Self::new()
}
}
impl DirPathBuilder {
pub fn new() -> Self {
DirPathBuilder {}
}
pub fn pick_path<'a, R: Rng, RT: Runtime>(
&self,
rng: &mut R,
netdir: DirInfo<'a>,
guards: Option<&GuardMgr<RT>>,
) -> Result<(TorPath<'a>, Option<GuardMonitor>, Option<GuardUsable>)> {
match (netdir, guards) {
(dirinfo, Some(guardmgr)) => {
let netdir = match dirinfo {
DirInfo::Directory(netdir) => {
guardmgr.update_network(netdir);
Some(netdir)
}
_ => None,
};
let guard_usage = tor_guardmgr::GuardUsageBuilder::default()
.kind(tor_guardmgr::GuardUsageKind::OneHopDirectory)
.build()
.expect("Unable to build directory guard usage");
let (guard, mon, usable) = guardmgr.select_guard(guard_usage, netdir)?;
return Ok((TorPath::new_one_hop_owned(&guard), Some(mon), Some(usable)));
}
(DirInfo::Fallbacks(f), None) => {
let relay = f.choose(rng)?;
return Ok((TorPath::new_fallback_one_hop(relay), None, None));
}
(DirInfo::Directory(netdir), None) => {
let relay = netdir.pick_relay(rng, WeightRole::BeginDir, Relay::is_dir_cache);
if let Some(r) = relay {
return Ok((TorPath::new_one_hop(r), None, None));
}
}
(DirInfo::Nothing, None) => {
return Err(bad_api_usage!(
"Tried to build a one hop path with no directory, fallbacks, or guard manager"
)
.into());
}
}
Err(Error::NoPath(
"No relays found for use as directory cache".into(),
))
}
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
#![allow(clippy::clone_on_copy)]
use super::*;
use crate::path::assert_same_path_when_owned;
use crate::test::OptDummyGuardMgr;
use std::collections::HashSet;
use tor_basic_utils::test_rng::testing_rng;
use tor_guardmgr::fallback::{FallbackDir, FallbackList};
use tor_linkspec::ChanTarget;
use tor_netdir::testnet;
#[test]
fn dirpath_relay() {
let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
let mut rng = testing_rng();
let dirinfo = (&netdir).into();
let guards: OptDummyGuardMgr<'_> = None;
for _ in 0..1000 {
let p = DirPathBuilder::default().pick_path(&mut rng, dirinfo, guards);
let (p, _, _) = p.unwrap();
assert!(p.exit_relay().is_none());
assert_eq!(p.len(), 1);
assert_same_path_when_owned(&p);
if let crate::path::TorPathInner::OneHop(r) = p.inner {
assert!(r.is_dir_cache());
} else {
panic!("Generated the wrong kind of path.");
}
}
}
#[test]
fn dirpath_fallback() {
let fb_owned = vec![
{
let mut bld = FallbackDir::builder();
bld.rsa_identity([0x01; 20].into())
.ed_identity([0x01; 32].into())
.orports()
.push("127.0.0.1:9000".parse().unwrap());
bld.build().unwrap()
},
{
let mut bld = FallbackDir::builder();
bld.rsa_identity([0x03; 20].into())
.ed_identity([0x03; 32].into())
.orports()
.push("127.0.0.1:9003".parse().unwrap());
bld.build().unwrap()
},
];
let fb: FallbackList = fb_owned.clone().into();
let dirinfo = (&fb).into();
let mut rng = testing_rng();
let guards: OptDummyGuardMgr<'_> = None;
for _ in 0..10 {
let p = DirPathBuilder::default().pick_path(&mut rng, dirinfo, guards);
let (p, _, _) = p.unwrap();
assert!(p.exit_relay().is_none());
assert_eq!(p.len(), 1);
assert_same_path_when_owned(&p);
if let crate::path::TorPathInner::FallbackOneHop(f) = p.inner {
assert!(f == &fb_owned[0] || f == &fb_owned[1]);
} else {
panic!("Generated the wrong kind of path.");
}
}
}
#[test]
fn dirpath_no_fallbacks() {
let fb = FallbackList::from([]);
let dirinfo = DirInfo::Fallbacks(&fb);
let mut rng = testing_rng();
let guards: OptDummyGuardMgr<'_> = None;
let err = DirPathBuilder::default().pick_path(&mut rng, dirinfo, guards);
dbg!(err.as_ref().err());
assert!(matches!(
err,
Err(Error::Guard(
tor_guardmgr::PickGuardError::AllFallbacksDown { .. }
))
));
}
#[test]
fn dirpath_with_guards() {
tor_rtcompat::test_with_all_runtimes!(|rt| async move {
let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
let mut rng = testing_rng();
let dirinfo = (&netdir).into();
let statemgr = tor_persist::TestingStateMgr::new();
let guards = tor_guardmgr::GuardMgr::new(rt.clone(), statemgr, [].into()).unwrap();
guards.update_network(&netdir);
let mut distinct_guards = HashSet::new();
for _ in 0..40 {
let (path, mon, usable) = DirPathBuilder::new()
.pick_path(&mut rng, dirinfo, Some(&guards))
.unwrap();
if let crate::path::TorPathInner::OwnedOneHop(relay) = path.inner {
distinct_guards.insert(relay.ed_identity().clone());
mon.unwrap().succeeded();
assert!(usable.unwrap().await.unwrap());
} else {
panic!("Generated the wrong kind of path.");
}
}
assert_eq!(
distinct_guards.len(),
netdir.params().guard_dir_use_parallelism.get() as usize
);
});
}
}