1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#![deny(missing_docs)]

//! Provides utilities for Netstack integration tests.

pub mod constants;
pub mod devices;
pub mod dhcpv4;
pub mod interfaces;
pub mod ndp;
pub mod nud;
pub mod packets;
pub mod ping;
#[macro_use]
pub mod realms;

use anyhow::Context as _;
use component_events::events::EventStream;
use diagnostics_hierarchy::{filter_hierarchy, DiagnosticsHierarchy, HierarchyMatcher};
use fidl::endpoints::DiscoverableProtocolMarker;
use fidl_fuchsia_diagnostics::Selector;
use fidl_fuchsia_inspect_deprecated::InspectMarker;
use fuchsia_async::{self as fasync, DurationExt as _};
use fuchsia_component::client;
use futures::future::FutureExt as _;
use futures::stream::{Stream, StreamExt as _, TryStreamExt as _};
use futures::{select, Future};
use std::pin::pin;
use {fidl_fuchsia_io as fio, fidl_fuchsia_netemul as fnetemul};

use crate::realms::TestSandboxExt as _;

/// An alias for `Result<T, anyhow::Error>`.
pub type Result<T = ()> = std::result::Result<T, anyhow::Error>;

/// Extra time to use when waiting for an async event to occur.
///
/// A large timeout to help prevent flakes.
pub const ASYNC_EVENT_POSITIVE_CHECK_TIMEOUT: zx::MonotonicDuration =
    zx::MonotonicDuration::from_seconds(120);

/// Extra time to use when waiting for an async event to not occur.
///
/// Since a negative check is used to make sure an event did not happen, its okay to use a
/// smaller timeout compared to the positive case since execution stall in regards to the
/// monotonic clock will not affect the expected outcome.
pub const ASYNC_EVENT_NEGATIVE_CHECK_TIMEOUT: zx::MonotonicDuration =
    zx::MonotonicDuration::from_seconds(5);

/// The time to wait between two consecutive checks of an event.
pub const ASYNC_EVENT_CHECK_INTERVAL: zx::MonotonicDuration =
    zx::MonotonicDuration::from_seconds(1);

/// Returns `true` once the stream yields a `true`.
///
/// If the stream never yields `true` or never terminates, `try_any` may never resolve.
pub async fn try_any<S: Stream<Item = Result<bool>>>(stream: S) -> Result<bool> {
    let stream = pin!(stream);
    stream.try_filter(|v| futures::future::ready(*v)).next().await.unwrap_or(Ok(false))
}

/// Returns `true` if the stream only yields `true`.
///
/// If the stream never yields `false` or never terminates, `try_all` may never resolve.
pub async fn try_all<S: Stream<Item = Result<bool>>>(stream: S) -> Result<bool> {
    let stream = pin!(stream);
    stream.try_filter(|v| futures::future::ready(!*v)).next().await.unwrap_or(Ok(true))
}

/// Asynchronously sleeps for specified `secs` seconds.
pub async fn sleep(secs: i64) {
    fasync::Timer::new(zx::MonotonicDuration::from_seconds(secs).after_now()).await;
}

/// Gets a component event stream yielding component stopped events.
pub async fn get_component_stopped_event_stream() -> Result<component_events::events::EventStream> {
    EventStream::open_at_path("/events/stopped")
        .await
        .context("failed to subscribe to `Stopped` events")
}

/// Waits for a `stopped` event to be emitted for a component in a test realm.
///
/// Optionally specifies a matcher for the expected exit status of the `stopped`
/// event.
pub async fn wait_for_component_stopped_with_stream(
    event_stream: &mut component_events::events::EventStream,
    realm: &netemul::TestRealm<'_>,
    component_moniker: &str,
    status_matcher: Option<component_events::matcher::ExitStatusMatcher>,
) -> Result<component_events::events::Stopped> {
    let matcher = get_child_component_event_matcher(realm, component_moniker)
        .await
        .context("get child component matcher")?;
    matcher.stop(status_matcher).wait::<component_events::events::Stopped>(event_stream).await
}

/// Like [`wait_for_component_stopped_with_stream`] but retrieves an event
/// stream for the caller.
///
/// Note that this function fails to observe stop events that happen in early
/// realm creation, which is especially true for eager components.
pub async fn wait_for_component_stopped(
    realm: &netemul::TestRealm<'_>,
    component_moniker: &str,
    status_matcher: Option<component_events::matcher::ExitStatusMatcher>,
) -> Result<component_events::events::Stopped> {
    let mut stream = get_component_stopped_event_stream().await?;
    wait_for_component_stopped_with_stream(&mut stream, realm, component_moniker, status_matcher)
        .await
}

