use std::path::PathBuf;
use boxlite::net::{NetworkBackendConfig, NetworkBackendFactory};
fn test_socket_path() -> PathBuf {
PathBuf::from("/tmp/test-network-backend.sock")
}
#[test]
#[cfg(all(not(feature = "libslirp"), not(feature = "gvproxy")))]
fn test_no_backend_when_no_features_enabled() {
let config = NetworkBackendConfig::new(vec![], test_socket_path());
let backend = NetworkBackendFactory::create(config).unwrap();
assert!(
backend.is_none(),
"Expected None when no backend features are enabled"
);
}
#[test]
fn test_network_config_creation() {
let port_mappings = vec![(8080, 80), (3000, 3000), (5432, 5432)];
let config = NetworkBackendConfig::new(port_mappings.clone(), test_socket_path());
assert_eq!(config.port_mappings.len(), 3);
assert_eq!(config.port_mappings, port_mappings);
assert_eq!(config.socket_path, test_socket_path());
}
#[tokio::test]
#[cfg(any(feature = "libslirp", feature = "gvproxy"))]
async fn test_backend_trait_send_sync() {
use boxlite::net::NetworkBackend;
fn assert_send_sync<T: Send + Sync>() {}
let config = NetworkBackendConfig::new(vec![], test_socket_path());
let backend = NetworkBackendFactory::create(config).unwrap();
fn check_send_sync(backend: Box<dyn NetworkBackend>) {
assert_send_sync::<Box<dyn NetworkBackend>>();
drop(backend);
}
if let Some(backend) = backend {
check_send_sync(backend);
}
}
#[test]
fn test_network_config_carries_unique_socket_paths() {
let config_a = NetworkBackendConfig::new(
vec![(8080, 80)],
PathBuf::from("/boxes/box-a/sockets/net.sock"),
);
let config_b = NetworkBackendConfig::new(
vec![(8080, 80)],
PathBuf::from("/boxes/box-b/sockets/net.sock"),
);
assert_ne!(config_a.socket_path, config_b.socket_path);
let json = serde_json::to_string(&config_a).unwrap();
let deserialized: NetworkBackendConfig = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.socket_path, config_a.socket_path);
}