use std::sync::Arc;
use std::time::{Duration, Instant};
use sansio::Protocol;
use stun::attributes::ATTR_USERNAME;
use super::agent_rfc8445_test::*;
use super::agent_selector::{MAX_TCP_CONNECTION_ATTEMPTS_PER_ADDRESS, TransportError};
use super::*;
use crate::candidate::candidate_host::CandidateHostConfig;
const MS: Duration = Duration::from_millis(1);
fn candidate(
network: &str,
address: &str,
port: u16,
priority: u32,
tcp_type: TcpType,
) -> Candidate {
CandidateHostConfig {
base_config: CandidateConfig {
network: network.to_owned(),
address: address.to_owned(),
port,
component: 1,
priority,
..Default::default()
},
tcp_type,
}
.new_candidate_host()
.unwrap()
}
fn udp(address: &str, port: u16, priority: u32) -> Candidate {
candidate("udp", address, port, priority, TcpType::Unspecified)
}
fn path(local: &str, remote: &str, transport_protocol: TransportProtocol) -> TransportContext {
TransportContext {
local_addr: addr(local),
peer_addr: addr(remote),
ecn: None,
transport_protocol,
}
}
fn events(a: &mut Agent) -> Vec<Event> {
std::iter::from_fn(|| a.poll_event())
.map(|e| e.event)
.collect()
}
fn selected(a: &Agent) -> Option<SocketAddr> {
a.get_selected_candidate_pair().map(|(_, r)| r.addr())
}
fn lite_agent(now: Instant, controlling: bool, remotes: &[Candidate]) -> Agent {
lite_agent_with(now, controlling, &[], remotes)
}
fn lite_agent_with(
now: Instant,
controlling: bool,
locals: &[Candidate],
remotes: &[Candidate],
) -> Agent {
let cfg = AgentConfig {
lite: true,
candidate_types: vec![CandidateType::Host],
..config(controlling)
};
let mut a = bare_agent(now, cfg);
for local in locals {
assert!(a.add_local_candidate(local.clone()).unwrap());
}
a.set_remote_lite(true);
for remote in remotes {
a.add_remote_candidate(remote.clone()).unwrap();
}
a.start_connectivity_checks(
now,
controlling,
REMOTE_UFRAG.to_owned(),
REMOTE_PWD.to_owned(),
)
.unwrap();
a
}
#[test]
fn test_a_lite_agent_takes_one_passive_and_one_active_tcp_candidate_per_address() {
let now = Instant::now();
let mut a = lite_agent(now, false, &[]);
let tcp = |port, tcp_type| candidate("tcp", "192.168.0.2", port, 0, tcp_type);
assert!(a.add_local_candidate(tcp(2000, TcpType::Passive)).unwrap());
assert!(a.add_local_candidate(tcp(9, TcpType::Active)).unwrap());
assert!(
!a.add_local_candidate(tcp(2001, TcpType::SimultaneousOpen))
.unwrap(),
"no simultaneous-open candidate"
);
assert!(
!a.add_local_candidate(tcp(2002, TcpType::Passive)).unwrap(),
"a second passive candidate on the address"
);
assert!(
!a.add_local_candidate(udp("192.168.0.2", 1001, 0)).unwrap(),
"a second UDP candidate on the address"
);
let kinds: Vec<(bool, TcpType)> = a
.get_local_candidates()
.iter()
.map(|c| (c.network_type().is_tcp(), c.tcp_type()))
.collect();
assert_eq!(
kinds,
vec![
(false, TcpType::Unspecified),
(true, TcpType::Passive),
(true, TcpType::Active)
]
);
}
#[test]
fn test_two_lite_agents_pair_the_peers_active_tcp_candidate() {
let now = Instant::now();
let active = candidate("tcp", "192.168.0.3", 9, 1000, TcpType::Active);
let peer_udp = udp("192.168.0.3", 2000, 2000);
let passive = candidate("tcp", "192.168.0.2", 2000, 0, TcpType::Passive);
let mut b = lite_agent_with(
now,
false,
std::slice::from_ref(&passive),
&[active.clone(), peer_udp],
);
run(&mut b, now, now + Duration::from_secs(1));
assert!(b.is_remote_lite());
assert_eq!(
b.get_remote_candidates().len(),
2,
"the active candidate is a remote candidate"
);
assert!(
b.awaits_lite_update(),
"several pairs: the update is to come"
);
let later = now + Duration::from_secs(1);
assert!(b.replace_remote_candidates(later, vec![active.clone()]));
run(&mut b, later, later + Duration::from_secs(1));
assert!(!b.awaits_lite_update(), "the update was taken");
let (local, remote) = b.get_selected_candidate_pair().expect("a pair");
assert_eq!(
(
local.tcp_type(),
local.addr(),
remote.tcp_type(),
remote.addr()
),
(
TcpType::Passive,
addr("192.168.0.2:2000"),
TcpType::Active,
addr("192.168.0.3:9")
)
);
let mut a = lite_agent_with(
now,
true,
&[passive],
&[active.clone(), udp("192.168.0.3", 2000, 0)],
);
run(&mut a, now, now + Duration::from_secs(1));
assert!(a.get_selected_candidate_pair().is_some());
assert!(!a.awaits_lite_update());
let mut with_full_peer = bare_agent(
now,
AgentConfig {
lite: true,
candidate_types: vec![CandidateType::Host],
..config(false)
},
);
assert!(!with_full_peer.is_remote_lite());
assert!(!with_full_peer.add_remote_candidate(active.clone()).unwrap());
assert!(with_full_peer.get_remote_candidates().is_empty());
let mut full = started_agent(now, config(false), &[]);
assert!(!full.add_remote_candidate(active).unwrap());
assert!(full.get_remote_candidates().is_empty());
}
#[test]
fn test_two_lite_agents_select_the_one_pair_without_checks() {
for controlling in [true, false] {
let now = Instant::now();
let mut a = lite_agent(now, controlling, &[udp("192.168.0.3", 2000, 0)]);
a.set_local_gathering_complete();
a.set_remote_end_of_candidates();
let out = run(&mut a, now, now + Duration::from_secs(60));
assert!(
requests(&out).is_empty(),
"a lite agent sent a Binding request (controlling: {controlling})"
);
assert!(!out.is_empty(), "keepalives");
assert!(
out.iter()
.all(|(m, to)| m.typ.class == CLASS_INDICATION && *to == addr(REMOTE_A))
);
assert_eq!(selected(&a), Some(addr(REMOTE_A)));
assert_eq!(a.media_path().unwrap().peer_addr, addr(REMOTE_A));
assert_eq!(a.state(), ConnectionState::Completed);
assert!(
!events(&mut a)
.iter()
.any(|e| matches!(e, Event::LiteCandidateUpdate(..))),
"one pair needs no updated candidate exchange"
);
}
}
#[test]
fn test_two_lite_agents_with_several_pairs_select_by_rfc6724_and_update() {
let now = Instant::now();
let elsewhere = udp("10.0.0.1", 5000, 3000);
let same_network = udp("192.168.0.9", 5000, 1000);
let mut a = lite_agent(now, true, &[elsewhere.clone(), same_network.clone()]);
let out = run(&mut a, now, now + Duration::from_secs(5));
assert!(requests(&out).is_empty());
assert_eq!(selected(&a), Some(addr("192.168.0.9:5000")));
let updates: Vec<(SocketAddr, SocketAddr)> = events(&mut a)
.into_iter()
.filter_map(|e| match e {
Event::LiteCandidateUpdate(local, remote) => Some((local.addr(), remote.addr())),
_ => None,
})
.collect();
assert_eq!(updates, vec![(addr(LOCAL), addr("192.168.0.9:5000"))]);
run(
&mut a,
now + Duration::from_secs(5),
now + Duration::from_secs(30),
);
assert!(events(&mut a).is_empty(), "the update is asked for once");
let mut b = lite_agent(now, false, &[elsewhere.clone(), same_network]);
run(&mut b, now, now + Duration::from_secs(1));
assert_eq!(selected(&b), Some(addr("192.168.0.9:5000")));
assert!(
events(&mut b)
.iter()
.all(|e| !matches!(e, Event::LiteCandidateUpdate(..)))
);
let later = now + Duration::from_secs(1);
assert!(b.replace_remote_candidates(later, vec![elsewhere]));
run(&mut b, later, later + Duration::from_secs(1));
assert_eq!(selected(&b), Some(addr("10.0.0.1:5000")));
assert_eq!(b.get_remote_candidates().len(), 1);
let mut full = started_agent(now, config(true), &[REMOTE_A]);
assert!(!full.replace_remote_candidates(now, vec![]));
assert_eq!(full.get_remote_candidates().len(), 1);
}
#[test]
fn test_a_lite_answerer_takes_the_local_candidate_remote_candidates_names() {
let now = Instant::now();
let remote = udp("192.168.0.9", 5000, 1000);
let mut b = lite_agent(now, false, std::slice::from_ref(&remote));
b.add_local_candidate(udp("10.0.0.2", 1000, 500)).unwrap();
run(&mut b, now, now + Duration::from_secs(1));
let local = |a: &Agent| a.get_selected_candidate_pair().map(|(l, _)| l.addr());
assert_eq!(
local(&b),
Some(addr(LOCAL)),
"the preference: the same network"
);
let later = now + Duration::from_secs(1);
assert!(b.replace_remote_candidates_pinning(
later,
vec![remote.clone()],
Some(addr("10.0.0.2:1000"))
));
run(&mut b, later, later + Duration::from_secs(1));
assert_eq!(
local(&b),
Some(addr("10.0.0.2:1000")),
"the named local candidate"
);
assert_eq!(selected(&b), Some(addr("192.168.0.9:5000")));
let later = later + Duration::from_secs(1);
assert!(b.replace_remote_candidates(later, vec![remote]));
run(&mut b, later, later + Duration::from_secs(1));
assert_eq!(local(&b), Some(addr(LOCAL)));
}
#[test]
fn test_lite_agent_with_a_full_peer_waits_for_the_nomination() {
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]);
run(&mut a, now, now + Duration::from_secs(2));
assert_eq!(selected(&a), None);
}
#[test]
fn test_ta_is_the_higher_of_the_two_proposals() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
assert_eq!(a.proposed_pacing(), None);
assert_eq!(a.pacing(), 50 * MS);
a.set_remote_pacing(Some(120 * MS));
assert_eq!(a.pacing(), 120 * MS);
a.set_remote_pacing(Some(10 * MS));
assert_eq!(a.pacing(), 50 * MS);
let cfg = AgentConfig {
ta: Some(20 * MS),
..config(true)
};
let mut a = started_agent(now, cfg, &[REMOTE_A]);
assert_eq!(a.proposed_pacing(), Some(20 * MS));
a.set_remote_pacing(None);
assert_eq!(
a.pacing(),
50 * MS,
"a peer that proposes nothing uses 50 ms"
);
a.set_remote_pacing(Some(30 * MS));
assert_eq!(a.pacing(), 30 * MS);
a.set_remote_pacing(Some(20 * MS));
assert_eq!(a.pacing(), 20 * MS);
a.set_remote_pacing(Some(MS));
assert_eq!(a.pacing(), 20 * MS);
let cfg = AgentConfig {
ta: Some(50 * MS),
..config(true)
};
assert_eq!(started_agent(now, cfg, &[]).proposed_pacing(), None);
let cfg = AgentConfig {
ta: Some(MS),
..config(true)
};
assert_eq!(started_agent(now, cfg, &[]).proposed_pacing(), Some(5 * MS));
}
#[test]
fn test_checks_are_paced_by_the_agreed_ta() {
let now = Instant::now();
let mut a = bare_agent(now, config(true));
for n in 1..=3 {
a.add_remote_candidate(udp(&format!("192.168.1.{n}"), 5000, 1000 - n))
.unwrap();
}
a.set_remote_pacing(Some(100 * MS));
a.start_connectivity_checks(now, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
.unwrap();
let mut started = vec![];
let mut at = now;
while at <= now + 450 * MS {
a.handle_timeout(at).unwrap();
while let Some(t) = a.poll_write() {
started.push(t.now.duration_since(now));
}
at = match a.poll_timeout() {
Some(next) => next.max(at + MS),
None => break,
};
}
assert_eq!(started, vec![Duration::ZERO, 100 * MS, 200 * MS]);
}
#[test]
fn test_ipv6_link_local_pairs_only_with_link_local() {
let now = Instant::now();
let cfg = AgentConfig {
allow_link_local_remote_candidates: true,
..config(true)
};
let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
a.add_local_candidate(udp("fe80::1", 1000, 0)).unwrap();
a.add_local_candidate(udp("2001:db8::1", 1000, 0)).unwrap();
a.add_remote_candidate(udp("fe80::2", 2000, 0)).unwrap();
a.add_remote_candidate(udp("2001:db8::2", 2000, 0)).unwrap();
let mut pairs: Vec<(String, String)> = a
.candidate_pairs
.iter()
.map(|p| {
(
a.local_candidates[p.local_index].addr().ip().to_string(),
a.remote_candidates[p.remote_index].addr().ip().to_string(),
)
})
.collect();
pairs.sort();
assert_eq!(
pairs,
vec![
("2001:db8::1".to_owned(), "2001:db8::2".to_owned()),
("fe80::1".to_owned(), "fe80::2".to_owned()),
]
);
}
#[test]
fn test_hard_icmp_error_fails_the_pair_whose_check_is_in_flight() {
let now = Instant::now();
let cfg = AgentConfig {
failed_timeout: Some(Duration::ZERO),
..config(false)
};
let mut a = started_agent(now, cfg, &[REMOTE_A, REMOTE_B]);
let out = run(&mut a, now, now + 10 * MS);
assert_eq!(requests(&out).len(), 1);
let udp_path = |remote: &str| path(LOCAL, remote, TransportProtocol::UDP);
assert!(
!a.handle_transport_error(
now + 10 * MS,
udp_path(REMOTE_B),
TransportError::IcmpHardError
),
"no check is in flight on that pair"
);
assert!(!a.handle_transport_error(
now + 10 * MS,
udp_path("192.168.0.77:1"),
TransportError::IcmpHardError
));
assert!(a.handle_transport_error(
now + 10 * MS,
udp_path(REMOTE_A),
TransportError::IcmpHardError
));
let pair_to = |a: &Agent, remote: &str| {
a.candidate_pairs[a
.find_pair(0, a.find_remote_candidate(addr(remote)).unwrap())
.unwrap()]
};
assert_eq!(pair_to(&a, REMOTE_A).state, CandidatePairState::Failed);
let out = run_answering(
&mut a,
now + 10 * MS,
now + Duration::from_secs(10),
|_, _| true,
);
assert!(requests(&out).iter().all(|(_, to)| *to == addr(REMOTE_B)));
assert!(pair_to(&a, REMOTE_B).valid);
assert!(!a.handle_transport_error(
now + Duration::from_secs(10),
udp_path(REMOTE_B),
TransportError::IcmpHardError
));
assert_eq!(pair_to(&a, REMOTE_B).state, CandidatePairState::Succeeded);
}
fn tcp_agent(now: Instant, n: u16) -> Agent {
let cfg = AgentConfig {
failed_timeout: Some(Duration::ZERO),
..config(true)
};
let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
a.add_local_candidate(candidate("tcp", "192.168.0.2", 9, 0, TcpType::Active))
.unwrap();
for i in 1..=n {
a.add_remote_candidate(
CandidateHostConfig {
base_config: CandidateConfig {
network: "tcp".to_owned(),
address: "192.168.0.3".to_owned(),
port: 5000 + i,
component: 1,
priority: 10_000 - u32::from(i),
foundation: format!("t{i}"),
..Default::default()
},
tcp_type: TcpType::Passive,
}
.new_candidate_host()
.unwrap(),
)
.unwrap();
}
a.start_connectivity_checks(now, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
.unwrap();
a
}
fn tcp_path(remote_port: u16) -> TransportContext {
path(
"192.168.0.2:40000",
&format!("192.168.0.3:{remote_port}"),
TransportProtocol::TCP,
)
}
fn tcp_state(a: &Agent, remote_port: u16) -> CandidatePairState {
let remote = addr(&format!("192.168.0.3:{remote_port}"));
a.candidate_pairs[a
.find_pair(0, a.find_remote_candidate(remote).unwrap())
.unwrap()]
.state
}
#[test]
fn test_tcp_connection_attempts_are_limited_and_a_failed_connection_fails_the_pair() {
let now = Instant::now();
let mut a = tcp_agent(now, 8);
let out = run(&mut a, now, now + Duration::from_secs(2));
let attempted: Vec<u16> = requests(&out).iter().map(|(_, to)| to.port()).collect();
assert_eq!(attempted, vec![5001, 5002, 5003, 5004, 5005]);
assert_eq!(attempted.len(), MAX_TCP_CONNECTION_ATTEMPTS_PER_ADDRESS);
assert_eq!(tcp_state(&a, 5006), CandidatePairState::Waiting);
let later = now + Duration::from_secs(2);
assert!(a.handle_transport_error(later, tcp_path(5002), TransportError::ConnectionFailed));
assert_eq!(tcp_state(&a, 5002), CandidatePairState::Failed);
let out = run(&mut a, later, later + Duration::from_secs(2));
let attempted: Vec<u16> = requests(&out).iter().map(|(_, to)| to.port()).collect();
assert_eq!(attempted, vec![5006], "one attempt ended, one more starts");
let later = later + Duration::from_secs(2);
assert_eq!(tcp_state(&a, 5008), CandidatePairState::Waiting);
assert!(a.handle_transport_error(later, tcp_path(5008), TransportError::ConnectionFailed));
assert_eq!(tcp_state(&a, 5008), CandidatePairState::Failed);
assert!(!a.handle_transport_error(
later,
path(
"192.168.0.2:40000",
"192.168.0.3:5007",
TransportProtocol::UDP
),
TransportError::ConnectionFailed
));
}
#[test]
fn test_first_answer_that_is_not_stun_fails_the_pairs_of_that_remote_candidate() {
let now = Instant::now();
let mut a = tcp_agent(now, 2);
run(&mut a, now, now + 10 * MS);
assert_eq!(tcp_state(&a, 5001), CandidatePairState::InProgress);
assert!(!a.handle_transport_error(now + 10 * MS, tcp_path(5002), TransportError::NotStun));
assert!(a.handle_transport_error(now + 10 * MS, tcp_path(5001), TransportError::NotStun));
assert_eq!(tcp_state(&a, 5001), CandidatePairState::Failed);
assert_ne!(tcp_state(&a, 5002), CandidatePairState::Failed);
assert!(a.pending_binding_requests.is_empty());
}
#[test]
fn test_non_stun_data_on_a_connection_this_agent_opened_fails_the_pairs() {
let now = Instant::now();
let mut a = tcp_agent(now, 2);
run(&mut a, now, now + 10 * MS);
assert_eq!(tcp_state(&a, 5001), CandidatePairState::InProgress);
let mut udp = tcp_path(5001);
udp.transport_protocol = TransportProtocol::UDP;
assert!(!a.handle_non_stun_before_validation(now + 10 * MS, udp));
assert_eq!(tcp_state(&a, 5001), CandidatePairState::InProgress);
assert!(a.handle_non_stun_before_validation(now + 10 * MS, tcp_path(5001)));
assert_eq!(tcp_state(&a, 5001), CandidatePairState::Failed);
}
const RELAYED: &str = "203.0.113.5";
fn relayed_tcp(port: u16, tcp_type: TcpType) -> Candidate {
let mut c = crate::candidate::candidate_relay::CandidateRelayConfig {
base_config: CandidateConfig {
network: "tcp".to_owned(),
address: RELAYED.to_owned(),
port,
component: 1,
..Default::default()
},
rel_addr: "192.168.0.2".to_owned(),
rel_port: 40000,
url: None,
}
.new_candidate_relay()
.unwrap();
c.set_tcp_type(tcp_type);
c
}
#[test]
fn test_relayed_tcp_candidates_pair_by_tcptype_and_check_on_the_relayed_path() {
let now = Instant::now();
let cfg = AgentConfig {
failed_timeout: Some(Duration::ZERO),
..config(true)
};
let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
let passive = relayed_tcp(5000, TcpType::Passive);
let active = relayed_tcp(9, TcpType::Active);
assert!(active.priority() > passive.priority());
a.add_local_candidate(passive).unwrap();
a.add_local_candidate(active).unwrap();
a.add_remote_candidate(candidate("tcp", "192.168.0.3", 6000, 0, TcpType::Passive))
.unwrap();
assert!(
!a.add_remote_candidate(candidate("tcp", "192.168.0.3", 9, 0, TcpType::Active))
.unwrap()
);
a.start_connectivity_checks(now, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
.unwrap();
let local = |port: u16| {
a.find_local_candidate(addr(&format!("{RELAYED}:{port}")), TransportProtocol::TCP)
};
let remote =
|a: &Agent, port: u16| a.find_remote_candidate(addr(&format!("192.168.0.3:{port}")));
let (lp, la) = (local(5000).unwrap(), local(9).unwrap());
let rp = remote(&a, 6000).unwrap();
assert!(
a.find_pair(la, rp).is_some(),
"active relayed with remote passive"
);
assert!(a.find_pair(lp, rp).is_none(), "passive with passive");
a.handle_timeout(now + 10 * MS).unwrap();
let out: Vec<TaggedBytesMut> = std::iter::from_fn(|| a.poll_write()).collect();
let check = out
.iter()
.find(|t| t.transport.peer_addr == addr("192.168.0.3:6000"))
.expect("a check to the passive remote candidate");
assert_eq!(check.transport.transport_protocol, TransportProtocol::TCP);
assert_eq!(
check.transport.local_addr.ip(),
addr(&format!("{RELAYED}:9")).ip()
);
assert!(
out.iter()
.all(|t| t.transport.peer_addr != addr("192.168.0.3:9")),
"nothing goes to an active candidate's port 9"
);
let request = peer_request(
&a,
vec![
Box::new(crate::attributes::control::AttrControlled(1)),
Box::new(crate::attributes::priority::PriorityAttr(1)),
],
);
a.handle_read(TaggedBytesMut {
now: now + 20 * MS,
transport: TransportContext {
local_addr: addr(&format!("{RELAYED}:5000")),
peer_addr: addr("192.168.0.3:51000"),
ecn: None,
transport_protocol: TransportProtocol::TCP,
},
message: BytesMut::from(&request.raw[..]),
})
.unwrap();
let answers: Vec<TaggedBytesMut> = std::iter::from_fn(|| a.poll_write()).collect();
let answer = answers
.iter()
.find(|t| t.transport.peer_addr == addr("192.168.0.3:51000"))
.expect("the check is answered");
assert_eq!(
answer.transport.local_addr,
addr(&format!("{RELAYED}:5000"))
);
assert_eq!(answer.transport.transport_protocol, TransportProtocol::TCP);
let mut m = Message {
raw: answer.message.to_vec(),
..Message::default()
};
m.decode().unwrap();
assert_eq!(m.typ, BINDING_SUCCESS);
let learned = remote(&a, 51000).expect("a peer-reflexive candidate for the peer's connection");
assert_eq!(a.remote_candidates[learned].tcp_type(), TcpType::Active);
assert!(
a.find_pair(lp, learned).is_some(),
"passive relayed with the peer's active side"
);
}
#[test]
fn test_server_reflexive_tcp_direction_preferences() {
let srflx = |tcp_type: TcpType| {
let mut c = crate::candidate::candidate_server_reflexive::CandidateServerReflexiveConfig {
base_config: CandidateConfig {
network: "tcp".to_owned(),
address: "198.51.100.7".to_owned(),
port: 7000,
component: 1,
..Default::default()
},
rel_addr: "192.168.0.2".to_owned(),
rel_port: 40000,
url: None,
}
.new_candidate_server_reflexive()
.unwrap();
c.set_tcp_type(tcp_type);
c.priority()
};
let (so, active, passive) = (
srflx(TcpType::SimultaneousOpen),
srflx(TcpType::Active),
srflx(TcpType::Passive),
);
assert!(so > active && active > passive, "{so} {active} {passive}");
}
#[test]
fn test_local_candidates_are_refused_after_the_nomination() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
assert!(a.add_local_candidate(udp("192.168.0.20", 1000, 0)).unwrap());
run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
assert!(a.get_selected_candidate_pair().is_some());
let before = a.get_local_candidates().len();
assert!(!a.add_local_candidate(udp("192.168.0.21", 1000, 0)).unwrap());
assert_eq!(a.get_local_candidates().len(), before);
a.restart(
now + Duration::from_secs(2),
String::new(),
String::new(),
true,
)
.unwrap();
assert!(a.add_local_candidate(udp("192.168.0.21", 1000, 0)).unwrap());
}
const OTHER: &str = "192.168.0.22:1000";
fn selected_agent(now: Instant) -> Agent {
let mut a = started_agent(now, config(true), &[REMOTE_A]);
assert!(a.add_local_candidate(udp("192.168.0.22", 1000, 0)).unwrap());
run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
assert!(
a.get_selected_candidate_pair().is_some(),
"a pair is selected"
);
a
}
fn selected_local(a: &Agent) -> Option<SocketAddr> {
a.get_selected_candidate_pair().map(|(l, _)| l.addr())
}
fn pairs_of(a: &Agent, local: SocketAddr) -> usize {
a.get_candidate_pairs()
.iter()
.filter(|(l, _)| l.addr() == local)
.count()
}
fn signalled(a: &Agent, local: SocketAddr) -> bool {
a.get_local_candidates_to_signal()
.iter()
.any(|c| c.addr() == local)
}
fn held(a: &Agent, local: SocketAddr) -> bool {
a.get_local_candidates().iter().any(|c| c.addr() == local)
}
const UDP: shared::TransportProtocol = shared::TransportProtocol::UDP;
#[test]
fn test_withdrawing_a_candidate_that_carries_no_data_removes_its_pairs() {
let now = Instant::now();
let mut a = selected_agent(now);
let selected = selected_local(&a).unwrap();
let other = [addr(LOCAL), addr(OTHER)]
.into_iter()
.find(|c| *c != selected)
.unwrap();
assert!(pairs_of(&a, other) > 0);
assert!(a.withdraw_local_candidate(now, other, UDP));
assert_eq!(pairs_of(&a, other), 0, "its pairs are removed");
assert!(!a.withdraw_local_candidate(now, other, UDP), "once");
a.add_remote_candidate(udp("10.0.0.9", 7000, 0)).unwrap();
assert_eq!(
pairs_of(&a, other),
0,
"no pair with a later remote candidate"
);
assert!(signalled(&a, selected) && !signalled(&a, other));
assert_eq!(
selected_local(&a),
Some(selected),
"the selected pair is untouched"
);
a.restart(now, String::new(), String::new(), true).unwrap();
assert!(!held(&a, other), "the next restart drops it");
assert!(held(&a, selected));
}
#[test]
fn test_withdrawing_the_candidate_of_the_selected_pair_keeps_that_pair() {
let now = Instant::now();
let mut a = selected_agent(now);
let selected = selected_local(&a).unwrap();
a.add_remote_candidate(udp("10.0.0.9", 7000, 0)).unwrap();
assert!(a.withdraw_local_candidate(now, selected, UDP));
assert_eq!(
selected_local(&a),
Some(selected),
"the selected pair stays"
);
assert_eq!(pairs_of(&a, selected), 1, "only the selected pair is left");
assert!(a.media_path().is_some_and(|p| p.local_addr == selected));
assert!(!signalled(&a, selected));
a.restart(now, String::new(), String::new(), true).unwrap();
assert!(!held(&a, selected), "the next session does not have it");
assert!(
a.media_path().is_some_and(|p| p.local_addr == selected),
"the restart's previous media path runs on it until the new pair"
);
}
#[test]
fn test_a_candidate_withdrawn_while_a_restart_is_staged_leaves_the_current_session() {
let now = Instant::now();
let mut a = selected_agent(now);
let selected = selected_local(&a).unwrap();
a.generate_restart_credentials(String::new(), String::new())
.unwrap();
assert!(a.withdraw_local_candidate(now, selected, UDP));
assert_eq!(
selected_local(&a),
Some(selected),
"the current session keeps it"
);
assert!(a.media_path().is_some_and(|p| p.local_addr == selected));
a.apply_restart(now, true).unwrap();
assert!(!held(&a, selected), "the new session does not have it");
assert!(
a.media_path().is_some_and(|p| p.local_addr == selected),
"the previous path stays until the restart selects a pair"
);
}
#[test]
fn test_staged_credentials_are_the_next_generation_s() {
let now = Instant::now();
let mut a = selected_agent(now);
let live = a.ufrag_pwd.local_credentials.ufrag.clone();
a.generate_restart_credentials(String::new(), String::new())
.unwrap();
let staged = a.get_local_credentials().ufrag.clone();
assert_ne!(staged, live);
assert_eq!(
a.ufrag_pwd.local_credentials.ufrag, live,
"the live session's stay"
);
a.apply_restart(now, true).unwrap();
assert_eq!(a.ufrag_pwd.local_credentials.ufrag, staged);
assert_eq!(a.get_local_credentials().ufrag, staged);
}
#[test]
fn test_a_candidate_withdrawn_after_the_restart_leaves_the_new_session_only() {
let now = Instant::now();
let mut a = selected_agent(now);
let selected = selected_local(&a).unwrap();
a.restart(now, String::new(), String::new(), true).unwrap();
assert!(held(&a, selected), "kept into the new session");
a.add_remote_candidate(udp("10.0.0.9", 7000, 0)).unwrap();
assert!(pairs_of(&a, selected) > 0, "paired in the new session");
assert!(a.withdraw_local_candidate(now, selected, UDP));
assert_eq!(pairs_of(&a, selected), 0, "no pair of the new session");
assert!(
a.media_path().is_some_and(|p| p.local_addr == selected),
"the previous path is untouched"
);
}
#[test]
fn test_a_withdrawal_outlives_an_abandoned_staged_restart() {
let now = Instant::now();
let mut a = selected_agent(now);
let selected = selected_local(&a).unwrap();
a.generate_restart_credentials(String::new(), String::new())
.unwrap();
assert!(a.withdraw_local_candidate(now, selected, UDP));
a.discard_restart_credentials();
assert_eq!(selected_local(&a), Some(selected));
assert!(!signalled(&a, selected));
a.restart(now, String::new(), String::new(), true).unwrap();
assert!(!held(&a, selected), "the next restart drops it");
}
#[test]
fn test_a_reverted_restart_restores_the_session_and_a_new_withdrawal_keeps_its_pair() {
let now = Instant::now();
let mut a = selected_agent(now);
let selected = selected_local(&a).unwrap();
a.generate_restart_credentials(String::new(), String::new())
.unwrap();
a.apply_restart_revertible(now, true).unwrap();
assert!(a.withdraw_local_candidate(now, selected, UDP));
assert!(a.revert_restart(now));
assert_eq!(selected_local(&a), Some(selected), "the session is back");
assert!(signalled(&a, selected), "as it was before the withdrawal");
assert!(
a.withdraw_local_candidate(now, selected, UDP),
"withdrawn again"
);
assert_eq!(
selected_local(&a),
Some(selected),
"its selected pair stays"
);
}
#[test]
fn test_a_withdrawn_candidate_gathered_again_stays_withdrawn() {
let now = Instant::now();
let mut a = selected_agent(now);
let selected = selected_local(&a).unwrap();
let other = [addr(LOCAL), addr(OTHER)]
.into_iter()
.find(|c| *c != selected)
.unwrap();
assert!(a.withdraw_local_candidate(now, other, UDP));
assert!(
!a.add_local_candidate(udp(&other.ip().to_string(), other.port(), 0))
.unwrap()
);
assert!(!signalled(&a, other));
assert_eq!(pairs_of(&a, other), 0);
}
#[test]
fn test_a_candidate_gathered_for_a_staged_restart_is_kept_and_not_paired_before_it() {
let now = Instant::now();
let mut a = started_agent(now, config(true), &[REMOTE_A]);
assert!(a.add_local_candidate(udp("192.168.0.20", 1000, 0)).unwrap());
assert!(a.add_local_candidate(udp("192.168.0.22", 1000, 0)).unwrap());
run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
assert!(a.get_selected_candidate_pair().is_some());
let pairs = a.get_candidate_pairs().len();
a.generate_restart_credentials(String::new(), String::new())
.unwrap();
assert!(a.add_local_candidate(udp("192.168.0.21", 1000, 0)).unwrap());
assert!(!a.add_local_candidate(udp("192.168.0.20", 1000, 0)).unwrap());
assert_eq!(
a.get_candidate_pairs().len(),
pairs,
"not paired in the session that nominated"
);
let staged: Vec<String> = a
.get_restart_generation_local_candidates()
.iter()
.map(|c| c.address().to_owned())
.collect();
assert_eq!(staged, ["192.168.0.20", "192.168.0.21"]);
a.apply_restart(now + Duration::from_secs(2), false)
.unwrap();
let kept: Vec<String> = a
.get_local_candidates()
.iter()
.map(|c| c.address().to_owned())
.collect();
assert_eq!(kept, ["192.168.0.20", "192.168.0.21"]);
assert!(a.get_restart_generation_local_candidates().is_empty());
}
#[test]
fn test_consent_checks_run_on_a_tcp_pair() {
let now = Instant::now();
let mut a = tcp_agent(now, 1);
let answer = |a: &mut Agent, from: Instant, until: Instant| {
let mut out = vec![];
let mut at = from;
loop {
a.handle_timeout(at).unwrap();
while let Some(t) = a.poll_write() {
assert_eq!(t.transport.transport_protocol, TransportProtocol::TCP);
let mut m = Message {
raw: t.message.to_vec(),
..Message::default()
};
m.decode().unwrap();
let response = peer_response(
BINDING_SUCCESS,
m.transaction_id,
addr("192.168.0.2:40000"),
None,
);
a.handle_read(TaggedBytesMut {
now: at,
transport: tcp_path(5001),
message: BytesMut::from(&response.raw[..]),
})
.unwrap();
out.push((t.now, m));
}
match a.poll_timeout() {
Some(next) if next <= until => at = next.max(at + MS),
_ => return out,
}
}
};
answer(&mut a, now, now + Duration::from_secs(1));
assert_eq!(a.state(), ConnectionState::Connected);
let consent = answer(
&mut a,
now + Duration::from_secs(1),
now + Duration::from_secs(31),
);
assert!(
consent.len() >= 5,
"{} consent checks in 30 s",
consent.len()
);
let mut ids: Vec<TransactionId> = consent.iter().map(|(_, m)| m.transaction_id).collect();
ids.dedup();
assert_eq!(ids.len(), consent.len(), "each one is sent once");
for pair in consent.windows(2) {
let gap = pair[1].0.duration_since(pair[0].0);
assert!(
(Duration::from_secs(4)..=Duration::from_secs(6)).contains(&gap),
"{gap:?}"
);
}
assert_eq!(a.state(), ConnectionState::Connected);
}
#[test]
fn test_consent_is_kept_on_a_tcp_pair_whose_round_trip_is_long() {
for round_trip in [800 * MS, 6000 * MS] {
let now = Instant::now();
let mut a = tcp_agent(now, 1);
let mut in_flight: Vec<(Instant, Message)> = vec![];
let mut states = vec![];
let mut consent_requests = 0;
let until = now + Duration::from_secs(120);
let mut at = now;
loop {
in_flight.retain(|(arrival, response)| {
if *arrival > at {
return true;
}
a.handle_read(TaggedBytesMut {
now: at,
transport: tcp_path(5001),
message: BytesMut::from(&response.raw[..]),
})
.unwrap();
false
});
a.handle_timeout(at).unwrap();
while let Some(t) = a.poll_write() {
assert_eq!(t.transport.transport_protocol, TransportProtocol::TCP);
let mut m = Message {
raw: t.message.to_vec(),
..Message::default()
};
m.decode().unwrap();
let response = peer_response(
BINDING_SUCCESS,
m.transaction_id,
addr("192.168.0.2:40000"),
None,
);
if a.get_selected_candidate_pair().is_some() {
consent_requests += 1;
in_flight.push((at + round_trip, response));
} else {
in_flight.push((at, response));
}
}
states.extend(events(&mut a).into_iter().filter_map(|e| match e {
Event::ConnectionStateChange(s) => Some(s),
_ => None,
}));
let arrival = in_flight.iter().map(|(arrival, _)| *arrival).min();
let next = match (a.poll_timeout(), arrival) {
(Some(timer), Some(arrival)) => Some(timer.min(arrival)),
(timer, arrival) => timer.or(arrival),
};
match next {
Some(next) if next <= until => at = next.max(at + Duration::from_micros(1)),
_ => break,
}
}
assert!(
consent_requests >= 15,
"{consent_requests} consent requests"
);
assert!(
!states.contains(&ConnectionState::Failed),
"{round_trip:?}: {states:?}"
);
if round_trip < Duration::from_secs(5) {
assert!(
!states.contains(&ConnectionState::Disconnected),
"{round_trip:?}: {states:?}"
);
}
let (_, remote) = a.get_selected_candidate_pair().unwrap();
assert!(
until - remote.last_consent().unwrap() < Duration::from_secs(7),
"{round_trip:?}"
);
}
}
#[test]
fn test_after_consent_is_lost_the_same_credentials_are_not_used_again() {
let now = Instant::now();
let cfg = AgentConfig {
local_ufrag: "pinnedufrag".to_owned(),
local_pwd: "pinnedpasswordpinnedpassword".to_owned(),
..config(true)
};
let mut a = started_agent(now, cfg, &[REMOTE_A]);
a.set_local_gathering_complete();
a.set_remote_end_of_candidates();
let out = run_answering(&mut a, now, now + Duration::from_secs(1), |_, _| true);
let username = |m: &Message| String::from_utf8(m.get(ATTR_USERNAME).unwrap()).unwrap();
let before = username(&requests(&out)[0].0);
assert_eq!(before, format!("{REMOTE_UFRAG}:pinnedufrag"));
run(
&mut a,
now + Duration::from_secs(1),
now + Duration::from_secs(40),
);
assert_eq!(a.state(), ConnectionState::Failed);
let silent = run(
&mut a,
now + Duration::from_secs(40),
now + Duration::from_secs(80),
);
assert!(silent.is_empty(), "nothing is sent without consent");
let later = now + Duration::from_secs(80);
a.restart(
later,
"pinnedufrag".to_owned(),
"pinnedpasswordpinnedpassword".to_owned(),
false,
)
.unwrap();
a.add_local_candidate(host(addr(LOCAL))).unwrap();
a.add_remote_candidate(host(addr(REMOTE_A))).unwrap();
a.start_connectivity_checks(later, true, REMOTE_UFRAG.to_owned(), REMOTE_PWD.to_owned())
.unwrap();
let out = run(&mut a, later, later + Duration::from_secs(1));
let after = username(&requests(&out)[0].0);
assert_ne!(after, before, "the same credentials on the same 5-tuple");
}
const MDNS_GROUP: &str = "224.0.0.251:5353";
const NAME: &str = "61b445d2-6503-41ac-96ce-ee3edac00e9f.local";
fn mdns_candidate() -> Candidate {
unmarshal_candidate(&format!(
"1114572465 1 udp 2113939711 {NAME} 61163 typ host"
))
.unwrap()
}
fn first_mdns_write(a: &mut Agent, from: Instant) -> Option<Duration> {
let mut at = from;
for _ in 0..1000 {
a.handle_timeout(at).unwrap();
while let Some(t) = a.poll_write() {
if t.transport.peer_addr == addr(MDNS_GROUP) {
return Some(t.now.duration_since(from));
}
}
at = a.poll_timeout()?.max(at + MS);
if at > from + Duration::from_secs(2) {
return None;
}
}
None
}
#[test]
fn test_first_mdns_query_waits_20_to_120_ms_by_default() {
let now = Instant::now();
let mut a = Agent::new(now, Arc::new(AgentConfig::default()), provider()).unwrap();
assert!(!a.add_remote_candidate(mdns_candidate()).unwrap());
let delay = first_mdns_write(&mut a, now).expect("a query");
assert!((20 * MS..=120 * MS).contains(&delay), "{delay:?}");
assert_eq!(a.pending_mdns_candidates().count(), 1);
assert!(a.get_remote_candidates().is_empty());
let cfg = AgentConfig {
multicast_dns_first_query_delay: Some(false),
..AgentConfig::default()
};
let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
a.add_remote_candidate(mdns_candidate()).unwrap();
assert_eq!(first_mdns_write(&mut a, now), Some(Duration::ZERO));
}
#[test]
fn test_agents_share_one_mdns_rate_limit() {
let now = Instant::now();
let limit = mdns::MdnsRateLimit::new(1, 1);
let agent = |limit: Option<mdns::MdnsRateLimit>| {
let cfg = AgentConfig {
multicast_dns_first_query_delay: Some(false),
multicast_dns_rate_limit: limit,
..AgentConfig::default()
};
let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
a.add_remote_candidate(mdns_candidate()).unwrap();
a
};
let (mut first, mut second) = (agent(Some(limit.clone())), agent(Some(limit)));
assert_eq!(first_mdns_write(&mut first, now), Some(Duration::ZERO));
let waited = first_mdns_write(&mut second, now).expect("the second agent's query");
assert!(waited >= Duration::from_millis(900), "{waited:?}");
let (mut first, mut second) = (agent(None), agent(None));
assert_eq!(first_mdns_write(&mut first, now), Some(Duration::ZERO));
assert_eq!(first_mdns_write(&mut second, now), Some(Duration::ZERO));
}
#[test]
fn test_legacy_unicast_query_is_answered_by_unicast() {
let now = Instant::now();
let cfg = AgentConfig {
multicast_dns_mode: MulticastDnsMode::QueryAndGather,
..AgentConfig::default()
};
let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
a.add_local_candidate(host(addr(LOCAL))).unwrap();
let name = a.get_local_candidates()[0].address().to_owned();
assert!(name.ends_with(".local"));
let mut at = now;
while at < now + Duration::from_secs(5) {
a.handle_timeout(at).unwrap();
while a.poll_write().is_some() {}
at = a
.poll_timeout()
.map_or(at + Duration::from_secs(1), |t| t.max(at + MS));
}
let mut query = vec![0x12, 0x34, 0x00, 0x00, 0, 1, 0, 0, 0, 0, 0, 0];
for label in name.split('.') {
query.push(u8::try_from(label.len()).unwrap());
query.extend_from_slice(label.as_bytes());
}
query.extend_from_slice(&[0, 0, 1, 0, 1]);
a.handle_read(TaggedBytesMut {
now: at,
transport: path("0.0.0.0:5353", "192.168.0.50:40123", TransportProtocol::UDP),
message: BytesMut::from(&query[..]),
})
.unwrap();
a.handle_timeout(at).unwrap();
let replies: Vec<TaggedBytesMut> = std::iter::from_fn(|| a.poll_write()).collect();
assert_eq!(replies.len(), 1, "one reply");
assert_eq!(replies[0].transport.peer_addr, addr("192.168.0.50:40123"));
assert_eq!(&replies[0].message[..2], &[0x12, 0x34], "the query's ID");
assert_ne!(replies[0].message[2] & 0x80, 0, "a response");
}
#[test]
fn test_lost_mdns_name_is_replaced_and_the_candidate_announced_again() {
use mdns::{Mdns, MdnsConfig, MdnsProbePolicy};
let now = Instant::now();
let lost = "ice-agent-host.local";
let cfg = AgentConfig {
multicast_dns_mode: MulticastDnsMode::QueryAndGather,
multicast_dns_local_name: lost.to_owned(),
multicast_dns_local_ip: Some("192.168.0.2".parse().unwrap()),
..AgentConfig::default()
};
let mut a = Agent::new(now, Arc::new(cfg), provider()).unwrap();
a.add_local_candidate(host(addr(LOCAL))).unwrap();
assert_eq!(a.get_local_candidates()[0].address(), lost);
let id = a.get_local_candidates()[0].id().to_owned();
let probed = first_mdns_write(&mut a, now).expect("a probe");
let at = now + probed;
let mut owner = Mdns::new(
now,
MdnsConfig::new()
.with_local_names(vec![lost.to_owned()])
.with_local_ip("192.168.0.77".parse().unwrap())
.with_probe_policy(MdnsProbePolicy::Never),
);
owner.handle_timeout(at).unwrap();
let announcement = owner.poll_write().expect("the owner announces its name");
a.handle_read(TaggedBytesMut {
now: at,
transport: path(MDNS_GROUP, "192.168.0.77:5353", TransportProtocol::UDP),
message: announcement.message,
})
.unwrap();
let renamed: Vec<Candidate> = events(&mut a)
.into_iter()
.filter_map(|e| match e {
Event::LocalCandidateRenamed(c) => Some(*c),
_ => None,
})
.collect();
assert_eq!(renamed.len(), 1);
let name = renamed[0].address().to_owned();
assert_ne!(name, lost);
assert!(crate::mdns::is_ice_mdns_name(&name), "{name}");
assert_eq!(renamed[0].id(), id, "the same candidate");
assert_eq!(a.get_local_candidates()[0].address(), name);
assert_eq!(
a.get_local_candidates_to_signal()[0].marshal(),
renamed[0].marshal()
);
assert!(renamed[0].marshal().contains(&name));
a.add_local_candidate(udp("192.168.0.2", 1001, 0)).unwrap();
assert_eq!(a.get_local_candidates()[1].address(), name);
}
#[test]
fn test_supplied_credentials_are_held_to_the_rfc_8839_grammar() {
let now = Instant::now();
let made = |ufrag: &str, pwd: &str| {
Agent::new(
now,
Arc::new(AgentConfig {
local_ufrag: ufrag.to_owned(),
local_pwd: pwd.to_owned(),
..Default::default()
}),
provider(),
)
.map(|_| ())
};
let pwd = "p".repeat(22);
assert_eq!(made("abc", &pwd), Err(Error::ErrLocalUfragInsufficientBits));
assert_eq!(
made("abcd", &"p".repeat(21)),
Err(Error::ErrLocalPwdInsufficientBits)
);
assert_eq!(
made(&"u".repeat(33), &pwd),
Err(Error::ErrLocalUfragInvalid)
);
assert_eq!(made("ab-d", &pwd), Err(Error::ErrLocalUfragInvalid));
assert_eq!(made("ab d", &pwd), Err(Error::ErrLocalUfragInvalid));
assert_eq!(
made("abcd", &"p".repeat(257)),
Err(Error::ErrLocalPwdInvalid)
);
assert_eq!(
made("abcd", &format!("{}:", "p".repeat(22))),
Err(Error::ErrLocalPwdInvalid)
);
assert_eq!(made("abcd", &pwd), Ok(()));
assert_eq!(made(&"u".repeat(32), &"p".repeat(256)), Ok(()));
assert_eq!(made("a+/9", &format!("{}+/", "p".repeat(20))), Ok(()));
let mut a = Agent::new(now, Arc::new(AgentConfig::default()), provider()).unwrap();
assert_eq!(
a.restart(now, "ab-d".to_owned(), pwd.clone(), false),
Err(Error::ErrLocalUfragInvalid)
);
a.restart(now, "abcd".to_owned(), pwd, false).unwrap();
assert_eq!(a.get_local_credentials().ufrag, "abcd");
}
#[test]
fn test_a_local_reflexive_candidate_needs_its_related_address() {
use crate::candidate::candidate_relay::CandidateRelayConfig;
use crate::candidate::candidate_server_reflexive::CandidateServerReflexiveConfig;
let now = Instant::now();
let mut a = Agent::new(now, Arc::new(AgentConfig::default()), provider()).unwrap();
let base = |port: u16| CandidateConfig {
network: "udp".to_owned(),
address: "198.51.100.7".to_owned(),
port,
component: 1,
..Default::default()
};
let srflx = |rel_addr: &str, port: u16| {
CandidateServerReflexiveConfig {
base_config: base(port),
rel_addr: rel_addr.to_owned(),
rel_port: 5000,
url: None,
}
.new_candidate_server_reflexive()
.unwrap()
};
let relay = |rel_addr: &str, port: u16| {
CandidateRelayConfig {
base_config: base(port),
rel_addr: rel_addr.to_owned(),
rel_port: 5000,
url: None,
}
.new_candidate_relay()
.unwrap()
};
assert_eq!(
a.add_local_candidate(srflx("", 6000)),
Err(Error::ErrLocalCandidateWithoutRelatedAddress)
);
assert_eq!(
a.add_local_candidate(relay("", 6001)),
Err(Error::ErrLocalCandidateWithoutRelatedAddress)
);
assert_eq!(a.add_local_candidate(srflx("192.0.2.1", 6002)), Ok(true));
assert_eq!(a.add_local_candidate(relay("0.0.0.0", 6003)), Ok(true));
}