Implement UDP mux gather test
use super::agent_vnet_test::*;
use super::*;
+use crate::udp_mux::{UDPMuxDefault, UDPMuxParams};
use crate::util::*;
use ipnet::IpNet;
use std::str::FromStr;
+use tokio::net::UdpSocket;
use util::vnet::*;
#[tokio::test]
@@ -412,3 +414,70 @@ async fn test_vnet_gather_turn_connection_leak() -> Result<()> {
Ok(())
}
+
+#[tokio::test]
+async fn test_vnet_gather_muxed_udp() -> Result<()> {
+ let udp_socket = UdpSocket::bind("0.0.0.0:0").await?;
+ let udp_mux = UDPMuxDefault::new(UDPMuxParams::new(udp_socket));
+
+ let lan = Arc::new(Mutex::new(router::Router::new(router::RouterConfig {
+ cidr: "10.0.0.0/24".to_owned(),
+ nat_type: Some(nat::NatType {
+ mode: nat::NatMode::Nat1To1,
+ ..Default::default()
+ }),
+ ..Default::default()
+ })?));
+
+ let nw = Arc::new(net::Net::new(Some(net::NetConfig {
+ static_ips: vec!["10.0.0.1".to_owned()],
+ ..Default::default()
+ })));
+
+ connect_net2router(&nw, &lan).await?;
+
+ let a = Agent::new(AgentConfig {
+ network_types: vec![NetworkType::Udp4],
+ nat_1to1_ips: vec!["1.2.3.4".to_owned()],
+ net: Some(nw),
+ udp_network: UDPNetwork::Muxed(udp_mux),
+ ..Default::default()
+ })
+ .await?;
+
+ let (done_tx, mut done_rx) = mpsc::channel::<()>(1);
+ let done_tx = Arc::new(Mutex::new(Some(done_tx)));
+ a.on_candidate(Box::new(
+ move |c: Option<Arc<dyn Candidate + Send + Sync>>| {
+ let done_tx_clone = Arc::clone(&done_tx);
+ Box::pin(async move {
+ if c.is_none() {
+ let mut tx = done_tx_clone.lock().await;
+ tx.take();
+ }
+ })
+ },
+ ))
+ .await;
+
+ a.gather_candidates().await?;
+
+ log::debug!("wait for gathering is done...");
+ let _ = done_rx.recv().await;
+ log::debug!("gathering is done");
+
+ let candidates = a.get_local_candidates().await?;
+ assert_eq!(candidates.len(), 1, "There must be a single candidate");
+
+ let candi = &candidates[0];
+ let laddr = candi.get_conn().unwrap().local_addr().await?;
+ assert_eq!(candi.address(), "1.2.3.4");
+ assert_eq!(
+ candi.port(),
+ laddr.port(),
+ "Unexpected candidate port: {}",
+ candi.port()
+ );
+
+ Ok(())
+}
