use std::time::{Duration, Instant};
use sansio::Protocol;
use stun::attributes::ATTR_USERNAME;
use super::agent_rfc8445_test::*;
use super::*;
use crate::attributes::control::AttrControlling;
use crate::attributes::priority::PriorityAttr;
#[derive(Default)]
struct Trace {
requests: Vec<(Instant, Message, SocketAddr)>,
states: Vec<(Instant, ConnectionState)>,
}
fn run_traced(
a: &mut Agent,
from: Instant,
until: Instant,
mut answer: impl FnMut(Instant, &Message) -> bool,
) -> Trace {
let mut trace = Trace::default();
let mut now = from;
loop {
a.handle_timeout(now).unwrap();
while let Some(t) = a.poll_write() {
let mut m = Message {
raw: t.message.to_vec(),
..Message::default()
};
m.decode().unwrap();
if m.typ != BINDING_REQUEST {
continue;
}
if answer(now, &m) {
let response = peer_response(BINDING_SUCCESS, m.transaction_id, addr(LOCAL), None);
let _ = deliver(a, now, &response, &t.transport.peer_addr.to_string());
}
trace.requests.push((t.now, m, t.transport.peer_addr));
}
while let Some(e) = a.poll_event() {
if let Event::ConnectionStateChange(s) = e.event {
trace.states.push((e.now, s));
}
}
match a.poll_timeout() {
Some(next) if next <= until => now = next.max(now + Duration::from_micros(1)),
_ => return trace,
}
}
}
fn connected(config: AgentConfig) -> (Agent, Instant) {
let start = Instant::now();
let mut a = started_agent(start, config, &[REMOTE_A]);
let now = start + Duration::from_millis(500);
run_traced(&mut a, start, now, |_, _| true);
assert_eq!(a.state(), ConnectionState::Connected);
assert!(a.get_selected_candidate_pair().is_some());
(a, now)
}
fn intervals(trace: &Trace) -> Vec<Duration> {
trace
.requests
.windows(2)
.map(|w| w[1].0.duration_since(w[0].0))
.collect()
}
#[test]
fn test_consent_checks_are_four_to_six_seconds_apart_and_randomized() {
let (mut a, now) = connected(config(true));
let trace = run_traced(&mut a, now, now + Duration::from_secs(600), |_, _| true);
let gaps = intervals(&trace);
assert!(
(100..=150).contains(&trace.requests.len()),
"{} consent checks in 600 s",
trace.requests.len()
);
let (min, max) = (gaps.iter().min().unwrap(), gaps.iter().max().unwrap());
assert!(*min >= Duration::from_secs(4), "shortest interval {min:?}");
assert!(*max <= Duration::from_secs(6), "longest interval {max:?}");
assert!(
*max - *min > Duration::from_millis(500),
"intervals are not randomized: {min:?} to {max:?}"
);
let mut ids: Vec<_> = trace
.requests
.iter()
.map(|(_, m, _)| m.transaction_id)
.collect();
ids.sort_by_key(|id| id.0);
ids.dedup();
assert_eq!(
ids.len(),
trace.requests.len(),
"a transaction id was reused"
);
assert!(
trace
.requests
.iter()
.all(|(_, _, to)| *to == addr(REMOTE_A))
);
assert!(trace.states.is_empty(), "state changed: {:?}", trace.states);
}
#[test]
fn test_a_consent_period_under_the_minimum_is_raised() {
for period in [Duration::from_millis(100), Duration::from_secs(4)] {
let (mut a, now) = connected(AgentConfig {
keepalive_interval: Some(period),
..config(true)
});
let trace = run_traced(&mut a, now, now + Duration::from_secs(120), |_, _| true);
let gaps = intervals(&trace);
assert!(gaps.len() >= 15);
assert!(
gaps.iter().all(|gap| *gap >= Duration::from_secs(4)),
"period {period:?}: {:?}",
gaps.iter().min()
);
}
}
#[test]
fn test_consent_expires_after_thirty_seconds_whatever_the_timeouts() {
for (disconnected, failed) in [
(Duration::from_secs(5), Duration::ZERO),
(Duration::from_secs(3600), Duration::from_secs(3600)),
] {
let (mut a, now) = connected(AgentConfig {
disconnected_timeout: Some(disconnected),
failed_timeout: Some(failed),
..config(true)
});
let last_consent = a.remote_candidates[0].last_consent().unwrap();
let trace = run_traced(&mut a, now, now + Duration::from_secs(120), |_, _| false);
let failed_at = trace
.states
.iter()
.find(|(_, s)| *s == ConnectionState::Failed)
.map(|(at, _)| *at)
.expect("consent expires");
assert_eq!(
failed_at.duration_since(last_consent),
Duration::from_secs(30)
);
assert!(a.media_path().is_none(), "nothing is sent without consent");
assert!(a.pending_binding_requests.is_empty());
assert!(trace.requests.len() >= 4);
assert!(a.poll_timeout().is_none());
}
}
#[test]
fn test_shorter_timeouts_fail_a_silent_pair_sooner() {
let (mut a, now) = connected(AgentConfig {
disconnected_timeout: Some(Duration::from_secs(5)),
failed_timeout: Some(Duration::from_secs(7)),
..config(true)
});
let last_consent = a.remote_candidates[0].last_consent().unwrap();
let trace = run_traced(&mut a, now, now + Duration::from_secs(60), |_, _| false);
let (failed_at, _) = trace
.states
.iter()
.find(|(_, s)| *s == ConnectionState::Failed)
.unwrap();
assert_eq!(
failed_at.duration_since(last_consent),
Duration::from_secs(12)
);
}
#[test]
fn test_disconnected_after_an_unanswered_consent_request_and_back() {
let (mut a, now) = connected(config(true));
let silent_from = now + Duration::from_secs(20);
let silent_until = silent_from + Duration::from_secs(14);
let trace = run_traced(&mut a, now, now + Duration::from_secs(60), |at, _| {
at < silent_from || at >= silent_until
});
let first_unanswered = trace
.requests
.iter()
.map(|(at, _, _)| *at)
.find(|at| *at >= silent_from)
.unwrap();
assert_eq!(trace.states.len(), 2, "{:?}", trace.states);
assert_eq!(trace.states[0].1, ConnectionState::Disconnected);
assert_eq!(
trace.states[0].0.duration_since(first_unanswered),
Duration::from_secs(5)
);
assert_eq!(trace.states[1].1, ConnectionState::Connected);
assert!(trace.states[1].0 >= silent_until);
assert!(a.media_path().is_some());
}
fn old_generation_request(local: &Credentials) -> Message {
let mut msg = Message::new();
msg.build(&[
Box::new(BINDING_REQUEST),
Box::new(TransactionId::new()),
Box::new(Username::new(
ATTR_USERNAME,
local.ufrag.clone() + ":" + REMOTE_UFRAG,
)),
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(MessageIntegrity::new_short_term_integrity_with_provider(
local.pwd.clone(),
provider().crypto(),
)),
Box::new(FINGERPRINT),
])
.unwrap();
msg
}
#[test]
fn test_consent_checks_continue_on_the_previous_path_during_a_restart() {
for keep_local_candidates in [true, false] {
let (mut a, now) = connected(config(true));
let old_local = a.ufrag_pwd.local_credentials.clone();
let path = a.media_path().unwrap();
a.restart(now, String::new(), String::new(), keep_local_candidates)
.unwrap();
assert_ne!(a.ufrag_pwd.local_credentials.ufrag, old_local.ufrag);
assert_eq!(a.media_path(), Some(path));
let trace = run_traced(&mut a, now, now + Duration::from_secs(90), |_, _| true);
assert_eq!(a.media_path(), Some(path), "an answered path was dropped");
assert!(
trace.requests.len() >= 14,
"{} checks",
trace.requests.len()
);
for gap in intervals(&trace) {
assert!(gap >= Duration::from_secs(4) && gap <= Duration::from_secs(6));
}
for (_, m, to) in &trace.requests {
assert_eq!(*to, path.peer_addr);
let mut username = Username::new(ATTR_USERNAME, String::new());
username.get_from(m).unwrap();
assert_eq!(
username.to_string(),
format!("{REMOTE_UFRAG}:{}", old_local.ufrag),
"consent on the old pair uses the old generation's credentials"
);
let mut copy = m.clone();
MessageIntegrity::check(&mut copy, REMOTE_PWD.as_bytes(), provider().crypto()).unwrap();
}
}
}
#[test]
fn test_previous_path_is_dropped_when_its_consent_expires() {
let (mut a, now) = connected(AgentConfig {
failed_timeout: Some(Duration::ZERO),
..config(true)
});
let last_consent = a.remote_candidates[0].last_consent().unwrap();
a.restart(now, String::new(), String::new(), true).unwrap();
assert!(a.media_path().is_some());
let mut dropped_at = None;
let mut t = now;
while t < now + Duration::from_secs(60) {
run_traced(&mut a, t, t, |_, _| false);
if a.media_path().is_none() {
dropped_at = Some(t);
break;
}
t = a
.poll_timeout()
.expect("a timer runs while the path is kept")
.max(t + Duration::from_millis(1));
}
let dropped_at = dropped_at.expect("the path is dropped");
assert_eq!(
dropped_at.duration_since(last_consent),
Duration::from_secs(30)
);
assert!(a.previous_generation.is_none());
}
#[test]
fn test_old_generation_consent_requests_are_answered_during_a_restart() {
for lite in [false, true] {
let cfg = AgentConfig {
lite,
candidate_types: if lite {
vec![CandidateType::Host]
} else {
vec![]
},
..config(false)
};
let start = Instant::now();
let mut a = started_agent(start, cfg, &[REMOTE_A]);
if lite {
let nominate = peer_request(
&a,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(crate::attributes::use_candidate::UseCandidateAttr::new()),
],
);
deliver(&mut a, start, &nominate, REMOTE_A).unwrap();
} else {
a.candidate_pairs[0].state = CandidatePairState::Succeeded;
a.remote_candidates[0].last_consent = Some(start);
a.set_selected_pair(Some(start), Some(0));
}
let _ = sent(&mut a);
let old_local = a.ufrag_pwd.local_credentials.clone();
let now = start + Duration::from_secs(1);
a.restart(now, String::new(), String::new(), false).unwrap();
assert!(a.local_candidates.is_empty());
let request = old_generation_request(&old_local);
deliver(&mut a, now, &request, REMOTE_A).unwrap();
let out = sent(&mut a);
assert_eq!(out.len(), 1, "lite={lite}");
let (response, to) = &out[0];
assert_eq!(response.typ, BINDING_SUCCESS);
assert_eq!(response.transaction_id, request.transaction_id);
assert_eq!(*to, addr(REMOTE_A));
let mut copy = response.clone();
MessageIntegrity::check(&mut copy, old_local.pwd.as_bytes(), provider().crypto()).unwrap();
assert!(
a.remote_candidates.is_empty(),
"no candidate of the new generation"
);
assert!(deliver(&mut a, now, &old_generation_request(&old_local), REMOTE_B).is_err());
assert!(sent(&mut a).is_empty());
}
}
#[test]
fn test_lite_previous_path_lives_on_the_peers_requests() {
let cfg = AgentConfig {
lite: true,
candidate_types: vec![CandidateType::Host],
..config(false)
};
let start = Instant::now();
let mut a = started_agent(start, cfg, &[REMOTE_A]);
let nominate = peer_request(
&a,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(crate::attributes::use_candidate::UseCandidateAttr::new()),
],
);
deliver(&mut a, start, &nominate, REMOTE_A).unwrap();
let old_local = a.ufrag_pwd.local_credentials.clone();
a.restart(start, String::new(), String::new(), true)
.unwrap();
let _ = sent(&mut a);
let mut now = start;
for _ in 0..10 {
now += Duration::from_secs(5);
deliver(&mut a, now, &old_generation_request(&old_local), REMOTE_A).unwrap();
let trace = run_traced(&mut a, now, now, |_, _| false);
assert!(trace.requests.is_empty(), "a lite agent sent a request");
assert!(a.media_path().is_some());
}
let _ = sent(&mut a);
run_traced(&mut a, now, now + Duration::from_secs(31), |_, _| false);
assert!(a.media_path().is_none());
}
#[test]
fn test_lite_agent_sends_binding_indications_as_keepalives() {
let cfg = AgentConfig {
lite: true,
candidate_types: vec![CandidateType::Host],
disconnected_timeout: Some(Duration::ZERO),
failed_timeout: Some(Duration::ZERO),
..config(false)
};
let start = Instant::now();
let mut a = started_agent(start, cfg, &[REMOTE_A]);
let nominate = peer_request(
&a,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(crate::attributes::use_candidate::UseCandidateAttr::new()),
],
);
deliver(&mut a, start, &nominate, REMOTE_A).unwrap();
let _ = sent(&mut a);
let mut indications = vec![];
let mut now = start;
while now < start + Duration::from_secs(100) {
a.handle_timeout(now).unwrap();
while let Some(t) = a.poll_write() {
let mut m = Message {
raw: t.message.to_vec(),
..Message::default()
};
m.decode().unwrap();
assert_eq!(m.typ, MessageType::new(METHOD_BINDING, CLASS_INDICATION));
assert_eq!(t.transport.peer_addr, addr(REMOTE_A));
assert_eq!(m.attributes.0.len(), 1, "FINGERPRINT and nothing else");
FINGERPRINT.check(&m).unwrap();
indications.push(t.now);
}
now = a
.poll_timeout()
.unwrap()
.max(now + Duration::from_millis(1));
}
assert!(
(4..=7).contains(&indications.len()),
"{}",
indications.len()
);
for pair in indications.windows(2) {
assert!(pair[1].duration_since(pair[0]) >= Duration::from_secs(15));
}
}
#[test]
fn test_a_pair_selected_long_after_its_last_consent_is_checked_at_once() {
use crate::attributes::use_candidate::UseCandidateAttr;
let start = Instant::now();
let mut a = started_agent(start, config(false), &[REMOTE_A]);
run_traced(&mut a, start, start + Duration::from_millis(500), |_, _| {
true
});
assert!(
a.get_selected_candidate_pair().is_none(),
"controlled: waits"
);
let later = start + Duration::from_secs(15);
let nomination = peer_request(
&a,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(UseCandidateAttr::new()),
],
);
deliver(&mut a, later, &nomination, REMOTE_A).unwrap();
assert!(a.get_selected_candidate_pair().is_some());
while a.poll_write().is_some() {}
let trace = run_traced(&mut a, later, later + Duration::from_secs(1), |_, _| true);
assert_eq!(
1,
trace.requests.len(),
"a consent check at the selection: {:?}",
trace
.requests
.iter()
.map(|(at, _, _)| *at - later)
.collect::<Vec<_>>()
);
}
#[test]
fn test_a_pair_selected_at_once_is_checked_an_interval_later() {
let (mut a, now) = connected(config(true));
let trace = run_traced(&mut a, now, now + Duration::from_secs(10), |_, _| true);
let first = trace.requests.first().expect("a consent check").0;
let after = first - now;
assert!(after >= Duration::from_millis(3500), "{after:?}");
}