repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by admin
xtask/tests/e2e_mutants_guest_artifacts.rs
Raw//! Mutation guest artifact preflight (`SMH-SPEC-SPEC0001`).
use std::{
fs,
path::{Path, PathBuf},
time::{Duration, SystemTime, UNIX_EPOCH},
};
use assert_cmd::Command;
#[path = "../src/guest_build.rs"]
mod guest_build;
const GUESTS: &[&str] = &[
"smith-plugin-oauth",
"smith-plugin-coding-tools",
"smith-plugin-protocol-openai-compatible",
"smith-plugin-protocol-openai-responses",
"smith-plugin-protocol-anthropic",
"smith-plugin-protocol-google",
];
const COMPONENTS: &[(&str, &str)] = &[
("smith_plugin_oauth.wasm", "plugins/oauth/component.sha256"),
(
"smith_plugin_coding_tools.wasm",
"plugins/coding-tools/component.sha256",
),
(
"smith_plugin_protocol_openai_compatible.wasm",
"plugins/protocol-openai-compatible/component.sha256",
),
(
"smith_plugin_protocol_openai_responses.wasm",
"plugins/protocol-openai-responses/component.sha256",
),
(
"smith_plugin_protocol_anthropic.wasm",
"plugins/protocol-anthropic/component.sha256",
),
(
"smith_plugin_protocol_google.wasm",
"plugins/protocol-google/component.sha256",
),
];
#[test]
fn copied_workspace_harness_uses_hash_verified_prepared_guest_artifacts() {
let source = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..");
let scratch = Scratch::new();
let workspace = scratch.0.join("workspace");
copy_workspace(&source, &workspace);
let artifact_target = scratch.0.join("guest-target");
for guest in GUESTS {
let command = guest_build::canonical_guest_cargo_command(&source).unwrap();
let output = Command::from(command)
.timeout(Duration::from_secs(120))
.env_remove("CARGO_TARGET_DIR")
.args([
"build",
"--locked",
"--release",
"--target",
"wasm32-wasip2",
"-p",
guest,
"--target-dir",
])
.arg(&artifact_target)
.output()
.unwrap();
assert!(
output.status.success(),
"guest {guest} build failed:\n{}",
String::from_utf8_lossy(&output.stderr)
);
}
let artifacts = artifact_target.join("wasm32-wasip2/release");
// Reuse dependencies while proving each changed input invalidates the build script.
let host_target = scratch.0.join("host-target");
assert_harness_builds(&workspace, &host_target, &artifacts);
let (_, pin) = COMPONENTS[0];
let pin = workspace.join(pin);
let original_pin = fs::read(&pin).unwrap();
fs::write(&pin, "0".repeat(64)).unwrap();
assert_harness_rejects(&workspace, &host_target, &artifacts);
fs::write(pin, original_pin).unwrap();
assert_harness_builds(&workspace, &host_target, &artifacts);
let (artifact, _) = COMPONENTS[0];
fs::write(artifacts.join(artifact), b"changed artifact").unwrap();
assert_harness_rejects(&workspace, &host_target, &artifacts);
}
fn assert_harness_builds(workspace: &Path, target: &Path, artifacts: &Path) {
let output = cargo(workspace)
.timeout(Duration::from_secs(120))
.args(["build", "--locked", "-p", "smith-harness", "--target-dir"])
.arg(target)
.env("SMITH_COMPONENT_ARTIFACT_DIR", artifacts)
.output()
.unwrap();
assert!(
output.status.success(),
"harness build failed ({}):\n{}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
fn assert_harness_rejects(workspace: &Path, target: &Path, artifacts: &Path) {
let output = cargo(workspace)
.timeout(Duration::from_secs(120))
.args(["build", "--locked", "-p", "smith-harness", "--target-dir"])
.arg(target)
.env("SMITH_COMPONENT_ARTIFACT_DIR", artifacts)
.output()
.unwrap();
assert!(
!output.status.success(),
"harness accepted changed guest input"
);
assert!(
String::from_utf8_lossy(&output.stderr).contains("hash mismatch"),
"unexpected harness failure:\n{}",
String::from_utf8_lossy(&output.stderr)
);
}
fn cargo(workspace: &Path) -> Command {
let mut command = Command::new("cargo");
command
.current_dir(workspace)
.env_remove("CARGO_TARGET_DIR")
// This build already runs inside a parallel test runner.
.env("CARGO_BUILD_JOBS", "2");
command
}
fn copy_workspace(source: &Path, destination: &Path) {
fs::create_dir_all(destination).unwrap();
for entry in fs::read_dir(source).unwrap() {
let entry = entry.unwrap();
let name = entry.file_name();
if matches!(name.to_str(), Some("target" | ".git" | ".jj")) {
continue;
}
let source = entry.path();
let destination = destination.join(name);
if source.is_dir() {
copy_workspace(&source, &destination);
} else {
fs::copy(source, destination).unwrap();
}
}
}
struct Scratch(PathBuf);
impl Scratch {
fn new() -> Self {
let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let path = std::env::temp_dir().join(format!("smith-mutants-guest-artifacts-{nonce}"));
fs::create_dir_all(&path).unwrap();
Self(path)
}
}
impl Drop for Scratch {
fn drop(&mut self) {
fs::remove_dir_all(&self.0).unwrap();
}
}