builtins/
root_job.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use anyhow::Error;
6use fidl_fuchsia_kernel as fkernel;
7use fuchsia_runtime::job_default;
8use futures::TryStreamExt;
9
10/// An implementation of the `fuchsia.kernel.RootJob` protocol.
11pub struct RootJob;
12
13impl RootJob {
14    pub async fn serve(
15        mut stream: fkernel::RootJobRequestStream,
16        rights: zx::Rights,
17    ) -> Result<(), Error> {
18        let job = job_default();
19        while let Some(fkernel::RootJobRequest::Get { responder }) = stream.try_next().await? {
20            responder.send(job.duplicate(rights)?)?;
21        }
22        Ok(())
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29    use fuchsia_async as fasync;
30    use futures::TryFutureExt;
31    use zx::AsHandleRef;
32
33    #[fuchsia::test]
34    async fn has_correct_rights() -> Result<(), Error> {
35        let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<fkernel::RootJobMarker>();
36        fasync::Task::local(
37            RootJob::serve(stream, zx::Rights::TRANSFER)
38                .unwrap_or_else(|err| panic!("Error serving root job: {}", err)),
39        )
40        .detach();
41
42        let root_job = proxy.get().await?;
43        let info = zx::Handle::from(root_job).basic_info()?;
44        assert_eq!(info.rights, zx::Rights::TRANSFER);
45        Ok(())
46    }
47}