use super::robustness_test::*;
use super::*;
use crate::chunk::chunk_type::*;
use crate::endpoint::{DatagramEvent, Endpoint};
use crate::{ClientConfig, EndpointConfig, TimerConfig};
const PPI: PayloadProtocolIdentifier = PayloadProtocolIdentifier::Binary;
fn established(config: TransportConfig) -> Association {
let mut a = create_association(config);
a.control_queue.clear();
a.timers.stop(Timer::T1Init);
a.set_state(AssociationState::Established);
a.use_forward_tsn = true;
a.rwnd = 1 << 20;
a.ssthresh = 1 << 20;
a.rto_mgr.set_rto(1000, true);
a
}
fn connecting() -> (Association, Instant) {
let mut endpoint = Endpoint::new(
remote(),
TransportProtocol::UDP,
Arc::new(EndpointConfig::default()),
None,
);
let now = Instant::now();
let (_, mut a) = endpoint
.connect(
now,
ClientConfig::new(TransportConfig::default()),
"10.0.0.2:5000".parse().unwrap(),
)
.unwrap();
let _ = sent_datagrams(&mut a, now);
(a, now)
}
#[test]
fn a_packet_with_a_wrong_or_zero_checksum_is_discarded() {
let good = raw_packet(
5000,
5000,
0,
&[init_chunk(CT_INIT.0, 0xAABB_CCDD, 100, &[])],
);
let mut wrong = good.to_vec();
wrong[8] ^= 0x01;
let mut zero = good.to_vec();
zero[8..12].fill(0);
let mut endpoint = accepting_endpoint();
let now = Instant::now();
for bad in [wrong, zero] {
assert!(
endpoint
.handle(now, remote(), None, Bytes::from(bad))
.is_none(),
"a packet with a bad checksum was taken"
);
assert!(endpoint.poll_transmit().is_none(), "and answered");
}
assert!(matches!(
endpoint.handle(now, remote(), None, good),
Some((_, DatagramEvent::NewAssociation(_)))
));
}
#[test]
fn a_packet_with_port_zero_is_not_answered() {
for (source, destination) in [(0u16, 5000u16), (5000, 0)] {
let mut endpoint = accepting_endpoint();
let now = Instant::now();
let init = raw_packet(
source,
destination,
0,
&[init_chunk(CT_INIT.0, 0xAABB_CCDD, 100, &[])],
);
if let Some((_, DatagramEvent::NewAssociation(mut a))) =
endpoint.handle(now, remote(), None, init)
{
assert!(
sent_datagrams(&mut a, now).is_empty(),
"an INIT between ports {source} and {destination} was answered"
);
}
}
}
#[test]
fn an_init_offering_zero_checksum_is_accepted() {
let zero_checksum = raw_tlv(0x8001, &[0, 0, 0, 1]);
let (_, a, sent) = accept_init(&[zero_checksum]);
assert!(a.is_some());
let init_acks = chunks_of_type(&sent, CT_INIT_ACK.0);
assert_eq!(1, init_acks.len());
let params = tlvs(&init_acks[0][20..]);
assert!(
params.iter().all(|(typ, _)| *typ != 8 && *typ != 0x8001),
"neither reported nor echoed: {params:?}"
);
assert!(crate::packet::PartialDecode::unmarshal(&sent[0]).is_ok());
}
#[test]
fn the_stream_sequence_number_wraps_to_zero() -> Result<()> {
let mut a = established(TransportConfig::default());
let now = Instant::now();
a.open_stream(1, PPI)?;
a.streams.get_mut(&1).unwrap().sequence_number = 65535;
for _ in 0..2 {
a.stream(1)?
.write_sctp(now, &Bytes::from_static(b"x"), PPI)?;
}
let sent = transmitted_data(&a.gather_outbound(now).0);
assert_eq!(
vec![65535, 0],
sent.iter()
.map(|c| c.stream_sequence_number)
.collect::<Vec<_>>()
);
Ok(())
}
#[test]
fn data_chunks_in_a_packet_are_in_tsn_order() -> Result<()> {
let mut a = established(TransportConfig::default());
let now = Instant::now();
let first = a.my_next_tsn;
{
let mut s = a.open_stream(1, PPI)?;
for _ in 0..10 {
s.write_sctp(now, &Bytes::from_static(b"small"), PPI)?;
}
}
let datagrams = a.gather_outbound(now).0;
assert_eq!(1, datagrams.len(), "ten small chunks share a packet");
let tsns: Vec<u32> = transmitted_data(&datagrams).iter().map(|c| c.tsn).collect();
assert_eq!(
(0..10).map(|i| first.wrapping_add(i)).collect::<Vec<_>>(),
tsns
);
Ok(())
}
#[test]
fn no_data_is_sent_before_the_association_is_established() {
let (mut a, now) = connecting();
assert_eq!(AssociationState::CookieWait, a.state());
let mut s = a.open_stream(1, PPI).unwrap();
assert!(
s.write_sctp(now, &Bytes::from_static(b"early"), PPI)
.is_err()
);
assert!(sent_datagrams(&mut a, now).is_empty());
let init_ack = init_chunk(CT_INIT_ACK.0, 4242, 9000, &[raw_tlv(7, b"cookie")]);
let raw = packet_to(&a, &[init_ack]);
inject(&mut a, now, raw);
assert_eq!(AssociationState::CookieEchoed, a.state());
assert!(
a.stream(1)
.unwrap()
.write_sctp(now, &Bytes::from_static(b"early"), PPI)
.is_err()
);
let sent = sent_datagrams(&mut a, now);
assert_eq!(1, sent.len(), "the COOKIE ECHO and nothing else");
assert_eq!(
vec![CT_COOKIE_ECHO.0],
chunks_of(&sent[0]).iter().map(|c| c[0]).collect::<Vec<_>>()
);
}
#[test]
fn a_sack_outside_the_data_states_is_discarded() -> Result<()> {
for state in [
AssociationState::CookieEchoed,
AssociationState::ShutdownSent,
AssociationState::ShutdownAckSent,
] {
let mut a = established(TransportConfig::default());
let now = Instant::now();
let first = a.my_next_tsn;
a.open_stream(1, PPI)?
.write_sctp(now, &Bytes::from_static(b"x"), PPI)?;
let _ = a.gather_outbound(now);
a.set_state(state);
a.handle_sack(
&ChunkSelectiveAck {
cumulative_tsn_ack: first,
advertised_receiver_window_credit: 5,
gap_ack_blocks: vec![],
duplicate_tsn: vec![],
},
now,
)?;
assert_eq!(first.wrapping_sub(1), a.cumulative_tsn_ack_point, "{state}");
assert_eq!(1, a.inflight_queue.len(), "{state}");
}
Ok(())
}
#[test]
fn a_sack_behind_the_ack_point_is_dropped() -> Result<()> {
let mut a = established(TransportConfig::default());
let now = Instant::now();
let first = a.my_next_tsn;
{
let mut s = a.open_stream(1, PPI)?;
for _ in 0..3 {
s.write_sctp(now, &Bytes::from_static(b"x"), PPI)?;
}
}
let _ = a.gather_outbound(now);
let sack = |cumulative: u32, a_rwnd: u32| ChunkSelectiveAck {
cumulative_tsn_ack: cumulative,
advertised_receiver_window_credit: a_rwnd,
gap_ack_blocks: vec![],
duplicate_tsn: vec![],
};
a.handle_sack(&sack(first.wrapping_add(1), 50_000), now)?;
let rwnd = a.rwnd;
a.handle_sack(&sack(first, 7), now)?;
assert_eq!(first.wrapping_add(1), a.cumulative_tsn_ack_point);
assert_eq!(rwnd, a.rwnd, "the window of an old SACK is not taken");
Ok(())
}
#[test]
fn initial_tsns_and_tags_differ_between_associations() {
let mut endpoint = Endpoint::new(
remote(),
TransportProtocol::UDP,
Arc::new(EndpointConfig::default()),
None,
);
let now = Instant::now();
let mut tsns = std::collections::HashSet::new();
let mut tags = std::collections::HashSet::new();
for port in 0..16u16 {
let peer: SocketAddr = format!("10.0.0.2:{}", 6000 + port).parse().unwrap();
let (_, a) = endpoint
.connect(now, ClientConfig::new(TransportConfig::default()), peer)
.unwrap();
assert_ne!(0, a.verification_tag());
tsns.insert(a.my_next_tsn);
tags.insert(a.verification_tag());
}
assert_eq!(16, tags.len(), "a tag of its own for each association");
assert!(tsns.len() >= 15, "initial TSNs are not random: {tsns:?}");
}
#[test]
fn an_unanswered_shutdown_ack_is_given_up() -> Result<()> {
let mut a = established(TransportConfig::default().with_timer_config(TimerConfig {
max_t2_shutdown_retrans: 3,
..TimerConfig::default()
}));
let mut now = Instant::now();
a.handle_shutdown(
&ChunkShutdown {
cumulative_tsn_ack: a.my_next_tsn.wrapping_sub(1),
},
now,
)?;
assert_eq!(AssociationState::ShutdownAckSent, a.state());
let mut acks = chunks_of_type(&sent_datagrams(&mut a, now), CT_SHUTDOWN_ACK.0).len();
while let Some(at) = a.poll_timeout() {
now = at;
a.handle_timeout(now);
acks += chunks_of_type(&sent_datagrams(&mut a, now), CT_SHUTDOWN_ACK.0).len();
assert!(acks < 20);
}
assert_eq!(4, acks, "the SHUTDOWN ACK and three retransmissions");
assert!(a.is_ended());
Ok(())
}
#[test]
fn a_shutdown_travels_without_data() -> Result<()> {
let mut a = established(TransportConfig::default());
let now = Instant::now();
let first = a.my_next_tsn;
a.open_stream(1, PPI)?
.write_sctp(now, &Bytes::from_static(b"last"), PPI)?;
a.shutdown()?;
let sent = a.gather_outbound(now).0;
assert!(
chunks_of_type(&sent, CT_SHUTDOWN.0).is_empty(),
"DATA first"
);
a.handle_sack(
&ChunkSelectiveAck {
cumulative_tsn_ack: first,
advertised_receiver_window_credit: 1 << 20,
gap_ack_blocks: vec![],
duplicate_tsn: vec![],
},
now,
)?;
let sent = a.gather_outbound(now).0;
let with_shutdown: Vec<&Bytes> = sent
.iter()
.filter(|d| chunks_of(d).iter().any(|c| c[0] == CT_SHUTDOWN.0))
.collect();
assert_eq!(1, with_shutdown.len());
assert_eq!(1, chunks_of(with_shutdown[0]).len(), "SHUTDOWN is alone");
Ok(())
}
#[test]
fn forward_tsn_flags_are_ignored() {
let mut link = connected_link(601);
let now = link.now;
let a = link.server.conn();
let target = a.peer_last_tsn.wrapping_add(3);
let raw = packet_to(
a,
&[raw_chunk(CT_FORWARD_TSN.0, 0xFF, &target.to_be_bytes())],
);
inject(a, now, raw);
assert_eq!(target, a.peer_last_tsn);
}
#[test]
fn abandoning_at_a_timeout_still_adjusts_the_window_and_the_rto() -> Result<()> {
let mut a = established(TransportConfig::default());
a.rto_mgr.set_rto(1000, false);
let now = Instant::now();
a.cwnd = 20 * a.pmdcs;
{
let mut s = a.open_stream(1, PPI)?;
s.set_reliability_params(false, ReliabilityType::Rexmit, 0)?;
s.write_sctp(now, &Bytes::from(vec![0u8; 1000]), PPI)?;
}
assert_eq!(1, transmitted_data(&a.gather_outbound(now).0).len());
let (cwnd, pmdcs) = (a.cwnd, a.pmdcs);
let at = a.timers.get(Timer::T3RTX).expect("T3-rtx runs");
a.handle_timeout(at);
let datagrams = a.gather_outbound(at).0;
assert!(
transmitted_data(&datagrams).is_empty(),
"abandoned, not retransmitted"
);
assert_eq!(1, chunks_of_type(&datagrams, CT_FORWARD_TSN.0).len());
assert_eq!(pmdcs, a.cwnd);
assert_eq!(std::cmp::max(cwnd / 2, 4 * pmdcs), a.ssthresh);
assert_eq!(Duration::from_secs(2), a.rto(), "the RTO backed off");
Ok(())
}
#[test]
fn a_limited_retransmission_stream_still_reaches_the_association_limit() -> Result<()> {
let mut a = established(TransportConfig::default().with_timer_config(TimerConfig {
max_t3_rtx_retrans: 3,
..TimerConfig::default()
}));
let now = Instant::now();
{
let mut s = a.open_stream(1, PPI)?;
s.set_reliability_params(false, ReliabilityType::Rexmit, 0)?;
s.write_sctp(now, &Bytes::from_static(b"never acknowledged"), PPI)?;
}
let _ = a.gather_outbound(now);
let mut expiries = 0;
while let Some(at) = a.poll_timeout() {
a.handle_timeout(at);
let _ = a.gather_outbound(at);
expiries += 1;
assert!(expiries < 20);
}
assert_eq!(4, expiries, "three tolerated, the fourth ends it");
assert!(a.is_ended());
Ok(())
}
fn deliver(a: &mut Association, now: Instant, raw: Bytes) -> bool {
if crate::packet::PartialDecode::unmarshal(&raw).is_err() {
return false;
}
inject(a, now, raw);
true
}
fn closed(a: &mut Association) -> bool {
let mut closed = a.is_ended();
while let Some(e) = a.poll() {
closed |= matches!(e, Event::AssociationClosed { .. });
}
closed
}
fn still_stands(a: &mut Association, now: Instant, case: &str, failures: &mut Vec<String>) {
if closed(a) {
failures.push(format!("{case}: the association closed"));
return;
}
if !chunks_of_type(&sent_after(a, now, Duration::from_secs(1)), CT_ABORT.0).is_empty() {
failures.push(format!("{case}: an ABORT was sent"));
}
let tsn = a.peer_last_tsn.wrapping_add(1);
let raw = packet_to(a, &[data_chunk(tsn, 1, 0, b"after")]);
inject(a, now + Duration::from_secs(1), raw);
if read_all(a, 1) != vec![Bytes::from_static(b"after")] {
failures.push(format!("{case}: the DATA after it was not delivered"));
}
}
#[test]
fn a_chunk_length_below_four_is_no_chunk() {
let mut failures = vec![];
for length in [0u16, 2, 3] {
for after_pad in [false, true] {
let case = format!("Chunk Length {length}, after a PAD chunk: {after_pad}");
let mut link = connected_link(97);
let now = link.now;
let a = link.server.conn();
let _ = sent_datagrams(a, now);
let mut abort = raw_chunk(CT_ABORT.0, 0, &[]);
abort[2..4].copy_from_slice(&length.to_be_bytes());
let mut chunks = vec![];
if after_pad {
chunks.push(raw_chunk(CT_PAD.0, 0, &[0; 4]));
}
chunks.push(abort);
let raw = packet_to(a, &chunks);
deliver(a, now, raw);
still_stands(a, now, &case, &mut failures);
}
}
assert!(failures.is_empty(), "{failures:#?}");
}
#[test]
fn an_abort_with_a_format_error_is_silently_discarded() {
let mut overrun = raw_tlv(12, b"why");
overrun[2..4].copy_from_slice(&40u16.to_be_bytes());
let mut short = raw_tlv(12, b"why");
short[2..4].copy_from_slice(&2u16.to_be_bytes());
let cases = [
("two bytes that are no error cause", vec![0u8, 12]),
("a cause longer than the chunk", overrun),
("a cause shorter than its header", short),
];
let mut failures = vec![];
for (case, value) in cases {
let mut link = connected_link(98);
let now = link.now;
let a = link.server.conn();
let _ = sent_datagrams(a, now);
let raw = packet_to(a, &[raw_chunk(CT_ABORT.0, 0, &value)]);
deliver(a, now, raw);
still_stands(a, now, case, &mut failures);
}
assert!(failures.is_empty(), "{failures:#?}");
for value in [raw_tlv(12, b"why"), raw_tlv(12, b"why!")] {
let mut link = connected_link(99);
let now = link.now;
let a = link.server.conn();
let raw = packet_to(a, &[raw_chunk(CT_ABORT.0, 0, &value)]);
inject(a, now, raw);
assert!(
closed(a),
"a well-formed ABORT ({} bytes of cause)",
value.len()
);
}
}
#[test]
fn a_host_name_in_supported_address_types_is_ignored() {
let mut failures = vec![];
for types in [vec![5u16, 11], vec![11], vec![11, 6], vec![5, 6, 11]] {
let value: Vec<u8> = types.iter().flat_map(|t| t.to_be_bytes()).collect();
let (_, a, sent) = accept_init(&[raw_tlv(12, &value)]);
let Some(mut a) = a else {
failures.push(format!("{types:?}: no association"));
continue;
};
if closed(&mut a) {
failures.push(format!("{types:?}: the association closed"));
}
if !chunks_of_type(&sent, CT_ABORT.0).is_empty() {
failures.push(format!("{types:?}: answered with an ABORT"));
}
let init_acks = chunks_of_type(&sent, CT_INIT_ACK.0);
match init_acks.as_slice() {
[init_ack] => {
if tlvs(&init_ack[20..])
.iter()
.any(|(typ, _)| *typ == PARAM_UNRECOGNIZED_PARAMETER)
{
failures.push(format!("{types:?}: reported as unrecognized"));
}
}
_ => failures.push(format!("{types:?}: {} INIT ACKs", init_acks.len())),
}
}
assert!(failures.is_empty(), "{failures:#?}");
}