mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 21:29:35 +00:00
Merge pull request #6888 from dotCooCoo/fix/read-roots-scripts-recovery
MCP: plug-in, recovery and script report replies leave out paths outside the read roots
This commit is contained in:
@@ -1433,6 +1433,92 @@ fn plugins_folder_set_again_after_clearing_installs_its_plugins() {
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
|
||||
/// Under automation read roots, `plugin.list` names the plug-in folder last loaded, its failed
|
||||
/// files and a plug-in's source file only inside them: a folder outside is `null`, and a file
|
||||
/// outside is named by its file name.
|
||||
#[test]
|
||||
fn plugin_list_leaves_out_paths_outside_the_read_root() {
|
||||
use crate::file_access::{AutomationRoots, confine};
|
||||
let _folder = PREFS_FOLDER.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let base = crate::tests_links::Folder::new("plugin-list-read-root");
|
||||
let (inside, outside) = (base.0.join("in"), base.0.join("out"));
|
||||
std::fs::create_dir_all(&inside).unwrap();
|
||||
std::fs::create_dir_all(&outside).unwrap();
|
||||
let id = "test.engine.listroots";
|
||||
let src = tpl::returning(&tpl::manifest(id, "filter", "{}"), r#"{"objects":[]}"#);
|
||||
std::fs::write(outside.join("hidden.wasm"), wat::parse_str(src).unwrap()).unwrap();
|
||||
std::fs::write(outside.join("broken.wasm"), b"\0asm?").unwrap();
|
||||
let folder = outside.to_string_lossy().to_string();
|
||||
let mut s = session();
|
||||
let r = run(&mut s, "plugin.reload", json!({"path": folder}));
|
||||
assert_eq!(r["loaded"], json!([id]), "{r}");
|
||||
let broken = outside.join("broken.wasm").to_string_lossy().to_string();
|
||||
let source = outside.join("hidden.wasm").to_string_lossy().to_string();
|
||||
let entry = |list: &Value| list["plugins"].as_array().unwrap().iter().find(|p| p["id"] == id).cloned().unwrap();
|
||||
|
||||
// The person sees the paths.
|
||||
let person = run(&mut s, "plugin.list", json!({}));
|
||||
assert_eq!((&person["folder"]["folder"], &person["folder"]["failed"][0]["file"]), (&json!(folder), &json!(broken)), "{person}");
|
||||
assert_eq!(entry(&person)["source"], json!(source));
|
||||
|
||||
// The client sees their names.
|
||||
let roots = AutomationRoots::new(&[&inside], &[]).unwrap();
|
||||
let listed = confine(roots.as_ref(), || s.execute("plugin.list", &json!({}))).unwrap();
|
||||
let report = &listed["folder"];
|
||||
assert_eq!((&report["folder"], &report["loaded"], &report["failed"][0]["file"]), (&Value::Null, &json!([id]), &json!("broken.wasm")), "{listed}");
|
||||
assert_eq!(entry(&listed)["source"], "hidden.wasm");
|
||||
let info = confine(roots.as_ref(), || s.execute("plugin.info", &json!({"id": id}))).unwrap();
|
||||
assert_eq!(info["source"], "hidden.wasm");
|
||||
let outside_text = json!(folder).to_string();
|
||||
for reply in [&listed, &info] {
|
||||
assert!(!reply.to_string().contains(outside_text.trim_matches('"')), "no path outside the root: {reply}");
|
||||
}
|
||||
|
||||
// A read root holding the folder shows it whole.
|
||||
let wider = AutomationRoots::new(&[&outside], &[]).unwrap();
|
||||
let listed = confine(wider.as_ref(), || s.execute("plugin.list", &json!({}))).unwrap();
|
||||
assert_eq!(listed["folder"]["folder"], json!(folder), "{listed}");
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
|
||||
/// Under automation read roots, a plug-in turned off for the next launch is listed with a file
|
||||
/// outside them named by its file name, and `plugin.setEnabled` turns it on by that name.
|
||||
#[test]
|
||||
fn disabled_plugins_name_files_outside_the_read_root_by_their_names() {
|
||||
if !in_own_process("disabled_plugins_name_files_outside_the_read_root_by_their_names") {
|
||||
return;
|
||||
}
|
||||
use crate::file_access::{AutomationRoots, confine};
|
||||
let base = crate::tests_links::Folder::new("plugin-disabled-read-root");
|
||||
let (inside, outside) = (base.0.join("in"), base.0.join("out"));
|
||||
std::fs::create_dir_all(&inside).unwrap();
|
||||
std::fs::create_dir_all(&outside).unwrap();
|
||||
let id = "test.engine.disabledroots";
|
||||
let src = tpl::returning(&tpl::manifest(id, "filter", "{}"), r#"{"objects":[]}"#);
|
||||
let file = outside.join("off.wasm");
|
||||
std::fs::write(&file, wat::parse_str(src).unwrap()).unwrap();
|
||||
let file = file.to_string_lossy().to_string();
|
||||
let mut s = session();
|
||||
run(&mut s, "plugin.install", json!({"path": file}));
|
||||
let roots = AutomationRoots::new(&[&inside], &[]).unwrap();
|
||||
// The client turns it off by the source plugin.list shows.
|
||||
let listed = confine(roots.as_ref(), || s.execute("plugin.list", &json!({}))).unwrap();
|
||||
let source = listed["plugins"].as_array().unwrap().iter().find(|p| p["id"] == id).unwrap()["source"].clone();
|
||||
assert_eq!(source, "off.wasm", "{listed}");
|
||||
confine(roots.as_ref(), || s.execute("plugin.setEnabled", &json!({"file": source, "enabled": false}))).unwrap();
|
||||
assert_eq!(run(&mut s, "plugin.safeMode", json!({}))["disabled"][0]["file"], json!(file), "the person sees the path");
|
||||
|
||||
let outside_text = json!(outside.to_string_lossy()).to_string();
|
||||
for cmd in ["plugin.list", "plugin.safeMode"] {
|
||||
let r = confine(roots.as_ref(), || s.execute(cmd, &json!({}))).unwrap();
|
||||
assert_eq!(r["disabled"][0]["file"], "off.wasm", "{cmd}: {r}");
|
||||
assert!(!r.to_string().contains(outside_text.trim_matches('"')), "{cmd}: no path outside the root: {r}");
|
||||
}
|
||||
let r = confine(roots.as_ref(), || s.execute("plugin.setEnabled", &json!({"file": "off.wasm", "enabled": true}))).unwrap();
|
||||
assert_eq!((&r["changed"], &r["disabled"]), (&json!(true), &json!([])), "{r}");
|
||||
run(&mut s, "plugin.remove", json!({"id": id}));
|
||||
}
|
||||
|
||||
/// #2541: a plug-in installed in a session with a plug-in store is installed again in the next
|
||||
/// session that attaches the store, and Remove Plug-in takes it out of the store.
|
||||
#[test]
|
||||
|
||||
@@ -1545,6 +1545,71 @@ impl RecoveryStore for DiesAfterDocument {
|
||||
}
|
||||
}
|
||||
|
||||
/// Under automation read roots, `file.recovery.save`, `file.recovery.list` and
|
||||
/// `file.recovery.restore` name a document's file and the recovery folder only inside them.
|
||||
#[test]
|
||||
fn recovery_replies_leave_out_paths_outside_the_read_root() {
|
||||
use crate::file_access::{AutomationRoots, confine};
|
||||
use serde_json::Value;
|
||||
let base = crate::tests_links::Folder::new("recovery-read-root");
|
||||
let (inside, outside) = (base.0.join("in"), base.0.join("out"));
|
||||
std::fs::create_dir_all(&inside).unwrap();
|
||||
std::fs::create_dir_all(&outside).unwrap();
|
||||
let store: Arc<dyn RecoveryStore> = Arc::new(FolderStore::new(outside.join("recovery")));
|
||||
let location = outside.join("recovery").to_string_lossy().to_string();
|
||||
let kept = inside.join("kept.vectorcraft").to_string_lossy().to_string();
|
||||
let hidden = outside.join("hidden.vectorcraft").to_string_lossy().to_string();
|
||||
let mut s = Session::new();
|
||||
s.recovery.set_store(store.clone());
|
||||
for path in [&kept, &hidden] {
|
||||
s.execute("file.new", &json!({"width": 200, "height": 100})).unwrap();
|
||||
s.execute("file.saveAs", &json!({"path": path})).unwrap();
|
||||
s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 20})).unwrap();
|
||||
}
|
||||
let roots = AutomationRoots::new(&[&inside], &[]).unwrap();
|
||||
let saved = confine(roots.as_ref(), || s.execute("file.recovery.save", &json!({}))).unwrap();
|
||||
assert_eq!(saved["location"], Value::Null, "{saved}");
|
||||
s.execute("shape.ellipse", &json!({"x": 80, "y": 20, "width": 20, "height": 20})).unwrap();
|
||||
assert_eq!(s.execute("file.recovery.save", &json!({})).unwrap()["location"], json!(location), "the person sees the folder");
|
||||
|
||||
let sorted = |v: &Value, key: &str| -> Vec<String> {
|
||||
let mut out: Vec<String> = v[key].as_array().unwrap().iter().map(|c| c["path"].to_string()).collect();
|
||||
out.sort();
|
||||
out
|
||||
};
|
||||
let both = |first: Value| {
|
||||
let mut out = vec![first.to_string(), json!(kept).to_string()];
|
||||
out.sort();
|
||||
out
|
||||
};
|
||||
assert_eq!(sorted(&s.execute("file.recovery.list", &json!({})).unwrap(), "copies"), both(json!(hidden)));
|
||||
let listed = confine(roots.as_ref(), || s.execute("file.recovery.list", &json!({}))).unwrap();
|
||||
assert_eq!((sorted(&listed, "copies"), &listed["location"]), (both(Value::Null), &Value::Null), "{listed}");
|
||||
let outside_text = json!(outside.to_string_lossy()).to_string();
|
||||
assert!(!listed.to_string().contains(outside_text.trim_matches('"')), "no path outside the root: {listed}");
|
||||
// A relative recovery folder is checked as the file system reads it; a store that is no folder
|
||||
// keeps its name.
|
||||
let mut relative = Session::new();
|
||||
relative.recovery.set_store(Arc::new(FolderStore::new("vc-recovery-relative-folder")));
|
||||
let l = confine(roots.as_ref(), || relative.execute("file.recovery.list", &json!({}))).unwrap();
|
||||
assert_eq!(l["location"], Value::Null, "{l}");
|
||||
let (mut memory, _) = session();
|
||||
let l = confine(roots.as_ref(), || memory.execute("file.recovery.list", &json!({}))).unwrap();
|
||||
assert_eq!(l["location"], "memory", "{l}");
|
||||
|
||||
// After a crash, the copies restored.
|
||||
drop(s);
|
||||
let mut s = Session::new();
|
||||
s.recovery.set_store(store.clone());
|
||||
let r = confine(roots.as_ref(), || s.execute("file.recovery.restore", &json!({}))).unwrap();
|
||||
assert_eq!(sorted(&r, "restored"), both(Value::Null), "{r}");
|
||||
let mut opened: Vec<Option<String>> = s.documents().iter().map(|d| d.path.clone()).collect();
|
||||
opened.sort();
|
||||
let mut files = vec![Some(hidden), Some(kept)];
|
||||
files.sort();
|
||||
assert_eq!(opened, files, "the documents keep their files");
|
||||
}
|
||||
|
||||
/// A crash between writing a copy's document and its details restores the new document with
|
||||
/// every image, the one placed since the last copy too (and the first copy likewise).
|
||||
#[test]
|
||||
|
||||
@@ -265,3 +265,31 @@ fn the_slowest_commands_are_the_longest_in_all_then_by_id() {
|
||||
assert_eq!(ids, ["b.two", "a.one", "c.three", "f", "e"]);
|
||||
assert_eq!((slow[0].calls, slow[0].total_ms, slow[0].max_ms), (3, 9, 4));
|
||||
}
|
||||
|
||||
/// Under automation read roots, the error lines of a failed run name the included file it stopped
|
||||
/// in by its file name also when the console cut a line inside the file's path.
|
||||
#[test]
|
||||
fn a_cut_error_line_leaves_out_an_included_files_folder_outside_the_read_root() {
|
||||
use crate::file_access::{AutomationRoots, confine};
|
||||
let base = crate::tests_links::Folder::new("script-report-cut-line");
|
||||
let inside = base.0.join("in");
|
||||
std::fs::create_dir_all(&inside).unwrap();
|
||||
// A path longer than a console line, as the report of a failed run names it.
|
||||
let file = base.0.join("out").join("d".repeat(MAX_LINE_BYTES)).join("bad.jsxinc").to_string_lossy().to_string();
|
||||
let mut report = json!({"ok": false, "name": "t.js", "file": file, "line": 2, "column": 7, "message": format!("Error: stopped\nin {file}")});
|
||||
let error = script::error_lines(&report);
|
||||
assert!(error.len() == 2 && error.iter().all(|l| l.len() <= MAX_LINE_BYTES && l.ends_with(CUT_MARK)), "both lines are cut: {error:?}");
|
||||
let written: Vec<String> = std::iter::once("written by the script".to_string()).chain(error).collect();
|
||||
report["console"] = json!({"lines": written, "droppedLines": 0, "truncatedLines": 2});
|
||||
let mut s = Session::new();
|
||||
s.script_report = Some(report.clone());
|
||||
|
||||
let roots = AutomationRoots::new(&[&inside], &[]).unwrap();
|
||||
let shown = confine(roots.as_ref(), || s.shown_script_report()).unwrap();
|
||||
assert_eq!((&shown["file"], &shown["message"]), (&json!("bad.jsxinc"), &json!("Error: stopped\nin bad.jsxinc")), "{shown}");
|
||||
assert_eq!(shown["console"]["lines"], json!(["written by the script", "t.js: bad.jsxinc:2:7: Error: stopped", "in bad.jsxinc"]), "{shown}");
|
||||
let outside = json!(base.0.join("out").to_string_lossy()).to_string();
|
||||
assert!(!shown.to_string().contains(outside.trim_matches('"')), "no path outside the root: {shown}");
|
||||
// The person sees the report as it is.
|
||||
assert_eq!(s.shown_script_report(), Some(report));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user