use super::*;
fn shutdown_chunks(packets: &[Bytes]) -> Vec<u32> {
packets
.iter()
.flat_map(|raw| Packet::unmarshal(raw).unwrap().chunks)
.filter_map(|c| {
c.as_any()
.downcast_ref::<ChunkShutdown>()
.map(|s| s.cumulative_tsn_ack)
})
.collect()
}
#[test]
fn shutdown_carries_the_last_tsn_received_from_the_peer() -> Result<()> {
let mut a = timed_test_association();
let now = Instant::now();
a.peer_last_tsn = 1000;
a.my_next_tsn = 5001;
a.cumulative_tsn_ack_point = 5000;
a.advanced_peer_tsn_ack_point = 5000;
a.shutdown()?;
assert_eq!(AssociationState::ShutdownSent, a.state());
let sent = shutdown_chunks(&a.gather_outbound(now).0);
assert_eq!(vec![1000], sent, "the peer's last in-sequence TSN");
Ok(())
}
#[test]
fn shutdown_acknowledges_outstanding_data() -> Result<()> {
let mut a = timed_test_association();
let now = Instant::now();
a.my_next_tsn = 100;
a.cumulative_tsn_ack_point = 99;
a.advanced_peer_tsn_ack_point = 99;
let ppi = PayloadProtocolIdentifier::Binary;
a.open_stream(1, ppi)?
.write_sctp(now, &Bytes::from_static(b"last words"), ppi)?;
let sent = transmitted_data(&a.gather_outbound(now).0);
assert_eq!(vec![100], sent.iter().map(|c| c.tsn).collect::<Vec<_>>());
a.handle_shutdown(
&ChunkShutdown {
cumulative_tsn_ack: 100,
},
now,
)?;
assert!(
a.inflight_queue.is_empty(),
"the SHUTDOWN acknowledged TSN 100"
);
assert_eq!(100, a.cumulative_tsn_ack_point);
assert_eq!(AssociationState::ShutdownAckSent, a.state());
assert!(a.will_send_shutdown_ack);
assert_eq!(0, a.stream(1)?.buffered_amount()?);
Ok(())
}
#[test]
fn shutdown_with_data_still_outstanding_waits_for_its_acknowledgement() -> Result<()> {
let mut a = timed_test_association();
let now = Instant::now();
a.my_next_tsn = 100;
a.cumulative_tsn_ack_point = 99;
a.advanced_peer_tsn_ack_point = 99;
let ppi = PayloadProtocolIdentifier::Binary;
let mut stream = a.open_stream(1, ppi)?;
stream.write_sctp(now, &Bytes::from_static(b"one"), ppi)?;
stream.write_sctp(now, &Bytes::from_static(b"two"), ppi)?;
assert_eq!(2, transmitted_data(&a.gather_outbound(now).0).len());
a.handle_shutdown(
&ChunkShutdown {
cumulative_tsn_ack: 100,
},
now,
)?;
assert_eq!(AssociationState::ShutdownReceived, a.state());
assert_eq!(1, a.inflight_queue.len());
a.handle_sack(
&ChunkSelectiveAck {
cumulative_tsn_ack: 101,
advertised_receiver_window_credit: 65536,
gap_ack_blocks: vec![],
duplicate_tsn: vec![],
},
now,
)?;
assert_eq!(AssociationState::ShutdownAckSent, a.state());
assert!(a.will_send_shutdown_ack);
Ok(())
}
#[test]
fn shutdown_ack_point_outside_the_window_is_ignored() -> Result<()> {
for cumulative_tsn_ack in [50, 200] {
let mut a = timed_test_association();
let now = Instant::now();
a.my_next_tsn = 100;
a.cumulative_tsn_ack_point = 99;
a.advanced_peer_tsn_ack_point = 99;
let ppi = PayloadProtocolIdentifier::Binary;
a.open_stream(1, ppi)?
.write_sctp(now, &Bytes::from_static(b"x"), ppi)?;
assert_eq!(1, transmitted_data(&a.gather_outbound(now).0).len());
a.handle_shutdown(&ChunkShutdown { cumulative_tsn_ack }, now)?;
assert_eq!(99, a.cumulative_tsn_ack_point, "{cumulative_tsn_ack}");
assert_eq!(1, a.inflight_queue.len(), "{cumulative_tsn_ack}");
assert_eq!(AssociationState::ShutdownReceived, a.state());
}
Ok(())
}