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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// 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.

//! library for target side of filesystem integrity host-target interaction tests

#![deny(missing_docs)]

use {
    anyhow::{anyhow, Context as _, Result},
    async_trait::async_trait,
    device_watcher::{recursive_wait, recursive_wait_and_open},
    fidl_fuchsia_blackout_test::{ControllerRequest, ControllerRequestStream},
    fidl_fuchsia_device::{ControllerMarker, ControllerProxy},
    fidl_fuchsia_hardware_block::BlockMarker,
    fidl_fuchsia_io as fio, fuchsia_async as fasync,
    fuchsia_component::client::{
        connect_to_named_protocol_at_dir_root, connect_to_protocol_at_path,
    },
    fuchsia_component::server::{ServiceFs, ServiceObj},
    fuchsia_fs::directory::readdir,
    fuchsia_zircon as zx,
    futures::{future, FutureExt, StreamExt, TryFutureExt, TryStreamExt},
    rand::{distributions, rngs::StdRng, Rng, SeedableRng},
    std::sync::Arc,
    storage_isolated_driver_manager::fvm,
    uuid::Uuid,
};

pub mod static_tree;

/// The three steps the target-side of a blackout test needs to implement.
#[async_trait]
pub trait Test {
    /// Setup the test run on the given block_device.
    async fn setup(
        self: Arc<Self>,
        device_label: String,
        device_path: Option<String>,
        seed: u64,
    ) -> Result<()>;
    /// Run the test body on the given device_path.
    async fn test(
        self: Arc<Self>,
        device_label: String,
        device_path: Option<String>,
        seed: u64,
    ) -> Result<()>;
    /// Verify the consistency of the filesystem on the device_path.
    async fn verify(
        self: Arc<Self>,
        device_label: String,
        device_path: Option<String>,
        seed: u64,
    ) -> Result<()>;
}

struct BlackoutController(ControllerRequestStream);

/// A test server, which serves the fuchsia.blackout.test.Controller protocol.
pub struct TestServer<'a, T> {
    fs: ServiceFs<ServiceObj<'a, BlackoutController>>,
    test: Arc<T>,
}

impl<'a, T> TestServer<'a, T>
where
    T: Test + Copy + 'static,
{
    /// Create a new test server for this test.
    pub fn new(test: T) -> Result<TestServer<'a, T>> {
        let mut fs = ServiceFs::new();
        fs.dir("svc").add_fidl_service(BlackoutController);
        fs.take_and_serve_directory_handle()?;

        Ok(TestServer { fs, test: Arc::new(test) })
    }

    /// Start serving the outgoing directory. Blocks until all connections are closed.
    pub async fn serve(self) {
        const MAX_CONCURRENT: usize = 10_000;
        let test = self.test;
        self.fs
            .for_each_concurrent(MAX_CONCURRENT, move |stream| {
                handle_request(test.clone(), stream).unwrap_or_else(|e| tracing::error!("{}", e))
            })
            .await;
    }
}

async fn handle_request<T: Test + 'static>(
    test: Arc<T>,
    BlackoutController(mut stream): BlackoutController,
) -> Result<()> {
    while let Some(request) = stream.try_next().await? {
        handle_controller(test.clone(), request).await?;
    }

    Ok(())
}

async fn handle_controller<T: Test + 'static>(
    test: Arc<T>,
    request: ControllerRequest,
) -> Result<()> {
    match request {
        ControllerRequest::Setup { responder, device_label, device_path, seed } => {
            let res = test.setup(device_label, device_path, seed).await.map_err(|e| {
                tracing::error!("{}", e);
                zx::Status::INTERNAL.into_raw()
            });
            responder.send(res)?;
        }
        ControllerRequest::Test { responder, device_label, device_path, seed, duration } => {
            let test_fut = test.test(device_label, device_path, seed).map_err(|e| {
                tracing::error!("{}", e);
                zx::Status::INTERNAL.into_raw()
            });
            if duration != 0 {
                // If a non-zero duration is provided, spawn the test and then return after that
                // duration.
                tracing::info!("starting test and replying in {} seconds...", duration);
                let timer = fasync::Timer::new(std::time::Duration::from_secs(duration));
                let res = match future::select(test_fut, timer).await {
                    future::Either::Left((res, _)) => res,
                    future::Either::Right((_, test_fut)) => {
                        fasync::Task::spawn(test_fut.map(|_| ())).detach();
                        Ok(())
                    }
                };
                responder.send(res)?;
            } else {
                // If a zero duration is provided, return once the test step is complete.
                tracing::info!("starting test...");
                responder.send(test_fut.await)?;
            }
        }
        ControllerRequest::Verify { responder, device_label, device_path, seed } => {
            let res = test.verify(device_label, device_path, seed).await.map_err(|e| {
                // The test tries failing on purpose, so only print errors as warnings.
                tracing::warn!("{}", e);
                zx::Status::BAD_STATE.into_raw()
            });
            responder.send(res)?;
        }
    }

    Ok(())
}