/// Gets an event matcher for `component_moniker` in `realm`.
pub async fn get_child_component_event_matcher(
    realm: &netemul::TestRealm<'_>,
    component_moniker: &str,
) -> Result<component_events::matcher::EventMatcher> {
    let realm_moniker = &realm.get_moniker().await.context("calling get moniker")?;
    let moniker_for_match =
        format!("./{}/{}/{}", NETEMUL_SANDBOX_MONIKER, realm_moniker, component_moniker);
    Ok(component_events::matcher::EventMatcher::ok().moniker(moniker_for_match))
}

/// The name of the netemul sandbox component, which is the parent component of
/// managed test realms.
const NETEMUL_SANDBOX_MONIKER: &str = "sandbox";

/// Gets the moniker of a component in a test realm, relative to the root of the
/// dynamic collection in which it is running.
pub async fn get_component_moniker<'a>(
    realm: &netemul::TestRealm<'a>,
    component: &str,
) -> Result<String> {
    let realm_moniker = realm.get_moniker().await.context("calling get moniker")?;
    Ok([NETEMUL_SANDBOX_MONIKER, &realm_moniker, component].join("/"))
}

/// Gets inspect data in realm.
///
/// Returns the resulting inspect data for `component`, filtered by
/// `tree_selector` and with inspect file starting with `file_prefix`.
pub async fn get_inspect_data(
    realm: &netemul::TestRealm<'_>,
    component_moniker: impl Into<String>,
    tree_selector: impl Into<String>,
    file_prefix: &str,
) -> Result<diagnostics_hierarchy::DiagnosticsHierarchy> {
    let moniker = realm.get_moniker().await.context("calling get moniker")?;
    let realm_moniker = selectors::sanitize_string_for_selectors(&moniker);
    let mut archive_reader = diagnostics_reader::ArchiveReader::new();
    let _archive_reader_ref = archive_reader.add_selector(
        diagnostics_reader::ComponentSelector::new(vec![
            NETEMUL_SANDBOX_MONIKER.into(),
            realm_moniker.into_owned(),
            component_moniker.into(),
        ])
        .with_tree_selector(tree_selector.into()),
    );

    // Loop to wait for the component to begin publishing inspect data after it
    // starts.
    loop {
        let mut data = archive_reader
            .snapshot::<diagnostics_reader::Inspect>()
            .await
            .context("snapshot did not return any inspect data")?
            .into_iter()
            .filter_map(|inspect_data| {
                if inspect_data.name().starts_with(file_prefix) {
                    Some(inspect_data.payload.ok_or_else(|| {
                        anyhow::anyhow!(
                            "empty inspect payload, metadata errors: {:?}",
                            inspect_data.metadata.errors
                        )
                    }))
                } else {
                    None
                }
            });
        match data.next() {
            Some(datum) => {
                let data: Vec<_> = data.collect();
                assert!(
                    data.is_empty(),
                    "expected a single inspect entry; got {:?} and also {:?}",
                    datum,
                    data
                );
                return datum;
            }
            None => {
                fasync::Timer::new(zx::MonotonicDuration::from_millis(100).after_now()).await;
            }
        }
    }
}

/// Like [`get_inspect_data`] but returns a single property matched by
/// `property_selector`.
pub async fn get_inspect_property(
    realm: &netemul::TestRealm<'_>,
    component_moniker: impl Into<String>,
    property_selector: impl Into<String>,
    file_prefix: &str,
) -> Result<diagnostics_hierarchy::Property> {
    let property_selector = property_selector.into();
    let hierarchy =
        get_inspect_data(&realm, component_moniker, property_selector.clone(), file_prefix)
            .await
            .context("getting hierarchy")?;
    let property_selector = property_selector.split(&['/', ':']).skip(1).collect::<Vec<_>>();
    let property = hierarchy
        .get_property_by_path(&property_selector)
        .ok_or_else(|| anyhow::anyhow!("property not found in hierarchy: {hierarchy:?}"))?;
    Ok(property.clone())
}

