policy_testing_common/
lib.rs1use std::pin::pin;
6
7use fidl_fuchsia_net_interfaces as fnet_interfaces;
8use futures::future::{FutureExt as _, LocalBoxFuture};
9use netstack_testing_common::realms::{
10 KnownServiceProvider, Manager, ManagerConfig, Netstack, TestSandboxExt,
11};
12use netstack_testing_common::{
13 interfaces, wait_for_component_stopped, ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT,
14};
15
16pub async fn with_netcfg_owned_device<
22 M: Manager,
23 N: Netstack,
24 F: for<'a> FnOnce(
25 u64,
26 &'a netemul::TestNetwork<'a>,
27 &'a fnet_interfaces::StateProxy,
28 &'a netemul::TestRealm<'a>,
29 &'a netemul::TestSandbox,
30 ) -> LocalBoxFuture<'a, ()>,
31>(
32 name: &str,
33 manager_config: ManagerConfig,
34 use_out_of_stack_dhcp_client: bool,
35 extra_known_service_providers: impl IntoIterator<Item = KnownServiceProvider>,
36 after_interface_up: F,
37) -> String {
38 let sandbox = netemul::TestSandbox::new().expect("create sandbox");
39 let realm = sandbox
40 .create_netstack_realm_with::<N, _, _>(
41 name,
42 [
43 KnownServiceProvider::Manager {
44 agent: M::MANAGEMENT_AGENT,
45 use_dhcp_server: false,
46 use_out_of_stack_dhcp_client,
47 config: manager_config,
48 },
49 KnownServiceProvider::DnsResolver,
50 KnownServiceProvider::FakeClock,
51 ]
52 .into_iter()
53 .chain(extra_known_service_providers)
54 .chain(
57 use_out_of_stack_dhcp_client
58 .then_some(KnownServiceProvider::DhcpClient)
59 .into_iter(),
60 ),
61 )
62 .expect("create netstack realm");
63
64 let network = sandbox.create_network(name).await.expect("create network");
66 let endpoint = network.create_endpoint(name).await.expect("create endpoint");
67 endpoint.set_link_up(true).await.expect("set link up");
68 let endpoint_mount_path = netemul::devfs_device_path("ep");
69 let endpoint_mount_path = endpoint_mount_path.as_path();
70 realm.add_virtual_device(&endpoint, endpoint_mount_path).await.unwrap_or_else(|e| {
71 panic!("add virtual device {}: {:?}", endpoint_mount_path.display(), e)
72 });
73
74 let interface_state = realm
76 .connect_to_protocol::<fnet_interfaces::StateMarker>()
77 .expect("connect to fuchsia.net.interfaces/State service");
78 let wait_for_netmgr =
79 wait_for_component_stopped(&realm, M::MANAGEMENT_AGENT.get_component_name(), None).fuse();
80 let mut wait_for_netmgr = pin!(wait_for_netmgr);
81 let (if_id, if_name): (u64, String) = interfaces::wait_for_non_loopback_interface_up(
82 &interface_state,
83 &mut wait_for_netmgr,
84 None,
85 ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT,
86 )
87 .await
88 .expect("wait for non loopback interface");
89
90 after_interface_up(if_id, &network, &interface_state, &realm, &sandbox).await;
91
92 realm.shutdown().await.expect("failed to shutdown realm");
99
100 if_name
101}