policy_testing_common/
lib.rsuse std::pin::pin;
use fidl_fuchsia_net_interfaces as fnet_interfaces;
use futures::future::{FutureExt as _, LocalBoxFuture};
use netstack_testing_common::realms::{
KnownServiceProvider, Manager, ManagerConfig, Netstack, TestSandboxExt,
};
use netstack_testing_common::{
interfaces, wait_for_component_stopped, ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT,
};
pub async fn with_netcfg_owned_device<
M: Manager,
N: Netstack,
F: for<'a> FnOnce(
u64,
&'a netemul::TestNetwork<'a>,
&'a fnet_interfaces::StateProxy,
&'a netemul::TestRealm<'a>,
&'a netemul::TestSandbox,
) -> LocalBoxFuture<'a, ()>,
>(
name: &str,
manager_config: ManagerConfig,
use_out_of_stack_dhcp_client: bool,
extra_known_service_providers: impl IntoIterator<Item = KnownServiceProvider>,
after_interface_up: F,
) -> String {
let sandbox = netemul::TestSandbox::new().expect("create sandbox");
let realm = sandbox
.create_netstack_realm_with::<N, _, _>(
name,
[
KnownServiceProvider::Manager {
agent: M::MANAGEMENT_AGENT,
use_dhcp_server: false,
use_out_of_stack_dhcp_client,
config: manager_config,
},
KnownServiceProvider::DnsResolver,
KnownServiceProvider::FakeClock,
]
.into_iter()
.chain(extra_known_service_providers)
.chain(
use_out_of_stack_dhcp_client
.then_some(KnownServiceProvider::DhcpClient)
.into_iter(),
),
)
.expect("create netstack realm");
let network = sandbox.create_network(name).await.expect("create network");
let endpoint = network.create_endpoint(name).await.expect("create endpoint");
endpoint.set_link_up(true).await.expect("set link up");
let endpoint_mount_path = netemul::devfs_device_path("ep");
let endpoint_mount_path = endpoint_mount_path.as_path();
realm.add_virtual_device(&endpoint, endpoint_mount_path).await.unwrap_or_else(|e| {
panic!("add virtual device {}: {:?}", endpoint_mount_path.display(), e)
});
let interface_state = realm
.connect_to_protocol::<fnet_interfaces::StateMarker>()
.expect("connect to fuchsia.net.interfaces/State service");
let wait_for_netmgr =
wait_for_component_stopped(&realm, M::MANAGEMENT_AGENT.get_component_name(), None).fuse();
let mut wait_for_netmgr = pin!(wait_for_netmgr);
let (if_id, if_name): (u64, String) = interfaces::wait_for_non_loopback_interface_up(
&interface_state,
&mut wait_for_netmgr,
None,
ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT,
)
.await
.expect("wait for non loopback interface");
after_interface_up(if_id, &network, &interface_state, &realm, &sandbox).await;
realm.shutdown().await.expect("failed to shutdown realm");
if_name
}