/// Read an Inspect hierarchy and filter it down to properties of interest from the diagnostics
/// directory of Netstack2. For any other component, please use `get_inspect_data`, this function
/// doesn't apply to any other component and won't work.
// TODO(https://fxbug.dev/324494668): remove when Netstack2 is gone.
pub async fn get_deprecated_netstack2_inspect_data(
    diagnostics_dir: &fio::DirectoryProxy,
    subdir: &str,
    selectors: impl IntoIterator<Item = Selector>,
) -> DiagnosticsHierarchy {
    let matcher = HierarchyMatcher::new(selectors.into_iter()).expect("invalid selectors");
    loop {
        // NOTE: For current test purposes we just need to read from the deprecated inspect
        // protocol. If this changes in the future, then we'll need to update this code to be able
        // to read from other kind-of files such as fuchsia.inspect.Tree or a *.inspect VMO file.
        let proxy = client::connect_to_named_protocol_at_dir_root::<InspectMarker>(
            diagnostics_dir,
            &format!("{subdir}/{}", InspectMarker::PROTOCOL_NAME),
        )
        .unwrap();
        match inspect_fidl_load::load_hierarchy(proxy).await {
            Ok(hierarchy) => return filter_hierarchy(hierarchy, &matcher).unwrap(),
            Err(err) => {
                println!("Failed to load hierarchy, retrying. Error: {err:?}")
            }
        }
        fasync::Timer::new(fasync::MonotonicDuration::from_millis(100)).await;
    }
}

/// Sets up a realm with a network with no required services.
pub async fn setup_network<'a, N: realms::Netstack>(
    sandbox: &'a netemul::TestSandbox,
    name: &'a str,
    metric: Option<u32>,
) -> Result<(
    netemul::TestNetwork<'a>,
    netemul::TestRealm<'a>,
    netemul::TestInterface<'a>,
    netemul::TestFakeEndpoint<'a>,
)> {
    setup_network_with::<N, _>(sandbox, name, metric, std::iter::empty::<fnetemul::ChildDef>())
        .await
}

/// Sets up a realm with required services and a network used for tests
/// requiring manual packet inspection and transmission.
///
/// Returns the network, realm, netstack client, interface (added to the
/// netstack and up) and a fake endpoint used to read and write raw ethernet
/// packets.
pub async fn setup_network_with<'a, N: realms::Netstack, I>(
    sandbox: &'a netemul::TestSandbox,
    name: &'a str,
    metric: Option<u32>,
    children: I,
) -> Result<(
    netemul::TestNetwork<'a>,
    netemul::TestRealm<'a>,
    netemul::TestInterface<'a>,
    netemul::TestFakeEndpoint<'a>,
)>
where
    I: IntoIterator,
    I::Item: Into<fnetemul::ChildDef>,
{
    let network = sandbox.create_network(name).await.context("failed to create network")?;
    let realm = sandbox
        .create_netstack_realm_with::<N, _, _>(name, children)
        .context("failed to create netstack realm")?;
    // It is important that we create the fake endpoint before we join the
    // network so no frames transmitted by Netstack are lost.
    let fake_ep = network.create_fake_endpoint()?;

    let iface = realm
        .join_network_with_if_config(
            &network,
            name,
            netemul::InterfaceConfig { name: Some(name.into()), metric, ..Default::default() },
        )
        .await
        .context("failed to configure networking")?;

    Ok((network, realm, iface, fake_ep))
}

/// Pauses the fake clock in the given realm.
pub async fn pause_fake_clock(realm: &netemul::TestRealm<'_>) -> Result<()> {
    let fake_clock_control = realm
        .connect_to_protocol::<fidl_fuchsia_testing::FakeClockControlMarker>()
        .context("failed to connect to FakeClockControl")?;
    let () = fake_clock_control.pause().await.context("failed to pause time")?;
    Ok(())
}

/// Wraps `fut` so that it prints `event_name` and the caller's location to
/// stderr every `interval` until `fut` completes.
#[track_caller]
pub fn annotate<'a, 'b: 'a, T>(
    fut: impl Future<Output = T> + 'a,
    interval: std::time::Duration,
    event_name: &'b str,
) -> impl Future<Output = T> + 'a {
    let caller = std::panic::Location::caller();

    async move {
        let mut fut = pin!(fut.fuse());
        let event_name = event_name.to_string();
        let mut print_fut = pin!(futures::stream::repeat(())
            .for_each(|()| async {
                fasync::Timer::new(interval).await;
                eprintln!("waiting for {} at {}", event_name, caller);
            })
            .fuse());
        let result = select! {
            result = fut => result,
            () = print_fut => unreachable!("should repeat printing forever"),
        };
        eprintln!("completed {} at {}", event_name, caller);
        result
    }
}