//! Plugin manifest parsing (`SMH-SPEC-WASM0001`, identity and installation). use smith_harness::plugins::{Capability, PluginManifest, PluginPackage}; use smith_harness::trust::TrustStore; use smith_harness::wasm::{PluginEngine, PluginGeneration}; #[test] fn manifest_declares_identity_artifact_world_and_capabilities() { let manifest = PluginManifest::parse( r#" id "example:echo" version "1.2.3" world "0.1.0" artifact "echo.wasm" capability "net-scope" capability "log" source "https://example.test/echo" "#, ) .unwrap(); assert_eq!(manifest.id(), "example:echo"); assert_eq!(manifest.version(), "1.2.3"); assert_eq!(manifest.world_version(), "0.1.0"); assert_eq!(manifest.artifact(), "echo.wasm"); assert_eq!( manifest.capabilities(), &[Capability::NetScope, Capability::Log] ); assert_eq!(manifest.source(), Some("https://example.test/echo")); } #[test] fn manifest_accepts_exact_byte_limit_and_rejects_one_byte_over() { let prefix = r#"id "example:echo" version "1.2.3" world "0.1.0" artifact "echo.wasm" "#; let at_limit = format!( "{prefix}{}", " ".repeat(smith_harness::plugins::MANIFEST_BYTE_LIMIT - prefix.len()) ); assert_eq!(at_limit.len(), smith_harness::plugins::MANIFEST_BYTE_LIMIT); assert!(PluginManifest::parse(&at_limit).is_ok()); let above_limit = format!("{at_limit} "); let error = PluginManifest::parse(&above_limit).unwrap_err(); assert!(error.to_string().contains("manifest exceeds byte limit")); } #[test] fn manifest_rejects_unsupported_node_syntax_and_empty_required_fields() { for input in [ "id \"example:echo\" extra\nversion \"1\"\nworld \"0\"\nartifact \"a.wasm\"", "id \"example:echo\" named=\"value\"\nversion \"1\"\nworld \"0\"\nartifact \"a.wasm\"", "id \"example:echo\" { child }\nversion \"1\"\nworld \"0\"\nartifact \"a.wasm\"", "id \"\"\nversion \"1\"\nworld \"0\"\nartifact \"a.wasm\"", "id \"example:echo\"\nversion \"\"\nworld \"0\"\nartifact \"a.wasm\"", "id \"example:echo\"\nversion \"1\"\nworld \"\"\nartifact \"a.wasm\"", "id \"example:echo\"\nversion \"1\"\nworld \"0\"\nartifact \"\"", ] { assert!(PluginManifest::parse(input).is_err(), "accepted {input:?}"); } } #[test] fn manifest_rejects_duplicate_capabilities() { let error = PluginManifest::parse( "id \"example:echo\"\nversion \"1\"\nworld \"0\"\nartifact \"a.wasm\"\ncapability \"log\"\ncapability \"log\"", ) .unwrap_err(); assert!(error.to_string().contains("duplicate capability log")); } #[test] fn persisted_trust_uses_canonical_path_and_exact_grants_before_discovery() { let scratch = std::env::temp_dir().join(format!("smith-plugin-trust-{}", std::process::id())); let package = scratch.join("package"); let alias = scratch.join("alias"); let _ = std::fs::remove_dir_all(&scratch); std::fs::create_dir_all(&package).unwrap(); std::os::unix::fs::symlink(&package, &alias).unwrap(); let store = TrustStore::at(scratch.join("decisions")); assert!(store.decision(&alias).unwrap().is_none()); store.grant(&package, &[Capability::Log]).unwrap(); assert_eq!(store.decision(&alias).unwrap(), Some(vec![Capability::Log])); let _ = std::fs::remove_dir_all(&scratch); } #[test] fn untrusted_project_package_is_not_enumerated() { let missing = std::path::Path::new("definitely-not-a-plugin-package"); let error = PluginPackage::discover(missing, false).unwrap_err(); assert!(error.to_string().contains("project plugin is untrusted")); } #[test] fn package_rejects_manifest_symlink_outside_package_root() { let scratch = std::env::temp_dir().join(format!( "smith-plugin-manifest-symlink-{}", std::process::id() )); let package_root = scratch.join("package"); let outside_manifest = scratch.join("outside.kdl"); let _ = std::fs::remove_dir_all(&scratch); std::fs::create_dir_all(&package_root).unwrap(); std::fs::write( &outside_manifest, r#"id "example:outside" version "1" world "0" artifact "fixture.wasm""#, ) .unwrap(); std::fs::write(package_root.join("fixture.wasm"), b"fixture").unwrap(); std::os::unix::fs::symlink(&outside_manifest, package_root.join("plugin.kdl")).unwrap(); let error = PluginPackage::discover(&package_root, true).unwrap_err(); let _ = std::fs::remove_dir_all(&scratch); assert!( error .to_string() .contains("manifest escapes the declared package root") ); } #[test] fn trusted_package_discovers_manifest_and_confined_artifact() { let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/plugin-package"); let package = PluginPackage::discover(&root, true).unwrap(); assert_eq!(package.manifest().id(), "example:fixture"); assert_eq!(package.artifact_bytes(), b"not a wasm component\n"); let engine = PluginEngine::new().unwrap(); let error = match PluginGeneration::load_package(&engine, package, &[]) { Err(error) => error, Ok(_) => panic!("fixture artifact must fail component validation"), }; assert!(error.to_string().contains("validation failed")); } #[test] fn package_snapshot_cannot_execute_replaced_artifact() { let status = std::process::Command::new("cargo") .args([ "build", "-p", "smith-plugin-echo", "--target", "wasm32-wasip2", "--locked", ]) .status() .unwrap(); assert!(status.success()); let scratch = std::env::temp_dir().join(format!("smith-package-snapshot-{}", std::process::id())); let _ = std::fs::remove_dir_all(&scratch); std::fs::create_dir_all(&scratch).unwrap(); std::fs::write( scratch.join("plugin.kdl"), "id \"example:echo\"\nversion \"1\"\nworld \"0.1.0\"\nartifact \"echo.wasm\"", ) .unwrap(); let artifact = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) .join("../target/wasm32-wasip2/debug/smith_plugin_echo.wasm"); std::fs::copy(artifact, scratch.join("echo.wasm")).unwrap(); let package = PluginPackage::discover(&scratch, true).unwrap(); std::fs::write(scratch.join("echo.wasm"), b"not a component").unwrap(); let engine = PluginEngine::new().unwrap(); assert!(PluginGeneration::load_package(&engine, package, &[]).is_ok()); let _ = std::fs::remove_dir_all(&scratch); } #[test] fn external_package_cannot_claim_reserved_smith_identity() { let scratch = std::env::temp_dir().join(format!("smith-reserved-package-{}", std::process::id())); let _ = std::fs::remove_dir_all(&scratch); std::fs::create_dir_all(&scratch).unwrap(); std::fs::write( scratch.join("plugin.kdl"), "id \"smith:malicious\"\nversion \"1\"\nworld \"0.1.0\"\nartifact \"plugin.wasm\"", ) .unwrap(); std::fs::write(scratch.join("plugin.wasm"), b"not a component").unwrap(); let package = PluginPackage::discover(&scratch, true).unwrap(); let engine = PluginEngine::new().unwrap(); let error = match PluginGeneration::load_package(&engine, package, &[]) { Err(error) => error, Ok(_) => panic!("reserved package identity must fail"), }; assert!(error.to_string().contains("reserved smith: identity")); let _ = std::fs::remove_dir_all(&scratch); } #[test] fn manifest_rejects_unknown_capability() { let error = PluginManifest::parse( r#" id "example:echo" version "1.2.3" world "0.1.0" artifact "echo.wasm" capability "filesystem" "#, ) .unwrap_err(); assert!(error.to_string().contains("unknown capability filesystem")); }