#![cfg(feature = "runtime-mock")]
mod common;
use common::{Peer, Result, negotiate, run_until};
use rtci::ice::mdns::MulticastDnsMode;
use rtci::mdns::{MDNS_MULTICAST_IPV4, MDNS_PORT, MdnsRateLimit};
use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
use rtci::peer_connection::transport::RTCIceCandidateType;
use rtci_stream_driver::peer::PeerDriverConfig;
use rtci_stream_driver::runtime::mock::LinkConfig;
use rtci_stream_driver::runtime::{MockRuntime, Runtime};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;
const A: IpAddr = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
const B: IpAddr = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 2));
const STEP: Duration = Duration::from_millis(10);
fn publishing() -> SettingEngineBuilder {
SettingEngineBuilder::new()
.with_multicast_dns_mode(MulticastDnsMode::QueryAndGather)
.with_tcp_candidate_gathering(false)
}
#[test]
fn two_publishing_drivers_resolve_each_other_and_connect_on_virtual_time() -> Result<()> {
let runtime = MockRuntime::new();
let shared: Arc<dyn Runtime> = Arc::new(runtime.clone());
let mut a = Peer::with_settings(shared.clone(), A, publishing())?;
let mut b = Peer::with_settings(shared, B, publishing())?;
let mut up = false;
runtime.run(async {
negotiate(&mut a, &mut b)?;
up = run_until(&mut a, &mut b, STEP, 2_000, |a, b| {
a.connected() && b.connected()
})
.await?;
Ok::<(), Box<dyn std::error::Error>>(())
})?;
let mdns: Vec<_> = runtime
.net()
.log()
.into_iter()
.filter(|d| d.to.port() == MDNS_PORT)
.collect();
let state = |p: &Peer| {
format!(
"{:?}, mDNS sent {} received {}, unsent {}",
p.driver.pc().connection_state(),
p.driver.mdns_datagrams_sent(),
p.driver.mdns_datagrams_received(),
p.driver.unsent_datagrams()
)
};
assert!(
up,
"connected over the resolved .local candidates: a {} / b {}",
state(&a),
state(&b)
);
assert!(
mdns.iter().any(|d| d.from.ip() == A) && mdns.iter().any(|d| d.from.ip() == B),
"each driver's mDNS answers left from the address of their record: {mdns:?}"
);
assert_eq!(
(
a.driver.local_addresses().len(),
b.driver.local_addresses().len()
),
(1, 1),
"each driver still holds the UDP socket of its .local candidate"
);
Ok(())
}
fn cut(runtime: &MockRuntime, a: IpAddr, b: IpAddr) {
runtime.net().set_links(
a,
b,
LinkConfig {
blocked: true,
..Default::default()
},
);
}
fn resolve_and_connect(runtime: &MockRuntime, a: &mut Peer, b: &mut Peer) -> Result<bool> {
runtime.run(async {
negotiate(a, b)?;
run_until(a, b, STEP, 2_000, |a, b| {
a.connected()
&& b.connected()
&& a.selected_remote_type() == Some(RTCIceCandidateType::Host)
&& b.selected_remote_type() == Some(RTCIceCandidateType::Host)
})
.await
})
}
#[test]
fn drivers_whose_link_is_not_the_default_multicast_interface_resolve_each_other() -> Result<()> {
const DEFAULT: IpAddr = IpAddr::V4(Ipv4Addr::new(10, 9, 9, 9));
const A1: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
const B1: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2));
let runtime = MockRuntime::new();
runtime.net().set_default_multicast_interface(DEFAULT);
cut(&runtime, DEFAULT, A1);
cut(&runtime, DEFAULT, B1);
let shared: Arc<dyn Runtime> = Arc::new(runtime.clone());
let mut a = Peer::with_settings(shared.clone(), A1, publishing())?;
let mut b = Peer::with_settings(shared, B1, publishing())?;
let up = resolve_and_connect(&runtime, &mut a, &mut b)?;
assert!(
up,
"both resolved the other's name and connected over it: a {:?} {:?}, b {:?} {:?}",
a.driver.pc().connection_state(),
a.selected_remote_type(),
b.driver.pc().connection_state(),
b.selected_remote_type()
);
Ok(())
}
#[test]
fn a_multi_homed_driver_answers_for_each_name_on_the_interface_of_its_address() -> Result<()> {
const A0: IpAddr = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
const A1: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1));
const B1: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2));
let runtime = MockRuntime::new();
runtime.net().set_default_multicast_interface(A0);
cut(&runtime, A0, B1);
let shared: Arc<dyn Runtime> = Arc::new(runtime.clone());
let mut a = Peer::with_config(
shared.clone(),
publishing(),
PeerDriverConfig::on(vec![A0, A1]),
)?;
let mut b = Peer::with_settings(shared, B1, publishing())?;
let up = resolve_and_connect(&runtime, &mut a, &mut b)?;
let answers_from_a1 = runtime
.net()
.log()
.iter()
.filter(|d| d.to.port() == MDNS_PORT && d.from.ip() == A1)
.count();
assert!(
up,
"b resolved a's name on their shared link: a {:?} {:?}, b {:?} {:?}",
a.driver.pc().connection_state(),
a.selected_remote_type(),
b.driver.pc().connection_state(),
b.selected_remote_type()
);
assert!(
answers_from_a1 > 0,
"a's mDNS for its 192.168.1.1 name left from it"
);
Ok(())
}
#[test]
fn a_publishing_driver_gives_each_of_its_sockets_a_port_of_its_own() -> Result<()> {
const A0: IpAddr = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
const A1: IpAddr = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 3));
let runtime = MockRuntime::new();
let shared: Arc<dyn Runtime> = Arc::new(runtime.clone());
let mut a = Peer::with_config(
shared.clone(),
publishing(),
PeerDriverConfig::on(vec![A0, A1]),
)?;
let mut b = Peer::with_settings(shared, B, publishing())?;
let up = resolve_and_connect(&runtime, &mut a, &mut b)?;
let ports: Vec<u16> = a
.driver
.local_addresses()
.iter()
.map(|l| l.port())
.collect();
assert_eq!(ports.len(), 2, "{ports:?}");
assert_ne!(ports[0], ports[1], "one port per socket: {ports:?}");
assert!(up, "connected over the resolved names");
Ok(())
}
#[test]
fn drivers_on_a_lan_of_public_addresses_resolve_each_other() -> Result<()> {
const A1: IpAddr = IpAddr::V4(Ipv4Addr::new(198, 51, 100, 1));
const B1: IpAddr = IpAddr::V4(Ipv4Addr::new(198, 51, 100, 2));
let runtime = MockRuntime::new();
let shared: Arc<dyn Runtime> = Arc::new(runtime.clone());
let mut a = Peer::with_settings(shared.clone(), A1, publishing())?;
let mut b = Peer::with_settings(shared, B1, publishing())?;
let up = resolve_and_connect(&runtime, &mut a, &mut b)?;
assert!(
up,
"resolved over the group from public sources: a {:?} {:?}, b {:?} {:?}",
a.driver.pc().connection_state(),
a.selected_remote_type(),
b.driver.pc().connection_state(),
b.selected_remote_type()
);
Ok(())
}
#[test]
fn two_drivers_send_their_mdns_within_one_shared_budget_on_virtual_time() -> Result<()> {
const BURST: u32 = 1;
const PER_SECOND: u32 = 1;
let runtime = MockRuntime::new();
let shared: Arc<dyn Runtime> = Arc::new(runtime.clone());
let limit = MdnsRateLimit::new(BURST, PER_SECOND);
let config = |ip: IpAddr| PeerDriverConfig {
mdns_rate_limit: Some(limit.clone()),
..PeerDriverConfig::on(vec![ip])
};
let mut a = Peer::with_config(shared.clone(), publishing(), config(A))?;
let mut b = Peer::with_config(shared, publishing(), config(B))?;
let start = runtime.now();
let up = runtime.run(async {
negotiate(&mut a, &mut b)?;
let up = run_until(&mut a, &mut b, STEP, 10_000, |a, b| {
a.connected() && b.connected()
})
.await?;
run_until(&mut a, &mut b, STEP, 10_000, |a, _| {
a.now().duration_since(start) >= Duration::from_secs(4)
})
.await?;
Ok::<bool, Box<dyn std::error::Error>>(up)
})?;
let elapsed = runtime.now().duration_since(start);
let sent = a.driver.mdns_datagrams_sent() + b.driver.mdns_datagrams_sent();
let allowed = u64::from(BURST) + (elapsed.as_secs_f64() * f64::from(PER_SECOND)).ceil() as u64;
assert!(
elapsed >= Duration::from_secs(4),
"the exchange ran {elapsed:?} of virtual time"
);
assert!(
sent <= allowed,
"{sent} mDNS datagrams in {elapsed:?}: the shared budget allows {allowed}"
);
assert!(
sent > u64::from(BURST),
"the held mDNS left after the burst: {sent}"
);
assert!(
up,
"connected, the held mDNS sent as the budget allowed: a {:?} / b {:?}",
a.driver.pc().connection_state(),
b.driver.pc().connection_state()
);
Ok(())
}
struct OnLoopback {
group_from_loopback: usize,
available: (bool, bool),
up: bool,
}
const LOOPBACK: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
fn drivers_on_loopback(config: impl Fn() -> PeerDriverConfig) -> Result<OnLoopback> {
let runtime = MockRuntime::new();
let shared: Arc<dyn Runtime> = Arc::new(runtime.clone());
let mut a = Peer::with_config(shared.clone(), publishing(), config())?;
let mut b = Peer::with_config(shared, publishing(), config())?;
let up = resolve_and_connect(&runtime, &mut a, &mut b)?;
let group_from_loopback = runtime
.net()
.log()
.iter()
.filter(|d| d.to == SocketAddr::from((MDNS_MULTICAST_IPV4, MDNS_PORT)))
.filter(|d| d.from.ip() == LOOPBACK)
.count();
Ok(OnLoopback {
group_from_loopback,
available: (
a.driver.mdns_available(false),
b.driver.mdns_available(false),
),
up,
})
}
#[test]
fn drivers_asked_for_mdns_on_loopback_send_it_there_and_connect() -> Result<()> {
let run = drivers_on_loopback(|| PeerDriverConfig {
mdns_on_loopback_on_windows: true,
..PeerDriverConfig::on(vec![LOOPBACK])
})?;
assert!(
run.group_from_loopback > 0,
"mDNS went to the group from 127.0.0.1"
);
assert_eq!(run.available, (true, true), "both IPv4 mDNS sockets bound");
assert!(run.up, "connected over the resolved loopback names");
Ok(())
}
#[test]
fn by_default_drivers_on_loopback_send_no_mdns_there_on_windows_only() -> Result<()> {
let run = drivers_on_loopback(|| PeerDriverConfig::on(vec![LOOPBACK]))?;
if cfg!(windows) {
assert_eq!(
run.group_from_loopback, 0,
"no mDNS to the group from 127.0.0.1"
);
assert_eq!(run.available, (false, false), "no IPv4 mDNS socket");
} else {
assert!(run.group_from_loopback > 0, "unchanged off Windows");
assert_eq!(run.available, (true, true));
assert!(run.up);
}
Ok(())
}