/// Generate a Vec<u8> of random bytes from a seed using a standard distribution.
pub fn generate_content(seed: u64) -> Vec<u8> {
    let mut rng = StdRng::seed_from_u64(seed);

    let size = rng.gen_range(1..1 << 16);
    rng.sample_iter(&distributions::Standard).take(size).collect()
}

/// Find the device in /dev/class/block that represents a given topological path. Returns the full
/// path of the device in /dev/class/block.
pub async fn find_dev(dev: &str) -> Result<String> {
    let dev_class_block = fuchsia_fs::directory::open_in_namespace(
        "/dev/class/block",
        fuchsia_fs::OpenFlags::RIGHT_READABLE,
    )?;
    for entry in readdir(&dev_class_block).await? {
        let path = format!("/dev/class/block/{}", entry.name);
        let proxy = connect_to_protocol_at_path::<ControllerMarker>(&path)?;
        let topo_path = proxy.get_topological_path().await?.map_err(|s| zx::Status::from_raw(s))?;
        tracing::info!("{} => {}", path, topo_path);
        if dev == topo_path {
            return Ok(path);
        }
    }
    Err(anyhow::anyhow!("Couldn't find {} in /dev/class/block", dev))
}

/// Returns a directory proxy connected to /dev.
pub fn dev() -> fio::DirectoryProxy {
    fuchsia_fs::directory::open_in_namespace("/dev", fuchsia_fs::OpenFlags::RIGHT_READABLE)
        .expect("failed to open /dev")
}

fn dev_class_block() -> fio::DirectoryProxy {
    fuchsia_fs::directory::open_in_namespace(
        "/dev/class/block",
        fuchsia_fs::OpenFlags::RIGHT_READABLE,
    )
    .expect("failed to open /dev/class/block")
}

const RAMDISK_PREFIX: &'static str = "/dev/sys/platform/00:00:2d/ramctl";

/// During the setup step, formats a device with fvm, creating a single partition named
/// [`partition_label`]. If [`device_path`] is `None`, finds a device which already had fvm, erase
/// it, and then set it up in this way. Returns the path to the device with the created partition,
/// only once the device is enumerated, so it can be used immediately.
pub async fn set_up_partition(
    partition_label: &str,
    device_dir: Option<&fio::DirectoryProxy>,
    skip_ramdisk: bool,
) -> Result<ControllerProxy> {
    let mut device_controller = None;
    let mut owned_device_dir = None;
    let device_dir = match device_dir {
        Some(device_dir) => {
            device_controller = Some(
                connect_to_named_protocol_at_dir_root::<ControllerMarker>(
                    device_dir,
                    "device_controller",
                )
                .context("new class path connect failed")?,
            );
            device_dir
        }
        None => {
            let dev_class_block_dir = dev_class_block();
            for entry in readdir(&dev_class_block_dir).await.context("readdir failed")? {
                let entry_controller = connect_to_named_protocol_at_dir_root::<ControllerMarker>(
                    &dev_class_block_dir,
                    &format!("{}/device_controller", entry.name),
                )
                .context("get_topo controller connect failed")?;
                let topo_path = entry_controller
                    .get_topological_path()
                    .await
                    .context("transport error on get_topological_path")?
                    .map_err(zx::Status::from_raw)
                    .context("get_topo failed")?;
                if skip_ramdisk && topo_path.starts_with(RAMDISK_PREFIX) {
                    continue;
                }
                if let Some(fvm_index) = topo_path.find("/block/fvm") {
                    let fvm_path = format!("{}/block", &topo_path[..fvm_index]);
                    let controller_path = format!("{fvm_path}/device_controller");
                    let fvm_controller =
                        connect_to_protocol_at_path::<ControllerMarker>(&controller_path)
                            .context("new class path connect failed")?;
                    fvm_controller
                        .unbind_children()
                        .await
                        .context("unbind children call failed")?
                        .map_err(zx::Status::from_raw)
                        .context("unbind children returned error")?;
                    device_controller = Some(fvm_controller);
                    owned_device_dir = Some(fuchsia_fs::directory::open_in_namespace(
                        &fvm_path,
                        fuchsia_fs::OpenFlags::empty(),
                    )?);
                    break;
                }
            }
            owned_device_dir
                .as_ref()
                .ok_or_else(|| anyhow!("failed to find a device with fvm on it"))?
        }
    };

    let fvm_slice_size = 8192_usize * 4;

    // Get the size of the underlying device so we can use a bunch of it for our fancy new fvm
    // partition without dealing with expansion. We do this because some tests use the size of the
    // device to figure out what they can do, but getting the total used bytes from a filesystem
    // doesn't take into account possible expansion.
    let device_size_bytes = {
        let fvm_block = fuchsia_component::client::connect_to_named_protocol_at_dir_root::<
            BlockMarker,
        >(device_dir, ".")?;
        let info = fvm_block
            .get_info()
            .await
            .context("fvm path get info call failed")?
            .map_err(zx::Status::from_raw)
            .context("fvm path get info returned error")?;
        u64::from(info.block_size) * info.block_count
    };

    // It's tricky to really allocate the maximum number of slices, so we use a whole lot less than
    // that. It should still be enough to count.
    let num_slices = device_size_bytes / fvm_slice_size as u64 / 2;
    let fvm_volume_size = num_slices * fvm_slice_size as u64;

    let device_controller =
        device_controller.ok_or_else(|| anyhow!("invalid device controller"))?;
    let volume_manager = fvm::set_up_fvm(&device_controller, device_dir, fvm_slice_size)
        .await
        .context("set_up_fvm failed")?;
    fvm::create_fvm_volume(
        &volume_manager,
        partition_label,
        Uuid::new_v4().as_bytes(),
        Uuid::new_v4().as_bytes(),
        Some(fvm_volume_size),
        0,
    )
    .await
    .context("create_fvm_volume failed")?;
    recursive_wait_and_open::<ControllerMarker>(
        device_dir,
        &format!("/fvm/{}-p-1/block/device_controller", partition_label),
    )
    .await
    .context("recursive_wait for new fvm path failed")
}

