use std::sync::Arc;
use std::time::{Duration, Instant};
use sansio::Protocol;
use stun::attributes::{
ATTR_ICE_CONTROLLED, ATTR_ICE_CONTROLLING, ATTR_PRIORITY, ATTR_USE_CANDIDATE, ATTR_USERNAME,
};
use stun::error_code::{CODE_BAD_REQUEST, CODE_ROLE_CONFLICT, ErrorCode, ErrorCodeAttribute};
use super::*;
use crate::attributes::control::{AttrControlled, AttrControlling};
use crate::attributes::priority::PriorityAttr;
use crate::attributes::use_candidate::UseCandidateAttr;
use crate::candidate::candidate_host::CandidateHostConfig;
pub(super) fn provider() -> Arc<dyn crypto::RTCCryptoProvider> {
crypto::default_provider().expect("a built-in crypto provider must be enabled for tests")
}
pub(super) const LOCAL: &str = "192.168.0.2:1000";
pub(super) const REMOTE_A: &str = "192.168.0.3:2000";
pub(super) const REMOTE_B: &str = "192.168.0.4:3000";
pub(super) const REMOTE_UFRAG: &str = "remoteufrag";
pub(super) const REMOTE_PWD: &str = "remotepwdremotepwdremotepwd";
pub(super) fn addr(s: &str) -> SocketAddr {
s.parse().unwrap()
}
pub(super) fn host(addr: SocketAddr) -> Candidate {
host_with_priority(addr, 0)
}
pub(super) fn host_with_priority(addr: SocketAddr, priority: u32) -> Candidate {
CandidateHostConfig {
base_config: CandidateConfig {
network: "udp".to_owned(),
address: addr.ip().to_string(),
port: addr.port(),
component: 1,
priority,
..Default::default()
},
..Default::default()
}
.new_candidate_host()
.unwrap()
}
pub(super) fn bare_agent(now: Instant, config: AgentConfig) -> Agent {
let mut a = Agent::new(now, Arc::new(config), provider()).unwrap();
a.add_local_candidate(host(addr(LOCAL))).unwrap();
a
}
pub(super) fn started_agent(now: Instant, config: AgentConfig, remotes: &[&str]) -> Agent {
let controlling = config.is_controlling;
let mut a = bare_agent(now, config);
for remote in remotes {
a.add_remote_candidate(host(addr(remote))).unwrap();
}
a.start_connectivity_checks(
now,
controlling,
REMOTE_UFRAG.to_owned(),
REMOTE_PWD.to_owned(),
)
.unwrap();
a
}
pub(super) fn config(controlling: bool) -> AgentConfig {
AgentConfig {
is_controlling: controlling,
..AgentConfig::default()
}
}
pub(super) fn peer_request(a: &Agent, extra: Vec<Box<dyn Setter>>) -> Message {
let username = a.ufrag_pwd.local_credentials.ufrag.clone() + ":" + REMOTE_UFRAG;
let provider = provider();
let mut setters: Vec<Box<dyn Setter>> = vec![
Box::new(BINDING_REQUEST),
Box::new(TransactionId::new()),
Box::new(Username::new(ATTR_USERNAME, username)),
];
setters.extend(extra);
setters.push(Box::new(
MessageIntegrity::new_short_term_integrity_with_provider(
a.ufrag_pwd.local_credentials.pwd.clone(),
provider.crypto(),
),
));
setters.push(Box::new(FINGERPRINT));
let mut msg = Message::new();
msg.build(&setters).unwrap();
msg
}
pub(super) fn peer_response(
typ: MessageType,
id: TransactionId,
mapped: SocketAddr,
error: Option<ErrorCode>,
) -> Message {
let provider = provider();
let mut setters: Vec<Box<dyn Setter>> = vec![Box::new(typ), Box::new(id)];
match error {
Some(code) => setters.push(Box::new(code)),
None => setters.push(Box::new(XorMappedAddress {
ip: mapped.ip(),
port: mapped.port(),
})),
}
setters.push(Box::new(
MessageIntegrity::new_short_term_integrity_with_provider(
REMOTE_PWD.to_owned(),
provider.crypto(),
),
));
setters.push(Box::new(FINGERPRINT));
let mut msg = Message::new();
msg.build(&setters).unwrap();
msg
}
pub(super) fn deliver_raw(a: &mut Agent, now: Instant, raw: &[u8], from: &str) -> Result<()> {
a.handle_read(TaggedBytesMut {
now,
transport: TransportContext {
local_addr: addr(LOCAL),
peer_addr: addr(from),
ecn: None,
transport_protocol: TransportProtocol::UDP,
},
message: BytesMut::from(raw),
})
}
pub(super) fn deliver(a: &mut Agent, now: Instant, m: &Message, from: &str) -> Result<()> {
deliver_raw(a, now, &m.raw, from)
}
pub(super) fn sent(a: &mut Agent) -> Vec<(Message, SocketAddr)> {
let mut out = vec![];
while let Some(t) = a.poll_write() {
let mut m = Message {
raw: t.message.to_vec(),
..Message::default()
};
m.decode().unwrap();
out.push((m, t.transport.peer_addr));
}
out
}
pub(super) fn requests(sent: &[(Message, SocketAddr)]) -> Vec<&(Message, SocketAddr)> {
sent.iter()
.filter(|(m, _)| m.typ == BINDING_REQUEST)
.collect()
}
pub(super) fn error_code(m: &Message) -> u16 {
let mut code = ErrorCodeAttribute::default();
code.get_from(m).unwrap();
code.code.0
}
pub(super) fn run(a: &mut Agent, from: Instant, until: Instant) -> Vec<(Message, SocketAddr)> {
let mut out = vec![];
let mut now = from;
loop {
a.handle_timeout(now).unwrap();
out.extend(sent(a));
match a.poll_timeout() {
Some(next) if next <= until => now = next.max(now + Duration::from_millis(1)),
_ => break,
}
if now > until {
break;
}
}
out
}
pub(super) fn run_answering(
a: &mut Agent,
from: Instant,
until: Instant,
mut answer: impl FnMut(&Message, SocketAddr) -> bool,
) -> Vec<(Message, SocketAddr)> {
let mut out = vec![];
let mut now = from;
loop {
a.handle_timeout(now).unwrap();
loop {
let batch = sent(a);
if batch.is_empty() {
break;
}
for (m, to) in &batch {
if m.typ == BINDING_REQUEST && answer(m, *to) {
let response =
peer_response(BINDING_SUCCESS, m.transaction_id, addr(LOCAL), None);
let _ = deliver(a, now, &response, &to.to_string());
}
}
out.extend(batch);
}
match a.poll_timeout() {
Some(next) if next <= until => now = next.max(now + Duration::from_millis(1)),
_ => break,
}
}
out
}
fn first_check(a: &mut Agent, now: Instant) -> (Message, SocketAddr) {
let out = run(a, now, now + Duration::from_millis(100));
requests(&out)
.first()
.map(|(m, to)| (m.clone(), *to))
.expect("the agent sends a check")
}
#[test]
fn test_ice_restart_draws_a_new_tie_breaker_only_when_configured() {
const FIXED: u64 = 0x0123_4567_89ab_cdef;
let controlling_value = |m: &Message| {
let mut attr = AttrControlling::default();
attr.get_from(m).unwrap();
attr.0
};
for new_on_restart in [true, false] {
let now = Instant::now();
let cfg = AgentConfig {
tie_breaker: Some(FIXED),
new_tie_breaker_on_restart: new_on_restart,
..config(true)
};
let mut a = started_agent(now, cfg, &[REMOTE_A]);
let (check, _) = first_check(&mut a, now);
assert_eq!(controlling_value(&check), FIXED);
let later = now + Duration::from_secs(1);
a.restart(later, String::new(), String::new(), true)
.unwrap();
let after = a.tie_breaker();
if new_on_restart {
assert_ne!(after, FIXED, "a new tiebreaker is drawn");
} else {
assert_eq!(after, FIXED, "the tiebreaker stays");
}
a.set_remote_credentials(
"restartufrag".to_owned(),
"restartpwdrestartpwdrestart".to_owned(),
)
.unwrap();
a.add_remote_candidate(host(addr(REMOTE_B))).unwrap();
let (check, _) = first_check(&mut a, later);
assert_eq!(controlling_value(&check), after);
let before = a.tie_breaker();
a.generate_restart_credentials(String::new(), String::new())
.unwrap();
a.apply_restart_revertible(later, true).unwrap();
assert_eq!(a.tie_breaker() != before, new_on_restart);
assert!(a.revert_restart(later));
assert_eq!(a.tie_breaker(), before);
}
assert!(!AgentConfig::default().new_tie_breaker_on_restart);
}
#[test]
fn test_role_attribute_after_message_integrity_is_ignored() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
a.tie_breaker = 100;
let _ = sent(&mut a);
let mut m = peer_request(
&a,
vec![Box::new(AttrControlling(5)), Box::new(PriorityAttr(1000))],
);
m.add(ATTR_ICE_CONTROLLED, &0u64.to_be_bytes());
assert!(m.contains(ATTR_ICE_CONTROLLED));
assert!(deliver(&mut a, now, &m, REMOTE_A).is_err());
assert!(!a.is_controlling);
assert!(sent(&mut a).is_empty());
let mut m = Message::new();
m.build(&[
Box::new(BINDING_REQUEST),
Box::new(TransactionId::new()),
Box::new(Username::new(
ATTR_USERNAME,
a.ufrag_pwd.local_credentials.ufrag.clone() + ":" + REMOTE_UFRAG,
)),
Box::new(AttrControlling(5)),
Box::new(PriorityAttr(1000)),
Box::new(MessageIntegrity::new_short_term_integrity_with_provider(
a.ufrag_pwd.local_credentials.pwd.clone(),
provider().crypto(),
)),
])
.unwrap();
m.add(ATTR_ICE_CONTROLLED, &0u64.to_be_bytes());
assert!(m.contains(ATTR_ICE_CONTROLLED));
deliver(&mut a, now, &m, REMOTE_A).unwrap();
assert!(
!a.is_controlling,
"an unauthenticated attribute flipped the role"
);
let out = sent(&mut a);
let answers: Vec<_> = out
.iter()
.filter(|(r, _)| r.transaction_id == m.transaction_id)
.collect();
assert_eq!(answers.len(), 1);
assert_eq!(answers[0].0.typ, BINDING_SUCCESS);
}
fn conflict_case(
controlling: bool,
own: u64,
theirs: u64,
) -> (Agent, Vec<(Message, SocketAddr)>, TransactionId) {
let now = Instant::now();
let mut a = started_agent(now, config(controlling), &[REMOTE_A]);
a.tie_breaker = own;
let _ = sent(&mut a);
let role: Box<dyn Setter> = if controlling {
Box::new(AttrControlling(theirs))
} else {
Box::new(AttrControlled(theirs))
};
let m = peer_request(&a, vec![role, Box::new(PriorityAttr(1000))]);
deliver(&mut a, now, &m, REMOTE_A).unwrap();
let out = sent(&mut a)
.into_iter()
.filter(|(r, _)| r.transaction_id == m.transaction_id)
.collect();
(a, out, m.transaction_id)
}
#[test]
fn test_role_conflict_controlling_winner_answers_487_only() {
for theirs in [100, 500] {
let (a, out, _) = conflict_case(true, 500, theirs);
assert!(a.is_controlling);
assert_eq!(out.len(), 1, "one transaction, one response");
assert_eq!(out[0].0.typ, BINDING_ERROR);
assert_eq!(error_code(&out[0].0), CODE_ROLE_CONFLICT.0);
assert_eq!(out[0].1, addr(REMOTE_A));
}
}
#[test]
fn test_role_conflict_controlling_loser_switches_and_answers_success_only() {
let (a, out, _) = conflict_case(true, 100, 500);
assert!(!a.is_controlling);
assert_eq!(out.len(), 1);
assert_eq!(out[0].0.typ, BINDING_SUCCESS);
}
#[test]
fn test_role_conflict_controlled_winner_or_equal_switches_to_controlling() {
for theirs in [3, 7] {
let (a, out, _) = conflict_case(false, 7, theirs);
assert!(a.is_controlling, "tiebreaker 7 against {theirs}");
assert_eq!(out.len(), 1);
assert_eq!(out[0].0.typ, BINDING_SUCCESS);
}
}
#[test]
fn test_role_conflict_controlled_loser_answers_487_only() {
let (a, out, _) = conflict_case(false, 7, 9);
assert!(!a.is_controlling);
assert_eq!(out.len(), 1);
assert_eq!(out[0].0.typ, BINDING_ERROR);
assert_eq!(error_code(&out[0].0), CODE_ROLE_CONFLICT.0);
}
#[test]
fn test_487_response_switches_role_changes_tiebreaker_and_rechecks() {
for controlling in [true, false] {
role_conflict_response_case(controlling);
}
}
fn role_conflict_response_case(controlling: bool) {
let (was, becomes) = if controlling {
(ATTR_ICE_CONTROLLING, ATTR_ICE_CONTROLLED)
} else {
(ATTR_ICE_CONTROLLED, ATTR_ICE_CONTROLLING)
};
let now = Instant::now();
let mut a = started_agent(now, config(controlling), &[REMOTE_A]);
let tie_breaker = a.tie_breaker;
let (check, to) = first_check(&mut a, now);
assert!(check.contains(was));
let later = now + Duration::from_millis(120);
let response = peer_response(
BINDING_ERROR,
check.transaction_id,
addr(LOCAL),
Some(CODE_ROLE_CONFLICT),
);
deliver(&mut a, later, &response, &to.to_string()).unwrap();
assert_eq!(
a.is_controlling, !controlling,
"a 487 response must switch the role"
);
assert_ne!(a.tie_breaker, tie_breaker, "the tiebreaker must change");
let out = run(&mut a, later, later + Duration::from_secs(1));
let again = requests(&out);
let (recheck, recheck_to) = again.first().expect("the pair is checked again");
assert_eq!(*recheck_to, to);
assert_ne!(recheck.transaction_id, check.transaction_id);
assert!(recheck.contains(becomes));
assert!(!recheck.contains(was));
let new_tie_breaker = u64::from_be_bytes(recheck.get(becomes).unwrap().try_into().unwrap());
assert_eq!(new_tie_breaker, a.tie_breaker);
}
#[test]
fn test_unauthenticated_or_unsolicited_487_is_ignored() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
let (check, to) = first_check(&mut a, now);
let mut unsigned = Message::new();
unsigned
.build(&[
Box::new(BINDING_ERROR),
Box::new(check.transaction_id),
Box::new(CODE_ROLE_CONFLICT),
Box::new(FINGERPRINT),
])
.unwrap();
let _ = deliver(&mut a, now, &unsigned, &to.to_string());
assert!(a.is_controlling);
let unsolicited = peer_response(
BINDING_ERROR,
TransactionId::new(),
addr(LOCAL),
Some(CODE_ROLE_CONFLICT),
);
let _ = deliver(&mut a, now, &unsolicited, &to.to_string());
assert!(a.is_controlling);
}
#[test]
fn test_error_response_fails_the_pair() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
let (check, to) = first_check(&mut a, now);
let response = peer_response(
BINDING_ERROR,
check.transaction_id,
addr(LOCAL),
Some(CODE_BAD_REQUEST),
);
deliver(&mut a, now, &response, &to.to_string()).unwrap();
assert_eq!(a.candidate_pairs[0].state, CandidatePairState::Failed);
assert!(a.is_controlling);
}
#[test]
fn test_a_server_error_to_a_check_fails_the_pair_and_is_not_resent() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
let (check, to) = first_check(&mut a, now);
let response = peer_response(
BINDING_ERROR,
check.transaction_id,
addr(LOCAL),
Some(stun::error_code::CODE_SERVER_ERROR),
);
deliver(&mut a, now, &response, &to.to_string()).unwrap();
assert_eq!(a.candidate_pairs[0].state, CandidatePairState::Failed);
let mut checks = vec![];
for ms in (50..=8_000).step_by(50) {
let _ = a.handle_timeout(now + Duration::from_millis(ms));
checks.extend(
sent(&mut a)
.into_iter()
.filter(|(m, dest)| m.typ == BINDING_REQUEST && *dest == to),
);
}
assert!(checks.is_empty(), "the check is not resent: {checks:?}");
}
#[test]
fn test_binding_request_before_remote_credentials_is_answered() {
let now = Instant::now();
let mut a = bare_agent(now, config(true));
assert!(a.ufrag_pwd.remote_credentials.is_none());
let m = peer_request(
&a,
vec![Box::new(AttrControlled(1)), Box::new(PriorityAttr(1234))],
);
deliver(&mut a, now, &m, REMOTE_A).unwrap();
let out = sent(&mut a);
assert_eq!(out.len(), 1, "exactly the response");
let (response, to) = &out[0];
assert_eq!(response.typ, BINDING_SUCCESS);
assert_eq!(response.transaction_id, m.transaction_id);
assert_eq!(*to, addr(REMOTE_A));
let mut mapped = XorMappedAddress::default();
mapped.get_from(response).unwrap();
assert_eq!(SocketAddr::new(mapped.ip, mapped.port), addr(REMOTE_A));
FINGERPRINT.check(response).unwrap();
assert_eq!(
response.attributes.0.last().unwrap().typ,
stun::attributes::ATTR_FINGERPRINT
);
let mut copy = response.clone();
MessageIntegrity::check(
&mut copy,
a.ufrag_pwd.local_credentials.pwd.as_bytes(),
provider().crypto(),
)
.unwrap();
}
#[test]
fn test_early_request_with_a_wrong_password_is_answered_401() {
let now = Instant::now();
let mut a = bare_agent(now, config(true));
let mut m = Message::new();
m.build(&[
Box::new(BINDING_REQUEST),
Box::new(TransactionId::new()),
Box::new(Username::new(
ATTR_USERNAME,
a.ufrag_pwd.local_credentials.ufrag.clone() + ":x",
)),
Box::new(MessageIntegrity::new_short_term_integrity_with_provider(
"not the password".to_owned(),
provider().crypto(),
)),
Box::new(FINGERPRINT),
])
.unwrap();
assert!(deliver(&mut a, now, &m, REMOTE_A).is_err());
let out = sent(&mut a);
assert_eq!(out.len(), 1);
assert_eq!(out[0].0.typ, BINDING_ERROR);
assert_eq!(out[0].0.transaction_id, m.transaction_id);
assert_eq!(error_code(&out[0].0), 401);
assert!(a.early_requests.is_empty());
}
#[test]
fn test_early_request_triggers_a_check_when_credentials_arrive() {
let now = Instant::now();
let mut a = bare_agent(now, config(true));
let m = peer_request(
&a,
vec![Box::new(AttrControlled(1)), Box::new(PriorityAttr(1234))],
);
deliver(&mut a, now, &m, REMOTE_A).unwrap();
let _ = sent(&mut a);
assert!(a.remote_candidates.is_empty());
let later = now + Duration::from_millis(30);
a.start_connectivity_checks(later, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
.unwrap();
let out = run(&mut a, later, later + Duration::from_millis(200));
assert_eq!(a.remote_candidates.len(), 1);
assert_eq!(
a.remote_candidates[0].candidate_type(),
CandidateType::PeerReflexive
);
assert_eq!(a.remote_candidates[0].priority(), 1234);
let checks = requests(&out);
assert!(
checks.iter().any(|(_, to)| *to == addr(REMOTE_A)),
"a triggered check goes to the early request's source"
);
assert!(out.iter().all(|(r, _)| r.typ != BINDING_SUCCESS));
}
#[test]
fn test_username_is_valid_when_its_first_part_is_the_local_ufrag() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let _ = sent(&mut a);
let provider = provider();
let build = |username: String| {
let mut m = Message::new();
m.build(&[
Box::new(BINDING_REQUEST),
Box::new(TransactionId::new()),
Box::new(Username::new(ATTR_USERNAME, username)),
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(99)),
Box::new(MessageIntegrity::new_short_term_integrity_with_provider(
a.ufrag_pwd.local_credentials.pwd.clone(),
provider.crypto(),
)),
Box::new(FINGERPRINT),
])
.unwrap();
m
};
let ufrag = a.ufrag_pwd.local_credentials.ufrag.clone();
let other_second_part = build(format!("{ufrag}:someoneelse"));
let wrong_first_part = build(format!("x{ufrag}:{REMOTE_UFRAG}"));
let no_colon = build(ufrag.clone());
let fragment_is_a_prefix_only = build(format!("{ufrag}x:{REMOTE_UFRAG}"));
deliver(&mut a, now, &other_second_part, REMOTE_A).unwrap();
assert_eq!(sent(&mut a)[0].0.typ, BINDING_SUCCESS);
assert!(deliver(&mut a, now, &wrong_first_part, REMOTE_A).is_err());
assert!(deliver(&mut a, now, &no_colon, REMOTE_A).is_err());
assert!(deliver(&mut a, now, &fragment_is_a_prefix_only, REMOTE_A).is_err());
let out = sent(&mut a);
assert_eq!(out.len(), 3);
assert!(
out.iter()
.all(|(m, _)| m.typ == BINDING_ERROR && error_code(m) == 401)
);
}
#[test]
fn test_check_carries_the_peer_reflexive_priority() {
for controlling in [true, false] {
let now = Instant::now();
let mut a = started_agent(now, config(controlling), &[REMOTE_A]);
let (check, _) = first_check(&mut a, now);
let mut priority = PriorityAttr::default();
priority.get_from(&check).unwrap();
assert_eq!(a.local_candidates[0].priority(), 2130706431);
assert_eq!(priority.0, (110 << 24) + (65535 << 8) + 255);
assert_eq!(priority.0, 1862270975);
}
}
#[test]
fn test_peer_reflexive_remote_candidate_takes_priority_from_the_request() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let m = peer_request(
&a,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1845501695)),
],
);
deliver(&mut a, now, &m, REMOTE_B).unwrap();
let index = a.find_remote_candidate(addr(REMOTE_B)).unwrap();
let prflx = &a.remote_candidates[index];
assert_eq!(prflx.candidate_type(), CandidateType::PeerReflexive);
assert_eq!(prflx.priority(), 1845501695);
assert_eq!(prflx.component(), a.local_candidates[0].component());
assert_ne!(prflx.foundation(), a.remote_candidates[0].foundation());
}
#[test]
fn test_lite_agent_selects_on_use_candidate_and_sends_no_checks() {
let now = Instant::now();
let cfg = AgentConfig {
lite: true,
candidate_types: vec![CandidateType::Host],
..config(false)
};
let mut a = started_agent(now, cfg, &[REMOTE_A]);
let plain = peer_request(
&a,
vec![Box::new(AttrControlling(1)), Box::new(PriorityAttr(1000))],
);
deliver(&mut a, now, &plain, REMOTE_A).unwrap();
assert!(a.get_selected_candidate_pair().is_none());
let nominating = peer_request(
&a,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(UseCandidateAttr::new()),
],
);
deliver(&mut a, now, &nominating, REMOTE_A).unwrap();
let (_, remote) = a
.get_selected_candidate_pair()
.expect("USE-CANDIDATE alone selects the pair on a lite agent");
assert_eq!(remote.addr(), addr(REMOTE_A));
assert_eq!(
a.checklist_state(),
crate::agent::agent_selector::ChecklistState::Completed
);
let mut out = sent(&mut a);
out.extend(run(&mut a, now, now + Duration::from_secs(3)));
assert!(
requests(&out).is_empty(),
"a lite agent sends no Binding requests"
);
assert_eq!(
out.iter().filter(|(m, _)| m.typ == BINDING_SUCCESS).count(),
2
);
}
#[test]
fn test_request_on_a_succeeded_pair_triggers_no_check() {
for controlling in [true, false] {
let now = Instant::now();
let mut a = started_agent(now, config(controlling), &[REMOTE_A]);
let end = now + Duration::from_millis(300);
run_answering(&mut a, now, end, |_, _| true);
assert_eq!(a.candidate_pairs[0].state, CandidatePairState::Succeeded);
let _ = sent(&mut a);
let role: Box<dyn Setter> = if controlling {
Box::new(AttrControlled(1))
} else {
Box::new(AttrControlling(1))
};
let m = peer_request(&a, vec![role, Box::new(PriorityAttr(1000))]);
deliver(&mut a, end, &m, REMOTE_A).unwrap();
let out = sent(&mut a);
assert_eq!(out.len(), 1, "controlling={controlling}");
assert_eq!(out[0].0.typ, BINDING_SUCCESS);
}
}
#[test]
fn test_request_on_a_failed_pair_rechecks_it() {
for controlling in [true, false] {
let now = Instant::now();
let cfg = AgentConfig {
max_binding_requests: Some(2),
..config(controlling)
};
let mut a = started_agent(now, cfg, &[REMOTE_A]);
let mut end = now;
while a.candidate_pairs.first().map(|p| p.state) != Some(CandidatePairState::Failed) {
end += Duration::from_millis(100);
run(&mut a, end, end);
assert!(end < now + Duration::from_secs(60), "the pair never failed");
assert_ne!(a.state(), ConnectionState::Failed, "agent failed first");
}
let _ = sent(&mut a);
let role: Box<dyn Setter> = if controlling {
Box::new(AttrControlled(1))
} else {
Box::new(AttrControlling(1))
};
let m = peer_request(&a, vec![role, Box::new(PriorityAttr(1000))]);
deliver(&mut a, end, &m, REMOTE_A).unwrap();
assert_ne!(
a.candidate_pairs[0].state,
CandidatePairState::Failed,
"controlling={controlling}"
);
let mut out = sent(&mut a);
out.extend(run(&mut a, end, end + Duration::from_secs(2)));
assert!(
requests(&out).len() >= 2,
"controlling={controlling}: {} requests",
requests(&out).len()
);
}
}
#[test]
fn test_failed_nomination_falls_back_to_the_next_valid_pair() {
let now = Instant::now();
let mut a = bare_agent(
now,
AgentConfig {
failed_nomination: FailedNomination::Retry,
..config(true)
},
);
a.add_remote_candidate(host_with_priority(addr(REMOTE_A), 2130706431))
.unwrap();
a.add_remote_candidate(host_with_priority(addr(REMOTE_B), 2130706175))
.unwrap();
a.start_connectivity_checks(now, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
.unwrap();
let mut states = vec![];
let mut nominated_a = 0;
let end = now + Duration::from_secs(70);
let out = run_answering(&mut a, now, end, |m, to| {
if m.contains(ATTR_USE_CANDIDATE) && to == addr(REMOTE_A) {
nominated_a += 1;
return false;
}
true
});
while let Some(e) = a.poll_event() {
if let Event::ConnectionStateChange(s) = e.event {
states.push(s);
}
}
assert!(nominated_a >= 1, "the better pair is nominated first");
assert!(
!states.contains(&ConnectionState::Failed),
"the agent failed with a valid pair left: {states:?}"
);
let (_, remote) = a
.get_selected_candidate_pair()
.expect("the other valid pair is selected");
assert_eq!(remote.addr(), addr(REMOTE_B));
assert!(
out.iter()
.any(|(m, to)| m.contains(ATTR_USE_CANDIDATE) && *to == addr(REMOTE_B))
);
}
#[test]
fn test_restart_with_the_credentials_in_use_generates_new_ones() {
let now = Instant::now();
let mut a = bare_agent(now, config(true));
let old = a.get_local_credentials().clone();
a.generate_restart_credentials(old.ufrag.clone(), old.pwd.clone())
.unwrap();
let new = a.get_local_credentials().clone();
assert_ne!(new.ufrag, old.ufrag);
assert_ne!(new.pwd, old.pwd);
assert!(new.ufrag.len() * 8 >= 24 && new.pwd.len() * 8 >= 128);
a.generate_restart_credentials(
"freshufrag".to_owned(),
"freshpwdfreshpwdfreshpwd".to_owned(),
)
.unwrap();
assert_eq!(a.get_local_credentials().ufrag, "freshufrag");
}
#[test]
fn test_restart_keeps_the_role() {
for controlling in [true, false] {
let now = Instant::now();
let mut a = started_agent(now, config(controlling), &[REMOTE_A]);
a.restart(now, String::new(), String::new(), true).unwrap();
assert_eq!(a.role(), controlling);
}
}
#[test]
fn test_checks_carry_role_tiebreaker_integrity_and_fingerprint() {
for controlling in [true, false] {
let now = Instant::now();
let mut a = started_agent(now, config(controlling), &[REMOTE_A, REMOTE_B]);
let out = run(&mut a, now, now + Duration::from_secs(2));
let checks = requests(&out);
assert!(checks.len() >= 2);
for (m, _) in checks {
assert!(m.contains(ATTR_PRIORITY));
let mut username = Username::new(ATTR_USERNAME, String::new());
username.get_from(m).unwrap();
assert_eq!(
username.to_string(),
format!("{REMOTE_UFRAG}:{}", a.ufrag_pwd.local_credentials.ufrag)
);
if controlling {
let mut attr = AttrControlling::default();
attr.get_from(m).unwrap();
assert_eq!(attr.0, a.tie_breaker);
assert!(!m.contains(ATTR_ICE_CONTROLLED));
} else {
let mut attr = AttrControlled::default();
attr.get_from(m).unwrap();
assert_eq!(attr.0, a.tie_breaker);
assert!(!m.contains(ATTR_ICE_CONTROLLING));
assert!(!m.contains(ATTR_USE_CANDIDATE));
}
let mut copy = m.clone();
MessageIntegrity::check(&mut copy, REMOTE_PWD.as_bytes(), provider().crypto()).unwrap();
FINGERPRINT.check(m).unwrap();
assert_eq!(
m.attributes.0.last().unwrap().typ,
stun::attributes::ATTR_FINGERPRINT
);
}
}
}
fn candidate(network: &str, address: &str, port: u16, tcp_type: TcpType) -> Candidate {
CandidateHostConfig {
base_config: CandidateConfig {
network: network.to_owned(),
address: address.to_owned(),
port,
component: 1,
..Default::default()
},
tcp_type,
}
.new_candidate_host()
.unwrap()
}
#[test]
fn test_local_candidates_of_one_type_get_unique_interleaved_priorities() {
let mut a = Agent::new(Instant::now(), Arc::new(config(true)), provider()).unwrap();
for (address, port) in [
("192.0.2.1", 1000),
("192.0.2.2", 1000),
("192.0.2.3", 1000),
("2001:db8::1", 1000),
("2001:db8::2", 1000),
] {
assert!(
a.add_local_candidate(candidate("udp", address, port, TcpType::Unspecified))
.unwrap()
);
}
let local_preference = |i: usize| -> u32 { (a.local_candidates[i].priority() >> 8) & 0xffff };
assert_eq!(a.local_candidates[0].priority(), 2130706431);
assert_eq!(
(0..5).map(local_preference).collect::<Vec<_>>(),
vec![65535, 65533, 65531, 65534, 65532]
);
for (address, server) in [
("198.51.100.1", "turn:one.example"),
("198.51.100.2", "turn:two.example"),
] {
let relay = crate::candidate::candidate_relay::CandidateRelayConfig {
base_config: CandidateConfig {
network: "udp".to_owned(),
address: address.to_owned(),
port: 3478,
component: 1,
..Default::default()
},
rel_addr: "192.0.2.1".to_owned(),
rel_port: 1000,
url: Some(server.to_owned()),
}
.new_candidate_relay()
.unwrap();
assert!(a.add_local_candidate(relay).unwrap());
}
assert_eq!(a.local_candidates[5].priority(), 16777215);
assert_eq!(a.local_candidates[6].priority(), 16777215 - (2 << 8));
let mut priorities: Vec<u32> = a.local_candidates.iter().map(|c| c.priority()).collect();
priorities.sort_unstable();
priorities.dedup();
assert_eq!(priorities.len(), 7, "priorities are unique");
for c in &a.local_candidates {
assert!((1..=(1u32 << 31) - 1).contains(&c.priority()));
}
}
#[test]
fn test_candidates_gathered_together_give_ipv6_a_head_start() {
let udp = |address: &str| candidate("udp", address, 1000, TcpType::Unspecified);
let mut set: Vec<Candidate> = [
"192.0.2.1",
"192.0.2.2",
"2001:db8::1",
"2001:db8::2",
"2001:db8::3",
"2001:db8::4",
"2001:db8::5",
"2001:db8::6",
]
.into_iter()
.map(udp)
.collect();
crate::candidate::rank_local_candidates(&mut set);
let mut by_priority: Vec<&Candidate> = set.iter().collect();
by_priority.sort_by_key(|c| std::cmp::Reverse(c.priority()));
let order: Vec<String> = by_priority.iter().map(|c| c.address().to_owned()).collect();
assert_eq!(
order,
vec![
"2001:db8::1",
"2001:db8::2",
"2001:db8::3",
"2001:db8::4",
"192.0.2.1",
"2001:db8::5",
"192.0.2.2",
"2001:db8::6",
]
);
assert_eq!(by_priority[0].priority(), 2130706431);
let mut a = Agent::new(Instant::now(), Arc::new(config(true)), provider()).unwrap();
let added = a
.add_local_candidates(vec![udp("192.0.2.1"), udp("2001:db8::1")])
.unwrap();
assert_eq!(added, vec![true, true]);
a.add_local_candidate(udp("192.0.2.2")).unwrap();
let local_preference = |c: &Candidate| (c.priority() >> 8) & 0xffff;
assert_eq!(
a.local_candidates
.iter()
.map(|c| (c.address().to_owned(), local_preference(c)))
.collect::<Vec<_>>(),
vec![
("192.0.2.1".to_owned(), 65534),
("2001:db8::1".to_owned(), 65535),
("192.0.2.2".to_owned(), 65533),
]
);
let mut set = vec![udp("192.0.2.1"), udp("192.0.2.2"), udp("192.0.2.3")];
set[1].set_local_preference_rank(0);
crate::candidate::rank_local_candidates(&mut set);
assert_eq!(
set.iter()
.map(|c| c.local_preference_rank())
.collect::<Vec<_>>(),
vec![Some(1), Some(0), Some(2)]
);
}
#[test]
fn test_tcp_candidates_of_one_direction_get_unique_other_prefs() {
let mut a = Agent::new(Instant::now(), Arc::new(config(true)), provider()).unwrap();
a.add_local_candidate(candidate("tcp", "192.168.1.2", 4000, TcpType::Passive))
.unwrap();
a.add_local_candidate(candidate("tcp", "10.0.0.2", 4000, TcpType::Passive))
.unwrap();
a.add_local_candidate(candidate("tcp", "192.168.1.2", 9, TcpType::Active))
.unwrap();
let pref = |i: usize| a.local_candidates[i].local_preference();
assert_eq!(pref(0), (4 << 13) + 8191);
assert_eq!(pref(1), (4 << 13) + 8189);
assert_eq!(pref(2), (6 << 13) + 8191);
assert_ne!(
a.local_candidates[0].priority(),
a.local_candidates[1].priority()
);
}
#[test]
fn test_application_priorities_and_ranks_are_kept() {
let mut a = Agent::new(Instant::now(), Arc::new(config(true)), provider()).unwrap();
a.add_local_candidate(host_with_priority(addr("192.0.2.1:1000"), 12345))
.unwrap();
let mut unreliable = host(addr("192.0.2.2:1000"));
unreliable.set_local_preference_rank(1000);
a.add_local_candidate(unreliable).unwrap();
a.add_local_candidate(host(addr("192.0.2.3:1000"))).unwrap();
assert_eq!(a.local_candidates[0].priority(), 12345);
assert_eq!(a.local_candidates[1].local_preference(), 65535 - 1000);
assert_eq!(a.local_candidates[2].local_preference(), 65535);
}
#[test]
fn test_peer_reflexive_remote_candidate_is_paired_with_the_arrival_candidate_only() {
let now = Instant::now();
let mut a = bare_agent(now, config(false));
a.add_local_candidate(host(addr("192.168.0.9:1000")))
.unwrap();
a.start_connectivity_checks(now, false, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
.unwrap();
assert!(a.candidate_pairs.is_empty());
let m = peer_request(
&a,
vec![Box::new(AttrControlling(1)), Box::new(PriorityAttr(77))],
);
deliver(&mut a, now, &m, REMOTE_A).unwrap();
assert_eq!(a.remote_candidates.len(), 1);
assert_eq!(a.candidate_pairs.len(), 1);
let pair = a.candidate_pairs[0];
assert_eq!(a.local_candidates[pair.local_index].addr(), addr(LOCAL));
assert_eq!(pair.remote_priority, 77);
}
#[test]
fn test_request_with_an_unknown_required_attribute_is_answered_420() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[]);
let _ = sent(&mut a);
let unknown = stun::attributes::AttrType(0x7f00);
let m = peer_request(
&a,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(stun::attributes::RawAttribute {
typ: unknown,
length: 4,
value: vec![1, 2, 3, 4],
}),
],
);
assert!(deliver(&mut a, now, &m, REMOTE_A).is_err());
let out = sent(&mut a);
assert_eq!(out.len(), 1);
let (response, to) = &out[0];
assert_eq!(*to, addr(REMOTE_A));
assert_eq!(response.typ, BINDING_ERROR);
assert_eq!(response.transaction_id, m.transaction_id);
assert_eq!(error_code(response), 420);
let mut listed = stun::uattrs::UnknownAttributes(vec![]);
listed.get_from(response).unwrap();
assert_eq!(listed.0, vec![unknown]);
let mut copy = response.clone();
MessageIntegrity::check(
&mut copy,
a.ufrag_pwd.local_credentials.pwd.as_bytes(),
provider().crypto(),
)
.unwrap();
assert!(
a.remote_candidates.is_empty(),
"no candidate from such a request"
);
}
#[test]
fn test_message_with_a_wrong_fingerprint_is_discarded() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
let (check, to) = first_check(&mut a, now);
let corrupt = |m: &Message| {
let mut raw = m.raw.clone();
let last = raw.len() - 1;
raw[last] ^= 0xff;
raw
};
let response = peer_response(BINDING_SUCCESS, check.transaction_id, addr(LOCAL), None);
assert!(deliver_raw(&mut a, now, &corrupt(&response), &to.to_string()).is_err());
assert_eq!(a.candidate_pairs[0].state, CandidatePairState::InProgress);
assert_eq!(
a.pending_binding_requests.len(),
1,
"the transaction is still open"
);
let request = peer_request(
&a,
vec![Box::new(AttrControlled(1)), Box::new(PriorityAttr(1000))],
);
assert!(deliver_raw(&mut a, now, &corrupt(&request), REMOTE_B).is_err());
assert!(sent(&mut a).is_empty(), "nothing is answered");
assert_eq!(a.remote_candidates.len(), 1);
deliver(&mut a, now, &response, &to.to_string()).unwrap();
assert_eq!(a.candidate_pairs[0].state, CandidatePairState::Succeeded);
}
fn request_with(username: Option<String>, password: Option<&str>) -> Message {
let provider = provider();
let mut setters: Vec<Box<dyn Setter>> =
vec![Box::new(BINDING_REQUEST), Box::new(TransactionId::new())];
if let Some(username) = username {
setters.push(Box::new(Username::new(ATTR_USERNAME, username)));
}
setters.push(Box::new(AttrControlling(1)));
setters.push(Box::new(PriorityAttr(1000)));
if let Some(password) = password {
setters.push(Box::new(
MessageIntegrity::new_short_term_integrity_with_provider(
password.to_owned(),
provider.crypto(),
),
));
}
setters.push(Box::new(FINGERPRINT));
let mut msg = Message::new();
msg.build(&setters).unwrap();
msg
}
fn valid_username(a: &Agent) -> String {
a.ufrag_pwd.local_credentials.ufrag.clone() + ":" + REMOTE_UFRAG
}
#[test]
fn test_unauthenticated_requests_are_answered_400_or_401() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let _ = sent(&mut a);
let pwd = a.ufrag_pwd.local_credentials.pwd.clone();
let cases: Vec<(&str, Message, u16)> = vec![
("no credentials at all", request_with(None, None), 400),
(
"USERNAME without MESSAGE-INTEGRITY",
request_with(Some(valid_username(&a)), None),
400,
),
(
"MESSAGE-INTEGRITY without USERNAME",
request_with(None, Some(&pwd)),
400,
),
(
"a username that is not this agent's",
request_with(Some(format!("someoneelse:{REMOTE_UFRAG}")), Some(&pwd)),
401,
),
(
"a MESSAGE-INTEGRITY made with another password",
request_with(Some(valid_username(&a)), Some("not the password at all")),
401,
),
];
for (what, request, code) in cases {
assert!(deliver(&mut a, now, &request, REMOTE_B).is_err(), "{what}");
let out = sent(&mut a);
assert_eq!(out.len(), 1, "{what}");
let (response, to) = &out[0];
assert_eq!(*to, addr(REMOTE_B), "{what}");
assert_eq!(response.typ, BINDING_ERROR, "{what}");
assert_eq!(response.transaction_id, request.transaction_id, "{what}");
assert_eq!(error_code(response), code, "{what}");
assert!(!response.contains(ATTR_MESSAGE_INTEGRITY), "{what}");
assert!(
!response.contains(stun::attributes::ATTR_MESSAGE_INTEGRITY_SHA256),
"{what}"
);
assert!(!response.contains(ATTR_USERNAME), "{what}");
assert!(response.contains(ATTR_FINGERPRINT), "{what}");
assert!(response.raw.len() <= request.raw.len(), "{what}");
assert_eq!(response.raw.len(), 36, "{what}");
}
assert!(a.find_remote_candidate(addr(REMOTE_B)).is_none());
assert!(!a.is_controlling);
}
#[test]
fn test_error_responses_to_unauthenticated_requests_are_rate_limited() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let _ = sent(&mut a);
for _ in 0..50 {
let _ = deliver(&mut a, now, &request_with(None, None), REMOTE_B);
}
assert_eq!(
sent(&mut a).len(),
MAX_UNAUTHENTICATED_RESPONSE_BURST as usize
);
let later = now + UNAUTHENTICATED_RESPONSE_INTERVAL;
for _ in 0..50 {
let _ = deliver(&mut a, later, &request_with(None, None), REMOTE_B);
}
assert_eq!(sent(&mut a).len(), 1);
let much_later = later + Duration::from_secs(60);
for _ in 0..50 {
let _ = deliver(&mut a, much_later, &request_with(None, None), REMOTE_B);
}
assert_eq!(
sent(&mut a).len(),
MAX_UNAUTHENTICATED_RESPONSE_BURST as usize
);
let much_later = much_later + Duration::from_secs(60);
let mut bare = Message::new();
bare.build(&[Box::new(BINDING_REQUEST), Box::new(TransactionId::new())])
.unwrap();
assert_eq!(bare.raw.len(), 20);
let _ = deliver(&mut a, much_later, &bare, REMOTE_B);
assert!(sent(&mut a).is_empty());
let mut indication = Message::new();
indication
.build(&[
Box::new(MessageType::new(METHOD_BINDING, CLASS_INDICATION)),
Box::new(TransactionId::new()),
Box::new(PriorityAttr(1)),
Box::new(FINGERPRINT),
])
.unwrap();
let _ = deliver(&mut a, much_later, &indication, REMOTE_B);
assert!(sent(&mut a).is_empty());
let _ = deliver(&mut a, much_later, &request_with(None, None), "127.0.0.1:9");
assert!(sent(&mut a).is_empty());
}
#[test]
fn test_unauthenticated_requests_can_be_dropped_silently() {
let now = Instant::now();
let cfg = AgentConfig {
unauthenticated_requests: UnauthenticatedRequests::Drop,
..config(false)
};
let mut a = started_agent(now, cfg, &[REMOTE_A]);
let _ = sent(&mut a);
let pwd = a.ufrag_pwd.local_credentials.pwd.clone();
for request in [
request_with(None, None),
request_with(Some(format!("someoneelse:{REMOTE_UFRAG}")), Some(&pwd)),
request_with(Some(valid_username(&a)), Some("not the password at all")),
] {
assert!(deliver(&mut a, now, &request, REMOTE_B).is_err());
}
assert!(sent(&mut a).is_empty());
}
#[test]
fn test_configured_tiebreaker_is_sent() {
let now = Instant::now();
let cfg = AgentConfig {
tie_breaker: Some(0x0123_4567_89ab_cdef),
..config(true)
};
let mut a = started_agent(now, cfg, &[REMOTE_A]);
assert_eq!(a.tie_breaker(), 0x0123_4567_89ab_cdef);
let (check, _) = first_check(&mut a, now);
assert_eq!(
check.get(ATTR_ICE_CONTROLLING).unwrap(),
0x0123_4567_89ab_cdef_u64.to_be_bytes()
);
}
#[test]
fn test_set_role_reprioritises_the_pairs() {
let now = Instant::now();
let mut a = bare_agent(now, config(true));
a.add_remote_candidate(host_with_priority(addr(REMOTE_A), 5000))
.unwrap();
let controlling = a.candidate_pairs[0].priority();
a.candidate_pairs[0].nominate = true;
a.nominated_pair = Some(0);
a.set_role(false);
assert!(!a.role());
assert_ne!(a.candidate_pairs[0].priority(), controlling);
assert!(!a.candidate_pairs[0].nominate);
assert_eq!(a.nominated_pair, None);
a.set_role(true);
assert_eq!(a.candidate_pairs[0].priority(), controlling);
}
fn connected_controlling(now: Instant, tie_breaker: u64) -> Agent {
let cfg = AgentConfig {
tie_breaker: Some(tie_breaker),
..config(true)
};
let mut a = started_agent(now, cfg, &[REMOTE_A]);
run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
assert_eq!(a.state(), ConnectionState::Connected);
while a.poll_event().is_some() {}
a
}
#[test]
fn test_controlling_agent_that_wins_a_conflict_after_nominating_repeats_the_nomination() {
let now = Instant::now();
let mut a = connected_controlling(now, 100);
let later = now + Duration::from_secs(2);
let conflicting = peer_request(
&a,
vec![Box::new(AttrControlling(5)), Box::new(PriorityAttr(1000))],
);
deliver(&mut a, later, &conflicting, REMOTE_A).unwrap();
let out = sent(&mut a);
assert_eq!(out.len(), 1);
assert_eq!(out[0].0.typ, BINDING_ERROR);
assert_eq!(error_code(&out[0].0), CODE_ROLE_CONFLICT.0);
assert!(a.role(), "the larger tiebreaker keeps the controlling role");
let out = run_answering(&mut a, later, later + Duration::from_secs(1), |_, _| true);
let nominations: Vec<_> = requests(&out)
.into_iter()
.filter(|(m, _)| m.contains(ATTR_USE_CANDIDATE))
.collect();
assert_eq!(nominations.len(), 1, "the nomination is repeated, once");
let (nomination, to) = nominations[0];
assert_eq!(*to, addr(REMOTE_A));
assert_eq!(
nomination.get(ATTR_ICE_CONTROLLING).unwrap(),
100u64.to_be_bytes()
);
assert_eq!(
a.get_selected_candidate_pair().unwrap().1.addr(),
addr(REMOTE_A)
);
assert_eq!(a.state(), ConnectionState::Connected);
assert!(a.poll_event().is_none());
let out = run_answering(
&mut a,
later + Duration::from_secs(1),
later + Duration::from_secs(20),
|_, _| true,
);
assert!(requests(&out).len() >= 3);
assert!(
requests(&out)
.iter()
.all(|(m, _)| !m.contains(ATTR_USE_CANDIDATE))
);
}
#[test]
fn test_controlling_agent_that_loses_a_conflict_after_nominating_becomes_controlled() {
let now = Instant::now();
let mut a = connected_controlling(now, 5);
let later = now + Duration::from_secs(2);
let conflicting = peer_request(
&a,
vec![Box::new(AttrControlling(100)), Box::new(PriorityAttr(1000))],
);
deliver(&mut a, later, &conflicting, REMOTE_A).unwrap();
let out = sent(&mut a);
assert_eq!(out.len(), 1);
assert_eq!(out[0].0.typ, BINDING_SUCCESS);
assert!(!a.role());
let events: Vec<Event> = std::iter::from_fn(|| a.poll_event())
.map(|e| e.event)
.collect();
assert!(matches!(events.as_slice(), [Event::RoleChange(false)]));
let nomination = peer_request(
&a,
vec![
Box::new(AttrControlling(100)),
Box::new(PriorityAttr(1000)),
Box::new(UseCandidateAttr::new()),
],
);
deliver(&mut a, later, &nomination, REMOTE_A).unwrap();
assert_eq!(sent(&mut a)[0].0.typ, BINDING_SUCCESS);
assert_eq!(
a.get_selected_candidate_pair().unwrap().1.addr(),
addr(REMOTE_A)
);
assert_eq!(a.state(), ConnectionState::Connected);
assert!(a.poll_event().is_none(), "the pair in use did not change");
let out = run_answering(&mut a, later, later + Duration::from_secs(20), |_, _| true);
assert!(requests(&out).len() >= 3);
assert!(requests(&out).iter().all(|(m, _)| {
m.contains(ATTR_ICE_CONTROLLED)
&& !m.contains(ATTR_ICE_CONTROLLING)
&& !m.contains(ATTR_USE_CANDIDATE)
}));
assert_eq!(a.state(), ConnectionState::Connected);
}
fn datagram(local: &str, from: &str) -> TransportContext {
TransportContext {
local_addr: addr(local),
peer_addr: addr(from),
ecn: None,
transport_protocol: TransportProtocol::UDP,
}
}
fn two_valid_pairs(now: Instant, cfg: AgentConfig) -> Agent {
const SILENT: &str = "192.168.0.5:4000";
let mut a = started_agent(now, cfg, &[REMOTE_A, REMOTE_B, SILENT]);
run_answering(&mut a, now, now + Duration::from_secs(1), |_, to| {
to != addr(SILENT)
});
assert!(a.get_selected_candidate_pair().is_none());
assert_eq!(a.candidate_pairs.iter().filter(|p| p.valid).count(), 2);
a
}
fn nominate(a: &mut Agent, now: Instant, from: &str) {
let m = peer_request(
a,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(UseCandidateAttr::new()),
],
);
deliver(a, now, &m, from).unwrap();
let _ = sent(a);
}
#[test]
fn test_data_is_accepted_from_every_valid_pair() {
let now = Instant::now();
let mut a = two_valid_pairs(now, config(false));
assert!(a.media_path().is_none());
assert!(!a.accepts_data(datagram(LOCAL, REMOTE_A)));
nominate(&mut a, now + Duration::from_secs(1), REMOTE_A);
assert_eq!(a.media_path().unwrap().peer_addr, addr(REMOTE_A));
assert!(matches!(
a.data_path(datagram(LOCAL, REMOTE_A)),
Some(DataPath::InUse)
));
match a.data_path(datagram(LOCAL, REMOTE_B)) {
Some(DataPath::Valid(local, remote)) => {
assert_eq!(local.addr(), addr(LOCAL));
assert_eq!(remote.addr(), addr(REMOTE_B));
}
other => panic!("data from the other valid pair: {other:?}"),
}
assert!(
!a.accepts_data(datagram(LOCAL, "192.168.0.5:4000")),
"a remote candidate no check was answered from"
);
assert!(
!a.accepts_data(datagram(LOCAL, "192.168.0.9:4000")),
"an address that is no candidate"
);
assert!(
!a.accepts_data(datagram("192.168.0.2:1001", REMOTE_B)),
"an address that is no local candidate"
);
assert_eq!(a.media_path().unwrap().peer_addr, addr(REMOTE_A));
}
#[test]
fn test_data_flows_on_the_pair_being_nominated_until_its_response_comes() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
assert!(a.media_path().is_none(), "nothing is valid yet");
let out = run_answering(&mut a, now, now + Duration::from_secs(1), |m, _| {
!m.contains(ATTR_USE_CANDIDATE)
});
assert!(
requests(&out)
.iter()
.any(|(m, to)| m.contains(ATTR_USE_CANDIDATE) && *to == addr(REMOTE_A)),
"the pair is being nominated"
);
assert!(a.get_selected_candidate_pair().is_none());
let path = a.media_path().expect("the nominated pair's path");
assert_eq!(path.local_addr, addr(LOCAL));
assert_eq!(path.peer_addr, addr(REMOTE_A));
assert!(matches!(
a.data_path(datagram(LOCAL, REMOTE_A)),
Some(DataPath::InUse)
));
let _ = run(
&mut a,
now + Duration::from_secs(1),
now + Duration::from_secs(31),
);
assert_eq!(
a.nominated_pair,
Some(0),
"the nomination is still in flight"
);
assert!(a.media_path().is_none());
assert!(!a.accepts_data(datagram(LOCAL, REMOTE_A)));
let _ = run(
&mut a,
now + Duration::from_secs(31),
now + Duration::from_secs(60),
);
assert!(a.media_path().is_none());
}
#[test]
fn test_data_can_be_restricted_to_the_selected_pair() {
let now = Instant::now();
let cfg = AgentConfig {
data_from_selected_pair_only: true,
..config(false)
};
let mut a = two_valid_pairs(now, cfg);
nominate(&mut a, now + Duration::from_secs(1), REMOTE_A);
assert!(a.accepts_data(datagram(LOCAL, REMOTE_A)));
assert!(!a.accepts_data(datagram(LOCAL, REMOTE_B)));
}
#[test]
fn test_data_can_be_sent_on_a_valid_pair_before_the_nomination() {
let now = Instant::now();
let cfg = AgentConfig {
send_data_before_nomination: true,
..config(false)
};
let mut a = bare_agent(now, cfg);
a.add_remote_candidate(host_with_priority(addr(REMOTE_A), 1000))
.unwrap();
a.add_remote_candidate(host_with_priority(addr(REMOTE_B), 2000))
.unwrap();
a.start_connectivity_checks(now, false, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
.unwrap();
assert!(a.media_path().is_none(), "no valid pair yet");
run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
assert!(a.get_selected_candidate_pair().is_none());
let path = a.media_path().expect("the best valid pair");
assert_eq!(path.peer_addr, addr(REMOTE_B));
assert_eq!(path.local_addr, addr(LOCAL));
assert!(a.accepts_data(datagram(LOCAL, REMOTE_A)));
nominate(&mut a, now + Duration::from_secs(1), REMOTE_A);
assert_eq!(a.media_path().unwrap().peer_addr, addr(REMOTE_A));
}
#[test]
fn test_a_retransmitted_check_gets_the_same_answer_and_changes_nothing() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let _ = sent(&mut a);
let request = peer_request(
&a,
vec![Box::new(AttrControlling(1)), Box::new(PriorityAttr(1234))],
);
let mut answers = vec![];
for at in [now, now + Duration::from_millis(500)] {
deliver(&mut a, at, &request, REMOTE_A).unwrap();
let out = sent(&mut a);
let responses: Vec<_> = out
.iter()
.filter(|(m, _)| m.typ == BINDING_SUCCESS)
.cloned()
.collect();
assert_eq!(
responses.len(),
1,
"each copy of the request is answered once"
);
answers.push(responses[0].clone());
assert_eq!(
a.remote_candidates.len(),
1,
"no candidate learned from a copy"
);
assert_eq!(a.candidate_pairs.len(), 1);
}
for (response, to) in &answers {
assert_eq!(*to, addr(REMOTE_A));
assert_eq!(response.transaction_id, request.transaction_id);
let mut mapped = XorMappedAddress::default();
mapped.get_from(response).unwrap();
assert_eq!(SocketAddr::new(mapped.ip, mapped.port), addr(REMOTE_A));
assert!(
!response.contains(ATTR_USERNAME),
"a response carries no USERNAME"
);
let mut copy = response.clone();
MessageIntegrity::check(
&mut copy,
a.ufrag_pwd.local_credentials.pwd.as_bytes(),
provider().crypto(),
)
.unwrap();
FINGERPRINT.check(response).unwrap();
assert_eq!(
response.attributes.0.last().unwrap().typ,
stun::attributes::ATTR_FINGERPRINT
);
}
let types = |m: &Message| m.attributes.0.iter().map(|a| a.typ).collect::<Vec<_>>();
assert_eq!(
types(&answers[0].0),
types(&answers[1].0),
"the same answer"
);
}
#[test]
fn test_every_message_the_agent_sends_ends_with_fingerprint() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A, REMOTE_B]);
let mut out = run(&mut a, now, now + Duration::from_secs(3));
let request = peer_request(
&a,
vec![Box::new(AttrControlled(1)), Box::new(PriorityAttr(1234))],
);
deliver(&mut a, now + Duration::from_secs(3), &request, REMOTE_A).unwrap();
out.extend(sent(&mut a));
assert!(
out.iter().filter(|(m, _)| m.typ == BINDING_REQUEST).count() > 2,
"checks and retransmissions"
);
assert!(out.iter().any(|(m, _)| m.typ == BINDING_SUCCESS));
for (m, _) in &out {
FINGERPRINT.check(m).unwrap();
assert_eq!(
m.attributes.0.last().unwrap().typ,
stun::attributes::ATTR_FINGERPRINT
);
}
}
fn peer_request_sha256(a: &Agent, extra: Vec<Box<dyn Setter>>) -> Message {
let username = a.ufrag_pwd.local_credentials.ufrag.clone() + ":" + REMOTE_UFRAG;
let provider = provider();
let mut setters: Vec<Box<dyn Setter>> = vec![
Box::new(BINDING_REQUEST),
Box::new(TransactionId::new()),
Box::new(Username::new(ATTR_USERNAME, username)),
];
setters.extend(extra);
setters.push(Box::new(
MessageIntegritySha256::short_term(&a.ufrag_pwd.local_credentials.pwd, provider.crypto())
.unwrap(),
));
setters.push(Box::new(FINGERPRINT));
let mut msg = Message::new();
msg.build(&setters).unwrap();
msg
}
#[test]
fn test_a_request_with_message_integrity_sha256_is_answered_with_it_only() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
a.tie_breaker = 500;
let _ = sent(&mut a);
let provider = provider();
let key = a.ufrag_pwd.local_credentials.pwd.clone();
let answer_to = |a: &mut Agent, m: &Message| -> Message {
let _ = deliver(a, now, m, REMOTE_A);
let mut answers: Vec<Message> = sent(a)
.into_iter()
.filter(|(r, _)| r.transaction_id == m.transaction_id)
.map(|(r, _)| r)
.collect();
assert_eq!(answers.len(), 1, "one answer");
answers.remove(0)
};
let sha256_only = |r: &Message| {
assert!(r.contains(stun::attributes::ATTR_MESSAGE_INTEGRITY_SHA256));
assert!(!r.contains(stun::attributes::ATTR_MESSAGE_INTEGRITY));
MessageIntegritySha256::check(r, key.as_bytes(), provider.crypto())
.expect("MESSAGE-INTEGRITY-SHA256 with the local password");
FINGERPRINT.check(r).unwrap();
};
let check = peer_request_sha256(
&a,
vec![Box::new(AttrControlled(1)), Box::new(PriorityAttr(1000))],
);
let success = answer_to(&mut a, &check);
assert_eq!(success.typ, BINDING_SUCCESS);
sha256_only(&success);
let conflict = peer_request_sha256(
&a,
vec![Box::new(AttrControlling(100)), Box::new(PriorityAttr(1000))],
);
let error = answer_to(&mut a, &conflict);
assert_eq!(error.typ, BINDING_ERROR);
assert_eq!(error_code(&error), CODE_ROLE_CONFLICT.0);
sha256_only(&error);
let unknown = peer_request_sha256(
&a,
vec![
Box::new(AttrControlled(1)),
Box::new(PriorityAttr(1000)),
Box::new(stun::attributes::RawAttribute {
typ: stun::attributes::AttrType(0x7f00),
length: 4,
value: vec![1, 2, 3, 4],
}),
],
);
let error = answer_to(&mut a, &unknown);
assert_eq!(error_code(&error), 420);
sha256_only(&error);
let check = peer_request(
&a,
vec![Box::new(AttrControlled(1)), Box::new(PriorityAttr(1000))],
);
let success = answer_to(&mut a, &check);
assert!(success.contains(stun::attributes::ATTR_MESSAGE_INTEGRITY));
assert!(!success.contains(stun::attributes::ATTR_MESSAGE_INTEGRITY_SHA256));
}
#[test]
fn test_a_success_response_without_xor_mapped_address_is_discarded() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
let (check, to) = first_check(&mut a, now);
let provider = provider();
let mut bare = Message::new();
bare.build(&[
Box::new(BINDING_SUCCESS),
Box::new(check.transaction_id),
Box::new(MessageIntegrity::new_short_term_integrity_with_provider(
REMOTE_PWD.to_owned(),
provider.crypto(),
)),
Box::new(FINGERPRINT),
])
.unwrap();
let at = now + Duration::from_millis(150);
deliver(&mut a, at, &bare, &to.to_string()).unwrap();
assert_eq!(a.get_best_valid_candidate_pair(), None, "no valid pair");
assert!(a.remote_candidates[0].last_consent.is_none());
let response = peer_response(BINDING_SUCCESS, check.transaction_id, addr(LOCAL), None);
deliver(&mut a, at, &response, &to.to_string()).unwrap();
assert!(
a.get_best_valid_candidate_pair().is_some(),
"the transaction was still open"
);
}
fn lite_config() -> AgentConfig {
AgentConfig {
lite: true,
candidate_types: vec![CandidateType::Host],
..config(false)
}
}
#[test]
fn test_a_lite_agent_has_one_host_candidate_per_ip_address() {
let now = Instant::now();
let refused = Agent::new(
now,
Arc::new(AgentConfig {
lite: true,
candidate_types: vec![CandidateType::Host, CandidateType::ServerReflexive],
..config(false)
}),
provider(),
);
assert!(refused.is_err(), "a lite agent with reflexive candidates");
let mut a = bare_agent(now, lite_config());
assert!(
!a.add_local_candidate(host(addr("192.168.0.2:1001")))
.unwrap(),
"same IP"
);
let srflx = crate::candidate::candidate_server_reflexive::CandidateServerReflexiveConfig {
base_config: CandidateConfig {
network: "udp".to_owned(),
address: "203.0.113.9".to_owned(),
port: 4000,
component: 1,
..Default::default()
},
rel_addr: "192.168.0.2".to_owned(),
rel_port: 1000,
url: None,
}
.new_candidate_server_reflexive()
.unwrap();
assert!(
!a.add_local_candidate(srflx).unwrap(),
"not a host candidate"
);
assert!(
a.add_local_candidate(host(addr("192.168.0.7:1000")))
.unwrap()
);
assert_eq!(a.get_local_candidates().len(), 2);
}
#[test]
fn test_no_rfc_3489_compatibility_and_no_alternate_server() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let _ = run(&mut a, now, now + Duration::from_millis(1));
let request = peer_request(
&a,
vec![Box::new(AttrControlling(1)), Box::new(PriorityAttr(1000))],
);
let mut rfc3489 = request.raw.clone();
rfc3489[4..8].copy_from_slice(&[0x01, 0x02, 0x03, 0x04]);
let _ = deliver_raw(&mut a, now, &rfc3489, REMOTE_A);
let answers: Vec<_> = sent(&mut a)
.into_iter()
.filter(|(m, _)| m.transaction_id == request.transaction_id)
.collect();
assert!(
answers.is_empty(),
"a request without the magic cookie is not answered"
);
deliver(&mut a, now, &request, REMOTE_A).unwrap();
let answers: Vec<_> = sent(&mut a)
.into_iter()
.filter(|(m, _)| m.transaction_id == request.transaction_id)
.collect();
assert_eq!(answers.len(), 1);
let (answer, _) = &answers[0];
assert_eq!(answer.typ, stun::message::BINDING_SUCCESS);
assert!(!answer.contains(stun::attributes::ATTR_ALTERNATE_SERVER));
}
#[test]
fn test_no_data_path_without_a_pair() {
let now = Instant::now();
let mut lite = started_agent(now, lite_config(), &[REMOTE_A]);
let plain = peer_request(
&lite,
vec![Box::new(AttrControlling(1)), Box::new(PriorityAttr(1000))],
);
deliver(&mut lite, now, &plain, REMOTE_A).unwrap();
assert!(
lite.media_path().is_none(),
"lite: no data before the nomination"
);
let nominating = peer_request(
&lite,
vec![
Box::new(AttrControlling(1)),
Box::new(PriorityAttr(1000)),
Box::new(UseCandidateAttr::new()),
],
);
deliver(&mut lite, now, &nominating, REMOTE_A).unwrap();
assert!(lite.media_path().is_some(), "after it");
let mut a = started_agent(
now,
AgentConfig {
failed_timeout: Some(Duration::from_secs(1)),
disconnected_timeout: Some(Duration::from_secs(1)),
..config(true)
},
&[REMOTE_A],
);
a.set_local_gathering_complete();
a.set_remote_end_of_candidates();
run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
assert!(a.media_path().is_some(), "connected: a path");
run_answering(&mut a, now, now + Duration::from_secs(60), |_, _| false);
assert_eq!(a.state(), ConnectionState::Failed);
assert!(a.media_path().is_none(), "failed: none");
}
fn with_unknown_attribute(
typ: MessageType,
id: TransactionId,
error: Option<ErrorCode>,
) -> Message {
let provider = provider();
let mut m = Message::new();
m.set_type(typ);
m.transaction_id = id;
m.write_header();
match error {
Some(code) => code.add_to(&mut m).unwrap(),
None => XorMappedAddress {
ip: addr(LOCAL).ip(),
port: addr(LOCAL).port(),
}
.add_to(&mut m)
.unwrap(),
}
m.add(stun::attributes::AttrType(0x0099), &[1, 2, 3, 4]);
if typ.class != stun::message::CLASS_INDICATION {
MessageIntegrity::new_short_term_integrity_with_provider(
REMOTE_PWD.to_owned(),
provider.crypto(),
)
.add_to(&mut m)
.unwrap();
}
FINGERPRINT.add_to(&mut m).unwrap();
m
}
#[test]
fn test_an_error_response_with_an_unknown_required_attribute_fails_the_transaction() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let (check, to) = first_check(&mut a, now);
let response =
with_unknown_attribute(BINDING_ERROR, check.transaction_id, Some(CODE_BAD_REQUEST));
let _ = deliver(&mut a, now, &response, &to.to_string());
assert_eq!(a.candidate_pairs[0].state, CandidatePairState::Failed);
}
#[test]
fn test_a_success_response_with_an_unknown_required_attribute_is_discarded() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let (check, to) = first_check(&mut a, now);
let response = with_unknown_attribute(BINDING_SUCCESS, check.transaction_id, None);
assert!(deliver(&mut a, now, &response, &to.to_string()).is_err());
assert_ne!(a.candidate_pairs[0].state, CandidatePairState::Succeeded);
let good = peer_response(BINDING_SUCCESS, check.transaction_id, addr(LOCAL), None);
deliver(&mut a, now, &good, &to.to_string()).unwrap();
assert_eq!(a.candidate_pairs[0].state, CandidatePairState::Succeeded);
}
#[test]
fn test_an_indication_with_an_unknown_required_attribute_is_discarded() {
let now = Instant::now();
let mut a = started_agent(now, config(false), &[REMOTE_A]);
let indication = with_unknown_attribute(
stun::message::MessageType::new(
stun::message::METHOD_BINDING,
stun::message::CLASS_INDICATION,
),
TransactionId::new(),
None,
);
let _ = deliver(&mut a, now, &indication, REMOTE_A);
assert_eq!(a.remote_candidates[0].last_received(), None);
let mut plain = Message::new();
plain
.build(&[
Box::new(indication.typ),
Box::new(TransactionId::new()),
Box::new(FINGERPRINT),
])
.unwrap();
deliver(&mut a, now, &plain, REMOTE_A).unwrap();
assert_eq!(a.remote_candidates[0].last_received(), Some(now));
}