Commit 89de6227 authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

A little testing and refactgoring in tor-circmgr.

parent 8f8a2cf4
Loading
Loading
Loading
Loading
+0 −12
Original line number Diff line number Diff line
@@ -22,18 +22,6 @@ impl mgr::AbstractCirc for tor_proto::circuit::ClientCirc {
    }
}

impl mgr::AbstractSpec for SupportedCircUsage {
    type Usage = TargetCircUsage;

    fn supports(&self, usage: &TargetCircUsage) -> bool {
        self.contains(usage)
    }

    fn restrict_mut(&mut self, _usage: &TargetCircUsage) -> Result<()> {
        Ok(())
    }
}

/// The information generated by circuit planning, and used to build a
/// circuit.
pub(crate) struct Plan {
+5 −0
Original line number Diff line number Diff line
@@ -183,10 +183,15 @@ mod test {
        snd_r.send(5_usize).await.unwrap();
        snd_l.send(3_usize).await.unwrap();

        assert!(!s.is_terminated());
        drop(snd_r);

        assert_eq!(s.next().await, Some((L, 3)));
        assert_eq!(s.next().await, Some((R, 5)));

        drop(snd_l);
        assert_eq!(s.next().await, None);

        assert!(s.is_terminated());
    }
}
+18 −13
Original line number Diff line number Diff line
@@ -118,11 +118,10 @@ impl TargetCircUsage {
    }
}

impl SupportedCircUsage {
    /// Return true if this usage "contains" `target` -- in other words,
    /// if any circuit built for this purpose is also usable for the
    /// purpose of `target`.
    pub(crate) fn contains(&self, target: &TargetCircUsage) -> bool {
impl crate::mgr::AbstractSpec for SupportedCircUsage {
    type Usage = TargetCircUsage;

    fn supports(&self, target: &TargetCircUsage) -> bool {
        use SupportedCircUsage::*;
        match (self, target) {
            (Dir, TargetCircUsage::Dir) => true,
@@ -130,6 +129,10 @@ impl SupportedCircUsage {
            (_, _) => false,
        }
    }

    fn restrict_mut(&mut self, _usage: &TargetCircUsage) -> Result<()> {
        Ok(())
    }
}

#[cfg(test)]
@@ -180,6 +183,7 @@ mod test {

    #[test]
    fn usage_ops() {
        use crate::mgr::AbstractSpec;
        // Make an exit-policy object that allows web on IPv4 and
        // smtp on IPv6.
        let policy = ExitPolicy {
@@ -196,17 +200,18 @@ mod test {
            TargetCircUsage::Exit(vec![TargetPort::ipv4(80), TargetPort::ipv6(23)]);
        let targ_999_v6 = TargetCircUsage::Exit(vec![TargetPort::ipv6(999)]);

        assert!(supp_dir.contains(&targ_dir));
        assert!(!supp_dir.contains(&targ_80_v4));
        assert!(!supp_exit.contains(&targ_dir));
        assert!(supp_exit.contains(&targ_80_v4));
        assert!(supp_exit.contains(&targ_80_23_mixed));
        assert!(!supp_exit.contains(&targ_80_23_v4));
        assert!(!supp_exit.contains(&targ_999_v6));
        assert!(supp_dir.supports(&targ_dir));
        assert!(!supp_dir.supports(&targ_80_v4));
        assert!(!supp_exit.supports(&targ_dir));
        assert!(supp_exit.supports(&targ_80_v4));
        assert!(supp_exit.supports(&targ_80_23_mixed));
        assert!(!supp_exit.supports(&targ_80_23_v4));
        assert!(!supp_exit.supports(&targ_999_v6));
    }

    #[test]
    fn buildpath() {
        use crate::mgr::AbstractSpec;
        let mut rng = rand::thread_rng();
        let netdir = testnet::construct_netdir();
        let di = (&netdir).into();
@@ -221,7 +226,7 @@ mod test {
        let exit_usage = TargetCircUsage::Exit(vec![TargetPort::ipv4(995)]);
        let (p_exit, u_exit) = exit_usage.build_path(&mut rng, di).unwrap();
        assert!(matches!(u_exit, SupportedCircUsage::Exit(_)));
        assert!(u_exit.contains(&exit_usage));
        assert!(u_exit.supports(&exit_usage));
        assert_eq!(p_exit.len(), 3);
    }
}