/// During the test or verify steps, finds a block device which represents an fvm partition named
/// [`partition_label`]. If [`device_path`] is provided, this assumes the partition is on that
/// device. Returns the topological path to the device, only returning once the device is
/// enumerated, so it can be used immediately.
pub async fn find_partition(
    partition_label: &str,
    device_dir: Option<&fio::DirectoryProxy>,
) -> Result<ControllerProxy> {
    if let Some(device_dir) = device_dir {
        match fuchsia_fs::directory::open_no_describe::<ControllerMarker>(
            device_dir,
            &format!("/fvm/{}-p-1/block/device_controller", partition_label),
            fuchsia_fs::OpenFlags::empty(),
        ) {
            Ok(partition_controller) => {
                return Ok(partition_controller);
            }
            Err(fuchsia_fs::node::OpenError::OpenError(zx::Status::NOT_FOUND)) => {
                // If we failed to open that path, it might be because the fvm driver isn't bound yet.
                let device_controller =
                    fuchsia_component::client::connect_to_named_protocol_at_dir_root::<
                        ControllerMarker,
                    >(device_dir, "device_controller")?;
                fvm::bind_fvm_driver(&device_controller).await?;
                recursive_wait(
                    device_dir,
                    &format!("/fvm/{}-p-1/block/device_controller", partition_label),
                )
                .await
                .context("recursive_wait on expected fvm path failed")?;
            }
            Err(err) => return Err(err).context("failed to open fvm path"),
        }
    }

    let dev_class_block_dir = dev_class_block();
    for entry in readdir(&dev_class_block_dir).await? {
        let class_path = format!("/dev/class/block/{}", entry.name);
        let partition = connect_to_protocol_at_path::<
            fidl_fuchsia_hardware_block_partition::PartitionMarker,
        >(&class_path)
        .context("class path partition connect failed")?;
        // The device might not support the partition protocol, in which case we skip it. Also skip
        // it if an error is returned, or if no name is returned.
        let entry_name = if let Ok((0, Some(entry_name))) = partition.get_name().await {
            entry_name
        } else {
            continue;
        };
        if &entry_name == partition_label {
            let controller_proxy = connect_to_named_protocol_at_dir_root::<ControllerMarker>(
                &dev_class_block_dir,
                &format!("{}/device_controller", &entry.name),
            )?;
            return Ok(controller_proxy);
        }
    }

    return Err(anyhow::anyhow!("couldn't find device with name \"{}\"", partition_label));
}