use meshport::Network; use std::{net::SocketAddr, time::Duration}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; fn config(ip: &str, port: u16, server: bool) -> String { let transport = if server { format!("listeners = ['udp://127.0.0.1:{port}']\n") } else { format!("listeners = []\n[[peer]]\nuri = 'udp://127.0.0.1:{port}'\n") }; format!( "ipv4 = '{ip}'\nstun_servers = []\nstun_servers_v6 = []\n{transport}\n[network_identity]\nnetwork_name = 'meshport-test'\nnetwork_secret = 'test-only'\n[flags]\ndefault_protocol = 'udp'\n" ) } #[tokio::test(flavor = "multi_thread", worker_threads = 4)] async fn real_overlay_tcp_udp_and_restart() { let port = std::net::UdpSocket::bind("127.0.0.1:0") .unwrap() .local_addr() .unwrap() .port(); let server = Network::start(&config("10.77.0.2", port, true)) .await .unwrap(); let client = Network::start(&config("10.77.0.11", port, false)) .await .unwrap(); let timeout = Duration::from_secs(10); let mut listener = server.listen_tcp(7007, timeout).await.unwrap(); let tcp_echo = tokio::spawn(async move { let (mut stream, _) = listener.accept().await.unwrap(); let mut bytes = [0; 128]; let n = stream.read(&mut bytes).await.unwrap(); stream.write_all(&bytes[..n]).await.unwrap(); // Keep the connection alive until the client consumes its echo. let _ = stream.read(&mut bytes).await; }); let udp = server.bind_udp(7007, timeout).await.unwrap(); let client_udp = client.bind_udp(0, timeout).await.unwrap(); // Upstream UDP is outbound-flow scoped; register the known remote first. udp.send_to(b"warmup", client_udp.local_addr()) .await .unwrap(); let udp_echo = tokio::spawn(async move { let mut bytes = [0; 128]; for _ in 0..4 { let (n, addr) = udp.recv_from(&mut bytes).await.unwrap(); udp.send_to(&bytes[..n], addr).await.unwrap(); } tokio::time::sleep(Duration::from_millis(300)).await; }); let target: SocketAddr = "10.77.0.2:7007".parse().unwrap(); let started = std::time::Instant::now(); while client.snapshot().await["peer_count"].as_u64().unwrap() == 0 { assert!(started.elapsed() < timeout, "peer discovery timed out"); tokio::time::sleep(Duration::from_millis(100)).await; } tokio::time::sleep(Duration::from_millis(500)).await; let before = client.snapshot().await; assert_eq!(before["connectivity"]["disable_p2p"], false); assert_eq!(before["connectivity"]["disable_udp_hole_punching"], false); assert_eq!(before["connectivity"]["disable_tcp_hole_punching"], false); assert_eq!(before["connectivity"]["listeners"], serde_json::json!([])); assert!( !before["peers"][0]["transports"] .as_array() .unwrap() .is_empty() ); let tcp = client.probe(target, false, true, timeout).await.unwrap(); assert!(tcp["verified_bytes"].as_u64().unwrap() > 0); let received = meshport_netstack::udp_echo(&client_udp, target, b"udp-echo", timeout) .await .unwrap(); assert_eq!(received, b"udp-echo"); tokio::time::sleep(Duration::from_millis(200)).await; let after = client.snapshot().await; assert!(!after["native_events"].as_array().unwrap().is_empty()); assert!(!after.to_string().contains("test-only")); assert!( after["traffic"]["rx_bytes"].as_u64().unwrap() > before["traffic"]["rx_bytes"].as_u64().unwrap() ); assert!( after["traffic"]["tx_bytes"].as_u64().unwrap() > before["traffic"]["tx_bytes"].as_u64().unwrap() ); assert!(after["traffic"]["rx_bytes_per_second"].as_f64().unwrap() > 0.0); assert!(after["peers"][0]["rx_bytes"].as_u64().unwrap() > 0); let started = std::time::Instant::now(); while client.snapshot().await["peers"][0]["latency_ms"] .as_f64() .is_none() { assert!( started.elapsed() < timeout, "link RTT measurement timed out" ); tokio::time::sleep(Duration::from_millis(100)).await; } for sequence in 0..3 { let sample = client .sample_udp_quality(&client_udp, target, sequence, timeout) .await .unwrap(); assert_eq!(sample["success"], true); assert!(sample["elapsed_ms"].as_f64().unwrap() > 0.0); assert_eq!(sample["next_hop_peer_id"], sample["target_peer_id"]); } tcp_echo.abort(); udp_echo.abort(); client.stop().await; server.stop().await; client.stop().await; for _ in 0..3 { let client = Network::start(&config("10.77.0.11", port, false)) .await .unwrap(); client.stop().await; } }