mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Tests: quote temp paths as script strings in the evalFile refusal tests
script_eval_file_outside_scope_refused and script_eval_file_keeps_the_refusal_when_no_uri_form_is_read put the temp folder path into a single-quoted script string without escaping it. A path with an apostrophe, backslash or line break ended the string, and the run failed with a syntax error instead of the refusal under test. The tests now write the path as a JSON-escaped string literal, and their folder names contain an apostrophe so the quoting is exercised.
This commit is contained in:
1 parent
5f71c8498f
commit
f238d59b31
1 file changed
+11
-5
@@ -24,6 +24,11 @@ fn write(path: &Path, text: &str) {
|
||||
std::fs::write(path, text).unwrap();
|
||||
}
|
||||
|
||||
/// `text` as a quoted script string literal, with its quotes, backslashes and line breaks escaped.
|
||||
fn js_string(text: &str) -> String {
|
||||
serde_json::to_string(text).unwrap()
|
||||
}
|
||||
|
||||
/// Runs `main/main.jsx` in `base`, with the text `text`.
|
||||
fn run(base: &Path, text: &str) -> Result<Value> {
|
||||
let main = base.join("main/main.jsx");
|
||||
@@ -443,11 +448,12 @@ fn script_file_name_names_the_file_of_the_code_that_reads_it() {
|
||||
|
||||
#[test]
|
||||
fn script_eval_file_outside_scope_refused() {
|
||||
let base = folder("evaloutside");
|
||||
// The apostrophe in the folder name would end a quoted script string.
|
||||
let base = folder("eval'outside");
|
||||
write(&base.join("outside.jsx"), "1;");
|
||||
let absolute = base.join("outside.jsx").to_string_lossy().replace('\\', "/");
|
||||
let absolute = js_string(&base.join("outside.jsx").to_string_lossy().replace('\\', "/"));
|
||||
let script = format!(
|
||||
"var out = [];\ntry {{ $.evalFile('../outside.jsx'); }} catch (e) {{ out.push(String(e)); }}\ntry {{ $.evalFile('{absolute}'); }} catch (e) {{ out.push(String(e)); }}\nvc.log(out);"
|
||||
"var out = [];\ntry {{ $.evalFile('../outside.jsx'); }} catch (e) {{ out.push(String(e)); }}\ntry {{ $.evalFile({absolute}); }} catch (e) {{ out.push(String(e)); }}\nvc.log(out);"
|
||||
);
|
||||
let r = run(&base, &script).unwrap();
|
||||
let out = r["log"][0].as_array().unwrap();
|
||||
@@ -466,10 +472,10 @@ fn script_eval_file_outside_scope_refused() {
|
||||
#[cfg(not(windows))]
|
||||
#[test]
|
||||
fn script_eval_file_keeps_the_refusal_when_no_uri_form_is_read() {
|
||||
let base = folder("evaluri");
|
||||
let base = folder("eval'uri");
|
||||
write(&base.join("out%20side.jsx"), "1;");
|
||||
let absolute = base.join("out%20side.jsx").to_string_lossy().into_owned();
|
||||
let script = format!("try {{ $.evalFile('{absolute}'); }} catch (e) {{ vc.log(String(e)); }}");
|
||||
let script = format!("try {{ $.evalFile({}); }} catch (e) {{ vc.log(String(e)); }}", js_string(&absolute));
|
||||
let r = run(&base, &script).unwrap();
|
||||
let e = r["log"][0].as_str().unwrap_or_default().to_string();
|
||||
assert!(e.starts_with(&format!("Error: script file read refused: {absolute}")), "{e}");
|
||||
|
||||
Reference in new issue
Block a user