feat(stdlib): ship a verified bundled stdlib with std: imports
- Move the Compat-A print prototype into stdlib/ as the official offline bundle with stdlib.json (version 0.1.0 + sha256 content digest). - Add std:<relative> import resolution in the loader with bundle path confinement, extension inference and fail-closed digest verification. - Record the verified version/digest/instance_id in Analysis v2 project.stdlib; stdlib modules get a std:name@version#digest instance id. - Add CLI tests: std:io execution, digest-tamper rejection, missing manifest, Analysis identity, no foreign/plugin calls, and frozen binary hash equality. - Update ADR-0028, specs, status, changelog and evidence. Gate: fmt, clippy -D warnings, 415 tests passed.
This commit is contained in:
1 parent
1a978c7a42
commit
1377af1601
28 files changed
+879
-23
No files matched your search
@@ -26,7 +26,7 @@ fn stderr(output: &Output) -> String {
|
||||
|
||||
#[test]
|
||||
fn mqmd_stdlib_print_output_matches_builtin_print() {
|
||||
let output = cli(&["run", "examples/stdlib/demo.mq.md"]);
|
||||
let output = cli(&["run", "stdlib/demo.mq.md"]);
|
||||
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let lines: Vec<&str> = stdout.lines().collect();
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
//! Bundled stdlib (`std:`) and frozen-CLI closure tests (CM-A10).
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
fn repo_root() -> PathBuf {
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.parent()
|
||||
.unwrap()
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
fn binary() -> &'static str {
|
||||
env!("CARGO_BIN_EXE_marqdo")
|
||||
}
|
||||
|
||||
fn stdlib_dir() -> PathBuf {
|
||||
repo_root().join("stdlib")
|
||||
}
|
||||
|
||||
fn run(dir: &Path, stdlib: &Path, args: &[&str]) -> std::process::Output {
|
||||
Command::new(binary())
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.env("MARQDO_STDLIB_DIR", stdlib)
|
||||
.output()
|
||||
.expect("run marqdo")
|
||||
}
|
||||
|
||||
struct TempDir(PathBuf);
|
||||
|
||||
impl TempDir {
|
||||
fn new(tag: &str) -> Self {
|
||||
let path = std::env::temp_dir().join(format!("marqdo-stdlib-{}-{tag}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&path);
|
||||
std::fs::create_dir_all(&path).unwrap();
|
||||
Self(path)
|
||||
}
|
||||
fn path(&self) -> &Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TempDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_stdlib(source: &Path, target: &Path) {
|
||||
std::fs::create_dir_all(target).unwrap();
|
||||
for entry in std::fs::read_dir(source).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
copy_stdlib(&path, &target.join(entry.file_name()));
|
||||
} else {
|
||||
std::fs::copy(&path, target.join(entry.file_name())).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_project(dir: &Path, program: &str) {
|
||||
std::fs::write(dir.join("index.mq.md"), program).unwrap();
|
||||
}
|
||||
|
||||
const IMPORT_IO: &str = r#"---
|
||||
import:
|
||||
io: "std:io"
|
||||
---
|
||||
|
||||
## main
|
||||
|
||||
> io.写值(["bundle", 1, 2.5])
|
||||
|
||||
*None*
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn std_import_resolves_the_verified_bundle() {
|
||||
let dir = TempDir::new("resolve");
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &stdlib_dir(), &["run", "index.mq.md"]);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
"[\"bundle\", 1, 2.5]\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn analysis_records_the_verified_stdlib_digest() {
|
||||
let dir = TempDir::new("analysis");
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let target = dir.path().join("analysis.json");
|
||||
let output = run(
|
||||
dir.path(),
|
||||
&stdlib_dir(),
|
||||
&["check", "index.mq.md", "--emit", target.to_str().unwrap()],
|
||||
);
|
||||
assert_eq!(
|
||||
output.status.code(),
|
||||
Some(0),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
let analysis: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(&target).unwrap()).unwrap();
|
||||
let manifest: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(stdlib_dir().join("stdlib.json")).unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
analysis["project"]["stdlib"]["version"],
|
||||
manifest["version"]
|
||||
);
|
||||
assert_eq!(analysis["project"]["stdlib"]["digest"], manifest["digest"]);
|
||||
assert!(analysis["project"]["stdlib"]["instance_id"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.starts_with("std:marqdo-stdlib@"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frozen_binary_hash_is_unchanged_after_stdlib_execution() {
|
||||
let before = std::fs::read(binary()).unwrap();
|
||||
let before_hash = marqdo_value_sha(&before);
|
||||
let dir = TempDir::new("frozen");
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &stdlib_dir(), &["run", "index.mq.md"]);
|
||||
assert_eq!(output.status.code(), Some(0));
|
||||
let after = std::fs::read(binary()).unwrap();
|
||||
assert_eq!(before_hash, marqdo_value_sha(&after));
|
||||
}
|
||||
|
||||
fn marqdo_value_sha(bytes: &[u8]) -> String {
|
||||
use sha2::{Digest, Sha256};
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(bytes);
|
||||
hasher
|
||||
.finalize()
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tampered_bundle_is_rejected_before_module_loading() {
|
||||
let dir = TempDir::new("tamper");
|
||||
let bundle = dir.path().join("stdlib");
|
||||
copy_stdlib(&stdlib_dir(), &bundle);
|
||||
std::fs::write(bundle.join("io.mq.md"), "## tampered\n").unwrap();
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &bundle, &["run", "index.mq.md"]);
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(stderr.contains("stdlib digest mismatch"), "{stderr}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bundle_uses_no_foreign_or_plugin_calls() {
|
||||
fn visit(dir: &Path, offenders: &mut Vec<String>) {
|
||||
for entry in std::fs::read_dir(dir).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_dir() {
|
||||
visit(&path, offenders);
|
||||
} else if path.to_string_lossy().ends_with(".mq.md") {
|
||||
let source = std::fs::read_to_string(&path).unwrap();
|
||||
if source.contains("foreign.") || source.contains("plugin.") {
|
||||
offenders.push(path.display().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut offenders = Vec::new();
|
||||
visit(&stdlib_dir(), &mut offenders);
|
||||
assert!(
|
||||
offenders.is_empty(),
|
||||
"stdlib bundle must not depend on foreign/plugin calls: {offenders:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_bundle_manifest_is_a_hard_error() {
|
||||
let dir = TempDir::new("missing");
|
||||
let empty = dir.path().join("empty-stdlib");
|
||||
std::fs::create_dir_all(&empty).unwrap();
|
||||
write_project(dir.path(), IMPORT_IO);
|
||||
let output = run(dir.path(), &empty, &["run", "index.mq.md"]);
|
||||
assert!(!output.status.success());
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(stderr.contains("stdlib"), "{stderr}");
|
||||
}
|
||||
Reference in new issue
Block a user