Skip to content
Snippets Groups Projects
Commit 1b6cf240 authored by Nick Mathewson's avatar Nick Mathewson :game_die:
Browse files

More tests on circmgr::path.

parent 4e3e1982
No related branches found
No related tags found
No related merge requests found
......@@ -190,3 +190,29 @@ impl OwnedPath {
Ok(channel)
}
}
/// For testing: make sure that `path` is the same when it is an owned
/// path.
#[cfg(test)]
fn assert_same_owned_path(path: &TorPath<'_>) {
let owned: OwnedPath = path.try_into().unwrap();
match (owned, &path.inner) {
(OwnedPath::ChannelOnly(c), TorPathInner::FallbackOneHop(f)) => {
assert_eq!(c.ed_identity(), f.ed_identity());
}
(OwnedPath::Normal(p), TorPathInner::OneHop(h)) => {
assert_eq!(p.len(), 1);
assert_eq!(p[0].ed_identity(), h.ed_identity());
}
(OwnedPath::Normal(p1), TorPathInner::Path(p2)) => {
assert_eq!(p1.len(), p2.len());
for (n1, n2) in p1.iter().zip(p2.iter()) {
assert_eq!(n1.ed_identity(), n2.ed_identity());
}
}
(_, _) => {
panic!("Mismatched path types.")
}
}
}
......@@ -85,7 +85,7 @@ mod test {
.build()
.unwrap(),
];
let dirinfo = DirInfo::Fallbacks(&fb[..]);
let dirinfo = (&fb[..]).into();
let mut rng = rand::thread_rng();
for _ in 0..10 {
......@@ -93,6 +93,7 @@ mod test {
let p = p.unwrap();
assert!(p.exit_relay().is_none());
assert_eq!(p.len(), 1);
assert_same_owned_path(&p);
if let crate::path::TorPathInner::FallbackOneHop(f) = p.inner {
assert!(std::ptr::eq(f, &fb[0]) || std::ptr::eq(f, &fb[1]));
......
......@@ -73,3 +73,64 @@ impl<'a> ExitPathBuilder<'a> {
Ok(TorPath::new_multihop(vec![entry, middle, exit]))
}
}
#[cfg(test)]
mod test {
use super::*;
use tor_netdir::testnet;
fn assert_exit_path_ok<'a>(relays: &[Relay<'a>]) {
assert_eq!(relays.len(), 3);
// TODO: Eventually assert that r1 has Guard, once we enforce that.
let r1 = &relays[0];
let r2 = &relays[1];
let r3 = &relays[2];
assert!(r1.ed_identity() != r2.ed_identity());
assert!(r1.ed_identity() != r3.ed_identity());
assert!(r2.ed_identity() != r3.ed_identity());
assert!(!r1.in_same_family(r2));
assert!(!r1.in_same_family(r3));
assert!(!r2.in_same_family(r3));
}
#[test]
fn by_ports() {
let mut rng = rand::thread_rng();
let netdir = testnet::construct_netdir();
let ports = vec![TargetPort::ipv4(443), TargetPort::ipv4(1119)];
let dirinfo = (&netdir).into();
for _ in 0..1000 {
let path = ExitPathBuilder::from_target_ports(ports.clone())
.pick_path(&mut rng, dirinfo)
.unwrap();
if let TorPathInner::Path(p) = path.inner {
assert_exit_path_ok(&p[..]);
let exit = &p[2];
assert!(exit.ipv4_policy().allows_port(1119));
} else {
panic!("Generated the wrong kind of path");
}
}
let chosen = netdir.by_id(&[0x20; 32].into()).unwrap();
for _ in 0..1000 {
let path = ExitPathBuilder::from_chosen_exit(chosen.clone())
.pick_path(&mut rng, dirinfo)
.unwrap();
if let TorPathInner::Path(p) = path.inner {
assert_exit_path_ok(&p[..]);
let exit = &p[2];
assert_eq!(exit.ed_identity(), chosen.ed_identity());
} else {
panic!("Generated the wrong kind of path");
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment