Files
tsunagi/crates/tsunagi-tcp-tls/tests/transport_tests.rs
T
Aleksandr Bogomiakov 87fa83e482 Confirm peer data paths and fit the Devices window to its roster
Measure the encrypted round trip to each reachable peer using the existing overlay echo, including over mesh relays. Keep the last sample during a new probe or one missed reply. Clear it when the route or addresses are lost or two probes time out. Exclude these packets from application counters and idle activity. Ready and Idle require a confirmed path and a usable key.

Require a verified data path before a speed test can send, and let a responder finish its first echo if the initiator is ready first. Keep the payload on the encrypted tunnel and reserve time for reliable feedback within the five-second deadline. Native TCP TLS now prefers 9442, advertises the actual bound port and honors explicit overrides. Existing wire versions remain compatible.

Extend the network and peer traffic plots to the right edge. Set the initial Devices height from the roster and row spacing, bounded by the monitor and a 260–780 point range. Subsequent snapshots preserve manual resizing.

Validated on macOS arm64 with workspace formatting, strict Clippy and the locked all-targets suite (692 passed, 8 ignored by default). Also ran the opt-in 120-second WireGuard rekey test. Coverage includes a real three-agent relayed speed test, control-only rejection, delayed control feedback and resized egui frames. Cargo emitted a manifest warning about the existing directories dependency.
2026-11-26 17:13:56 +02:00

131 lines
4.5 KiB
Rust

#![allow(missing_docs)]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use bytes::Bytes;
use tokio::sync::mpsc;
use tsunagi::dataplane::transport::{InboundLink, PacketTransport};
use tsunagi::identity::{DeviceIdentity, NetworkId};
use tsunagi_tcp_tls::TcpTlsTransport;
#[tokio::test]
async fn default_listeners_use_high_ports_and_report_the_actual_bound_port() {
use tsunagi::dataplane::transport::DataCarrier;
use tsunagi_tcp_tls::{TcpTlsCarrier, TcpTlsConfig, transport::DEFAULT_PORT};
assert_eq!(DEFAULT_PORT, 9442);
let option = TcpTlsConfig::OPTIONS
.iter()
.find(|o| o.key == "port")
.unwrap();
assert_eq!(
option.default.unwrap().parse::<u16>().unwrap(),
DEFAULT_PORT
);
let identity = DeviceIdentity::generate();
let first = TcpTlsTransport::bind(&identity, None, None).await.unwrap();
let second = Arc::new(TcpTlsTransport::bind(&identity, None, None).await.unwrap());
assert!(first.bound_port() >= 1024 && second.bound_port() >= 1024);
assert_ne!(first.bound_port(), second.bound_port());
assert!(
TcpTlsTransport::bind(&identity, Some(first.bound_port()), None)
.await
.is_err()
);
let cap = TcpTlsCarrier::new(second.clone())
.local_capability(NetworkId::from_bytes([3; 32]))
.unwrap()
.unwrap();
let (port, _): (u16, Vec<std::net::IpAddr>) = postcard::from_bytes(&cap.data).unwrap();
assert_eq!(port, second.bound_port());
}
#[tokio::test]
async fn test_tcp_tls_transport_loopback() {
let identity_a = DeviceIdentity::generate();
let peer_a = identity_a.endpoint_id();
let identity_b = DeviceIdentity::generate();
let peer_b = identity_b.endpoint_id();
let network = NetworkId::from_bytes([42u8; 32]);
// Bind transport B on port 0 (ephemeral)
let transport_b = Arc::new(
TcpTlsTransport::bind(&identity_b, Some(0), Some("custom.example.com".to_string()))
.await
.expect("bind server should succeed"),
);
let port_b = transport_b.bound_port();
assert!(port_b > 0);
assert_eq!(transport_b.sni(), "custom.example.com");
// Bind transport A on port 0 (ephemeral)
let transport_a = Arc::new(
TcpTlsTransport::bind(&identity_a, Some(0), Some("custom.example.com".to_string()))
.await
.expect("bind client should succeed"),
);
assert_eq!(transport_a.sni(), "custom.example.com");
// Register B's address on transport A
let addr_b: SocketAddr = format!("127.0.0.1:{port_b}").parse().unwrap();
transport_a.set_peer_addr(peer_b, addr_b);
// Spawn accept loop on B
let (inbound_tx, mut inbound_rx) = mpsc::channel::<InboundLink>(1);
let transport_b_clone = Arc::clone(&transport_b);
tokio::spawn(async move {
transport_b_clone
.accept_loop(move |inbound| {
let _ = inbound_tx.try_send(inbound);
})
.await;
});
// Open link from A to B
let link_a = tokio::time::timeout(
Duration::from_secs(5),
transport_a.open(network, peer_b, "tcp-tls"),
)
.await
.expect("open should not timeout")
.expect("open should succeed");
// Receive inbound link on B
let inbound_b = tokio::time::timeout(Duration::from_secs(5), inbound_rx.recv())
.await
.expect("accept should not timeout")
.expect("inbound link should arrive");
assert_eq!(inbound_b.network, network);
assert_eq!(inbound_b.peer, peer_a);
assert_eq!(inbound_b.protocol, "tcp-tls");
let link_b = inbound_b.link;
// Send from A to B
let data_a_to_b = Bytes::from_static(b"packet from A to B");
link_a.send(data_a_to_b.clone()).expect("send A->B");
let received_on_b = tokio::time::timeout(Duration::from_secs(2), link_b.recv())
.await
.expect("recv B timeout")
.expect("recv B option");
assert_eq!(received_on_b, data_a_to_b);
// Send from B to A
let data_b_to_a = Bytes::from_static(b"reply from B to A");
link_b.send(data_b_to_a.clone()).expect("send B->A");
let received_on_a = tokio::time::timeout(Duration::from_secs(2), link_a.recv())
.await
.expect("recv A timeout")
.expect("recv A option");
assert_eq!(received_on_a, data_b_to_a);
// Path descriptions
assert!(link_a.path_description().contains("tcp-tls"));
assert!(link_b.path_description().contains("tcp-tls"));
}