use core::fmt;
use crate::auth::AuthToken;
use crate::frame::{Compression, FrameLimits};
use crate::handshake::features::FeatureFlags;
use crate::handshake::limits::NegotiatedLimits;
use crate::handshake::messages::{Hello, RefusalReason, Refused, Welcome};
use crate::handshake::role::{Role, RoleSet};
use crate::ids::SessionId;
use crate::version::{AstrsVersion, MIN_SUPPORTED_PROTOCOL, PROTOCOL_VERSION, negotiate_protocol};
pub const MIN_USABLE_PAYLOAD_BYTES: u64 = 64 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SessionAssignment {
Fresh(SessionId),
Resumed(SessionId),
ResumeUnavailable,
}
impl SessionAssignment {
#[must_use]
pub const fn session_id(&self) -> Option<SessionId> {
match self {
Self::Fresh(id) | Self::Resumed(id) => Some(*id),
Self::ResumeUnavailable => None,
}
}
#[must_use]
pub const fn is_resumed(&self) -> bool {
matches!(self, Self::Resumed(_))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Acceptor {
expected_auth: AuthToken,
require_auth: bool,
features: FeatureFlags,
limits: NegotiatedLimits,
accepted_roles: RoleSet,
connection_limit: Option<u32>,
connections_open: u32,
shutting_down: bool,
min_protocol: u16,
max_protocol: u16,
}
impl Acceptor {
#[must_use]
pub fn new(expected_auth: AuthToken) -> Self {
Self {
expected_auth,
require_auth: true,
features: FeatureFlags::EMPTY,
limits: NegotiatedLimits::new(),
accepted_roles: RoleSet::ALL,
connection_limit: None,
connections_open: 0,
shutting_down: false,
min_protocol: MIN_SUPPORTED_PROTOCOL,
max_protocol: PROTOCOL_VERSION,
}
}
#[must_use]
pub fn without_auth() -> Self {
Self {
require_auth: false,
..Self::new(AuthToken::ZERO)
}
}
#[must_use]
pub const fn with_features(mut self, features: FeatureFlags) -> Self {
self.features = features;
self
}
#[must_use]
pub const fn with_limits(mut self, limits: NegotiatedLimits) -> Self {
self.limits = limits;
self
}
#[must_use]
pub const fn with_accepted_roles(mut self, roles: RoleSet) -> Self {
self.accepted_roles = roles;
self
}
#[must_use]
pub const fn with_connection_limit(mut self, limit: Option<u32>) -> Self {
self.connection_limit = limit;
self
}
#[must_use]
pub const fn with_connections_open(mut self, open: u32) -> Self {
self.connections_open = open;
self
}
#[must_use]
pub const fn with_shutting_down(mut self, shutting_down: bool) -> Self {
self.shutting_down = shutting_down;
self
}
#[must_use]
pub const fn with_protocol_range(mut self, min: u16, max: u16) -> Self {
self.min_protocol = if min < MIN_SUPPORTED_PROTOCOL {
MIN_SUPPORTED_PROTOCOL
} else {
min
};
self.max_protocol = if max > PROTOCOL_VERSION {
PROTOCOL_VERSION
} else {
max
};
if self.min_protocol > self.max_protocol {
self.min_protocol = self.max_protocol;
}
self
}
#[must_use]
pub const fn features(&self) -> FeatureFlags {
self.features
}
#[must_use]
pub const fn limits(&self) -> NegotiatedLimits {
self.limits
}
#[must_use]
pub const fn accepted_roles(&self) -> RoleSet {
self.accepted_roles
}
#[must_use]
pub const fn accepts_role(&self, role: Role) -> bool {
self.accepted_roles.contains(role)
}
#[must_use]
pub const fn requires_auth(&self) -> bool {
self.require_auth
}
#[must_use]
pub const fn min_protocol(&self) -> u16 {
self.min_protocol
}
#[must_use]
pub const fn max_protocol(&self) -> u16 {
self.max_protocol
}
#[must_use]
pub const fn has_capacity(&self) -> bool {
match self.connection_limit {
Some(limit) => self.connections_open < limit,
None => true,
}
}
#[must_use]
pub const fn refuse(&self, reason: RefusalReason) -> Refused {
Refused {
max_protocol: self.max_protocol,
min_protocol: self.min_protocol,
reason,
}
}
#[must_use]
pub fn negotiate(&self, hello: &Hello, session: SessionAssignment) -> HandshakeOutcome {
negotiate(hello, self, session)
}
}
impl Default for Acceptor {
fn default() -> Self {
Self::new(AuthToken::ZERO)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NegotiatedSession {
pub protocol: u16,
pub session_id: SessionId,
pub features: FeatureFlags,
pub limits: NegotiatedLimits,
pub role: Role,
pub peer_version: AstrsVersion,
pub resumed: bool,
}
impl NegotiatedSession {
#[must_use]
pub const fn frame_limits(&self) -> FrameLimits {
self.limits.to_frame_limits()
}
#[must_use]
pub const fn supports(&self, features: FeatureFlags) -> bool {
self.features.contains(features)
}
#[must_use]
pub const fn compression(&self) -> Compression {
self.features.best_compression()
}
}
impl fmt::Display for NegotiatedSession {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"session {} with a {} (protocol {}, astrs {}, features {})",
self.session_id, self.role, self.protocol, self.peer_version, self.features
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HandshakeOutcome {
Accepted {
welcome: Welcome,
session: NegotiatedSession,
},
Refused(Refused),
}
impl HandshakeOutcome {
#[must_use]
pub const fn is_accepted(&self) -> bool {
matches!(self, Self::Accepted { .. })
}
#[must_use]
pub const fn welcome(&self) -> Option<&Welcome> {
match self {
Self::Accepted { welcome, .. } => Some(welcome),
Self::Refused(_) => None,
}
}
#[must_use]
pub const fn session(&self) -> Option<&NegotiatedSession> {
match self {
Self::Accepted { session, .. } => Some(session),
Self::Refused(_) => None,
}
}
#[must_use]
pub const fn refused(&self) -> Option<&Refused> {
match self {
Self::Accepted { .. } => None,
Self::Refused(refused) => Some(refused),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum HandshakeError {
#[error("connection refused: {0}")]
Refused(Refused),
#[error("peer chose protocol {chosen}, which this build does not speak ({minimum}–{maximum})")]
ProtocolNotSupported {
chosen: u16,
minimum: u16,
maximum: u16,
},
#[error("peer chose protocol {chosen}, above the offered {offered}")]
ProtocolAboveOffer {
chosen: u16,
offered: u16,
},
#[error("peer answered as a {echoed} connection, but a {offered} connection was opened")]
RoleMismatch {
offered: Role,
echoed: Role,
},
#[error("peer enabled features that were not offered: {unexpected}")]
FeaturesNotOffered {
unexpected: FeatureFlags,
},
#[error("peer granted limits beyond what was offered ({granted} > {offered})")]
LimitsExceedOffer {
offered: Box<NegotiatedLimits>,
granted: Box<NegotiatedLimits>,
},
#[error("peer resumed session {session}, which was not requested")]
UnexpectedResume {
session: SessionId,
},
}
impl HandshakeError {
#[must_use]
pub const fn is_retryable(&self) -> bool {
match self {
Self::Refused(refused) => refused.reason.is_retryable(),
_ => false,
}
}
#[must_use]
pub const fn refusal(&self) -> Option<&Refused> {
match self {
Self::Refused(refused) => Some(refused),
_ => None,
}
}
}
#[must_use]
pub fn negotiate(
hello: &Hello,
acceptor: &Acceptor,
session: SessionAssignment,
) -> HandshakeOutcome {
if acceptor.shutting_down {
return HandshakeOutcome::Refused(acceptor.refuse(RefusalReason::ShuttingDown));
}
let protocol = match negotiate_protocol(hello.protocol) {
Ok(protocol) => protocol.min(acceptor.max_protocol),
Err(_) => {
return HandshakeOutcome::Refused(acceptor.refuse(RefusalReason::ProtocolTooOld {
peer: hello.protocol,
minimum: acceptor.min_protocol,
}));
}
};
if protocol < acceptor.min_protocol {
return HandshakeOutcome::Refused(acceptor.refuse(RefusalReason::ProtocolTooOld {
peer: hello.protocol,
minimum: acceptor.min_protocol,
}));
}
if acceptor.require_auth && !acceptor.expected_auth.verify(&hello.auth) {
return HandshakeOutcome::Refused(acceptor.refuse(RefusalReason::BadAuth));
}
if !acceptor.accepts_role(hello.role) {
return HandshakeOutcome::Refused(
acceptor.refuse(RefusalReason::RoleNotPermitted { role: hello.role }),
);
}
if !acceptor.has_capacity() {
let limit = acceptor.connection_limit.unwrap_or(u32::MAX);
return HandshakeOutcome::Refused(
acceptor.refuse(RefusalReason::TooManyConnections { limit }),
);
}
let (session_id, resumed) = match (session, hello.resume) {
(SessionAssignment::Resumed(id), Some(_)) => (id, true),
(SessionAssignment::Resumed(id) | SessionAssignment::Fresh(id), _) => (id, false),
(SessionAssignment::ResumeUnavailable, Some(requested)) => {
return HandshakeOutcome::Refused(
acceptor.refuse(RefusalReason::UnknownSession { session: requested }),
);
}
(SessionAssignment::ResumeUnavailable, None) => {
return HandshakeOutcome::Refused(acceptor.refuse(RefusalReason::Internal {
message: "no session was assigned to this connection".to_owned(),
}));
}
};
let limits = acceptor.limits.negotiate(&hello.limits);
if limits.max_payload_bytes < MIN_USABLE_PAYLOAD_BYTES {
return HandshakeOutcome::Refused(acceptor.refuse(RefusalReason::LimitsUnacceptable {
message: format!(
"an agreed payload ceiling of {} B is below the {MIN_USABLE_PAYLOAD_BYTES} B \
the protocol needs",
limits.max_payload_bytes
),
}));
}
let features = acceptor.features.negotiate(hello.features).known();
let welcome = Welcome {
protocol,
limits,
session_id,
features,
peer_role: hello.role,
astrs_version: AstrsVersion::current(),
resumed,
};
let session = NegotiatedSession {
protocol,
session_id,
features,
limits,
role: hello.role,
peer_version: hello.astrs_version.clone(),
resumed,
};
HandshakeOutcome::Accepted { welcome, session }
}
pub fn accept_welcome(
hello: &Hello,
welcome: &Welcome,
) -> Result<NegotiatedSession, HandshakeError> {
if welcome.protocol > hello.protocol {
return Err(HandshakeError::ProtocolAboveOffer {
chosen: welcome.protocol,
offered: hello.protocol,
});
}
if !welcome.protocol_is_supported() {
return Err(HandshakeError::ProtocolNotSupported {
chosen: welcome.protocol,
minimum: MIN_SUPPORTED_PROTOCOL,
maximum: PROTOCOL_VERSION,
});
}
if welcome.peer_role != hello.role {
return Err(HandshakeError::RoleMismatch {
offered: hello.role,
echoed: welcome.peer_role,
});
}
let unexpected = welcome.features.difference(hello.features);
if !unexpected.is_empty() {
return Err(HandshakeError::FeaturesNotOffered { unexpected });
}
if !hello.limits.permits(&welcome.limits) {
return Err(HandshakeError::LimitsExceedOffer {
offered: Box::new(hello.limits),
granted: Box::new(welcome.limits),
});
}
if welcome.resumed && hello.resume.is_none() {
return Err(HandshakeError::UnexpectedResume {
session: welcome.session_id,
});
}
Ok(NegotiatedSession {
protocol: welcome.protocol,
session_id: welcome.session_id,
features: welcome.features,
limits: welcome.limits,
role: hello.role,
peer_version: welcome.astrs_version.clone(),
resumed: welcome.resumed,
})
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::common::DurationMs;
fn token() -> AuthToken {
AuthToken::from_bytes([0xAB; 32])
}
fn acceptor() -> Acceptor {
Acceptor::new(token())
.with_features(FeatureFlags::daemon_defaults())
.with_limits(NegotiatedLimits::network())
.with_accepted_roles(RoleSet::ALL)
}
fn hello() -> Hello {
Hello::new(Role::Node, token())
.with_features(FeatureFlags::SHM_ZERO_COPY | FeatureFlags::RECORDING)
}
fn fresh() -> SessionAssignment {
SessionAssignment::Fresh(SessionId::from_u128(0x5E55))
}
#[test]
fn a_good_greeting_is_accepted_on_intersected_terms() {
let outcome = negotiate(&hello(), &acceptor(), fresh());
let (welcome, session) = match outcome {
HandshakeOutcome::Accepted { welcome, session } => (welcome, session),
HandshakeOutcome::Refused(refused) => panic!("unexpected refusal: {refused}"),
};
assert_eq!(welcome.protocol, PROTOCOL_VERSION);
assert_eq!(welcome.session_id, SessionId::from_u128(0x5E55));
assert_eq!(welcome.peer_role, Role::Node);
assert!(!welcome.resumed);
assert_eq!(welcome.features, FeatureFlags::SHM_ZERO_COPY);
assert!(welcome.limits.require_crc, "the stricter policy wins");
let mirrored = accept_welcome(&hello(), &welcome).unwrap();
assert_eq!(mirrored.features, session.features);
assert_eq!(mirrored.limits, session.limits);
assert_eq!(mirrored.protocol, session.protocol);
assert_eq!(mirrored.session_id, session.session_id);
assert_eq!(mirrored.role, session.role);
}
#[test]
fn negotiation_is_a_pure_function_of_its_inputs() {
let first = negotiate(&hello(), &acceptor(), fresh());
let second = negotiate(&hello(), &acceptor(), fresh());
assert_eq!(first, second);
}
#[test]
fn the_acceptor_method_and_the_free_function_agree() {
let acceptor = acceptor();
assert_eq!(
acceptor.negotiate(&hello(), fresh()),
negotiate(&hello(), &acceptor, fresh())
);
}
#[test]
fn a_bad_token_is_refused_without_detail() {
let hello = Hello::new(Role::Node, AuthToken::from_bytes([0; 32]));
let refused = negotiate(&hello, &acceptor(), fresh())
.refused()
.cloned()
.expect("a bad token must be refused");
assert_eq!(refused.reason, RefusalReason::BadAuth);
assert_eq!(refused.max_protocol, PROTOCOL_VERSION);
}
#[test]
fn an_unauthenticated_acceptor_skips_the_token_check() {
let acceptor = Acceptor::without_auth();
assert!(!acceptor.requires_auth());
let hello = Hello::new(Role::Node, AuthToken::from_bytes([0x11; 32]));
assert!(negotiate(&hello, &acceptor, fresh()).is_accepted());
}
#[test]
fn a_protocol_below_the_floor_is_refused_with_the_range() {
let hello = hello().with_protocol(0);
let refused = negotiate(&hello, &acceptor(), fresh())
.refused()
.cloned()
.expect("protocol 0 is not a release");
assert!(refused.is_version_problem());
assert_eq!(refused.min_protocol, MIN_SUPPORTED_PROTOCOL);
assert_eq!(refused.max_protocol, PROTOCOL_VERSION);
assert!(matches!(
refused.reason,
RefusalReason::ProtocolTooOld { peer: 0, .. }
));
}
#[test]
fn a_newer_peer_is_met_at_this_builds_ceiling() {
let hello = hello().with_protocol(u16::MAX);
let welcome = negotiate(&hello, &acceptor(), fresh())
.welcome()
.cloned()
.expect("a newer peer is compatible downwards");
assert_eq!(welcome.protocol, PROTOCOL_VERSION);
}
#[test]
fn an_acceptor_that_dropped_old_protocols_refuses_them() {
let strict = acceptor().with_protocol_range(PROTOCOL_VERSION, PROTOCOL_VERSION);
assert_eq!(strict.min_protocol(), PROTOCOL_VERSION);
assert_eq!(strict.max_protocol(), PROTOCOL_VERSION);
assert!(negotiate(&hello(), &strict, fresh()).is_accepted());
}
#[test]
fn the_protocol_range_is_clamped_to_what_this_build_implements() {
let absurd = acceptor().with_protocol_range(0, u16::MAX);
assert_eq!(absurd.min_protocol(), MIN_SUPPORTED_PROTOCOL);
assert_eq!(absurd.max_protocol(), PROTOCOL_VERSION);
let inverted = acceptor().with_protocol_range(u16::MAX, MIN_SUPPORTED_PROTOCOL);
assert!(inverted.min_protocol() <= inverted.max_protocol());
}
#[test]
fn a_role_this_endpoint_does_not_serve_is_refused() {
let node_socket = acceptor().with_accepted_roles(RoleSet::NODES);
let refused = negotiate(&Hello::new(Role::Cli, token()), &node_socket, fresh())
.refused()
.cloned()
.expect("a CLI on the node socket is a misdial");
assert_eq!(
refused.reason,
RefusalReason::RoleNotPermitted { role: Role::Cli }
);
assert!(negotiate(&hello(), &node_socket, fresh()).is_accepted());
}
#[test]
fn a_full_acceptor_refuses_with_its_limit() {
let full = acceptor()
.with_connection_limit(Some(2))
.with_connections_open(2);
assert!(!full.has_capacity());
let refused = negotiate(&hello(), &full, fresh()).refused().cloned();
assert_eq!(
refused.map(|refused| refused.reason),
Some(RefusalReason::TooManyConnections { limit: 2 })
);
}
#[test]
fn a_shutting_down_acceptor_refuses_everything_first() {
let draining = acceptor().with_shutting_down(true);
let bad_token = Hello::new(Role::Node, AuthToken::ZERO);
assert_eq!(
negotiate(&bad_token, &draining, fresh())
.refused()
.map(|refused| refused.reason.clone()),
Some(RefusalReason::ShuttingDown)
);
}
#[test]
fn a_resumed_session_keeps_its_id() {
let session = SessionId::from_u128(0xBEEF);
let hello = hello().with_resume(session);
let welcome = negotiate(&hello, &acceptor(), SessionAssignment::Resumed(session))
.welcome()
.cloned()
.expect("a resumable session is accepted");
assert_eq!(welcome.session_id, session);
assert!(welcome.resumed);
assert!(accept_welcome(&hello, &welcome).unwrap().resumed);
}
#[test]
fn an_unresumable_session_is_refused_by_name() {
let session = SessionId::from_u128(0xDEAD);
let hello = hello().with_resume(session);
let refused = negotiate(&hello, &acceptor(), SessionAssignment::ResumeUnavailable)
.refused()
.cloned()
.expect("an unknown session cannot be resumed");
assert_eq!(refused.reason, RefusalReason::UnknownSession { session });
}
#[test]
fn a_resume_grant_for_a_cold_greeting_is_treated_as_fresh() {
let session = SessionId::from_u128(1);
let welcome = negotiate(&hello(), &acceptor(), SessionAssignment::Resumed(session))
.welcome()
.cloned()
.expect("a cold greeting is still acceptable");
assert_eq!(welcome.session_id, session);
assert!(!welcome.resumed);
}
#[test]
fn a_missing_session_assignment_is_an_internal_refusal() {
let refused = negotiate(&hello(), &acceptor(), SessionAssignment::ResumeUnavailable)
.refused()
.cloned()
.expect("a connection needs a session");
assert!(matches!(refused.reason, RefusalReason::Internal { .. }));
}
#[test]
fn limits_too_small_to_carry_the_protocol_are_refused() {
let tiny = hello().with_limits(NegotiatedLimits::uds().with_max_payload_bytes(128));
let refused = negotiate(&tiny, &acceptor(), fresh())
.refused()
.cloned()
.expect("128 bytes cannot carry a spawn spec");
assert!(matches!(
refused.reason,
RefusalReason::LimitsUnacceptable { .. }
));
}
#[test]
fn the_agreed_limits_are_the_stricter_of_the_two() {
let modest = hello().with_limits(
NegotiatedLimits::uds()
.with_max_payload_bytes(1 << 20)
.with_heartbeat_interval(DurationMs::from_secs(1)),
);
let welcome = negotiate(&modest, &acceptor(), fresh())
.welcome()
.cloned()
.expect("modest limits are acceptable");
assert_eq!(welcome.limits.max_payload_bytes, 1 << 20);
assert_eq!(welcome.limits.heartbeat_interval, DurationMs::from_secs(1));
assert!(welcome.limits.require_crc);
assert_eq!(
welcome.limits.to_frame_limits().max_payload_bytes(),
1 << 20
);
}
#[test]
fn unknown_feature_bits_never_become_live() {
let futuristic = hello().with_features(FeatureFlags::from_bits(u64::MAX));
let welcome = negotiate(&futuristic, &acceptor(), fresh())
.welcome()
.cloned()
.expect("an unknown bit is ignored, not fatal");
assert!(!welcome.features.has_unknown());
assert_eq!(welcome.features, FeatureFlags::daemon_defaults());
}
#[test]
fn the_initiator_rejects_a_welcome_that_grants_too_much() {
let hello = hello();
let above = Welcome::new(
hello.protocol + 1,
hello.limits,
SessionId::from_u128(1),
hello.role,
);
assert!(matches!(
accept_welcome(&hello, &above),
Err(HandshakeError::ProtocolAboveOffer { .. })
));
let wrong_role = Welcome::new(hello.protocol, hello.limits, SessionId::NIL, Role::Cli);
assert!(matches!(
accept_welcome(&hello, &wrong_role),
Err(HandshakeError::RoleMismatch { .. })
));
let greedy_features =
Welcome::new(hello.protocol, hello.limits, SessionId::NIL, hello.role)
.with_features(FeatureFlags::QUIC);
assert!(matches!(
accept_welcome(&hello, &greedy_features),
Err(HandshakeError::FeaturesNotOffered { .. })
));
let greedy_limits = Welcome::new(
hello.protocol,
NegotiatedLimits::new().with_max_payload_bytes(u64::from(u32::MAX)),
SessionId::NIL,
hello.role,
);
let small = hello
.clone()
.with_limits(NegotiatedLimits::uds().with_max_payload_bytes(1 << 20));
assert!(matches!(
accept_welcome(&small, &greedy_limits),
Err(HandshakeError::LimitsExceedOffer { .. })
));
let surprise = Welcome::new(hello.protocol, hello.limits, SessionId::NIL, hello.role)
.with_resumed(true);
assert!(matches!(
accept_welcome(&hello, &surprise),
Err(HandshakeError::UnexpectedResume { .. })
));
}
#[test]
fn the_initiator_rejects_a_protocol_it_cannot_speak() {
let hello = hello().with_protocol(u16::MAX);
let unspeakable = Welcome::new(
PROTOCOL_VERSION + 1,
hello.limits,
SessionId::NIL,
hello.role,
);
assert!(matches!(
accept_welcome(&hello, &unspeakable),
Err(HandshakeError::ProtocolNotSupported { .. })
));
}
#[test]
fn handshake_errors_classify_themselves() {
let refused = HandshakeError::Refused(Refused::new(RefusalReason::ShuttingDown));
assert!(refused.is_retryable());
assert!(refused.refusal().is_some());
let mismatch = HandshakeError::RoleMismatch {
offered: Role::Node,
echoed: Role::Cli,
};
assert!(!mismatch.is_retryable());
assert!(mismatch.refusal().is_none());
assert!(!mismatch.to_string().is_empty());
}
#[test]
fn session_assignment_reports_what_it_grants() {
let id = SessionId::from_u128(3);
assert_eq!(SessionAssignment::Fresh(id).session_id(), Some(id));
assert!(!SessionAssignment::Fresh(id).is_resumed());
assert!(SessionAssignment::Resumed(id).is_resumed());
assert_eq!(SessionAssignment::ResumeUnavailable.session_id(), None);
}
#[test]
fn the_negotiated_session_exposes_the_terms() {
let session = negotiate(&hello(), &acceptor(), fresh())
.session()
.cloned()
.expect("accepted");
assert!(session.supports(FeatureFlags::SHM_ZERO_COPY));
assert!(!session.supports(FeatureFlags::RECORDING));
assert!(session.frame_limits().require_crc());
assert_eq!(session.compression(), Compression::None);
assert!(session.to_string().contains("session"));
}
#[test]
fn compression_follows_the_negotiated_features() {
let both = Acceptor::new(token())
.with_features(FeatureFlags::COMPRESSION_LZ4 | FeatureFlags::COMPRESSION_ZSTD);
let hello = Hello::new(Role::Peer, token()).with_features(FeatureFlags::COMPRESSION_LZ4);
let session = negotiate(&hello, &both, fresh())
.session()
.cloned()
.expect("accepted");
assert_eq!(session.compression(), Compression::Lz4);
}
#[test]
fn the_default_acceptor_is_the_zero_token_one() {
assert_eq!(Acceptor::default(), Acceptor::new(AuthToken::ZERO));
assert!(Acceptor::default().accepts_role(Role::Peer));
assert_eq!(Acceptor::default().features(), FeatureFlags::EMPTY);
assert_eq!(Acceptor::default().limits(), NegotiatedLimits::new());
assert_eq!(Acceptor::default().accepted_roles(), RoleSet::ALL);
}
}