use anyhow::Error;
use fidl_fuchsia_kernel as fkernel;
use fuchsia_runtime::job_default;
use futures::TryStreamExt;
pub struct RootJob;
impl RootJob {
pub async fn serve(
mut stream: fkernel::RootJobRequestStream,
rights: zx::Rights,
) -> Result<(), Error> {
let job = job_default();
while let Some(fkernel::RootJobRequest::Get { responder }) = stream.try_next().await? {
responder.send(job.duplicate(rights)?)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use fuchsia_async as fasync;
use futures::TryFutureExt;
use zx::AsHandleRef;
#[fuchsia::test]
async fn has_correct_rights() -> Result<(), Error> {
let (proxy, stream) = fidl::endpoints::create_proxy_and_stream::<fkernel::RootJobMarker>();
fasync::Task::local(
RootJob::serve(stream, zx::Rights::TRANSFER)
.unwrap_or_else(|err| panic!("Error serving root job: {}", err)),
)
.detach();
let root_job = proxy.get().await?;
let info = zx::Handle::from(root_job).basic_info()?;
assert_eq!(info.rights, zx::Rights::TRANSFER);
Ok(())
}
}