Commit bf251402 authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

More tests and a little code-golf for coverage on tor-circmgr

parent 133d000c
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -63,12 +63,10 @@ impl<R: Runtime> crate::mgr::AbstractCircBuilder for Builder<R> {
        let mut rng = rand::thread_rng();
        let (path, final_spec) = usage.build_path(&mut rng, dir)?;

        let owned_path = (&path).try_into()?;
        let params = dir.circ_params();
        let plan = Plan {
            final_spec: final_spec.clone(),
            path: owned_path,
            params,
            path: (&path).try_into()?,
            params: dir.circ_params(),
        };

        Ok((plan, final_spec))
+16 −0
Original line number Diff line number Diff line
@@ -221,6 +221,7 @@ mod test {
        assert_eq!(p1.extend_by_ed25519_id(), false);
        assert_eq!(p1.initial_send_window(), 1000);

        // Now try with a directory and configured parameters.
        let (consensus, microdescs) = tor_netdir::testnet::construct_network();
        let mut params = NetParams::default();
        params.set("circwindow".into(), 100);
@@ -234,5 +235,20 @@ mod test {
        let p2 = di.circ_params();
        assert_eq!(p2.initial_send_window(), 100);
        assert_eq!(p2.extend_by_ed25519_id(), true);

        // Now try with a bogus circwindow value.
        let (consensus, microdescs) = tor_netdir::testnet::construct_network();
        let mut params = NetParams::default();
        params.set("circwindow".into(), 100_000);
        params.set("ExtendByEd25519ID".into(), 1);
        let mut dir = PartialNetDir::new(consensus, Some(&params));
        for m in microdescs {
            dir.add_microdesc(m);
        }
        let netdir = dir.unwrap_if_sufficient().unwrap();
        let di: DirInfo<'_> = (&netdir).into();
        let p2 = di.circ_params();
        assert_eq!(p2.initial_send_window(), 1000); // Not 100_000
        assert_eq!(p2.extend_by_ed25519_id(), true);
    }
}
+9 −10
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ enum TorPathInner<'a> {
    /// A single-hop path for use with a directory cache, when we don't have
    /// a consensus.
    FallbackOneHop(&'a FallbackDir),
    /// A multi-hop path, containing one or more paths.
    /// A multi-hop path, containing one or more relays.
    Path(Vec<Relay<'a>>),
}

@@ -148,7 +148,7 @@ impl OwnedPath {
        RNG: Rng + CryptoRng,
        RT: Runtime,
    {
        let chan = self.get_channel(chanmgr).await?;
        let chan = chanmgr.get_or_launch(self.first_hop()?).await?;
        let (pending_circ, reactor) = chan.new_circ(rng).await?;

        runtime.spawn(async {
@@ -183,13 +183,6 @@ impl OwnedPath {
            OwnedPath::Normal(p) => Ok(&p[0]),
        }
    }

    /// Internal: get or create a channel for the first hop of a path.
    async fn get_channel<R: Runtime>(&self, chanmgr: &ChanMgr<R>) -> Result<Arc<Channel>> {
        let first_hop = self.first_hop()?;
        let channel = chanmgr.get_or_launch(first_hop).await?;
        Ok(channel)
    }
}

/// For testing: make sure that `path` is the same when it is an owned
@@ -198,19 +191,25 @@ impl OwnedPath {
fn assert_same_path_when_owned(path: &TorPath<'_>) {
    let owned: OwnedPath = path.try_into().unwrap();

    match (owned, &path.inner) {
    match (&owned, &path.inner) {
        (OwnedPath::ChannelOnly(c), TorPathInner::FallbackOneHop(f)) => {
            assert_eq!(c.ed_identity(), f.ed_identity());
            assert_eq!(c.ed_identity(), owned.first_hop().unwrap().ed_identity());
        }
        (OwnedPath::Normal(p), TorPathInner::OneHop(h)) => {
            assert_eq!(p.len(), 1);
            assert_eq!(p[0].ed_identity(), h.ed_identity());
            assert_eq!(p[0].ed_identity(), owned.first_hop().unwrap().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());
            }
            assert_eq!(
                p1[0].ed_identity(),
                owned.first_hop().unwrap().ed_identity()
            );
        }
        (_, _) => {
            panic!("Mismatched path types.")
+20 −0
Original line number Diff line number Diff line
@@ -136,4 +136,24 @@ mod test {
            }
        }
    }

    #[test]
    fn empty_path() {
        // This shouldn't actually be constructable IRL, but let's test to
        // make sure our code can handle it.
        let bogus_path = TorPath {
            inner: TorPathInner::Path(vec![]),
        };

        assert!(bogus_path.exit_relay().is_none());
        assert!(bogus_path.exit_policy().is_none());
        assert_eq!(bogus_path.len(), 0);

        let owned: Result<OwnedPath> = (&bogus_path).try_into();
        assert!(owned.is_err());

        // This should also be unconstructable.
        let owned_bogus = OwnedPath::Normal(vec![]);
        assert!(owned_bogus.first_hop().is_err());
    }
}