Files
Robin Krahl ac106d63ab Simplify virtual store
This patch uses the simplified Store trait to also simplify the store
implementation used by the virt module:  Instead of using references to
static storages and filesystems protected by a mutex, we can now just
use separate non-static instances for every test.

The only downside is that we can no longer provide raw access to the
IFS via the StoreProvider::ifs function, so we cannot run the
provisioner app with this platform implementation.  But I think this is
an acceptable tradeoff.
2025-03-05 13:05:00 +01:00

72 lines
1.9 KiB
Rust

use littlefs2_core::path;
use trussed::{
api::{reply::ReadFile, Reply, Request},
backend::{self, BackendId},
client::FilesystemClient as _,
error::Error,
platform,
service::ServiceResources,
types::{CoreContext, Location, Message, PathBuf},
virt::{self, StoreConfig},
};
type Client<'a> = virt::Client<'a, Dispatch>;
const BACKENDS_TEST: &[BackendId<Backend>] = &[BackendId::Custom(Backend::Test), BackendId::Core];
pub enum Backend {
Test,
}
#[derive(Default, trussed_derive::Dispatch)]
#[dispatch(backend_id = "Backend")]
struct Dispatch {
test: TestBackend,
}
#[derive(Default)]
struct TestBackend;
impl backend::Backend for TestBackend {
type Context = ();
fn request<P: platform::Platform>(
&mut self,
_core_ctx: &mut CoreContext,
_backend_ctx: &mut Self::Context,
request: &Request,
_resources: &mut ServiceResources<P>,
) -> Result<Reply, Error> {
match request {
Request::ReadFile(_) => {
let mut data = Message::new();
data.push(0xff).unwrap();
Ok(Reply::ReadFile(ReadFile { data }))
}
_ => Err(Error::RequestNotAvailable),
}
}
}
fn run<F: FnOnce(&mut Client<'_>)>(backends: &'static [BackendId<Backend>], f: F) {
virt::with_platform(StoreConfig::ram(), |platform| {
platform.run_client_with_backends("test", Dispatch::default(), backends, |mut client| {
f(&mut client)
})
})
}
#[test]
fn override_syscall() {
let path = PathBuf::from(path!("test"));
run(&[], |client| {
assert!(trussed::try_syscall!(client.read_file(Location::Internal, path.clone())).is_err());
});
run(BACKENDS_TEST, |client| {
assert_eq!(
trussed::syscall!(client.read_file(Location::Internal, path.clone())).data,
&[0xff]
);
})
}