Files
marqdo-cli/crates/marqdo-host/tests/resource_streams.rs
T
cflmy 1be4662049 feat(host): add in-process ResourceProvider v1 (CM-A09 slice)
- Add ResourceProvider v1 trait with name/protocol/kinds/open/read/write/close;
  host still issues the opaque language Resource handle, provider tokens stay
  private.
- Add StdHost::register_resource_provider with protocol/version, reserved-name
  and duplicate-kind checks; add Capabilities.allow_resource_provider grants.
- Provider kinds are project-mode only; pure/strict refuse them; in-process
  providers are trusted and not a sandbox (ADR-0033).
- Reuse resource.* and Result shapes; no parser/Value/runtime/HostOp changes.
- Add mock read/write provider and rejection tests, docs/spec/status/evidence.

Gate: fmt, clippy -D warnings, 423 tests passed.
2026-11-20 22:38:16 +08:00

378 lines
13 KiB
Rust

//! Resource/stream host boundary tests (ADR-0025).
use std::path::Path;
use marqdo_host::{Capabilities, ExecutionContext, Host, HostOp, StdHost};
use marqdo_sandbox::SandboxMode;
use marqdo_value::{ErrorCategory, Value};
fn context() -> ExecutionContext {
ExecutionContext::new("index.mq.md".to_string(), None)
}
fn handle(value: Value) -> marqdo_value::ResourceHandle {
match value {
Value::Resource(handle) => handle,
other => panic!("expected a Resource handle, got {other:?}"),
}
}
fn write_project_file(root: &Path, name: &str, contents: &[u8]) {
std::fs::write(root.join(name), contents).unwrap();
}
#[test]
fn pure_mode_writes_stdout_through_a_resource() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(SandboxMode::Pure, Capabilities::pure(), dir.path());
let ctx = context();
let handle = handle(
host.resource_open("stdio", "stdout", &["write".to_string()], &ctx)
.unwrap(),
);
assert_eq!(host.resource_write(handle, b"hello", &ctx).unwrap(), 5);
assert_eq!(host.take_output(), b"hello");
host.resource_close(handle, &ctx).unwrap();
let error = host.resource_close(handle, &ctx).unwrap_err();
assert_eq!(error.category, ErrorCategory::ResourceClosed);
assert_eq!(error.code(), "resource.closed");
}
#[test]
fn stdout_write_obeys_the_output_budget_atomically() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(SandboxMode::Pure, Capabilities::pure(), dir.path());
host.budget.max_output_bytes = 3;
let ctx = context();
let handle = handle(
host.resource_open("stdio", "stdout", &["write".to_string()], &ctx)
.unwrap(),
);
let error = host.resource_write(handle, b"hello", &ctx).unwrap_err();
assert_eq!(error.category, ErrorCategory::ResourceExhausted);
assert!(host.take_output().is_empty());
}
#[test]
fn strict_mode_allows_only_the_existing_stdio_console_channel() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(SandboxMode::Strict, Capabilities::pure(), dir.path());
let ctx = context();
let handle = handle(
host.resource_open("stdio", "stdout", &["write".to_string()], &ctx)
.unwrap(),
);
assert_eq!(host.resource_write(handle, b"strict", &ctx).unwrap(), 6);
assert_eq!(host.take_output(), b"strict");
let file = host
.resource_open("file", "a.txt", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(file.category, ErrorCategory::PermissionDenied);
let socket = host
.resource_open("socket", "example.test", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(socket.category, ErrorCategory::PermissionDenied);
}
#[test]
fn resource_handles_cannot_cross_the_generic_host_boundary() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(SandboxMode::Pure, Capabilities::pure(), dir.path());
let ctx = context();
let handle = handle(
host.resource_open("stdio", "stdout", &["write".to_string()], &ctx)
.unwrap(),
);
let error = host
.invoke(HostOp::FsRead, vec![Value::Resource(handle)], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::Type);
assert!(error.message.contains("Resource handles"));
}
#[test]
fn pure_mode_refuses_file_resources_even_with_a_grant() {
let dir = tempfile::tempdir().unwrap();
write_project_file(dir.path(), "a.txt", b"abcdef");
let mut host = StdHost::new(
SandboxMode::Pure,
Capabilities::pure().allow_read("a.txt"),
dir.path(),
);
let error = host
.resource_open("file", "a.txt", &["read".to_string()], &context())
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
#[test]
fn project_file_writes_buffer_until_close_and_require_a_grant() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_write("out.bin"),
dir.path(),
);
let ctx = context();
let handle = handle(
host.resource_open("file", "out.bin", &["write".to_string()], &ctx)
.unwrap(),
);
assert_eq!(host.resource_write(handle, b"abc", &ctx).unwrap(), 3);
assert_eq!(host.resource_write(handle, b"def", &ctx).unwrap(), 3);
assert!(
!dir.path().join("out.bin").exists(),
"write must not flush early"
);
host.resource_close(handle, &ctx).unwrap();
assert_eq!(
std::fs::read(dir.path().join("out.bin")).unwrap(),
b"abcdef"
);
let error = host.resource_read(handle, 1, &ctx).unwrap_err();
assert_eq!(error.code(), "resource.closed");
let second = host.resource_close(handle, &ctx).unwrap_err();
assert_eq!(second.code(), "resource.closed");
let mut denied = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
let error = denied
.resource_open("file", "out.bin", &["write".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
#[test]
fn file_write_buffer_has_a_hard_ceiling() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_write("big.bin"),
dir.path(),
);
let ctx = context();
let handle = handle(
host.resource_open("file", "big.bin", &["write".to_string()], &ctx)
.unwrap(),
);
let oversized = vec![0u8; marqdo_host::fs::MAX_READ_BYTES as usize + 1];
let error = host.resource_write(handle, &oversized, &ctx).unwrap_err();
assert_eq!(error.category, ErrorCategory::ResourceExhausted);
assert_eq!(host.resource_write(handle, b"ok", &ctx).unwrap(), 2);
host.resource_close(handle, &ctx).unwrap();
assert_eq!(std::fs::read(dir.path().join("big.bin")).unwrap(), b"ok");
}
#[test]
fn project_file_reads_are_chunked_and_require_a_grant() {
let dir = tempfile::tempdir().unwrap();
write_project_file(dir.path(), "a.txt", b"abcdef");
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_read("a.txt"),
dir.path(),
);
let ctx = context();
let handle = handle(
host.resource_open("file", "a.txt", &["read".to_string()], &ctx)
.unwrap(),
);
let first = host.resource_read(handle, 2, &ctx).unwrap();
assert_eq!(first.bytes, b"ab");
assert!(!first.eof);
let second = host.resource_read(handle, 8, &ctx).unwrap();
assert_eq!(second.bytes, b"cdef");
assert!(second.eof);
assert!(host.resource_read(handle, 1, &ctx).unwrap().eof);
let mut denied = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
let error = denied
.resource_open("file", "a.txt", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
#[test]
fn traversal_and_unsupported_rights_are_refused() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_read("**"),
dir.path(),
);
let ctx = context();
let traversal = host
.resource_open("file", "../secret", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(traversal.category, ErrorCategory::PermissionDenied);
let write_rights = host
.resource_open("stdio", "stdout", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(write_rights.category, ErrorCategory::ResourceUnsupported);
let unknown_kind = host
.resource_open("socket", "example.test", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(unknown_kind.category, ErrorCategory::ResourceUnsupported);
}
struct MockProvider {
data: Vec<u8>,
position: usize,
written: Vec<u8>,
version: u32,
kinds: Vec<&'static str>,
}
impl MockProvider {
fn new(data: &[u8], version: u32, kinds: Vec<&'static str>) -> Self {
Self {
data: data.to_vec(),
position: 0,
written: Vec::new(),
version,
kinds,
}
}
}
impl marqdo_host::ResourceProvider for MockProvider {
fn name(&self) -> &str {
"mock"
}
fn protocol_version(&self) -> u32 {
self.version
}
fn kinds(&self) -> &[&str] {
&self.kinds
}
fn open(
&mut self,
_kind: &str,
_descriptor: &str,
_rights: &[String],
_span: Option<marqdo_span::Span>,
) -> Result<u64, marqdo_value::RuntimeError> {
self.position = 0;
Ok(7)
}
fn read(
&mut self,
token: u64,
max_bytes: usize,
_span: Option<marqdo_span::Span>,
) -> Result<marqdo_host::ResourceRead, marqdo_value::RuntimeError> {
assert_eq!(token, 7);
let start = self.position;
let end = start.saturating_add(max_bytes).min(self.data.len());
self.position = end;
Ok(marqdo_host::ResourceRead {
bytes: self.data[start..end].to_vec(),
eof: end >= self.data.len(),
})
}
fn write(
&mut self,
token: u64,
bytes: &[u8],
_span: Option<marqdo_span::Span>,
) -> Result<usize, marqdo_value::RuntimeError> {
assert_eq!(token, 7);
self.written.extend_from_slice(bytes);
Ok(bytes.len())
}
fn close(
&mut self,
token: u64,
_span: Option<marqdo_span::Span>,
) -> Result<(), marqdo_value::RuntimeError> {
assert_eq!(token, 7);
Ok(())
}
}
#[test]
fn registered_provider_streams_work_with_a_grant_in_project_mode() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_resource_provider("mock"),
dir.path(),
);
host.register_resource_provider(Box::new(MockProvider::new(
b"abcdef",
1,
vec!["mock.sensor"],
)))
.unwrap();
let ctx = context();
let handle = handle(
host.resource_open("mock.sensor", "sensor-1", &["read".to_string()], &ctx)
.unwrap(),
);
let first = host.resource_read(handle, 2, &ctx).unwrap();
assert_eq!(first.bytes, b"ab");
assert!(!first.eof);
let rest = host.resource_read(handle, 16, &ctx).unwrap();
assert_eq!(rest.bytes, b"cdef");
assert!(rest.eof);
assert_eq!(host.resource_write(handle, b"x", &ctx).unwrap(), 1);
host.resource_close(handle, &ctx).unwrap();
assert_eq!(
host.resource_close(handle, &ctx).unwrap_err().code(),
"resource.closed"
);
}
#[test]
fn provider_requires_a_name_grant_and_project_mode() {
let dir = tempfile::tempdir().unwrap();
let ctx = context();
let mut denied = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
denied
.register_resource_provider(Box::new(MockProvider::new(b"x", 1, vec!["mock.sensor"])))
.unwrap();
let error = denied
.resource_open("mock.sensor", "s", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
for mode in [SandboxMode::Pure, SandboxMode::Strict] {
let mut host = StdHost::new(
mode,
Capabilities::pure().allow_resource_provider("mock"),
dir.path(),
);
host.register_resource_provider(Box::new(MockProvider::new(b"x", 1, vec!["mock.sensor"])))
.unwrap();
let error = host
.resource_open("mock.sensor", "s", &["read".to_string()], &ctx)
.unwrap_err();
assert_eq!(error.category, ErrorCategory::PermissionDenied);
}
}
#[test]
fn provider_registration_rejects_versions_reserved_names_and_conflicts() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(
SandboxMode::Project,
Capabilities::project().allow_resource_provider("mock"),
dir.path(),
);
let bad_version = host
.register_resource_provider(Box::new(MockProvider::new(b"x", 2, vec!["mock.sensor"])))
.unwrap_err();
assert_eq!(bad_version.category, ErrorCategory::ResourceUnsupported);
let reserved = host
.register_resource_provider(Box::new(MockProvider::new(b"x", 1, vec!["stdio"])))
.unwrap_err();
assert_eq!(reserved.category, ErrorCategory::ResourceUnsupported);
host.register_resource_provider(Box::new(MockProvider::new(b"x", 1, vec!["mock.sensor"])))
.unwrap();
let conflict = host
.register_resource_provider(Box::new(MockProvider::new(b"y", 1, vec!["mock.sensor"])))
.unwrap_err();
assert_eq!(conflict.category, ErrorCategory::ResourceUnsupported);
}