chanmgr: Require HasRelayIds for AbstractChannel.

This is mostly a testing-only change for now, but soon I'll use it
so we can have IdMap for our channel map.
This commit is contained in:
Nick Mathewson 2022-10-13 11:09:31 -04:00
parent e94cff26e2
commit 71db8ebc2e
2 changed files with 43 additions and 1 deletions

View File

@ -12,6 +12,7 @@ use std::result::Result as StdResult;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tor_error::internal; use tor_error::internal;
use tor_linkspec::HasRelayIds;
use tor_netdir::params::NetParameters; use tor_netdir::params::NetParameters;
use tor_proto::channel::params::ChannelPaddingInstructionsUpdates; use tor_proto::channel::params::ChannelPaddingInstructionsUpdates;
@ -20,7 +21,7 @@ mod map;
/// Trait to describe as much of a /// Trait to describe as much of a
/// [`Channel`](tor_proto::channel::Channel) as `AbstractChanMgr` /// [`Channel`](tor_proto::channel::Channel) as `AbstractChanMgr`
/// needs to use. /// needs to use.
pub(crate) trait AbstractChannel: Clone { pub(crate) trait AbstractChannel: Clone + HasRelayIds {
/// Identity type for the other side of the channel. /// Identity type for the other side of the channel.
type Ident: Hash + Eq + Clone; type Ident: Hash + Eq + Clone;
/// Return this channel's identity. /// Return this channel's identity.
@ -343,6 +344,7 @@ mod test {
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use tor_error::bad_api_usage; use tor_error::bad_api_usage;
use tor_llcrypto::pk::ed25519::Ed25519Identity;
use crate::ChannelUsage as CU; use crate::ChannelUsage as CU;
use tor_rtcompat::{task::yield_now, test_with_one_runtime, Runtime}; use tor_rtcompat::{task::yield_now, test_with_one_runtime, Runtime};
@ -354,6 +356,7 @@ mod test {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct FakeChannel { struct FakeChannel {
ident: u32, ident: u32,
ed_ident: Ed25519Identity,
mood: char, mood: char,
closing: Arc<AtomicBool>, closing: Arc<AtomicBool>,
detect_reuse: Arc<char>, detect_reuse: Arc<char>,
@ -387,6 +390,18 @@ mod test {
fn engage_padding_activities(&self) {} fn engage_padding_activities(&self) {}
} }
impl HasRelayIds for FakeChannel {
fn identity(
&self,
key_type: tor_linkspec::RelayIdType,
) -> Option<tor_linkspec::RelayIdRef<'_>> {
match key_type {
tor_linkspec::RelayIdType::Ed25519 => Some((&self.ed_ident).into()),
_ => None,
}
}
}
impl FakeChannel { impl FakeChannel {
fn start_closing(&self) { fn start_closing(&self) {
self.closing.store(true, Ordering::SeqCst); self.closing.store(true, Ordering::SeqCst);
@ -417,6 +432,11 @@ mod test {
async fn build_channel(&self, target: &Self::BuildSpec) -> Result<FakeChannel> { async fn build_channel(&self, target: &Self::BuildSpec) -> Result<FakeChannel> {
yield_now().await; yield_now().await;
let (ident, mood) = *target; let (ident, mood) = *target;
let ed_ident = {
let mut bytes = [0; 32];
bytes[0..4].copy_from_slice(&ident.to_be_bytes());
bytes.into()
};
match mood { match mood {
// "X" means never connect. // "X" means never connect.
'❌' | '🔥' => return Err(Error::UnusableTarget(bad_api_usage!("emoji"))), '❌' | '🔥' => return Err(Error::UnusableTarget(bad_api_usage!("emoji"))),
@ -428,6 +448,7 @@ mod test {
} }
Ok(FakeChannel { Ok(FakeChannel {
ident, ident,
ed_ident,
mood, mood,
closing: Arc::new(AtomicBool::new(false)), closing: Arc::new(AtomicBool::new(false)),
detect_reuse: Default::default(), detect_reuse: Default::default(),

View File

@ -559,6 +559,7 @@ mod test {
use super::*; use super::*;
use std::sync::Arc; use std::sync::Arc;
use tor_llcrypto::pk::ed25519::Ed25519Identity;
use tor_proto::channel::params::ChannelPaddingInstructionsUpdates; use tor_proto::channel::params::ChannelPaddingInstructionsUpdates;
fn new_test_channel_map<C: AbstractChannel>() -> ChannelMap<C> { fn new_test_channel_map<C: AbstractChannel>() -> ChannelMap<C> {
@ -572,6 +573,7 @@ mod test {
#[derive(Eq, PartialEq, Clone, Debug)] #[derive(Eq, PartialEq, Clone, Debug)]
struct FakeChannel { struct FakeChannel {
ident: &'static str, ident: &'static str,
ed_ident: Ed25519Identity,
usable: bool, usable: bool,
unused_duration: Option<u64>, unused_duration: Option<u64>,
params_update: Option<Arc<ChannelPaddingInstructionsUpdates>>, params_update: Option<Arc<ChannelPaddingInstructionsUpdates>>,
@ -596,9 +598,26 @@ mod test {
} }
fn engage_padding_activities(&self) {} fn engage_padding_activities(&self) {}
} }
impl tor_linkspec::HasRelayIds for FakeChannel {
fn identity(
&self,
key_type: tor_linkspec::RelayIdType,
) -> Option<tor_linkspec::RelayIdRef<'_>> {
match key_type {
tor_linkspec::RelayIdType::Ed25519 => Some((&self.ed_ident).into()),
_ => None,
}
}
}
/// Get a fake ed25519 identity from the first byte of a string.
fn str_to_ed(s: &str) -> Ed25519Identity {
let byte = s.as_bytes()[0];
[byte; 32].into()
}
fn ch(ident: &'static str) -> ChannelState<FakeChannel> { fn ch(ident: &'static str) -> ChannelState<FakeChannel> {
let channel = FakeChannel { let channel = FakeChannel {
ident, ident,
ed_ident: str_to_ed(ident),
usable: true, usable: true,
unused_duration: None, unused_duration: None,
params_update: None, params_update: None,
@ -615,6 +634,7 @@ mod test {
) -> ChannelState<FakeChannel> { ) -> ChannelState<FakeChannel> {
let channel = FakeChannel { let channel = FakeChannel {
ident, ident,
ed_ident: str_to_ed(ident),
usable: true, usable: true,
unused_duration, unused_duration,
params_update: None, params_update: None,
@ -627,6 +647,7 @@ mod test {
fn closed(ident: &'static str) -> ChannelState<FakeChannel> { fn closed(ident: &'static str) -> ChannelState<FakeChannel> {
let channel = FakeChannel { let channel = FakeChannel {
ident, ident,
ed_ident: str_to_ed(ident),
usable: false, usable: false,
unused_duration: None, unused_duration: None,
params_update: None, params_update: None,