use crate::{Error, Result};
use tor_cell::chancell::msg::{self as chanmsg, ChanMsg};
#[cfg_attr(docsrs, doc(cfg(feature = "testing")))]
#[derive(Debug)]
#[allow(unreachable_pub)] #[allow(clippy::exhaustive_enums)]
pub enum CreateResponse {
Destroy(chanmsg::Destroy),
CreatedFast(chanmsg::CreatedFast),
Created2(chanmsg::Created2),
}
impl TryFrom<ChanMsg> for CreateResponse {
type Error = crate::Error;
fn try_from(m: ChanMsg) -> Result<CreateResponse> {
match m {
ChanMsg::Destroy(m) => Ok(CreateResponse::Destroy(m)),
ChanMsg::CreatedFast(m) => Ok(CreateResponse::CreatedFast(m)),
ChanMsg::Created2(m) => Ok(CreateResponse::Created2(m)),
_ => Err(Error::ChanProto(format!(
"Got a {} in response to circuit creation",
m.cmd()
))),
}
}
}
#[derive(Debug)]
#[allow(unreachable_pub)] pub enum ClientCircChanMsg {
Relay(chanmsg::Relay),
Destroy(chanmsg::Destroy),
}
impl TryFrom<ChanMsg> for ClientCircChanMsg {
type Error = crate::Error;
fn try_from(m: ChanMsg) -> Result<ClientCircChanMsg> {
match m {
ChanMsg::Destroy(m) => Ok(ClientCircChanMsg::Destroy(m)),
ChanMsg::Relay(m) => Ok(ClientCircChanMsg::Relay(m)),
_ => Err(Error::ChanProto(format!(
"Got a {} cell on an open circuit",
m.cmd()
))),
}
}
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn create_response() {
use tor_cell::chancell::msg::{self, ChanMsg};
fn good(m: ChanMsg) {
assert!(CreateResponse::try_from(m).is_ok());
}
fn bad(m: ChanMsg) {
assert!(CreateResponse::try_from(m).is_err());
}
good(msg::Destroy::new(2.into()).into());
good(msg::CreatedFast::new(&b"this offer is unrepeatable"[..]).into());
good(msg::Created2::new(&b"guaranteed guaranteed"[..]).into());
bad(msg::CreateFast::new(&b"for a lifetime or more"[..]).into());
bad(msg::Versions::new([1, 2, 3]).unwrap().into());
}
#[test]
fn client_circ_chan_msg() {
use tor_cell::chancell::msg::{self, ChanMsg};
fn good(m: ChanMsg) {
assert!(ClientCircChanMsg::try_from(m).is_ok());
}
fn bad(m: ChanMsg) {
assert!(ClientCircChanMsg::try_from(m).is_err());
}
good(msg::Destroy::new(2.into()).into());
bad(msg::CreatedFast::new(&b"guaranteed in this world"[..]).into());
bad(msg::Created2::new(&b"and the next"[..]).into());
good(msg::Relay::new(&b"guaranteed guaranteed"[..]).into());
bad(msg::ChanMsg::RelayEarly(msg::Relay::new(
&b"for the world and its mother"[..],
)));
bad(msg::Versions::new([1, 2, 3]).unwrap().into());
}
}