Files
MeshPort/crates/meshport-ffi/tests/udp_fixture.rs
T
shenleiandClaude Haiku 5.5 2f185f914d Snapshot uncommitted work: proxy UI, sing-box integration, socks bridge
- Lab app: proxy panel, node editor, configuration and detail pages,
  responsive layout, and related tests
- Add meshport-socks crate and sing-box bridge scripts/docs
- Add singbox_flutter binding and meshport_client_core package
- FFI, VPN helper (unified mac path), and Android/macOS runner updates
- Docs for architecture, client runtime, UI design, and validation
- UI design output artifacts under outputs/ui

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-10-12 10:06:15 +09:00

78 lines
3.1 KiB
Rust

use easytier::instance::factory::create_native_instance;
use easytier_core::config::toml::TomlConfig;
use serde_json::{Value, json};
use std::ffi::{CStr, CString};
fn call(id: u64, request: Value) -> Value {
let input = CString::new(request.to_string()).unwrap();
unsafe {
let reply = meshport_ffi::meshport_call(id, input.as_ptr());
let result = serde_json::from_slice(CStr::from_ptr(reply).to_bytes()).unwrap();
meshport_ffi::meshport_string_free(reply);
result
}
}
use std::time::Duration;
use tokio::net::UdpSocket;
#[test]
fn ffi_udp_through_adjacent_mapped_subnet() {
let runtime = tokio::runtime::Runtime::new().unwrap();
let _entered = runtime.enter();
let setup = async {
let port = UdpSocket::bind("127.0.0.1:0")
.await
.unwrap()
.local_addr()
.unwrap()
.port();
let identity =
"[network_identity]\nnetwork_name='unified-udp-fixture'\nnetwork_secret='test-only'\n";
let server_config = TomlConfig::new_from_str(&format!(
"ipv4='10.253.88.2/25'\nlisteners=['udp://127.0.0.1:{port}']\nstun_servers=[]\nstun_servers_v6=[]\n[[proxy_network]]\ncidr='127.0.0.0/25'\nmapped_cidr='10.253.88.128/25'\n{identity}[flags]\nno_tun=true\nuse_smoltcp=true"
)).unwrap();
let server = create_native_instance(server_config).unwrap();
server.start().await.unwrap();
let client = format!(
"ipv4='10.253.88.11/25'\nlisteners=[]\nstun_servers=[]\nstun_servers_v6=[]\n[[peer]]\nuri='udp://127.0.0.1:{port}'\n{identity}[flags]\nno_tun=true\nuse_smoltcp=true"
);
(server, client)
};
let (server, client) = runtime.block_on(setup);
drop(_entered);
let id = meshport_ffi::meshport_context_new();
assert_eq!(call(id, json!({"op":"start", "toml":client}))["ok"], true);
let deadline = std::time::Instant::now();
loop {
if call(id, json!({"op":"status"}))["data"]["platform_network"]["subnet_routes"]
.as_array()
.is_some_and(|v| v.iter().any(|r| r == "10.253.88.128/25"))
{
break;
}
assert!(deadline.elapsed() < Duration::from_secs(15));
std::thread::sleep(Duration::from_millis(100));
}
let (target, echo): (std::net::SocketAddr, _) = runtime.block_on(async {
let udp = UdpSocket::bind("127.0.0.1:0").await.unwrap();
let target = format!("10.253.88.129:{}", udp.local_addr().unwrap().port())
.parse()
.unwrap();
let echo = tokio::spawn(async move {
let mut buffer = [0; 128];
let (n, source) = udp.recv_from(&mut buffer).await.unwrap();
udp.send_to(&buffer[..n], source).await.unwrap();
});
(target, echo)
});
let result = call(
id,
json!({"op":"probe", "target":target.to_string(), "protocol":"UDP", "echo":true, "timeout_ms":3000}),
);
echo.abort();
call(id, json!({"op":"stop"}));
meshport_ffi::meshport_context_free(id);
runtime.block_on(server.stop());
assert_eq!(result["ok"], true, "FFI UDP failed: {result}");
}