tor-linkspec: Remove redundant method; add more tests.

The redundant method was a `to_owned` that probably shouldn't have
been called that.  It was only used in one place.

The tests should get tor-linkspec's line coverage up above 90%.
This commit is contained in:
Nick Mathewson 2021-12-04 15:42:53 -05:00
parent 214c251e41
commit 66d5f73b9c
4 changed files with 56 additions and 13 deletions

View File

@ -51,7 +51,7 @@ mod mgr;
#[cfg(test)]
mod testing;
use tor_linkspec::ChanTarget;
use tor_linkspec::{ChanTarget, OwnedChanTarget};
use tor_proto::channel::Channel;
pub use err::Error;
@ -88,7 +88,7 @@ impl<R: Runtime> ChanMgr<R> {
/// or fail depending on its outcome.
pub async fn get_or_launch<T: ChanTarget + ?Sized>(&self, target: &T) -> Result<Arc<Channel>> {
let ed_identity = target.ed_identity();
let targetinfo = target.to_owned();
let targetinfo = OwnedChanTarget::from_chan_target(target);
let chan = self.mgr.get_or_launch(*ed_identity, targetinfo).await?;
// Double-check the match to make sure that the RSA identity is

View File

@ -124,8 +124,10 @@ impl CircTarget for OwnedCircTarget {
mod test {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn targetinfo() {
#[allow(clippy::redundant_clone)]
fn chan_target() {
let ti = OwnedChanTarget::new(
vec!["127.0.0.1:11".parse().unwrap()],
[42; 32].into(),
@ -136,5 +138,28 @@ mod test {
assert_eq!(ti.addrs(), ti2.addrs());
assert_eq!(ti.ed_identity(), ti2.ed_identity());
assert_eq!(ti.rsa_identity(), ti2.rsa_identity());
assert_eq!(format!("{:?}", ti), format!("{:?}", ti2));
assert_eq!(format!("{:?}", ti), format!("{:?}", ti.clone()));
}
#[test]
#[allow(clippy::redundant_clone)]
fn circ_target() {
let ch = OwnedChanTarget::new(
vec!["127.0.0.1:11".parse().unwrap()],
[42; 32].into(),
[45; 20].into(),
);
let ct = OwnedCircTarget::new(ch.clone(), [99; 32].into(), "FlowCtrl=7".parse().unwrap());
assert_eq!(ct.addrs(), ch.addrs());
assert_eq!(ct.rsa_identity(), ch.rsa_identity());
assert_eq!(ct.ed_identity(), ch.ed_identity());
assert_eq!(ct.ntor_onion_key().as_bytes(), &[99; 32]);
assert_eq!(&ct.protovers().to_string(), "FlowCtrl=7");
let ct2 = OwnedCircTarget::from_circ_target(&ct);
assert_eq!(format!("{:?}", ct), format!("{:?}", ct2));
assert_eq!(format!("{:?}", ct), format!("{:?}", ct.clone()));
}
}

View File

@ -27,12 +27,6 @@ pub trait ChanTarget {
}
/// Return the RSA identity for this relay.
fn rsa_identity(&self) -> &pk::rsa::RsaIdentity;
/// Return a new [`OwnedChanTarget`](crate::OwnedChanTarget)
/// containing a copy of the information in this `ChanTarget`.
fn to_owned(&self) -> crate::OwnedChanTarget {
crate::OwnedChanTarget::from_chan_target(self)
}
}
/// Information about a Tor relay used to extend a circuit to it.
@ -92,9 +86,9 @@ mod test {
}
}
#[test]
fn test_linkspecs() {
let ex = Example {
/// Return an `Example` object, for use in tests below.
fn example() -> Example {
Example {
addrs: vec![
"127.0.0.1:99".parse::<SocketAddr>().unwrap(),
"[::1]:909".parse::<SocketAddr>().unwrap(),
@ -114,8 +108,12 @@ mod test {
726624ec26b3353b10a903a6d0ab1c4c"
)),
pv: tor_protover::Protocols::default(),
};
}
}
#[test]
fn test_linkspecs() {
let ex = example();
let specs = ex.linkspecs();
assert_eq!(4, specs.len());
@ -147,4 +145,21 @@ mod test {
LinkSpec::OrPort("::1".parse::<IpAddr>().unwrap(), 909)
);
}
#[test]
fn key_accessor() {
let ex = example();
// We can get the ed25519 key if it's valid...
let key = ex.ed_identity_key().unwrap();
assert_eq!(&pk::ed25519::Ed25519Identity::from(key), ex.ed_identity());
// Now try an invalid example.
let a = hex!("6d616e79737472696e677361726565646b6579736e6f74746869736f6e654091");
let ex = Example {
ed_id: pk::ed25519::Ed25519Identity::from_bytes(&a).unwrap(),
..ex
};
let key = ex.ed_identity_key();
assert!(key.is_none());
}
}

View File

@ -32,3 +32,6 @@ tor-units: MODIFIED
caret: BREAKING
(Removed unused-but-exported Error type.)
tor-linkspec: BREAKING
(Removed to_owned() on ChanTarget)